FPGARelated.com
Forums

Xilinx, VHDL, Verilog, ModelSim, BMP

Started by Brad Smallridge ●November 16, 2004
> Hi, > > Well, let me know if you figure this one out. I had > a similar issue -- I wanted a Verilog simulation to > write out a TIFF file. I couldn't find a way to write > out a binary file, so wrote out ASCII data values to > text file and then post-processed it into a binary > TIFF file using a small program I wrote in C.
The attached code is kind of kludge, but it works, with modelsim 5.8e anyway. HTH, Jim jimwu88NOOOSPAM@yahoo.com (remove capital letters) http://www.geocities.com/jimwu88/chips module wr_bin (); reg [7:0] data; integer fd; integer i; initial begin fd = $fopen("test.bin", "wb"); for (i = 0; i < 256; i = i + 1) begin data = i; if (data == 0) begin $fwriteb(fd, "%u", data); $fseek(fd, 1, 0); end else $fwriteb(fd, "%c", data); end $fclose(fd); $display("Done"); $finish; end endmodule
Dude, you rock!!!  Since I'm using Modelsim at home
for my hobby projects, this will work fantastic...
Now I can write out TIFF files direct from Verilog.

Let me ask another question, if you don't mind.  Do
you (or anyone reading) know if it's possible to
open files with dynamic file names?  By that, I
mean instead of using the string "test.bin" in $fopen
is there a way to form a string with a 2D reg and
then pass that as the file name?  I tried playing
around with this, but the simulator I was using at
the time did not like anything but actual strings...

My desire is to have a loop, that writes each display
frame to a separate file, with names like:

frame01.tif
frame02.tif
frame03.tif
frame04.tif
and so on...

Thanks, I appreciate your help,
Eric

Jim Wu wrote:
> > > Hi, > > > > Well, let me know if you figure this one out. I had > > a similar issue -- I wanted a Verilog simulation to > > write out a TIFF file. I couldn't find a way to write > > out a binary file, so wrote out ASCII data values to > > text file and then post-processed it into a binary > > TIFF file using a small program I wrote in C. > > The attached code is kind of kludge, but it works, with modelsim 5.8e > anyway. > > HTH, > Jim > jimwu88NOOOSPAM@yahoo.com (remove capital letters) > http://www.geocities.com/jimwu88/chips > > module wr_bin (); > > reg [7:0] data; > > integer fd; > integer i; > > initial begin > fd = $fopen("test.bin", "wb"); > > for (i = 0; i < 256; i = i + 1) begin > data = i; > > if (data == 0) begin > $fwriteb(fd, "%u", data); > $fseek(fd, 1, 0); > end > else > $fwriteb(fd, "%c", data); > end > > $fclose(fd); > $display("Done"); > $finish; > end > > endmodule
Hi Eric,

Eric Crabill wrote:

> Let me ask another question, if you don't mind. Do > you (or anyone reading) know if it's possible to > open files with dynamic file names? By that, I > mean instead of using the string "test.bin" in $fopen > is there a way to form a string with a 2D reg and > then pass that as the file name? I tried playing > around with this, but the simulator I was using at > the time did not like anything but actual strings...
I know it can be done in VHDL, but not sure about Verilog sorry. I can dig out the details if you're interested. John
Try this (tested in mti 5.8e):

module wr_bin ();

reg [7:0] data;
reg [10*8:0] fn;
reg [7:0] seq;

integer fd;
integer i, j;

initial begin

    for (j = 0; j < 8; j = j + 1) begin
        seq = 8'h30 + j;
        fn = {"frame", seq, ".tif"};
        fd = $fopen(fn, "wb");
        for (i = 0; i < 256; i = i + 1) begin
            data = i;

            if (data == 0) begin
                $fwriteb(fd, "%u", data);
                $fseek(fd, 1, 0);
            end
            else
                $fwriteb(fd, "%c", data);
        end


        $fclose(fd);
    end

    $display("Done");
    $finish;
end

HTH,
Jim
jimwu88NOOOSPAM@yahoo.com (remove capital letters)
http://www.geocities.com/jimwu88/chips

