This post is not meant for first years
--code for reading from a text file
-- text write code by Binoy B. Nair, Lecturer EIE
-- this code reads a value from the file binoy_file.txt at each rising edge of clk
-- the execution ends when end of file is reached
-- first it must be made sure that the file(here binoy_file.txt)exists,before you try to read from it
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use std.textio.all; -- this package must be included for all text read/write
entity reading is
port(clk:in std_logic);
end reading;
architecture reading_arch of reading is
signal sig_value: integer:=0; -- we will finally move this extracted value to this signal for easy observation
begin
process(clk)
file binoy_input_file : text is in "c:/binoy_file.txt"; -- open a file named binoy_file.txt
-- for reading from it (hence defined as in)
variable value: integer:=0; -- the read value is moved to this variable
variable in_line: line; -- the read value will be of type LINE always
begin
if clk'event and clk='1' then -- the process runs at every rising edge of clk
if (not(endfile(binoy_input_file))) then -- this code checks if the end of file has reached
readline( binoy_input_file,in_line);-- otherwise one line is read from the file.
-- Every time a line has been read, the file pointer automatically
-- points to the next line
read(in_line,value); -- converts the read variable 'in_line' from type LINE to data type of second operand(here INTEGER)
sig_value<=value; -- moving the contents of variable 'value' to a signal 'sig_value'
end if;
end if;
end process;
end reading_arch;
-- text write code by Binoy B. Nair, Lecturer EIE
-- this code counts the number of rising edges of clk input and writes to the file bin_out_text.txt
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use std.textio.all; -- this package must be included for all text read/write
entity writing is
port(clk:in std_logic);
end writing;
architecture writing_arch of writing is
begin
process(clk)
file binoy_output_file : text is out "c:/bin_out_text.txt"; -- open a file named bin_out_text.txt
-- for writing to it (hence defined as OUT )
-- if no path is specified, the file bin_out_text.txt
-- will be stored in the same directory as the program
variable out_line: line; -- each value that is passed to out_line will be
-- considered to be a line and is auto. terminated by a carriage return
variable cnt: integer:=0;
begin
if clk'event and clk='1' then -- at every clk event, the count is incremented by 1
cnt:=cnt+1;
write (out_line,cnt); -- the value of count after incrementing is written to out_line
-- (this converts input data type (here INTEGER) to type LINE which can then be written to a file)
-- it is not possible to directly write the data type INTEGER to a file,
--hence the above conversion is needed.
writeline(binoy_output_file,out_line); -- write the out_line to the output file
end if;
end process;
end writing_arch;
Featuring tutorials on soft computing, analytical Instrumentation, Basic Electronics and Robotics
Important Disclaimer
The purpose of this blog is purely to serve as a compilation of good technical material for my students. No financial or other motives are involved. Most of the content in this blog has been reproduced from other sources. I have made every attempt to mention the source link at the beginning of each blog. All readers are requested to kindly acknowledge that source and not this blog, in case you find the post helpful. However, I have not been able to trace the source links for some of my older posts. I wish to emphasize that this is not intentional and any help in this regard would be appreciated.
No comments:
Post a Comment