将值 0 写入二进制文件

发布于 2025-01-13 03:00:44 字数 837 浏览 1 评论 0原文

我正在从 SystemVerilog 模拟环境生成一个二进制文件。目前,我正在执行以下操作:

module main;
  byte arr[] = {0,32, 65, 66, 67};
  initial begin
    int fh=$fopen("/home/hagain/tmp/h.bin","w");
    for (int idx=0; idx<arr.size; idx++) begin //{
      $fwrite(fh, "%0s", arr[idx]);
    end //}
    $fclose(fh);
    $system("xxd /home/hagain/tmp/h.bin | tee /home/hagain/tmp/h.txt");
  end
endmodule : main

问题是,当 b 的值为 0 时,不会向文件写入任何内容。 xxd 输出是:

0000000: 2041 4243                                 ABC

转换为字符串时的结果如下:

$fwrite(fh, string'(arr[idx]));

我尝试将写入命令更改为:

$fwrite(fh, $sformatf("%0c", arr[idx]));

然后前两个字节('d0 和 'd32)得到相同的值:

0000000: 2020 4142 43                               ABC

关于如何生成此二进制文件的任何想法文件?

I am generating a binary file from a SystemVerilog simulation environment. Currently, I'm doing the following:

module main;
  byte arr[] = {0,32, 65, 66, 67};
  initial begin
    int fh=$fopen("/home/hagain/tmp/h.bin","w");
    for (int idx=0; idx<arr.size; idx++) begin //{
      $fwrite(fh, "%0s", arr[idx]);
    end //}
    $fclose(fh);
    $system("xxd /home/hagain/tmp/h.bin | tee /home/hagain/tmp/h.txt");
  end
endmodule : main

The problem is, that when b has the value of 0, nothing is written to the file. xxd output is:

0000000: 2041 4243                                 ABC

Same result when casting to string as follows:

$fwrite(fh, string'(arr[idx]));

I tried to change the write command to:

$fwrite(fh, $sformatf("%0c", arr[idx]));

And then I got the same value for the first two bytes ('d0 and 'd32):

0000000: 2020 4142 43                               ABC

Any idea on how to generate this binary file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2025-01-20 03:00:44

字符串中间不能有 null(0) 字符,它用于终止字符串。

您应该对 u 信息数据使用 %u 格式说明符。

module main;
  byte arr[] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9};
   int fh, c, tmp;
   initial begin
     fh = $fopen("h.bin","wb");
     for (int idx=0; idx<arr.size; idx+=4) begin
       tmp = {<<8{arr[idx+:4]}};
       $fwrite(fh, "%u", tmp);
      end
      $fclose(fh);
      fh  = $fopen("h.bin","r");
      while ((c = $fgetc(fh)) != -1)
        $write("%d ",c[7:0]);
    $display;
   end
endmodule : main

请注意,%u 以最低有效字节优先的顺序写入 32 位值,因此我反转了使用流运算符写入的字节 {<<8{arr[idx+:4]}}。如果字节数不能被 4 整除,它只会用空字节填充文件。

如果您需要确切的字节数,则必须使用一些 DPI C 代码

#include <stdio.h>
#include "svdpi.h"
void DPI_fwrite(const char* filename,
           const svOpenArrayHandle buffer){
  int size = svSize(buffer,1);
  char *buf = (char *)svGetArrayPtr(buffer);
  FILE *fp = fopen(filename,"wb");
  fwrite(buf,1,size,fp);
}

,然后使用以下命令导入它

import "DPI-C" function void DPI_fwrite(input string filename, byte buffer[]);
...
DPI_fwrite("filename", arr);

You cannot have a null(0) character in the middle of a string, it is used to terminate the string.

You should use the %u format specifier for unformated data.

module main;
  byte arr[] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9};
   int fh, c, tmp;
   initial begin
     fh = $fopen("h.bin","wb");
     for (int idx=0; idx<arr.size; idx+=4) begin
       tmp = {<<8{arr[idx+:4]}};
       $fwrite(fh, "%u", tmp);
      end
      $fclose(fh);
      fh  = $fopen("h.bin","r");
      while ((c = $fgetc(fh)) != -1)
        $write("%d ",c[7:0]);
    $display;
   end
endmodule : main

Note that %u writes a 32-bit value in least-significant bytes first order, so I reversed the bytes being written with the streaming operator {<<8{arr[idx+:4]}}. If the number of bytes is not divisible by 4, it will just pad the file with null bytes.

If you need the exact number of bytes, the you will have to use some DPI C code

#include <stdio.h>
#include "svdpi.h"
void DPI_fwrite(const char* filename,
           const svOpenArrayHandle buffer){
  int size = svSize(buffer,1);
  char *buf = (char *)svGetArrayPtr(buffer);
  FILE *fp = fopen(filename,"wb");
  fwrite(buf,1,size,fp);
}

And then import it with

import "DPI-C" function void DPI_fwrite(input string filename, byte buffer[]);
...
DPI_fwrite("filename", arr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文