In the example I have shown,I have two files.First one is named as "1.txt" and is my input file.The values will be read from this file and simply copied to the second file named "2.txt". The example is meant for just a basic introduction for file handling in VHDL.There are pretty large number of options when it comes to file handling,but I will post them in future.
--include this library for file handling in VHDL.
library std;
use std.textio.all; --include package textio.vhd
--entity declaration
entity filehandle is
end filehandle;
--architecture definition
architecture Behavioral of filehandle is
--period of clock,bit for indicating end of file.
signal clock,endoffile : bit := '0';
--data read from the file.
signal dataread : real;
--data to be saved into the output file.
signal datatosave : real;
--line number of the file read or written.
signal linenumber : integer:=1;
begin
clock <= not (clock) after 1 ns; --clock with time period 2 ns
--read process
reading :
process
file infile : text is in "1.txt"; --declare input file
variable inline : line; --line number declaration
variable dataread1 : real;
begin
wait until clock = '1' and clock'event;
if (not endfile(infile)) then --checking the "END OF FILE" is not reached.
readline(infile, inline); --reading a line from the file.
--reading the data from the line and putting it in a real type variable.
read(inline, dataread1);
dataread <=dataread1; --put the value available in variable in a signal.
else
endoffile <='1'; --set signal to tell end of file read file is reached.
end if;
end process reading;
--write process
writing :
process
file outfile : text is out "2.txt"; --declare output file
variable outline : line; --line number declaration
begin
wait until clock = '0' and clock'event;
if(endoffile='0') then --if the file end is not reached.
--write(linenumber,value(real type),justified(side),field(width),digits(natural));
write(outline, dataread, right, 16, 12);
-- write line to external file.
writeline(outfile, outline);
linenumber <= linenumber + 1;
else
null;
end if;
end process writing;
end Behavioral;
library std;
use std.textio.all; --include package textio.vhd
--entity declaration
entity filehandle is
end filehandle;
--architecture definition
architecture Behavioral of filehandle is
--period of clock,bit for indicating end of file.
signal clock,endoffile : bit := '0';
--data read from the file.
signal dataread : real;
--data to be saved into the output file.
signal datatosave : real;
--line number of the file read or written.
signal linenumber : integer:=1;
begin
clock <= not (clock) after 1 ns; --clock with time period 2 ns
--read process
reading :
process
file infile : text is in "1.txt"; --declare input file
variable inline : line; --line number declaration
variable dataread1 : real;
begin
wait until clock = '1' and clock'event;
if (not endfile(infile)) then --checking the "END OF FILE" is not reached.
readline(infile, inline); --reading a line from the file.
--reading the data from the line and putting it in a real type variable.
read(inline, dataread1);
dataread <=dataread1; --put the value available in variable in a signal.
else
endoffile <='1'; --set signal to tell end of file read file is reached.
end if;
end process reading;
--write process
writing :
process
file outfile : text is out "2.txt"; --declare output file
variable outline : line; --line number declaration
begin
wait until clock = '0' and clock'event;
if(endoffile='0') then --if the file end is not reached.
--write(linenumber,value(real type),justified(side),field(width),digits(natural));
write(outline, dataread, right, 16, 12);
-- write line to external file.
writeline(outfile, outline);
linenumber <= linenumber + 1;
else
null;
end if;
end process writing;
end Behavioral;
The contents of files 1.txt and 2.txt are shown below:
Now let us discuss about the textio.vhd package and its general features.
It offers the following new data types:
type LINE is access STRING; -- A LINE is a pointer to a STRING value
type TEXT is file of STRING; -- A file of variable-length ASCII records.
type SIDE is (RIGHT, LEFT); -- For justifying output data within fields.
subtype WIDTH is NATURAL; -- For specifying widths of output fields.
It offers a large number of functions to read and write to a file.You can see the list of all functions and the arguments used here.
In the example program given above I have two different processes,one for reading from the file and another for writing into the file.
endfile() is a function which is used to check whether the end of the file is reached.It returns a '1' when end of file is reached.
The data cannot be read directly into a signal.That is why I have first read it into a variable and then assigned it into a signal.
Every time you write something into the file or read something from the file,the line number is internally incremented.
Note :- One advantage of file handling in VHDL is that,you can test a large number of input combinations for checking the integrity of your design.Sometimes the automatically generated test cases(with the help of a program) can be easily used without much changes in the testbench code.