问题描述
我知道如果我在verilog中输出一个二进制文件,那么我可以使用下面的verilog IO标准函数:
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);
但是上面的命令将 4 字节的数据写入文件.如果我要写入的二进制数据只有一字节、两字节或三字节怎么办?我该怎么做?
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);
有什么办法可以将非 4 字节的多个数据写入文件?
Is there any way that I can write a non 4-byte multiple data into a file?
谢谢,
--鲁迪
推荐答案
我建议使用另一个版本的 dave_59 的答案.关键是使用多个%c.
I suggest another version of dave_59's answer. The key is using multiple %c.
wire [7:0] byte;
wire [15:0] two_bytes;
wire [23:0] three_bytes;
----
assign byte = 8'h24;
assign two_bytes = 16'hE124;
assign three_bytes = 24'hCDE124;
----
$fwrite(fd_s,"%c",byte);
$fwrite(fd_s,"%c%c",two_bytes[15-:8],two_bytes[7-:8]);
$fwrite(fd_s,"%c%c%c",three_bytes[23-:8],three_bytes[15-:8],three_bytes[7-:8]);
使用 %u 时 - 请注意您的系统字节顺序,它默认使用本机.
When using %u - be aware of Your system byte ordering, it uses native by default.
这篇关于verilog fwrite 输出字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!