Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

$fread - Invalid second parameter

425 views
Skip to first unread message

Dave Gowans

unread,
Oct 16, 2005, 3:44:15 PM10/16/05
to
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


Ajeetha

unread,
Oct 17, 2005, 1:49:35 AM10/17/05
to
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);

sh...@cadence.com

unread,
Oct 17, 2005, 1:30:41 PM10/17/05
to
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.

tak.s

unread,
Oct 18, 2005, 10:27:33 AM10/18/05
to
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

0 new messages