"Eric Crabill" <eric.crabill@xilinx.com> wrote in message
news:419BDA61.4AF0BC09@xilinx.com...
> > Dude, you rock!!! Since I'm using Modelsim at home > for my hobby projects, this will work fantastic... > Now I can write out TIFF files direct from Verilog. > > Let me ask another question, if you don't mind. Do > you (or anyone reading) know if it's possible to > open files with dynamic file names? By that, I > mean instead of using the string "test.bin" in $fopen > is there a way to form a string with a 2D reg and > then pass that as the file name? I tried playing > around with this, but the simulator I was using at > the time did not like anything but actual strings... > > My desire is to have a loop, that writes each display > frame to a separate file, with names like: > > frame01.tif > frame02.tif > frame03.tif > frame04.tif > and so on... > > Thanks, I appreciate your help, > Eric > > Jim Wu wrote: > > > > > Hi, > > > > > > Well, let me know if you figure this one out. I had > > > a similar issue -- I wanted a Verilog simulation to > > > write out a TIFF file. I couldn't find a way to write > > > out a binary file, so wrote out ASCII data values to > > > text file and then post-processed it into a binary > > > TIFF file using a small program I wrote in C. > > > > The attached code is kind of kludge, but it works, with modelsim 5.8e > > anyway. > > > > HTH, > > Jim > > jimwu88NOOOSPAM@yahoo.com (remove capital letters) > > http://www.geocities.com/jimwu88/chips > > > > module wr_bin (); > > > > reg [7:0] data; > > > > integer fd; > > integer i; > > > > initial begin > > fd = $fopen("test.bin", "wb"); > > > > for (i = 0; i < 256; i = i + 1) begin > > data = i; > > > > if (data == 0) begin > > $fwriteb(fd, "%u", data); > > $fseek(fd, 1, 0); > > end > > else > > $fwriteb(fd, "%c", data); > > end > > > > $fclose(fd); > > $display("Done"); > > $finish; > > end > > > > endmodule
A typo, fn should have been defined as "reg [10*8:1] fn;".

"Jim Wu" <nospam@nospam.com> wrote in message
news:cngs9m$jq2@cliff.xsj.xilinx.com...
> Try this (tested in mti 5.8e): > > module wr_bin (); > > reg [7:0] data; > reg [10*8:0] fn; > reg [7:0] seq; > > integer fd; > integer i, j; > > initial begin > > for (j = 0; j < 8; j = j + 1) begin > seq = 8'h30 + j; > fn = {"frame", seq, ".tif"}; > fd = $fopen(fn, "wb"); > for (i = 0; i < 256; i = i + 1) begin > data = i; > > if (data == 0) begin > $fwriteb(fd, "%u", data); > $fseek(fd, 1, 0); > end > else > $fwriteb(fd, "%c", data); > end > > > $fclose(fd); > end > > $display("Done"); > $finish; > end > > HTH, > Jim > jimwu88NOOOSPAM@yahoo.com (remove capital letters) > http://www.geocities.com/jimwu88/chips > > "Eric Crabill" <eric.crabill@xilinx.com> wrote in message > news:419BDA61.4AF0BC09@xilinx.com... > > > > Dude, you rock!!! Since I'm using Modelsim at home > > for my hobby projects, this will work fantastic... > > Now I can write out TIFF files direct from Verilog. > > > > Let me ask another question, if you don't mind. Do > > you (or anyone reading) know if it's possible to > > open files with dynamic file names? By that, I > > mean instead of using the string "test.bin" in $fopen > > is there a way to form a string with a 2D reg and > > then pass that as the file name? I tried playing > > around with this, but the simulator I was using at > > the time did not like anything but actual strings... > > > > My desire is to have a loop, that writes each display > > frame to a separate file, with names like: > > > > frame01.tif > > frame02.tif > > frame03.tif > > frame04.tif > > and so on... > > > > Thanks, I appreciate your help, > > Eric > > > > Jim Wu wrote: > > > > > > > Hi, > > > > > > > > Well, let me know if you figure this one out. I had > > > > a similar issue -- I wanted a Verilog simulation to > > > > write out a TIFF file. I couldn't find a way to write > > > > out a binary file, so wrote out ASCII data values to > > > > text file and then post-processed it into a binary > > > > TIFF file using a small program I wrote in C. > > > > > > The attached code is kind of kludge, but it works, with modelsim 5.8e > > > anyway. > > > > > > HTH, > > > Jim > > > jimwu88NOOOSPAM@yahoo.com (remove capital letters) > > > http://www.geocities.com/jimwu88/chips > > > > > > module wr_bin (); > > > > > > reg [7:0] data; > > > > > > integer fd; > > > integer i; > > > > > > initial begin > > > fd = $fopen("test.bin", "wb"); > > > > > > for (i = 0; i < 256; i = i + 1) begin > > > data = i; > > > > > > if (data == 0) begin > > > $fwriteb(fd, "%u", data); > > > $fseek(fd, 1, 0); > > > end > > > else > > > $fwriteb(fd, "%c", data); > > > end > > > > > > $fclose(fd); > > > $display("Done"); > > > $finish; > > > end > > > > > > endmodule > >
"Brad Smallridge" <bradsmallridge@dslextreme.com> writes:

> I would eventually like to be able to read a BMP file directly into the > ModelSim simulator. I understand now with VHDL the only file you can read > are text files. Is there a way to read in binary data in another way, > perhaps by using Verilog? > > Brad Smallridge > >
Personally I use PNG format for grayscale images and PPM for colour, as they can both be text only. The files are big, but the ease with which I can fiddle with them in VHDL compensates! I have a package which allows me to read and write both types of files to/from frame buffers. Just a thought! Martin -- martin.j.thompson@trw.com TRW Conekt, Solihull, UK http://www.trw.com/conekt
Hi,

