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

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?

EDIT:

Would something like this work:

while not endfile(load_file) loop
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?

• Why do you store the instruction in binary format? It's easier to store both words in the same format. What do you have so for? Aug 24, 2015 at 12:19
• It's a requirement of the project I'm working on, and I can't change that. Aug 24, 2015 at 21:57
• @Paebbels I updated my question with the function I tried. Aug 24, 2015 at 22:03

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:

• Sorry there's no code - I don't have an environment to try it out here. Aug 24, 2015 at 12:50
• Thanks for your suggestion. When I read the line, can I select the first eight characters and convert them from hex to binary using hread? Is there a way to automatically read the binary part into std_logic_vector (maybe using some default or built-in conversion?) Aug 24, 2015 at 14:28
• I'm not familiar with one for binary, but it is just a very simple case statement. You could look at the code of the textio package HREAD procedure and simplify it to only accept 1 or 0. Aug 24, 2015 at 14:36
• Since the hex and binary parts are separated by whitespace, you can use hread() to read from the line directly into your address, then use read() from the same line to read characters 1 and 0 into an std_logic_vector. This is using std_logic_textio. Aug 25, 2015 at 7:49

This is more of a comment, but I'll post it here for the sake of formatting. As mentioned elsewhere, this utility function might help.

    FUNCTION hex2bin (hex: character) RETURN std_logic_vector IS
VARIABLE result : std_logic_vector (3 downto 0);
BEGIN
CASE hex IS
WHEN '0' =>     result := "0000";
WHEN '1' =>     result := "0001";
WHEN '2' =>     result := "0010";
WHEN '3' =>     result := "0011";
WHEN '4' =>     result := "0100";
WHEN '5' =>     result := "0101";
WHEN '6' =>     result := "0110";
WHEN '7' =>     result := "0111";
WHEN '8' =>     result := "1000";
WHEN '9' =>     result := "1001";
WHEN 'A'|'a' => result := "1010";
WHEN 'B'|'b' => result := "1011";
WHEN 'C'|'c' => result := "1100";
WHEN 'D'|'d' => result := "1101";
WHEN 'E'|'e' => result := "1110";
WHEN 'F'|'f' => result := "1111";
WHEN 'X'|'x' => result := "XXXX";
WHEN others =>  NULL;
END CASE;
RETURN result;
END;

• Thanks for the manual implementation, I'm sure others will find it useful. Just curious, is this, in terms of functionality, in any way different than hread? Aug 24, 2015 at 21:58
• hread reads from a line. His function converts a CHARACTER to a 4-bit SLV. Aug 24, 2015 at 22:16
• For simulation I would probably make the others case throw an assert to warn you on case of a badly formatted text file. Aug 25, 2015 at 7:52