I know that if I am outputting a binary file in verilog, then I can use the following verilog IO standard function:
$fwrite(fd,"%u",32'hABCDE124);
But the above command writes 4-byte data into the file. What if the binary data that I want to write is only one-byte, two-bytes or three-bytes? How can I do this?
For example, I know the following won't do what I want:
$fwrite(fd,"%u",8'h24);
$fwrite(fd,"%u",16'hE124);
$fwrite(fd,"%u",24'hCDE124);
Is there any way that I can write a non 4-byte multiple data into a file?
Thanks,
--Rudy
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 doIf 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'.I suggest another version of dave_59's answer. The key is using multiple %c.
When using %u - be aware of Your system byte ordering, it uses native by default.
You can use %s instead of %u:
https://www.edaplayground.com/x/4vJn