1

我尝试在我的测试平台中的 SystemVerilog 中编写二进制文件。

int file   = $fopen(path,"w");
if (!file) begin
    $error("File could not be open: ", path);
    return;
end
$fwrite(file, "%u", 32'h4D424D42);
$fclose(file);

并得到结果:02 0c 02 0c

我使用 QuestaSum 10.2c。为什么我得到这个结果?谢谢。

4

1 回答 1

4

%u 将未格式化的原始二进制数据放入文件中。不要期望它是可读的格式。确保您以二进制格式“rb”或“wb”打开文件...。尝试读回二进制数据,这应该会显示写入的值。

如果要保存 4 个状态值,可以使用 %z。

int rd;
int file   = $fopen(path,"wb"); // open in binary mode
if (!file) begin
    $error("File could not be open: ", path);
    return;
end
$fwrite(file, "%u", 32'h4D424D42);
$fclose(file);
 // read back binary data from file 
file = $fopen (path,"rb");  // open in binary mode
if (!file) begin
    $error("File could not be open: ", path);
    return;
end
$fscanf(file,"%u",rd);
$fclose(file);
$display("%h",rd);
于 2016-07-21T13:39:06.917 回答