FPGA Central - World's 1st FPGA / CPLD Portal

FPGA Central

World's 1st FPGA Portal

 

Go Back   FPGA Groups > NewsGroup > Verilog

Verilog comp.lang.verilog newsgroup / usenet

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-05-2004, 05:49 AM
Charles Bailey
Guest
 
Posts: n/a
Default Writing a binary file in Verilog

I have a testbench in which I would like to monitor the sequence of
bytes on a data bus and write the bytes to a file in binary.

I open the output file with
integer wrdata, b1;

wrdata = $fopen("testdata.out", "wb");

and, on every clock cycle,
b1 = out_data[7:0];
$fwrite(wrdata,"%c", b1);

The problem is that, if out_data[7:0] == 8'h00, then
"$fwrite(wrdata,"%c", b1);" writes nothing. It seems that $fwrite sees
the all zeros byte as a string terminator (a la C null character) and
writes a zero-length string, that is, nothing.

I've also tried "$fwrite(wrdata,"%u", b1);", but that insists on writing
4 bytes to the file, regardless of how b1 is declared.

What I want to do is just write a straight binary file, without any
interpretation of the bytes and of any arbitrary length. I looked to
see if Verilog has something like $fputc, but apparently it doesn't. Is
there any way to do what I'm describing in Verilog?

Thanks,
Charles Bailey


Reply With Quote
  #2 (permalink)  
Old 10-05-2004, 07:24 AM
annoyed
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

Charles Bailey wrote:
> I have a testbench in which I would like to monitor the sequence of
> bytes on a data bus and write the bytes to a file in binary.
>
> I open the output file with
> integer wrdata, b1;
>
> wrdata = $fopen("testdata.out", "wb");
>
> and, on every clock cycle,
> b1 = out_data[7:0];
> $fwrite(wrdata,"%c", b1);
>
> The problem is that, if out_data[7:0] == 8'h00, then
> "$fwrite(wrdata,"%c", b1);" writes nothing. It seems that $fwrite sees
> the all zeros byte as a string terminator (a la C null character) and
> writes a zero-length string, that is, nothing.
>
> I've also tried "$fwrite(wrdata,"%u", b1);", but that insists on writing
> 4 bytes to the file, regardless of how b1 is declared.
>
> What I want to do is just write a straight binary file, without any
> interpretation of the bytes and of any arbitrary length. I looked to
> see if Verilog has something like $fputc, but apparently it doesn't. Is
> there any way to do what I'm describing in Verilog?


A while ago,
I tried to use the "$fwrite( ... "%c" ... )" mechanism to output a
binary file. I found that my success (not good) depended on
the host simulator. Modelsim invariably failed even without
0x00 (NULL) in the output stream. NC-Verilog worked "better", but
I didn't check it exhaustively ...

