Jump to content

Reading a binary file in UVM - $fread

Recommended Posts


I am trying to read a binary file, but I am getting an error when trying to load the design.

I have a transaction class and a sequence class parametrized with the transaction type.

The yy_data field from the transaction class will receive either:

- a constant or (this is ok - no errors);

- the data read from the external file (this doesn't works, error occurs);

The code looks like this:

class pixl_transaction extends uvm_sequence_item;

rand logic [7:0] yy_data;

function new( string name = "" );

super.new( name );

endfunction: new

`uvm_object_utils_begin( pixl_transaction )

`uvm_field_int (yy_data, UVM_ALL_ON)


endclass: pixl_transaction

class pixl_sequence extends uvm_sequence#( pixl_transaction );

rand int unsigned num_pix;

local integer fd; // file descriptor

local integer read_data_fd; // status from the $fread()

local reg [7:0] data_from_file; // temporary field that receives the content from the external file

local pixl_sequence self = this;

constraint num_pix_con { num_pix inside { [1:4] }; }

function new( string name = "" );

super.new( name );

// not sure if this is the correct place to open the external file

fd = $fopen ("ipp_in_yy_00.raw", "rb");

// no error - it seems that the file was open. A file descriptor was assigned;

$display("At time =%t, File ipp_in_yy_00.raw was open. FD = %h", $time(), fd);

endfunction: new

task body();

pixl_transaction px_tx;

repeat ( num_pix ) begin

px_tx = pixl_transaction::type_id::create( .name( "px_tx" ), .contxt( get_full_name() ) );

start_item( px_tx );


// update transaction field with a constant - ok

px_tx.yy_data = 8'hfa;//data_from_file;

// update transaction field with data from external file

read_data_fd = $fread(data_from_file, fd); // this is causing the error

//px_tx.yy_data = data_from_file;

`uvm_info( get_name(), { "\n", px_tx.sprint() }, UVM_LOW )

finish_item( px_tx );


endtask: body

`uvm_object_utils_begin( pixl_sequence )

`uvm_field_int( num_pix, UVM_ALL_ON )


endclass: pixl_sequence

The code compiles ok.

The load of the design generates the below error:

# Loading sv_std.std

# Loading mtiUvm.uvm_pkg(fast)

# Loading work.top(fast)

# Loading work.pixl_if(fast)

# Loading work.pixl_pkg(fast)

# Loading work.pixl_subsystem(fast)

# Loading mtiUvm.questa_uvm_pkg(fast)

# ** Error: (vsim-PLI-3068) pixl.sv(210): $fread : Invalid argument.


# Region: /pixl_pkg::pixl_sequence::body/#ublk#0#204

# Loading C:/programs/modeltech_10.1c/uvm-1.1b\win32\uvm_dpi.dll

# Error loading design

Please advise if you have an idea.

Thank you,


Link to comment
Share on other sites

Hi Dave,

Thank you for your reply.

I am trying to convert a working Verilog module, that uses $fread(), in UVM.

I think that the problem is more related to my UVM coding (I am a beginner in UVM) - the $fread(), I've used lots of time in Verilog coding without problems.

If anyone has a working example of reading a binary file in UVM - it will be great.

Thank you.

Link to comment
Share on other sites

  • 1 year later...

Hey Apafi (and all the others reading this;)),



It has been really long time since this question was posted here so I expect Apafi already found a solution of his problem - I just wanted to reply for other people facing a similar problem and looking for a solution...


I'm trying to do almost exactly the same thing as Apafi did in his code before and it works fine for me. There are basically only three "potentially significant" differences between his and my code:


   1) I don't open a binary file to be read from inside the sequence constructor, but directly from inside its body task.


   2) I don't declare a temporary variable storing data read from a binary file as a local sequence class attribute, but as a local body task variable (so it is declared inside the body task itself).


   3) I basically don't care of the $fread return code so I retype it to void like "void '($fread (read_data, file_desc))".


Hope the same will work for you there as well:)



Have fun with that and a nice day out there!




Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...