Verilog之文件操作

文件打开关闭

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参数。

猜你喜欢

转载自blog.csdn.net/zhong_ethan/article/details/120388691