2016-07-21 95 views
-1

我試着在我的測試平臺的SystemVerilog中寫入二進制文件。二進制寫入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。 爲什麼我得到這個結果?謝謝。

+0

使用H,檢查格式指示http://www.csee.umbc.edu/portal/help/VHDL/verilog/system.html –

+1

你期望得到?爲什麼這是個問題?你如何閱讀你的文件? – toolic

+0

當我使用%h時,數據以文本形式打印到文件中:「4D424D42」。 我試圖在調用$ fwrite數據類型之前將4狀態數據類型轉換爲2狀態,並獲得正確結果: int tmp = int'(32'h4D424D42); $ fwrite(file,「%u」,tmp); –

回答

1

%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);