verilog fwrite 输出字节

编程入门 行业动态 更新时间:2024-10-25 08:15:04
本文介绍了verilog fwrite 输出字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我知道如果我在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 输出字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 11:52:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字节   verilog   fwrite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!