systemverilog中输入输出系统任务和函数(二)——文件输入输出相关的任务和函数


前言

在systemverilog中提供了许多输入输出的系统任务和函数,本文主要介绍文件输入输出相关的任务和函数。

文件输入输出相关的任务和函数

  • $fclose $fopen
  • $fdisplay | $fdisplayb | $fdisplayh | $fdisplayo
  • $fwrite | $fwriteb | $fwriteh | $fwriteo
  • $fstrobe | $fstrobeb | $fstrobeh | $fstrobeo
  • $fmonitor | $fmonitorb | $fmonitorh | $fmonitoro
  • $swrite | $swriteb | $swriteh | $swriteo
  • $sformat | $sformatf
  • $fgetc | $ungetc | $fgets
  • $fread | $fscanf | $sscanf
  • $fseek
  • $rewind
  • $fflush
  • $ftell
  • $feof
  • $ferror

二、文件输入输出相关的任务和函数

2.1 $fopen $fclose

$fopen用于打开文件,$fclose 用于关闭文件。

integer handle;
handle = $fopen("filename", type);
fclose(handle);

type的选项描述如下:

ArgumentDescription
“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 end of file (EOF), or create for writing
“r+”, “r+b”, or “rb+”Open for update (reading and writing)
“w+”, “w+b”, or “wb+”Truncate or create for update
“a+”, “a+b”, or “ab+”Append; open or create for update at EOF

2.2 $fdisplay | $fdisplayb | $fdisplayh | $fdisplayo | $fwrite | $fwriteb | $fwriteh | $fwriteo | $fstrobe | $fstrobeb | $fstrobeh | $fstrobeo | $fmonitor | $fmonitorb | $fmonitorh | $fmonitoro

$fdisplay | $fwrite | $fmonitor 和 $fstrobe这些系统函数,与前面介绍的不带f的$display | $write | $monitor 和 $strobe,使用方法一致。
带f的和不带f的区别是,带f的是将内容打印到文件中,不带f的是显示在显示屏上。
示例:

