read binary file

4

44mc44

Hi

I would like to read data from the binary file.
I have written the following code:

LIBRARY IEEE;
use IEEE.std_logic_1164.all, IEEE.numeric_std.all;

entity io is
port
(
clk : in std_logic;
ena : in std_logic;
Q : out std_logic_vector (7 downto 0 )
);
end entity io;

architecture czytaj of io is

type log_file is file of std_logic_vector (7 downto 0);
file my_file : log_file;

begin

process (clk, ena)
variable my_byte : std_logic_vector (7 downto 0);
begin
file_open( my_file, "F:\cyfry\train-images.idx3-ubyte", read_mode);
if rising_edge(clk) then
if ena = '1' then read(my_file, my_byte);
end if;
end if;
Q <= my_byte;
end process;
end architecture czytaj ;

I tasted it in ModelSim but i was suprised by the values of Q : (for
example )
(U, U, ?(23), ?(78), U, U , U, ?(106))
What do I wrong? Why values of Q are so strange?

Please help me solve this mistery.

Regards
Michał
 
G

goouse99

Am Sonntag, 15. Juli 2012 23:16:17 UTC+2 schrieb 44mc44:
Hi

I would like to read data from the binary file.
I have written the following code:

LIBRARY IEEE;
use IEEE.std_logic_1164.all, IEEE.numeric_std.all;

entity io is
port
(
clk : in std_logic;
ena : in std_logic;
Q : out std_logic_vector (7 downto 0 )
);
end entity io;

architecture czytaj of io is

type log_file is file of std_logic_vector (7 downto 0);
file my_file : log_file;

begin

process (clk, ena)
variable my_byte : std_logic_vector (7 downto 0);
begin
file_open( my_file, &quot;F:\cyfry\train-images.idx3-ubyte&quot;, read_mode);
if rising_edge(clk) then
if ena = '1' then read(my_file, my_byte);
end if;
end if;
Q &lt;= my_byte;
end process;
end architecture czytaj ;

I tasted it in ModelSim but i was suprised by the values of Q : (for
example )
(U, U, ?(23), ?(78), U, U , U, ?(106))
What do I wrong? Why values of Q are so strange?

Please help me solve this mistery.

Regards
Michał

Hi,
read this section the forums FAQ:
4.2.11 How to Read/Write Binary Files.

You are not using any file_io package, so the read function will have problems to interpret the incoming data as std_logic values.

You might create a file of std_logic(_vector) type using the write functionand then inspect it with other tools to see what its contents looks like and compare this with an ordinary binary file.

Have a nice simulation
Eilert
 
M

Martin Thompson

44mc44 said:
Hi

I would like to read data from the binary file.
file_open( my_file, "F:\cyfry\train-images.idx3-ubyte", read_mode);

If you are attempting to read image files (which this line indicates you
might be) you might some of these pages I wrote helpful:

http://parallelpoints.com/node/65
http://parallelpoints.com/node/66
http://parallelpoints.com/node/67

Code is available here:
https://github.com/martinjthompson/image_processing_examples/tree/master/hdl
 
T

Tricky

Hi

I would like to read data from the binary file.
I have written the following code:

LIBRARY IEEE;
use IEEE.std_logic_1164.all, IEEE.numeric_std.all;

entity io is
port
(
clk : in std_logic;
ena : in std_logic;
Q : out std_logic_vector (7 downto 0 )
);
end entity io;

architecture czytaj of io is

type log_file is file of std_logic_vector (7 downto 0);
file my_file : log_file;

begin

process (clk, ena)
variable my_byte : std_logic_vector (7 downto 0);
begin
file_open( my_file, &quot;F:\cyfry\train-images.idx3-ubyte&quot;, read_mode);
if rising_edge(clk) then
if ena = '1' then read(my_file, my_byte);
end if;
end if;
Q &lt;= my_byte;
end process;
end architecture czytaj ;

I tasted it in ModelSim but i was suprised by the values of Q : (for
example )
(U, U, ?(23), ?(78), U, U , U, ?(106))
What do I wrong? Why values of Q are so strange?

Please help me solve this mistery.

Regards
Michał

First of all, remember that std_logic_vectors are arrays of std_logic, eachof which has 9 possible states. so for a 8 bit std_logic_vector, there are9^8 possible values. a file of bit_vectors may be more appriate - and Ive seen people use integers too.

BUT

The problem is there is no standard way to read binary files. In modelsim you can fudge it quite nicely by using character reading:

type char_file_t is file of character;

then you can read individual characters which corespond to individual bytes.. You can then peice these together to form std_logic_vectors, or integers,or whatever else you were expecting.

variable c_buf : character;
variable my_int : integer;
file df : char_file_t open read_mode is "my_data_file.dat";

....

read(df, c_buf);
my_int := character'pos(c_buf);

This all works fine and dandy in modelsim with no problems. Ive used it fora few years to read and write bitmap files (be sure to read up on the header formatting).

The second problem is that as there is no standard, some simulators wont handle a data file unless it has a specific header on it (Im talking to you Xilinx) and they wont tell you what that header should be. And I think others just wont work at all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top