Read raw binary file

Discussion in 'VHDL' started by Beanut, Sep 2, 2005.

  1. Beanut

    Beanut Guest

    I've been playing around with reading binary data into a testbench. I
    noticed a lot of people asking questions on the user group over the
    years, but didn't see a simple expample, so here's some code that might
    help:

    constant N : integer := 28671; --Number of bytes in file minus
    one
    subtype file_element is std_logic_vector(7 downto 0);
    type mem_array is array(N downto 0) of file_element;

    shared variable memory : mem_array;

    begin
    ----------------------------------------------------------------------------

    -- This code reads a raw binary file one byte at a time.
    load_memory : process is
    type bit_vector_file is file of bit_vector;
    file vectors : bit_vector_file open read_mode is "calconst.bin";
    variable next_vector : bit_vector (0 downto 0);
    variable actual_len : natural;
    variable index : integer := 0;
    begin
    while not endfile(vectors) loop
    read(vectors, next_vector, actual_len);
    if actual_len > next_vector'length then
    report "vector too long";
    else
    memory(index) :=
    conv_std_logic_vector(bit'pos(next_vector(0)),8);
    index := index + 1;
    end if;
    end loop;
    wait;
    end process load_memory;
    -----------------------------------------------------------------------------

    Enjoy,
    Beanut
     
    Beanut, Sep 2, 2005
    #1
    1. Advertisements

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,583
    Bruce Barker
    Nov 4, 2005
  2. Achim Domma
    Replies:
    3
    Views:
    766
    Sjoerd Mullender
    Feb 17, 2004
  3. Bill Janssen
    Replies:
    2
    Views:
    864
    Michael Hudson
    Mar 2, 2004
  4. Bill Janssen
    Replies:
    2
    Views:
    579
    Francis Avila
    Mar 3, 2004
  5. anon
    Replies:
    1
    Views:
    506
    Donn Cave
    Mar 5, 2004
  6. r2
    Replies:
    7
    Views:
    5,432
    Jan Kaliszewski
    Jul 27, 2009
  7. Sniper Abandon
    Replies:
    3
    Views:
    392
    Robert Klemme
    Nov 19, 2009
  8. Alex Dowad
    Replies:
    4
    Views:
    644
    Michel Demazure
    May 1, 2010
Loading...