$fread - Invalid second parameter

D

Dave Gowans

Guest
Hi

I've been trying to read a binary file into memory using the $fread system
task but on both ModelSim and Veritak simulators I get an error telling me
the second parameter/argument is wrong. I can't find any documentation that
specifies exactly how $fread should be used, though, and experimentation has
got me nowhere!

I'm currently using the follwoing code:
`define EOF 32'HFFFF_FFFF
`define MEM_SIZE 200_000

module load_mem;

integer file, i;
reg [7:0] mem[0:`MEM_SIZE];
reg [80*8:1] file_name;

initial
begin
file_name = "data.bin";
file = $fopen(file_name);
i = $fread(file, mem[0]);
$display("Loaded %0d entries \n", i);
// i = $fclose(file);
$stop;
end

endmodule // load_mem


and Veritak gives me the error:
VPI ERROR $fread: invalid second parameter (must be variable).
Loaded 0 entries

I'd appreciate any help anyone could give (or any working $fread code I
could take a look at).

Cheers

Dave
 
Dave,
From V2K LRM, $fread works as follows:

$fread(mem_or_reg, file_descriptor);

With respect to your Veritak error, perhaps they don't support mem as
argument, try reading it to a reg and assign it to mem.

HTH
Ajeetha
--
www.noveldv.com
Interested in expert PSL/SVA training in Bangalore?
Visit www.noveldv.com/cvc.html

A simple working code (in VCS) is:

module load_mem;
integer file, i;
reg [7:0] mem[0:`MEM_SIZE];
reg [80*8:1] file_name;

initial
begin
file_name = "data.bin";
file = $fopen(file_name, "r");
i = $fread(mem, file);
$display("Loaded %0d entries \n", i);
// i = $fclose(file);
$stop;
end


endmodule // load_mem
 
As Ajeetha says, the file_descriptor is the second argument, not the
first. The first argument is the memory or reg to be read into. There
are also optional additional arguments to be used with memories,
providing a starting address in the memory and a count of memory words
to be read.

Another issue is that the $fopen must include a second argument "r" to
open the file in read mode. Without it, the file will be opened in
write mode, and instead of a file descriptor you will get an old-style
multi-channel-descriptor, which cannot be used with the newer file I/O
routines.
 
Dave-san,

Veritak can handle Ajeetha-san's bench with one exception below.
file = $fopen(file_name, "r");
It is important to use "rb"/"wb" instead of "r"/"w" in
reading/writing BINARY format in Windows.

See example below.
http://www.sugawara-systems.com/verilog-2001/fileio.htm

Veritak project file can be found in your installed folder of
"regression_test/fileio" for that sample.

Tak
 

Welcome to EDABoard.com

Sponsor

Back
Top