How to read data using $fread

Guest
Hi,
I am trying to read the data data from file using $fread. But data is
not loading into memory correctly.
Here is code.

module fileio;
integer r1,c1;
reg [7:0] mem1[0:100];
reg [7:0] mem2[0:100];
integer i;
initial
begin
r1 = $fopen("data2.log","rb");
c1 = $fread(mem1,r1);
for(i = 0;i < 7; i = i+1)
begin
$display("mem1[%0d] = %b" ,i,mem1);
end

$display("file1 = %0d ",c1);
end
endmodule

data2.log file contains the following data
11110000
10101010
01010101

I am getting the following the output..

# mem1[0] = 00110001
# mem1[1] = 00110001
# mem1[2] = 00110001
# mem1[3] = 00110001
# mem1[4] = 00110000
# mem1[5] = 00110000
# mem1[6] = 00110000
# file1 = 32

I am using questasim6.2b in windows. Can anybody explain me, what is
wrong in code i have written?
 
On Sun, 27 Jan 2008 08:55:28 -0800 (PST), vishnuprasanth@gmail.com
wrote:

Hi,
I am trying to read the data data from file using $fread. But data is
not loading into memory correctly.
I think it is.

module fileio;
integer r1,c1;
reg [7:0] mem1[0:100];
reg [7:0] mem2[0:100];
integer i;
initial
begin
r1 = $fopen("data2.log","rb");
c1 = $fread(mem1,r1);
for(i = 0;i < 7; i = i+1)
begin
$display("mem1[%0d] = %b" ,i,mem1);
end

$display("file1 = %0d ",c1);
end
endmodule

data2.log file contains the following data
11110000
10101010
01010101

I am getting the following the output..

# mem1[0] = 00110001
# mem1[1] = 00110001
# mem1[2] = 00110001
# mem1[3] = 00110001
# mem1[4] = 00110000
# mem1[5] = 00110000
# mem1[6] = 00110000
# file1 = 32

Has it occurred to you to ask what "00110001" and
"00110000" (32) might be? They are the ASCII codes
(in 8-bit binary, and 32 in decimal) for the
characters '1' and '0'. So what you're seeing in
your first 8 locations is the eight ASCII codes
corresponding to the string "11110000", the
first few characters of your file.

Go back and read the documentation on $fread a
little more carefully, and then consider whether
you should instead be using $readmemb. I suspect
$readmemb will give you the effect you want with
a lot less effort.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 
On Sun, 27 Jan 2008 17:16:51 +0000,
Jonathan Bromley <jonathan.bromley@MYCOMPANY.com> wrote:

Another brain-fade. This is getting to be a habit :-(

Has it occurred to you to ask what "00110001" and
"00110000" (32) might be? They are the ASCII codes
(in 8-bit binary, and 32 in decimal) for the
The "32" result from the $fread call is the number of
characters consumed by the call; it has nothing to do
with the ASCII code for '0' which, of course, is 48
(binary 00110000).

I think I must have hit a few keys by mistake while
Matron was washing-out my dentures. Apologies.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan.bromley@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.
 

Welcome to EDABoard.com

Sponsor

Back
Top