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

What's the format of multidimensional data file for $readmemh?

3,135 views
Skip to first unread message

Peng Yu

unread,
Jul 27, 2003, 7:53:41 PM7/27/03
to
Hi,
Do somebody know what's the format of multidimensional data file for $readmemh?
For example, I have a memory
reg [1:0] mem [0:2][0:3];
How to write the file for the initialization of the memory.
$readmemh("mem.data", mem);
Best wishes,
Peng

Steven Sharp

unread,
Jul 28, 2003, 3:57:57 PM7/28/03
to
yup...@hotmail.com (Peng Yu) wrote in message news:<d7b3726c.03072...@posting.google.com>...

There is no such format. $readmem was defined to read memories,
which are 1-dimensional arrays. It is not defined to read the
multi-dimensional arrays that were added in Verilog-2001. If
you want to use $readmem, then you will have to flatten your
array into a 1-dimensional array.

If you want to use multi-dimensional arrays, then you cannot
use $readmem to read values into them. However, Verilog-2001
also added general file reading capabilities to the language.
This allows you to write your own Verilog code to read data
files into your multi-dimensional arrays. For example, you
could write

integer i,j,fd,rv;

initial
begin
fd = $fopen( "mem.data", "r");
for (i = 0; i < 4; i = i + 1)
for (j = 0; j < 3; j = j + 1)
rv = $fscanf(fd, "%h", mem[j][i]);
end

Your code determines what order the elements will be read
and what format the input data must be in. If you wanted
to add more complex capabilities like embedding addresses
in the file, you could do so, but you would have to write
the Verilog code to parse and interpret the contents of
the file for yourself.

If your simulator supports Verilog-2001 multi-dimensional
arrays but does not support Verilog-2001 file I/O routines,
then you have a problem. I suppose you could write VPI to
do the job, assuming that your simulator supports VPI and
VPI access to multi-dimensional arrays.

Peng Yu

unread,
Jul 30, 2003, 2:07:28 AM7/30/03
to
> integer i,j,fd,rv;
>
> initial
> begin
> fd = $fopen( "mem.data", "r");
> for (i = 0; i < 4; i = i + 1)
> for (j = 0; j < 3; j = j + 1)
> rv = $fscanf(fd, "%h", mem[j][i]);/* #1 rv = $fscanf(fd, "%h", mem[j][i]);*/
> end
Do I need to add some delay to the line with comment? Otherwise, it doesn't work.
Peng

Andy Peters

unread,
Jul 30, 2003, 3:46:27 PM7/30/03
to
yup...@hotmail.com (Peng Yu) wrote in message news:<d7b3726c.0307...@posting.google.com>...

No -- the simulation should read the entire file into the array in
zero simulation time.

-a

Steven Sharp

unread,
Jul 30, 2003, 6:13:44 PM7/30/03
to
yup...@hotmail.com (Peng Yu) wrote in message news:<d7b3726c.0307...@posting.google.com>...

> >
> > rv = $fscanf(fd, "%h", mem[j][i]);/* #1 rv = $fscanf(fd, "%h", mem[j][i]);*/
> > end
> Do I need to add some delay to the line with comment? Otherwise, it doesn't work.

There is no reason to add a delay to the line. Presumably you want the
entire memory initialized at once at the start of the simulation, not
one location at a time. For a large memory, the later locations in the
memory wouldn't get initialized until a long time into the simulation
if you added a delay between initializing each location.

If it isn't working without a delay, then your simulator is broken.

0 new messages