Jim posted some stuff that looks like it will solve
my problem.  I'm going to work on my TIFF writer over
the weekend and when I'm done (and if it works...)
I will post it here.

Thanks,
Eric

John Williams wrote:
> > Hi Eric, > > Eric Crabill wrote: > > > Let me ask another question, if you don't mind. Do > > you (or anyone reading) know if it's possible to > > open files with dynamic file names? By that, I > > mean instead of using the string "test.bin" in $fopen > > is there a way to form a string with a 2D reg and > > then pass that as the file name? I tried playing > > around with this, but the simulator I was using at > > the time did not like anything but actual strings... > > I know it can be done in VHDL, but not sure about Verilog sorry. > > I can dig out the details if you're interested. > > John
Thanks, I am going to try integrating this into my
existing TIFF file writer and I'll let you know
how it works out for me.  I really appreciate the
thought you put into it.

Eric

Jim Wu wrote:
> > A typo, fn should have been defined as "reg [10*8:1] fn;". > > "Jim Wu" <nospam@nospam.com> wrote in message > news:cngs9m$jq2@cliff.xsj.xilinx.com... > > Try this (tested in mti 5.8e): > > > > module wr_bin (); > > > > reg [7:0] data; > > reg [10*8:0] fn; > > reg [7:0] seq; > > > > integer fd; > > integer i, j; > > > > initial begin > > > > for (j = 0; j < 8; j = j + 1) begin > > seq = 8'h30 + j; > > fn = {"frame", seq, ".tif"}; > > fd = $fopen(fn, "wb"); > > for (i = 0; i < 256; i = i + 1) begin > > data = i; > > > > if (data == 0) begin > > $fwriteb(fd, "%u", data); > > $fseek(fd, 1, 0); > > end > > else > > $fwriteb(fd, "%c", data); > > end > > > > > > $fclose(fd); > > end > > > > $display("Done"); > > $finish; > > end > > > > HTH, > > Jim > > jimwu88NOOOSPAM@yahoo.com (remove capital letters) > > http://www.geocities.com/jimwu88/chips > > > > "Eric Crabill" <eric.crabill@xilinx.com> wrote in message > > news:419BDA61.4AF0BC09@xilinx.com... > > > > > > Dude, you rock!!! Since I'm using Modelsim at home > > > for my hobby projects, this will work fantastic... > > > Now I can write out TIFF files direct from Verilog. > > > > > > Let me ask another question, if you don't mind. Do > > > you (or anyone reading) know if it's possible to > > > open files with dynamic file names? By that, I > > > mean instead of using the string "test.bin" in $fopen > > > is there a way to form a string with a 2D reg and > > > then pass that as the file name? I tried playing > > > around with this, but the simulator I was using at > > > the time did not like anything but actual strings... > > > > > > My desire is to have a loop, that writes each display > > > frame to a separate file, with names like: > > > > > > frame01.tif > > > frame02.tif > > > frame03.tif > > > frame04.tif > > > and so on... > > > > > > Thanks, I appreciate your help, > > > Eric > > > > > > Jim Wu wrote: > > > > > > > > > Hi, > > > > > > > > > > Well, let me know if you figure this one out. I had > > > > > a similar issue -- I wanted a Verilog simulation to > > > > > write out a TIFF file. I couldn't find a way to write > > > > > out a binary file, so wrote out ASCII data values to > > > > > text file and then post-processed it into a binary > > > > > TIFF file using a small program I wrote in C. > > > > > > > > The attached code is kind of kludge, but it works, with modelsim 5.8e > > > > anyway. > > > > > > > > HTH, > > > > Jim > > > > jimwu88NOOOSPAM@yahoo.com (remove capital letters) > > > > http://www.geocities.com/jimwu88/chips > > > > > > > > module wr_bin (); > > > > > > > > reg [7:0] data; > > > > > > > > integer fd; > > > > integer i; > > > > > > > > initial begin > > > > fd = $fopen("test.bin", "wb"); > > > > > > > > for (i = 0; i < 256; i = i + 1) begin > > > > data = i; > > > > > > > > if (data == 0) begin > > > > $fwriteb(fd, "%u", data); > > > > $fseek(fd, 1, 0); > > > > end > > > > else > > > > $fwriteb(fd, "%c", data); > > > > end > > > > > > > > $fclose(fd); > > > > $display("Done"); > > > > $finish; > > > > end > > > > > > > > endmodule > > > >
You're welcome.

I didn't try this myself, but theoretically it should work:  if you need
"seq" to be more than 1 digit, you can try swrite* tasks.