`timescale 1ns / 1ps
 
module sim_top( );
    
localparam FILE_NAME = "../../../led_sim.txt";
integer file_handle = 0;
initial begin
    file_handle = $fopen(FILE_NAME,"w");
    if(!file_handle)
    begin
        $display("Could not open File \r");
        $stop;
    end
    $fdisplay (file_handle, "new data1: %h", file_handle);
    $fdisplay (file_handle, "new data2: %h", 16'h1234);
    $fdisplay (file_handle, "new data3: %d", 123);
 
    $fclose(file_handle);    
 
    #200;
    $stop;
end
 
endmodule

结果输出

new data1: ffffb1e0
new data2: 1234
new data3:         123

2.6 $swrite | $swriteb | $swriteh | $swriteo

格式:

$swrite(output_reg,list_of_arguments);

$swrite和$fwrite用法类似,也有针对二进制$swriteb、十六进制$swriteh、八进制$swriteo的格式,唯一不同的是$swrite的第一个参数是一个存储字符串的reg类型变量,而$fwrite为指向对应文件的句柄。

2.7 $sformat | $sformatf

2.7.1 任务$sformat

$sformat ( output_var , format_string [ , list_of_arguments ] )

1、系统任务$sformat与系统任务$write相似,但是有一个主要的不同。
2、与输出系统任务$display和$write不同的是,$sformat只把第二个参数作为格式字符串。
3、格式字符串可以是字符串常量,例如"data is%d",或者是整数表达式,非合并字节数组,字符串数据类型。
4、$sformat支持和$display一样所有支持的格式。
5、如果没有为格式说明符提供足够的参数或提供了太多的参数,那么应用程序将发出警告并继续执行。

2.7.2 函数$sformatf

$sformatf ( format_string [ , list_of_arguments ] )

系统函数$sformatf的行为类似于$sformat,只是字符串结果作为$sformatf的函数结果值传回,而不是像$sformat那样放在第一个参数中。

2.8 $fgetc | $ungetc | $fgets

2.8.1 $fgetc

integer c;
c = $fgetc ( fd );

该函数实现从fd指定的文件读取一个字节。如果读操作失败,那么char将被赋值为EOF(-1),因此建议指定的char的宽度大于8位,这样在char被赋予EOF时可与’hFF区别。注意,此处的文件句柄在获取时必须指明其访问方式,否则将不能获得有效数据。

2.8.2 $ungetc

integer code;
code = $ungetc ( c, fd );

将一个字符退回到输入流。
返回值:c表示操作成功;EOF表示失败。

2.8.3 $fgets

 integer code;
code = $fgets ( str, fd );

将从fd指定的文件读取一行字符并存储到str中,直到文件结束。如果读取失败,那么将会给code返回0,否则返回读取到的字符个数。

2.9 $fread | $fscanf | $sscanf | $fseek | $ftell | $feof | $ferror | $rewind | $fflush

2.9.1 $fread

integer <integer>;
<integer> = $fread(<store><file_desc>); 
<integer> = $fread(<store><file_desc>, <start> ); 
<integer> = $fread(<store><file_desc>, <start>, <count> ); 
<integer> = $fread(<store><file_desc>, , <count> ); 

integer:整型数值,返回本次$fread 读取的真实字节数量,当返回值为0 ,表示错误读取或者文件结束。
store:将二进制文件中的数据读取到寄存器或者二维数组中。
file_desc:为打开的文件句柄
start: 为二维数组的起始地址
count: 从起始地址开始, 写入二维数组的数量。

示例:

integer code;
reg [7:0] mem [4:0];
integer fd;

initial
begin
    fd = $fopen("test.bin", "rb");
    code = $fread(mem, fd,1,2); #表示从二进制文件中读取数据,一次存放到mem[1],mem[2]
end

使用$fread 一次能从二进制文件中读出多少数据, 完全取决于mem定义的大小,如果mem定义为reg [15:0]mem,那么是从二进制文件中读取两个字节。

2.9.2 $fscanf

$fscanf按照格式将文件中的数据读到变量中, 格式可以参考$display 中的格式化内容。如果遇到空格或者换行,表示一次读取结束。 读取时,如果发生错误 则返回值为0,正常读取数据时为1, 读取文件结束时为-1。

integer <integer>;
<integer> = $fscanf(<file_desc>, "<format>", <destination_regs>);

integer:定义一个整型数值,正常读取为1,出错时为0,文件读取结束为 -1。
file_desc:为打开的文件句柄
format: 格式化输出,具体可以参照$display 中的格式化参数。表示以什么样的格式读取文件
destination_regs: 读取文件数据后, 保存在这个目标寄存器中。

示例:

integer fd;
integer char_num;
reg [8*10-1:0]   fbuf = 0;
initial begin
    fd = $fopen(FILE_TXT, "r");
    if(fd == 0)
    begin
        $display("$open file failed") ;
        $stop;
    end
    char_num = $fscanf(fd,"%s",fbuf);
    while ($signed(char_num) != -1)
    begin
        $display("%s", fbuf) ;
        #10;
        char_num = $fscanf(fd,"%s",fbuf);
    end
    #10;
    $fclose(fd) ;
end

2.9.3 $sscanf

$sscanf 将字符串按照某个模板格式进行扫描,其字符串格式和C语言中的printf()函数类似。

$sscanf(str,format,args)

$sccanf的三个参数,第一个是扫描对象,第二个是扫描格式,第三个是提取出来的参数。它是具有返回值的,如果扫描不成功则返回0,如果扫描成功,每提取一个数据(即args的个数),则返回值加1。提取不会改变str值,除非将str作为args。

示例:

float f;
int i;
string the_string = "foo -3.6 fum dum 17";
sscanf(the_string, "foo %f fum dum %d", f, i);

更多使用方法,参考如下链接:
Analyzing Strings with sscanf

2.9.4 $fseek

$fseek从文件中定位读写指针的位置,根据文件的参考点和偏移的位置,确定当前对文件读写的指针位置。

<integer> = $fseek(<file_desc>, <offset_value>, <operation_number>);

integer:定义整型变量,用于$fseek的返回值。如果$fseek成功执行,返回0; $fseek执行失败,返回-1。
file_desc:为打开的文件句柄
offset_value:相对于文件的参考指针,偏移的位置, 可以是正数,也可以是负数。
operation_number:定位文件指针的位置。 0,文件的起始位置;1,文件的当前位置; 2 文件结束的位置。

更多具体示例用法参考如下:
Verilog 文件操作- $fseek,$ftell,$feof

2.9.5 $ftell

这个系统函数的作用是找到当前的位置(距离文件首部),以字节为单位。

<reg> = $ftell(<file_desc>);

reg:返回文件当前位置距离文件首部的偏移位置,初始位置为0.
file_desc:为打开的文件句柄

更多具体示例用法参考如下:
Verilog 文件操作-$fseek,$ftell,$feof

2.9.6 $feof

这个系统函数的作用是判断文件指针是否到达文件尾部了。

<reg> = $feof(<file_desc>);

reg:检查文件指针是否到达文件尾部, 如果到达文件尾部,返回1; 否则返回0;
file_desc:为打开的文件句柄

更多具体示例用法参考如下:
Verilog 文件操作-$fseek,$ftell,$feof

2.9.7 $ferror

该函数用来报告文件的操作状态是否正确,如果在读文件的过程中出现异常,那么该函数返回0,并且只返回一次后即刻被清除。

integer errno;
errno=$ferror(fd,str);

描述的错误信息字符串将被存放在字符串str中。如果没有错误,errno将为0,str将被清空

2.9.7 $rewind

相当于fseek(fd, 0, 0);

2.9.7 $fflush

将缓存中的内容,送入文件中。

writes any buffered output to the file(s) specified by mcd, to the file specified by fd, or if $fflush is invoked with no arguments, to all open files.

$fflush ( mcd );
$fflush ( fd );
$fflush ( );

总结

本文主要介绍systemverilog中,文件输入输出相关的任务和函数。

  • 7
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值