Forum Discussion
Altera_Forum
Honored Contributor
13 years agoReading a Text File
Hi to all,
I am trying to read a text file has some values in character type as follow: 21 30 36 30 17 14 13 13 but what I am getting wrong values as follow: 50 49 10 51 48 10 51 based in the following code: library ieee; USE ieee.std_logic_1164.all; USE ieee.numeric_bit.ALL; USE std.textio.all; Entity feeder Is port ( start: in std_logic; pixel_value: out bit_vector(7 downto 0) ); END feeder; Architecture Behavior OF feeder Is TYPE frame_txt IS FILE OF character; TYPE frame_array IS ARRAY (natural RANGE <>) OF bit_vector(7 DOWNTO 0); SIGNAL read_frame_array : frame_array(0 to 25343); begin read_file: PROCESS (start) IS FILE file_in : frame_txt OPEN read_mode IS " /afs/cad/u/s/m/smb34/vlsi2/frame1.txt"; -- open the frame file for reading VARIABLE char_buffer : character; BEGIN IF start'EVENT AND start = '1' THEN FOR i IN read_frame_array'RANGE LOOP read(file_in, char_buffer); read_frame_array(i) <= bit_vector(to_unsigned(character'POS(char_buffer), 8)); pixel_value <= read_frame_array(i); --frame_array_byte(i) <= CONV_STD_LOGIC_VECTOR(character'pos(char_buffer), 8); END LOOP; -- i file_close(file_in); END IF; END PROCESS read_file; End Behavior; please, let me know what i have done wrong, so i can read the write values out of the file?!7 Replies
- Altera_Forum
Honored Contributor
What you see is the ASCII code of your text file.
50 = 2 49 = 1 10 = line feed 51 = 3 48 = 0 10 = line feed 51 = 3 try this code:LIBRARY ieee; USE ieee.std_logic_1164.ALL; USE ieee.std_logic_textio.ALL; USE ieee.numeric_std.ALL; USE std.textio.ALL; ENTITY feeder IS PORT ( start : IN std_logic; pixel_value : OUT std_logic_vector(7 DOWNTO 0) ); END feeder; ARCHITECTURE Behavior OF feeder IS CONSTANT filename : string := "/afs/cad/u/s/m/smb34/vlsi2/frame1.txt"; TYPE frame_array IS ARRAY (natural RANGE <>) OF std_logic_vector(7 DOWNTO 0); SIGNAL read_frame_array : frame_array(0 TO 25343); BEGIN read_file : PROCESS (start) IS FILE file_in : text; -- file ID VARIABLE line_rd : line; -- text line VARIABLE data_in : integer; -- integer data input VARIABLE i : integer := 0; -- line counter BEGIN IF rising_edge(start) THEN file_open(file_in, filename, read_mode); WHILE (NOT endfile(file_in)) LOOP readline(file_in, line_rd); read(line_rd, data_in); read_frame_array(i) <= std_logic_vector(to_unsigned(data_in, 8)); pixel_value <= read_frame_array(i); i := i + 1; END LOOP; file_close(file_in); END IF; END PROCESS read_file; END Behavior; - Altera_Forum
Honored Contributor
thanks a lot for replying to my issue, but it does give me the following error for type of conversation
Illegal type conversion from ieee.NUMERIC_BIT.UNSIGNED to ieee.std_logic_1164.STD_LOGIC_VECTOR (array element type difference). i have tried different library, but it still did not solve the issue ?? - Altera_Forum
Honored Contributor
the error in this line command:
read_frame_array(i) <= std_logic_vector(to_unsigned(data_in, 8)); Illegal type conversion from ieee.NUMERIC_BIT.UNSIGNED to ieee.std_logic_1164.STD_LOGIC_VECTOR (array element type difference). i have tried different library, but it still did not solve the issue ?? - Altera_Forum
Honored Contributor
I see, you have to use the ieee.numeric_std.ALL instead of ieee.numeric_bit.ALL library.
- Altera_Forum
Honored Contributor
dear schmalisch,
actually the library did solve the compilation issue, but i do know getting some values in the following form instead of bits: uuuuuuuu uuuuuuuu uuuuuuuu at my simulation ? the matlab did show that the file contain character type , but you reading integer .. do you think that it is the reason?? - Altera_Forum
Honored Contributor
This undefined 'U' states occur when signals do not have a defined state right from the beginning of the simulation.
We can get rid of this by changing the process to this code:read_file : PROCESS IS FILE file_in : text; -- file ID VARIABLE line_rd : line; -- text line VARIABLE data_in : integer; -- integer data input VARIABLE i : integer := 0; -- line counter BEGIN file_open(file_in, filename, read_mode); pixel_value <= (OTHERS => '0'); WAIT UNTIL rising_edge(start); WHILE (NOT endfile(file_in)) LOOP readline(file_in, line_rd); read(line_rd, data_in); read_frame_array(i) <= std_logic_vector(to_unsigned(data_in, 8)); pixel_value <= std_logic_vector(to_unsigned(data_in, 8)); i := i + 1; WAIT UNTIL rising_edge(start); END LOOP; file_close(file_in); WAIT; END PROCESS read_file; - Altera_Forum
Honored Contributor
thanks so much , not only for solving my problem but also for explaining to me why it went wrong.
Regards