read binary file

Discussion in 'VHDL' started by 44mc44, Jul 15, 2012.

  1. 44mc44

    44mc44 Guest

    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ł
     
    44mc44, Jul 15, 2012
    #1
    1. Advertising

  2. 44mc44

    Guest

    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
     
    , Jul 16, 2012
    #2
    1. Advertising

  3. Martin Thompson, Jul 16, 2012
    #3
  4. 44mc44

    Tricky Guest

    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), ?(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.
     
    Tricky, Jul 18, 2012
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug
    Replies:
    3
    Views:
    4,379
    Bruce Barker
    Nov 4, 2005
  2. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    935
    James Kanze
    Apr 28, 2008
  3. Jim
    Replies:
    6
    Views:
    736
  4. Sniper Abandon
    Replies:
    3
    Views:
    238
    Robert Klemme
    Nov 19, 2009
  5. Alex Dowad
    Replies:
    4
    Views:
    273
    Michel Demazure
    May 1, 2010
Loading...

Share This Page