reading multiple columns from a single file

K

kaush

Guest
Hi ppl,
Well, I am trying to read input from a text file which is in
hex format. If it was only one column, then i would have
used $readmemh. But there are 8 columns, and i want to read
it from a single file itself.. Ya, i can always put them into
different files and read them(which i dont wanna do).

To be clear, the input file is as below ( in hex format )

b00a 100a 0d0a 500a 000a 000a 0f0a 100a
000b 0a0b 000b 070b 500b 000b f00b 080b
e00c 000c 600c 00ac 0e0c 000c 200c f00c
000d 0e0d 0c0d 00dd 000d 400d 000d 000d
700e e00e 000e 040e 000e 000e 300e a00e
000f 000f 0a0f 080f 000f 000f 000f a00f

to check for space, i guess that one has to check with "/"
if i am not wrong..
it would be kind if anybody can help me out...
i would be thankful to you...

hoping for best replies.. ;-)
 
"kaush" <telecow@gmail.com> writes:

To be clear, the input file is as below ( in hex format )

b00a 100a 0d0a 500a 000a 000a 0f0a 100a
If you have a Verilog-2001 compatible simulator you can use $fgets to
read it into a buffer and then shuffle chars and bytes into its actual
location. Or you can remove the spaces and readmemh it into a wide
memory and then copy. But this might be inefficient for large
data-sets.

Petter
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Hi petter,
First of all, let me thank you for your reply..
Ok, now coming to simulator, I am using Xilinx's Modelsim XE.
Well, as I said earlier, I have 8 ports.
If I am using $readmemh, then i am gonna mess it up
if all are in single file.
I have never used $fgets, as i am a bit new to this field..

well, this is what i have thought of... but i am not able to run..
there is no warning or no error.... but it just doesnt run...
ya, i know that i am a very bad programmer...

it would be nice if you could spend some time to take a look at the
below code

always @(posedge clk) begin
c1=$fgetc(file);
while ( c1 != `EOF ) begin
if ( enable ) begin
r1 = $ungetc(c1, file);
r1 = $fscanf( file, "%h", P1 );
r1 = $fscanf( file, "%h", P2 );
r1 = $fscanf( file, "%h", P3 );
r1 = $fscanf( file, "%h", P4 );
r1 = $fscanf( file, "%h", P5 );
r1 = $fscanf( file, "%h", P6 );
r1 = $fscanf( file, "%h", P7 );
r1 = $fscanf( file, "%h", P7 );
c1=$fgetc(file);
end // enable
else begin
w1_I <= 0;
w1_Q <= 0;
w2_I <= 0;
w2_Q <= 0;
w3_I <= 0;
w3_Q <= 0;
w4_I <= 0;
w4_Q <= 0;
end
end // EOF
$fclose(file);
end // always

Note : If the data is 16bit, can i just use %h or is there any other
format....?

I know that i am asking dumb silly doubts... but would be glad if you
could guide me...
thankz once again...
Petter Gustad wrote:
"kaush" <telecow@gmail.com> writes:

To be clear, the input file is as below ( in hex format )

b00a 100a 0d0a 500a 000a 000a 0f0a 100a

If you have a Verilog-2001 compatible simulator you can use $fgets to
read it into a buffer and then shuffle chars and bytes into its actual
location. Or you can remove the spaces and readmemh it into a wide
memory and then copy. But this might be inefficient for large
data-sets.

Petter
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Why not to use:
$fscanf(file, "%h %h %h %h %h %h %h %h\n", P1, P2, P3, P4, P5, P6, P7,
P8);
 
"kaush" <telecow@gmail.com> writes:

If I am using $readmemh, then i am gonna mess it up
As I mentioned earlier, you can use $readmemh if you remove the spaces
from your file. You can then copy the wide memory data into where you
need them for your simulation. This is not suitable for large
data-sets since you need twice as much storage to do this.

it would be nice if you could spend some time to take a look at the
below code

always @(posedge clk) begin
c1=$fgetc(file);
while ( c1 != `EOF ) begin
if ( enable ) begin
r1 = $ungetc(c1, file);
r1 = $fscanf( file, "%h", P1 );
You are reading the entire file (and perhaps more if your EOF value is
not correct, there is a systemtask called $feof which is suitable for
EOF detection) at every clock. I don't think this is what you want to
do. I guess you want to either read the entire file when you start
(you can do this in an initial block), or read a single line from the
file at each positive clock. In the latter case you do something like:

