cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Contributor
Contributor
5,865 Views
Registered: ‎07-25-2016

Reading a hexadecimal formatted file into fixed point values

Jump to solution

Hi all,

I'm not an expert of Vivado HLS but I'm trying to implement a simple module. In doing so, in the testbench I read a file where the data to be processed are stored in the hexadecimal format.

The module works with fixed-point signed values. So what I do is first storing the data as it is in a unsigned integer, then I copy it back bit after bit. Here is the code:

 

 

#define DATA_WIDTH 8
#define INT_WIDTH 1

[...]

ap_fixed<DATA_WIDTH, INT_WIDTH> din = 0;
ap_uint<DATA_WIDTH> indata_hex = 0;

[...]

// for each line/value
input_file >> hex >> indata_hex; for (int k = DATA_WIDTH-2; k >= 0; k--) { din[k] = indata_hex[k]; } if (indata_hex[DATA_WIDTH-1] == 1) { din = -din; }

[...]

 

The data are stored in this simple way:

 

...
e2
c4
ec
0b
f6
b6
13
...

 

However, when it comes to read the file, the code gets stuck when it's time to read the value '0b', giving me the following error (in Vivado 2016.2):

 

csim.exe: /opt/Xilinx/Vivado_HLS/2016.2/include/etc/ap_private.h:1581: ap_private<_AP_W, _AP_S, true>::ap_private(const string&, uint8_t) [with int _AP_W = 8, bool _AP_S = false, std::string = std::basic_string<char>, uint8_t = unsigned char]: Assertion `!val.empty() && "String empty?"' failed.
@E Simulation failed.
CRITICAL WARNING: [SIM 211-1] CSim failed with errors.

 

I know it sounds odd, especially because it happens with just that particular value. Even if I try to change its position in the file, as soon as it's time to read it, the code breaks, unable to read the value.

 

So if you have any suggestion on why this happens or if you have other clever ways for reading hex data into a fixed value, it will be highly appreciated.

 

(I attach my code, it's a work in progress, so it might not be perfect and yet buggy, but this issue is blocking my work at its initial stage...)

 

Thank you, Regards,

 

Stefano

 

 

 

0 Kudos
Reply
1 Solution

Accepted Solutions
Teacher
Teacher
10,195 Views
Registered: ‎03-31-2012
It's interesting that the value is the start of a binary value "0b" (ala 0x) so it's possible that there is some string processing somewhere which is getting confused into thinking it's looking at a string which has binary values in it and saying "hey there is no more stuff here. I need to see some ones & zeros". Sounds like a bug.

My suggestion would be to prepend 0x to your numbers in the file.
Another way is to use fscanf and then assign to fixed point variables by direct assignment after scaling:

int read_value;

din = read_value / (1.0 * (1 << (DATA_WIDTH-INT_WIDTH)));

With this you shouldn't need the bit loop.
- Please mark the Answer as "Accept as solution" if information provided is helpful.
Give Kudos to a post which you think is helpful and reply oriented.

View solution in original post

0 Kudos
Reply
3 Replies
Advisor
Advisor
5,824 Views
Registered: ‎04-26-2015

Well, that's a new one! Bizarre that it has trouble with that specific value.

 

Is there a particular need to read in hex? You could just store those values as normal decimal numbers, read them as floating-point (single-precision floating-point will be able to store any 16-bit fixed-point value with perfect accuracy), and let HLS do the floating-to-fixed conversion. You could also dod this to avoid the bit-by-bit conversion:

 

 

 

Apart from that, maybe just read them "manually" using fscanf? I'm pretty sure it'll have no trouble doing the hex conversion since I've used that before.

0 Kudos
Reply
Contributor
Contributor
5,817 Views
Registered: ‎07-25-2016

Thank you for the reply. What I'm trying to do is optimizing an already developed verilog component. So I would like to use the same data used for its testbench.

Hence, while I'm reading the file, no conversion mechanism should take place. Meaning that the data (each bit) should be copied into the variables as they are.

These test data are pre-generated by a particular software. I could handle floating point numbers, but I wanted to avoid Vivado making the conversion floating-to-fixed in order to have a perfect match/consistency.

 

I will try with fscanf in the meanwhile.

 

P.s. Sorry but I think you're missing a part when you say "You could also dod this to avoid the bit-by-bit conversion:"

 

0 Kudos
Reply
Teacher
Teacher
10,196 Views
Registered: ‎03-31-2012
It's interesting that the value is the start of a binary value "0b" (ala 0x) so it's possible that there is some string processing somewhere which is getting confused into thinking it's looking at a string which has binary values in it and saying "hey there is no more stuff here. I need to see some ones & zeros". Sounds like a bug.

My suggestion would be to prepend 0x to your numbers in the file.
Another way is to use fscanf and then assign to fixed point variables by direct assignment after scaling:

int read_value;

din = read_value / (1.0 * (1 << (DATA_WIDTH-INT_WIDTH)));

With this you shouldn't need the bit loop.
- Please mark the Answer as "Accept as solution" if information provided is helpful.
Give Kudos to a post which you think is helpful and reply oriented.

View solution in original post

0 Kudos
Reply