How can I read binary data in VHDL / modelsim without using special binary formats

Some background:

I am writing a VHDL test bench for an Ethernet MAC address. Testbench consists of a package and a combined file + architecture architecture. I want to read the ethernet frames that the test tape will send to the MAC from a binary file that I exported from wirehark.
I am writing in VHDL 2008 and I am using the Mentor Graphics Model ModelSim ALTERA vcom 10.0d Compiler.

Problem:

All the binary data reading solutions in VHDL / modelsim that I have found so far use special file formats, where 1 bit_vector is represented by several bits in the file. I would like VHDL to read a binary file in 8 bit bit_vectors.
The closest I have so far to use is a character type file where I can write 8-bit ASCII characters directly in binary representation.

+4
source share
3 answers

I used this, but found it more useful to write a short script (in a serious text processing language) to convert from any input file to a real VHDL file with data described as a constant array of the appropriate data type.

This is much easier than parsing files in VHDL IMHO.

+1
source

I have found a solution. If I want to interpret the data directly in 8-bit parts, I need to use a character type file and convert it to integers using the POS attribute. Then I can convert these integers to bit vectors. Here is how I did it:

LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.numeric_bit.ALL; LIBRARY std; USE std.textio.all; ... TYPE t_char_file IF FILE OF character; TYPE t_byte_arr IS ARRAY (natural RANGE <>) OF bit_vector(7 DOWNTO 0); SIGNAL read_arr_byte : t_byte_arr(0 to 199); ... read_file: PROCESS (start) IS FILE file_in : t_char_file OPEN read_mode IS "./38478.bin"; -- open the frame file for reading VARIABLE char_buffer : character; BEGIN IF start'EVENT AND start = '1' THEN FOR i IN read_arr_byte'RANGE LOOP read(file_in, char_buffer); read_arr_byte(i) <= bit_vector(to_unsigned(character'POS(char_buffer), 8)); END LOOP; -- i file_close(file_in); END IF; END PROCESS read_file; 
+3
source

Based on the previous answer from @ youR.Fate, I was able to reduce the example further:

 process is type char_file_t is file of character; file char_file : char_file_t; variable char_v : character; subtype byte_t is natural range 0 to 255; variable byte_v : byte_t; begin file_open(char_file, "test.bin"); while not endfile(char_file) loop read(char_file, char_v); byte_v := character'pos(char_v); report "Char: " & " #" & integer'image(byte_v); end loop; file_close(char_file); wait; end process; 
+1
source

All Articles