integer patfile;
integer fs;

reg [15:0] P1;
....

initial begin
patfile = $fopen("data.hex", "r");
end
....
always @(posedge clk) begin
fs = $fscanf(patfile,"%x%x%x%x%x%x%x%x",P1,P2,P3,P4,P5,P6,P7,P8);
if ($feof(patfile)) begin
$display("end of file");
$fclose(patfile);
$finish;
end
$displayh(P1,P2,P3,P4,P5,P6,P7,P8);
end

Petter
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
kaush wrote:
Hi ppl,
Well, I am trying to read input from a text file which is in
hex format. If it was only one column, then i would have
used $readmemh. But there are 8 columns, and i want to read
it from a single file itself.. Ya, i can always put them into
different files and read them(which i dont wanna do).
Kaush,

You can use $readmemh on a file with multiple columns. I've done this
with the "big-3" simulators with no problems. The simulator reads a
line and simply puts the data into sequential locations in the memory.

file.txt:
b00a 100a 0d0a 500a 000a 000a 0f0a 100a
000b 0a0b 000b 070b 500b 000b f00b 080b

reg [15:0] mem [0:1023];
initial $readmemh("file.txt", mem);

After the readmemh:
mem[0] = b00a
mem[1] = 100a
....
mem[7] = 100a
mem[8] = 000b
....
and so on.

In fact, in the case of Modelsim, you could declare a 2-d array and
read the file into columns.

reg [15:0] mem[0:127][0:7];
initial $readmemh("file.txt", mem);

After the readmemh:
mem[0][0] = b00a
mem[0][1] = 100a
....
mem[0][7] = 100a
mem[1][0] = 000b
....
and so on.

I talked to Synopsys about this for VCS but I don't know if they've
implemented it.

For Modelsim, VCS and NC, the first version works fine for smallish
files, if the target memory is less than 1 Mb. If you're dealing with a
lot of data, I'd go with the $fopen/$fscanf approach others have
described. It depends on the size the the file, how many files you're
reading, the amount of memory in your server, etc.

-cb
 
hi Chris, Petter, Michel,
Thankz a ton for your response... It finally worked... well, i got
lots of information form the responses.. finally, its working... but
the funnier part is the next problem...
well, how do i generate .bit file.....
if possible, plz let me know if my method is right..

use Synpify tool to compile the project and produce a
netlist. use this netlist with a constraints file (*.ucf) in Xilinx Ise

environment and generate bit stream file.
but i am a bit confused with this netlist file and constraints file..
my head is a bit messy.... sometimes i feel that there is nothing in my
head.. ;-(
well...... i try to do things by myself and mess it up big time...
would be greatful, if you could give me some guidelines

Chris Briggs wrote:
kaush wrote:
Hi ppl,
Well, I am trying to read input from a text file which is in
hex format. If it was only one column, then i would have
used $readmemh. But there are 8 columns, and i want to read
it from a single file itself.. Ya, i can always put them into
different files and read them(which i dont wanna do).

Kaush,

You can use $readmemh on a file with multiple columns. I've done this
with the "big-3" simulators with no problems. The simulator reads a
line and simply puts the data into sequential locations in the memory.

file.txt:
b00a 100a 0d0a 500a 000a 000a 0f0a 100a
000b 0a0b 000b 070b 500b 000b f00b 080b

reg [15:0] mem [0:1023];
initial $readmemh("file.txt", mem);

After the readmemh:
mem[0] = b00a
mem[1] = 100a
...
mem[7] = 100a
mem[8] = 000b
...
and so on.

In fact, in the case of Modelsim, you could declare a 2-d array and
read the file into columns.

reg [15:0] mem[0:127][0:7];
initial $readmemh("file.txt", mem);

After the readmemh:
mem[0][0] = b00a
mem[0][1] = 100a
...
mem[0][7] = 100a
mem[1][0] = 000b
...
and so on.

I talked to Synopsys about this for VCS but I don't know if they've
implemented it.

For Modelsim, VCS and NC, the first version works fine for smallish
files, if the target memory is less than 1 Mb. If you're dealing with a
lot of data, I'd go with the $fopen/$fscanf approach others have
described. It depends on the size the the file, how many files you're
reading, the amount of memory in your server, etc.

-cb
 

Welcome to EDABoard.com

Sponsor

Back
Top