天天看點

Verilog之檔案操作

文章目錄

      • 檔案打開關閉
      • 檔案輸出
      • 檔案讀入
        • load memory
      • 格式化

檔案打開關閉

integer file_descriptor = $("file_name", type);
$fclose(file_desriptor);
           

type 類型

“r” or “rb” - open for reading(讀)

“w” or “wb” - truncate to zero length or create for writing (覆寫寫)

“a” or “ab” - append, open for writing at the end of file, or create for writing(續寫)

以上“b”用來區分二進制檔案和其它文本檔案,在Linux平台上,無需區分,windows需要區分。

檔案輸出

file_output_task_name(file_descriptor, list_of_arguments);
file_output_task_name includes
$fdisplay | $fdisplayb | $fdisplayh | $fdisplayo
| $fwrite | $fwriteb | $fwriteh | $fwriteo
| $fstrobe | $fstrobeb | $fstrobeh | $fstrobeo
| $fmonitor | $fmonitorb | $fmonitorh | $fmonitoro
           
list_arguments can be a quoted string, an expression that returns a value, or a null argument

$fdisplay 與 $fwrite 函數的差別在于 $display 函數會自動換行, $fwrite不會。

檔案讀入

integer code = $fscanf (file_descriptor, format, args);
integer code = $fread( myreg, fd);
integer code = $fread( mem, fd);
integer code = $fread( mem, fd, start);
integer code = $fread( mem, fd, start, count);
integer code = $fread( mem, fd, , count);
           

$fscanf是将檔案按照某個模闆格式進行掃描,并将掃描結果存放到args中。第一個參數是掃描對象,第二個參數是掃描格式,第三個參數是存放結果的變量。

掃描格式有

%b - matches a binary number

%o - matches a octal number

%d - matches a decimal number

%h -matches a hexadecimal number

%s - matches a string, which is a sequence of non white space characters

load memory

$readmemb ("file_name", memory_name, start_addr, end_addr);
eg. 
initial $readmemh("mem.data", mem);
initial $readmemh("mem.data", mem, 16);
initial $readmemh("mem.data", mem, 128, 1);
           

格式化

$sformat (str, format, args)
integer code = $sformatf(format, args)
           

$sformat則是将參數args按format格式指派給str。

$sformatf 和 $sformat 的不同在于 $sformat 沒有傳回值,而 $sformatf有傳回值,傳回值就相當于 $sformat的str參數。

繼續閱讀