I have published a post on file reading and writing using VHDL before. You can access it here. As you know file manipulation will help you to verify your design more effectively at the debugging stage of your design.
For file operation we use the library named textio in the STD directory. This library contains the in built functions for reading and writing files.
Reading Files in VHDL:
The below code reads a set of binary numbers from the file named read.txt and put them into a 4 bit std_logic_vector signal. The text file used with the code can be downloaded from here.
For file operation we use the library named textio in the STD directory. This library contains the in built functions for reading and writing files.
Reading Files in VHDL:
The below code reads a set of binary numbers from the file named read.txt and put them into a 4 bit std_logic_vector signal. The text file used with the code can be downloaded from here.
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
use STD.textio.all; --Dont forget to include this library for file operations.
ENTITY read_file IS
END read_file;
ARCHITECTURE beha OF read_file IS
signal bin_value : std_logic_vector(3 downto 0):="0000";
BEGIN
--Read process
process
file file_pointer : text;
variable line_content : string(1 to 4);
variable line_num : line;
variable j : integer := 0;
variable char : character:='0';
begin
--Open the file read.txt from the specified location for reading(READ_MODE).
file_open(file_pointer,"C:\read.txt",READ_MODE);
while not endfile(file_pointer) loop --till the end of file is reached continue.
readline (file_pointer,line_num); --Read the whole line from the file
--Read the contents of the line from the file into a variable.
READ (line_num,line_content);
--For each character in the line convert it to binary value.
--And then store it in a signal named 'bin_value'.
for j in 1 to 4 loop
char := line_content(j);
if(char = '0') then
bin_value(4-j) <= '0';
else
bin_value(4-j) <= '1';
end if;
end loop;
wait for 10 ns; --after reading each line wait for 10ns.
end loop;
file_close(file_pointer); --after reading all the lines close the file.
wait;
end process;
end beha;
USE ieee.std_logic_1164.ALL;
use STD.textio.all; --Dont forget to include this library for file operations.
ENTITY read_file IS
END read_file;
ARCHITECTURE beha OF read_file IS
signal bin_value : std_logic_vector(3 downto 0):="0000";
BEGIN
--Read process
process
file file_pointer : text;
variable line_content : string(1 to 4);
variable line_num : line;
variable j : integer := 0;
variable char : character:='0';
begin
--Open the file read.txt from the specified location for reading(READ_MODE).
file_open(file_pointer,"C:\read.txt",READ_MODE);
while not endfile(file_pointer) loop --till the end of file is reached continue.
readline (file_pointer,line_num); --Read the whole line from the file
--Read the contents of the line from the file into a variable.
READ (line_num,line_content);
--For each character in the line convert it to binary value.
--And then store it in a signal named 'bin_value'.
for j in 1 to 4 loop
char := line_content(j);
if(char = '0') then
bin_value(4-j) <= '0';
else
bin_value(4-j) <= '1';
end if;
end loop;
wait for 10 ns; --after reading each line wait for 10ns.
end loop;
file_close(file_pointer); --after reading all the lines close the file.
wait;
end process;
end beha;
The above VHDL code can also be downloaded from here. I have commented the codes so that you can understand the flow of the code. The values read from the file are represented by a signal named bin_value. The simulation waveform is given below:
The main points to be noted are:
- Declare file pointers and other variables required as given in the above code.
- file_open() to open the file. Change the path of the file depending on where your file is stored.
- Use read and readline functions.
- Finally after everything is over, close the file using file_close.
Writing Files in VHDL:
The following code is used for writing a file. Remember that these codes contain just examples and depending on what you have to write to the file the code should be changed. The code writes binary numbers from 0000 to 1111 to a file named write.txt.
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_arith.ALL;
use STD.textio.all;
ENTITY write_file IS
END write_file;
ARCHITECTURE beha OF write_file IS
BEGIN
--Write process
process
file file_pointer : text;
variable line_content : string(1 to 4);
variable bin_value : std_logic_vector(3 downto 0);
variable line_num : line;
variable i,j : integer := 0;
variable char : character:='0';
begin
--Open the file write.txt from the specified location for writing(WRITE_MODE).
file_open(file_pointer,"C:\write.txt",WRITE_MODE);
--We want to store binary values from 0000 to 1111 in the file.
for i in 0 to 15 loop
bin_value := conv_std_logic_vector(i,4);
--convert each bit value to character for writing to file.
for j in 0 to 3 loop
if(bin_value(j) = '0') then
line_content(4-j) := '0';
else
line_content(4-j) := '1';
end if;
end loop;
write(line_num,line_content); --write the line.
writeline (file_pointer,line_num); --write the contents into the file.
wait for 10 ns; --wait for 10ns after writing the current line.
end loop;
file_close(file_pointer); --Close the file after writing.
wait;
end process;
end beha;
USE ieee.std_logic_1164.ALL;
USE ieee.std_logic_arith.ALL;
use STD.textio.all;
ENTITY write_file IS
END write_file;
ARCHITECTURE beha OF write_file IS
BEGIN
--Write process
process
file file_pointer : text;
variable line_content : string(1 to 4);
variable bin_value : std_logic_vector(3 downto 0);
variable line_num : line;
variable i,j : integer := 0;
variable char : character:='0';
begin
--Open the file write.txt from the specified location for writing(WRITE_MODE).
file_open(file_pointer,"C:\write.txt",WRITE_MODE);
--We want to store binary values from 0000 to 1111 in the file.
for i in 0 to 15 loop
bin_value := conv_std_logic_vector(i,4);
--convert each bit value to character for writing to file.
for j in 0 to 3 loop
if(bin_value(j) = '0') then
line_content(4-j) := '0';
else
line_content(4-j) := '1';
end if;
end loop;
write(line_num,line_content); --write the line.
writeline (file_pointer,line_num); --write the contents into the file.
wait for 10 ns; --wait for 10ns after writing the current line.
end loop;
file_close(file_pointer); --Close the file after writing.
wait;
end process;
end beha;
The above code can also be downloaded from here. Any file writing code should have the following structure:
- Declare file pointers and other variables required as given in the above code.
- file_open() to open the file. Change the path of the file depending on where your file is stored.
- Use write and writeline functions.
- Finally after everything is over, close the file using file_close.