UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Explorer
Explorer
6,209 Views
Registered: ‎08-26-2014

How to read a data file from the testbench?

Jump to solution

Hello,

 

I am trying to provide input values (generated in Matlab) to an HLS function, compute the calculations, and compare the results with the golden data I have from Matlab.

 

The way I thought to do it is writing double-precision data (in binary format) in a file using this Matlab code:

 

 

data = rand(10,1);
file = fopen('input.dat','w');
fwrite(file,typecast(data,'uint8'),'uint8');
fclose(file);

 

 

Then in Vivado HLS, I try to read the data byte-to-byte using pointers (remember a double has 8 bytes) in this way:

 

 

FILE *input_file;
double data, *data_ptr;

*data_ptr = 0;

input_file = fopen("D:\input.dat","r");

for(x=0;x<8;x++) //read only one value
{
    (char)*data_ptr++ = fgetc(input_file);
}

fclose(input_file);

data = *data_ptr;

 

But when I compile in HLS I get this error in the fgetc line:

 

lvalue required as left operand of assignment

Does anyone know why? Or does anyone know a better way to read double-precision data from a file?

 

Many thanks,

 

Cerilet

0 Kudos
1 Solution

Accepted Solutions
Scholar u4223374
Scholar
10,725 Views
Registered: ‎04-26-2015

Re: How to read a data file from the testbench?

Jump to solution

Lots of issues.

 

(1) As @muzaffer has said, typecasting a double to a uint8 will only give meaningful results if the original value is an integer between 0 and 255 (inclusive). Much better to just write it as floating-point:

 

file = fopen('input.dat','wb');
fwrite(file,data,'double');

Also note that I've opened the file with "wb" to force binary mode. I think Matlab does that by default, but it doesn't hurt to be sure.

 

(2) Your don't actually allocate any space for your "data" pointer. Malloc or calloc is your friend.

 

(3) You can read the whole lot at once very easily:

FILE * input_file = fopen("D:\input.dat","rb");
fread(data,10,8,input_file);

Note that I've opened this in binary mode again.

4 Replies
Teacher muzaffer
Teacher
6,187 Views
Registered: ‎03-31-2012

Re: How to read a data file from the testbench?

Jump to solution

the error is very descriptive: ++ operator doesn't return an lvalue so you can't write into it.
for reading binary data, on the same system, you can use fread into a buffer.

Also you may want to write a double directly from Matlab as double instead of casting it to a uint8 (doesn't uint8 mean an 8 bit unsigned integer?)

- 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.
Scholar u4223374
Scholar
10,726 Views
Registered: ‎04-26-2015

Re: How to read a data file from the testbench?

Jump to solution

Lots of issues.

 

(1) As @muzaffer has said, typecasting a double to a uint8 will only give meaningful results if the original value is an integer between 0 and 255 (inclusive). Much better to just write it as floating-point:

 

file = fopen('input.dat','wb');
fwrite(file,data,'double');

Also note that I've opened the file with "wb" to force binary mode. I think Matlab does that by default, but it doesn't hurt to be sure.

 

(2) Your don't actually allocate any space for your "data" pointer. Malloc or calloc is your friend.

 

(3) You can read the whole lot at once very easily:

FILE * input_file = fopen("D:\input.dat","rb");
fread(data,10,8,input_file);

Note that I've opened this in binary mode again.

Explorer
Explorer
6,125 Views
Registered: ‎08-26-2014

Re: How to read a data file from the testbench?

Jump to solution

Well, thanks, it works!

 

The thing was to use fread (completely forgot about this function) and store it into a string of doubles:

 

double data[10];
FILE *input_file;

input_file = fopen("D:\input.dat","rb");
fread(data,10,8,input_file);

// whatever you have to do with the data...

fclose(input_file);

However, what you say about the typecasting is not actually correct. When using this function, it converts the double into their 8 corresponding bytes. I was doing this because I needed to know the bytes of every double value in another application. I just copied the same code here, but it actually works like this, also without the typecasting and writing 'double' instead of 'uint8'.

 

Thanks again!

 

Cerilet

Scholar u4223374
Scholar
6,117 Views
Registered: ‎04-26-2015

Re: How to read a data file from the testbench?

Jump to solution

Huh, you're totally right about Matlab's typecasting. I had "typecast" confused with "cast" (which does indeed saturate it to 255).

 

I did a bit more research on binary mode for files. Matlab does default to binary mode with "fopen", although leaving the extra "b" there does no harm (you can force it to text-mode with "wt" or "rt"). C is the opposite; it opens everything in text mode by default but can be forced to binary mode with the extra "b".

 

For transfers like this, where you're using pure binary data and you don't want extra text-mode characters added or removed automatically, keeping it in binary mode is definitely the safest option.