(I don't have access to Synopsys VCS.)
Reply With Quote
  #3 (permalink)  
Old 10-05-2004, 09:59 AM
Petter Gustad
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

"Charles Bailey" <[email protected]> writes:

> I have a testbench in which I would like to monitor the sequence of
> bytes on a data bus and write the bytes to a file in binary.
>
> I open the output file with
> integer wrdata, b1;
>
> wrdata = $fopen("testdata.out", "wb");
>
> and, on every clock cycle,
> b1 = out_data[7:0];
> $fwrite(wrdata,"%c", b1);
>
> The problem is that, if out_data[7:0] == 8'h00, then
> "$fwrite(wrdata,"%c", b1);" writes nothing. It seems that $fwrite sees


You are telling it to use formatted output of characters. If you want
pure binary data I think you might want do to something like:

integer fd;

initial begin
fd = $fopen("data.out","wb");
$fwrite(fd,"%u",32'h61_62_00_63);
$fclose(fd);
end

If you want to preserve X and Z you will ahve to use %z format.

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?
Reply With Quote
  #4 (permalink)  
Old 10-05-2004, 04:39 PM
Stephen Williams
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

Charles Bailey wrote:
> I have a testbench in which I would like to monitor the sequence of
> bytes on a data bus and write the bytes to a file in binary.
>
> I open the output file with
> integer wrdata, b1;
>
> wrdata = $fopen("testdata.out", "wb");
>
> and, on every clock cycle,
> b1 = out_data[7:0];
> $fwrite(wrdata,"%c", b1);


I use $fputc in Icarus Verilog, i.e:

always @(posedge VID_CLK)
if (VID_FVV & VID_LVV * VID_DVAL) begin
$fputc(video_output_fd, VID_DATA[7:0]);
$fputc(video_output_fd, VID_DATA[15:8]);
$fputc(video_output_fd, VID_DATA[23:16]);
end

I do binary compares on the output files and get the expected
results, to I believe it works:-)

--
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,
http://www.icarus.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep."

Reply With Quote
  #5 (permalink)  
Old 10-05-2004, 05:58 PM
Petter Gustad
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

Stephen Williams <[email protected]> writes:

> Charles Bailey wrote:
> > I have a testbench in which I would like to monitor the sequence of
> > bytes on a data bus and write the bytes to a file in binary.
> >
> > I open the output file with
> > integer wrdata, b1;
> >
> > wrdata = $fopen("testdata.out", "wb");
> >
> > and, on every clock cycle,
> > b1 = out_data[7:0];
> > $fwrite(wrdata,"%c", b1);

>
> I use $fputc in Icarus Verilog, i.e:
>
> always @(posedge VID_CLK)
> if (VID_FVV & VID_LVV * VID_DVAL) begin
> $fputc(video_output_fd, VID_DATA[7:0]);
> $fputc(video_output_fd, VID_DATA[15:8]);
> $fputc(video_output_fd, VID_DATA[23:16]);
> end


I don't have my IEEE spec at hand, but I can't remember $fputc as
being a part of the standard. Have I missed it, or have you
implemented something which is a natural extension of the standard?

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?
Reply With Quote
  #6 (permalink)  
Old 10-05-2004, 09:01 PM
Steven Sharp
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

"Charles Bailey" <[email protected]> wrote in message news:<[email protected]>...
>
> The problem is that, if out_data[7:0] == 8'h00, then
> "$fwrite(wrdata,"%c", b1);" writes nothing. It seems that $fwrite sees
> the all zeros byte as a string terminator (a la C null character) and
> writes a zero-length string, that is, nothing.


The $fwrite implementation was probably written in C, and probably
formats the output into a C string and then writes out that string.
A NUL character in that string would be treated as a string terminator,
as you have described.

NC-Verilog used to have this behavior, but has since been fixed so that
you can output a NUL character with %c, to allow what you are trying to do.
I can't comment on other simulators, but would expect that they generally
don't do what you want. That takes special attention to this issue when
implementing %c.

> I've also tried "$fwrite(wrdata,"%u", b1);", but that insists on writing
> 4 bytes to the file, regardless of how b1 is declared.


Yes, the LRM specifies that it shall be written in units of 32 bits.

> What I want to do is just write a straight binary file, without any
> interpretation of the bytes and of any arbitrary length. I looked to
> see if Verilog has something like $fputc, but apparently it doesn't. Is
> there any way to do what I'm describing in Verilog?


Probably not in most simulators. The LRM doesn't address this issue, so
you could consider it an issue with Verilog, or just a widespread bug in
simulator implementations.
Reply With Quote
  #7 (permalink)  
Old 10-05-2004, 09:07 PM
Charles Bailey
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

Petter Gustad <[email protected]> wrote in message news:<[email protected]>...
> $fwrite(fd,"%u",32'h61_62_00_63);

%u seems to work if I want to write 4 bytes. What if the binary file
I want to write is not a multiple of 4 bytes? How can I write a
single byte, any byte 00-FF?

Charles Bailey
Reply With Quote
  #8 (permalink)  
Old 10-05-2004, 10:57 PM
Stephen Williams
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

Petter Gustad wrote:
> Stephen Williams <[email protected]> writes:
>>I use $fputc in Icarus Verilog, i.e:
>>
>> always @(posedge VID_CLK)
>> if (VID_FVV & VID_LVV * VID_DVAL) begin
>> $fputc(video_output_fd, VID_DATA[7:0]);
>> $fputc(video_output_fd, VID_DATA[15:8]);
>> $fputc(video_output_fd, VID_DATA[23:16]);
>> end

>
>
> I don't have my IEEE spec at hand, but I can't remember $fputc as
> being a part of the standard. Have I missed it, or have you
> implemented something which is a natural extension of the standard?


Well, I *thought* $fputc was standard, but I looked it up, and
sure enough there is nothing like it in the actual standard.
So I guess this is an extension so natural that I forgot it
was and extension.

--
Steve Williams "The woods are lovely, dark and deep.
steve at icarus.com But I have promises to keep,
http://www.icarus.com and lines to code before I sleep,
http://www.picturel.com And lines to code before I sleep."

Reply With Quote
  #9 (permalink)  
Old 10-06-2004, 09:11 AM
Petter Gustad
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

[email protected] (Charles Bailey) writes:

> Petter Gustad <[email protected]> wrote in message news:<[email protected]>...


> > $fwrite(fd,"%u",32'h61_62_00_63);

> %u seems to work if I want to write 4 bytes. What if the binary file
> I want to write is not a multiple of 4 bytes? How can I write a
> single byte, any byte 00-FF?


I don't think you can. You'll have to either use an ASCII file, use
Icarus, or make a $fputc yourself. Can't you use a four byte buffer?

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?
Reply With Quote
  #10 (permalink)  
Old 10-07-2004, 01:57 PM
Charles Bailey
Guest
 
Posts: n/a
Default Re: Writing a binary file in Verilog

I tried using Icarus, but my Verilog code contains a number of
statements like this:
cw[ 0:22] = lz1data[bitpos+:23];
outdata[outpos+:8] = outbyte;
which Icarus can't handle.
(It gives error messages like
eldc_enc.v:310: syntax error
eldc_enc.v:310: error: malformed statement
)

Charles Bailey

"Stephen Williams" <[email protected]> wrote in message
news:[email protected] ervers.com...
>
> I use $fputc in Icarus Verilog, i.e:
>
> always @(posedge VID_CLK)
> if (VID_FVV & VID_LVV * VID_DVAL) begin
> $fputc(video_output_fd, VID_DATA[7:0]);
> $fputc(video_output_fd, VID_DATA[15:8]);
> $fputc(video_output_fd, VID_DATA[23:16]);
> end
>
> I do binary compares on the output files and get the expected
> results, to I believe it works:-)
>
> --
> Steve Williams "The woods are lovely, dark and deep.
> steve at icarus.com But I have promises to keep,
> http://www.icarus.com and lines to code before I sleep,
> http://www.picturel.com And lines to code before I sleep."
>



Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
binary to bram (verilog) Paul Verilog 0 02-01-2004 11:52 PM
SOS : 4-bit binary divider circuit PLEASE!!!!!!! kpk Verilog 37 01-06-2004 02:53 PM
Synchronous Binary counter question. Denis Gleeson Verilog 12 09-26-2003 11:51 PM
Re: writing a 8bit char on a binary file, error... TorbaX Verilog 1 08-08-2003 06:47 PM
Re: writing a 8bit char on a binary file, error... Steven Sharp Verilog 0 08-08-2003 01:09 AM


All times are GMT +1. The time now is 04:24 AM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Copyright 2008 @ FPGA Central. All rights reserved