0
votes

I was asked to using the data in a bmp file as the simulation input for a verilog module and write the simulation output to a new bmp file, so that they can compare the two different bmp file to check the correction of verilog module. I succeeded in read, but met some problems in writing. module testsub1; integer fout; initial begin fout=$fopen("C:/Users/gyz/Pictures/testout1.bmp","wb"); $fwrite(fout,"%u",8'h42); $fclose(fout); end endmodule The output is supposed to be 8'h42. But the real output is enter image description here

Needless zero occur. I guess it is because verilog write row binary at least 16n bit at a time, n >=1. So I change the sixth line into $fwrite(fout,"%u",24'h424d5f);,the result becomes enter image description here There is still a redundant zero. How can I make the program just output 8 bits each time.

1
Duplicate of stackoverflow.com/questions/40033790/… By the way: I just tried "%u" in Vivado 2017.2 which gives a system fatal error.Oldfart
Thanks. The answer under that question do solve my problems. Though I still feel confused that %c or %C means display in ASCII character format in IEEE standard for verilog. How can it output binary file? But it do works, thank you again for your help.高yz
"%c" outputs a 8-bit value. Mostly it is used to output a printable character and that it can be any 8-bit value is often forgotten in the many text that explains the various output % formats. Also if you look at the printf explanations in C.Oldfart

1 Answers

0
votes

verilog fwrite output bytes

Under this question, dave_59's solution works well.

You can use %c to write out a single byte. You can use a bit-stream cast to convert your data into an array of bytes, then do foreach(array_of_bytes[i]) $fwrite(fd,"%c",array_of_bytes[i]);

If you have a large amount of data, you may want to optimize this by writing out the multiple of 4-bytes with %u, and the remaining bytes with %c.