Loading hex and binary data at the same time from text file into std_logic_vector

vhdl

I'm trying to create a simple cache to use in simulation with my processor design.

I want to populate the cache with instructions that are stored in a file, .bin or .txt.

The file has no more than 1023 lines, and each line has the following format:

addr[tab]instruction

Adr is the address of the instruction, and the instruction is the instruction code, and [tab] is a tab (space between addr and instruction).

Here's the problem:

Addr is written as a hex value, has 32 bits, and instruction is written as a binary value
and also has 32 bits.
So one line looks like, for example, this

0000000A[tab]00001000000100001110000000000011

My idea was to create an array of std logic vectors and each line was one std logic vector where elem(63 downto 32) would be the address and elem (31 downto 0) would be the instruction code.

How do I read each line and at the same time convert hex value into binary, ignore tab, read the instruction and store everything into one std logic vector?

Thanks in advance for your help.

EDIT:

Would something like this work:

while not endfile(load_file) loop
        readline(load_file, rdline);
        hread(rdline, address);
        read(rdline, data);
        memory(index) := data;
        index := index + 1;
end loop;

My question is: does hread move the cursor when reading the line? Also, how would I go about skipping the tab character?

Best Answer

The basic process is to

  1. Open the file for reading using file_open().
  2. Loop over each line until end-of-file is encountered:
    1. Read the line to a variable of type line using readline().
    2. Read the line as a string using read().
    3. Separate the string into the two parts you're interst using indeces.
    4. Convert the strings character-by-character to std_logic_vectors using a conversion function (I can't find a built in one - I might be missing the obvious - but it's just a simple case statement with cases for all of the characters you're interesed in and a flag or assert for any unexpected ones).

There's plenty of examples for the file IO part: