Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > VHDL > read binary file

Reply
Thread Tools

read binary file

 
 
44mc44
Guest
Posts: n/a
 
      07-15-2012
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), ?(7, U, U , U, ?(106))
What do I wrong? Why values of Q are so strange?

Please help me solve this mistery.

Regards
Michał


 
Reply With Quote
 
 
 
 
goouse99@googlemail.com
Guest
Posts: n/a
 
      07-16-2012
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), ?(7, 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
 
Reply With Quote
 
 
 
 
Martin Thompson
Guest
Posts: n/a
 
      07-16-2012
44mc44 <> writes:

> 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/i...ree/master/hdl


--

 
Reply With Quote
 
Tricky
Guest
Posts: n/a
 
      07-18-2012
On Sunday, July 15, 2012 10:16:17 PM UTC+1, 44mc44 wrote:
> 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), ?(7, 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File.read(fname) vs. File.read(fname,File.size(fname)) Alex Dowad Ruby 4 05-01-2010 08:20 AM
open a binary file-read bit-change bit-update same file Sniper Abandon Ruby 3 11-19-2009 12:49 PM
writing binary file (ios::binary) Ron Eggler C++ 9 04-28-2008 08:20 AM
Newbie: working with binary files/extract png from a binary file Jim Ruby 5 02-01-2008 11:21 AM
Binary data stored in SQL Server: can't read from ASP.NET, *can* read from Access? Doug ASP .Net 3 11-04-2005 07:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57