Jim

"Eric Crabill" <eric.crabill@xilinx.com> wrote in message
news:419CE33E.503B4253@xilinx.com...
> Thanks, I am going to try integrating this into my > existing TIFF file writer and I'll let you know > how it works out for me. I really appreciate the > thought you put into it. > > Eric > > Jim Wu wrote: > > > > A typo, fn should have been defined as "reg [10*8:1] fn;". > > > > "Jim Wu" <nospam@nospam.com> wrote in message > > news:cngs9m$jq2@cliff.xsj.xilinx.com... > > > Try this (tested in mti 5.8e): > > > > > > module wr_bin (); > > > > > > reg [7:0] data; > > > reg [10*8:0] fn; > > > reg [7:0] seq; > > > > > > integer fd; > > > integer i, j; > > > > > > initial begin > > > > > > for (j = 0; j < 8; j = j + 1) begin > > > seq = 8'h30 + j; > > > fn = {"frame", seq, ".tif"}; > > > fd = $fopen(fn, "wb"); > > > for (i = 0; i < 256; i = i + 1) begin > > > data = i; > > > > > > if (data == 0) begin > > > $fwriteb(fd, "%u", data); > > > $fseek(fd, 1, 0); > > > end > > > else > > > $fwriteb(fd, "%c", data); > > > end > > > > > > > > > $fclose(fd); > > > end > > > > > > $display("Done"); > > > $finish; > > > end > > > > > > HTH, > > > Jim > > > jimwu88NOOOSPAM@yahoo.com (remove capital letters) > > > http://www.geocities.com/jimwu88/chips > > > > > > "Eric Crabill" <eric.crabill@xilinx.com> wrote in message > > > news:419BDA61.4AF0BC09@xilinx.com... > > > > > > > > Dude, you rock!!! Since I'm using Modelsim at home > > > > for my hobby projects, this will work fantastic... > > > > Now I can write out TIFF files direct from Verilog. > > > > > > > > Let me ask another question, if you don't mind. Do > > > > you (or anyone reading) know if it's possible to > > > > open files with dynamic file names? By that, I > > > > mean instead of using the string "test.bin" in $fopen > > > > is there a way to form a string with a 2D reg and > > > > then pass that as the file name? I tried playing > > > > around with this, but the simulator I was using at > > > > the time did not like anything but actual strings... > > > > > > > > My desire is to have a loop, that writes each display > > > > frame to a separate file, with names like: > > > > > > > > frame01.tif > > > > frame02.tif > > > > frame03.tif > > > > frame04.tif > > > > and so on... > > > > > > > > Thanks, I appreciate your help, > > > > Eric > > > > > > > > Jim Wu wrote: > > > > > > > > > > > Hi, > > > > > > > > > > > > Well, let me know if you figure this one out. I had > > > > > > a similar issue -- I wanted a Verilog simulation to > > > > > > write out a TIFF file. I couldn't find a way to write > > > > > > out a binary file, so wrote out ASCII data values to > > > > > > text file and then post-processed it into a binary > > > > > > TIFF file using a small program I wrote in C. > > > > > > > > > > The attached code is kind of kludge, but it works, with modelsim
5.8e
> > > > > anyway. > > > > > > > > > > HTH, > > > > > Jim > > > > > jimwu88NOOOSPAM@yahoo.com (remove capital letters) > > > > > http://www.geocities.com/jimwu88/chips > > > > > > > > > > module wr_bin (); > > > > > > > > > > reg [7:0] data; > > > > > > > > > > integer fd; > > > > > integer i; > > > > > > > > > > initial begin > > > > > fd = $fopen("test.bin", "wb"); > > > > > > > > > > for (i = 0; i < 256; i = i + 1) begin > > > > > data = i; > > > > > > > > > > if (data == 0) begin > > > > > $fwriteb(fd, "%u", data); > > > > > $fseek(fd, 1, 0); > > > > > end > > > > > else > > > > > $fwriteb(fd, "%c", data); > > > > > end > > > > > > > > > > $fclose(fd); > > > > > $display("Done"); > > > > > $finish; > > > > > end > > > > > > > > > > endmodule > > > > > >
"Jim Wu" <nospam@nospam.com> wrote in message news:<cngkbn$4m1@cliff.xsj.xilinx.com>...
> > if (data == 0) begin > $fwriteb(fd, "%u", data); > $fseek(fd, 1, 0); > end > else > $fwriteb(fd, "%c", data);
Shouldn't that be $fseek(fd, -3, 1), to seek backwards 3 bytes from the current position to compensate for having written a 4-byte binary word instead of 1 byte? With $fseek(fd, 1, 0), you are seeking to the second byte in the file, which is only correct if you were at the start of the file when you wrote the NUL byte. Some simulators actually let you write a NUL byte out with %c, which avoids this nastiness.