verilog中的文件读写等操作指令

System file I/O tasks

mcd = $fopen("file_name" [ , type ] );

$fopen opens a disk file for writing, and returns a 32-bit unsigned integer multi-channel descriptor pointer to the file. It returns a zero if the file could not be opened for writing. In Verilog-2001 the type indicates how the file is to be opened. The "b" distinguishes a binary file from a text file:


$fclose(mcd);

$fclose closes a disk file that was opened by $fopen.


$readmemb("file_name", memory_name [ , start_address [ , end_address ]] );
$readmemh("file_name", memory_name [ , start_address [ , end_address ]] );

$readmemb and $readmemh initialize a memory array with the values from the file. The file must be an ASCII file with values represented in binary ($readmemb) or hexadecimal ($readmemh). The data values must be the same width as the memory array, and be separated by white spaces. The start and end address are hexadecimal numbers, even for $readmemb, preceded by @.


c = $fgetc(mcd);

$fgetc reads a byte (a character) from the file. 


ungetc(c, mcd);

$ungetc inserts a specified character into a buffer specified by the file.


i = $fgets(str, mcd);

$fgets reads a line from the file.


i = $fscanf(mcd, "text", signal, signal, ...);
i = $sscanf(str, "text", signal, signal, ...);

$fscanf and $sscanf reads data and interprets the data according to a format.


i = $fread(reg_or_mem, mcd [ , start_address [ , end_address ]] );

$fread reads binary data from the file.


i = $ftell(mcd)

$ftell returns the offset from the beginning of the file.


i = $fseek(mcd, offset, operation);
i = $rewind(mcd);

$fseek sets the position of the next input or output operation on the file.

$rewind is the same as $fseek(0,0).


$fflush( [ mcd ] );

 $fflush writes any buffered output to the file.


i = $ferror(mcd, str);

 $ferror can be used to obtain more information about an error. 


$swrite(output_reg, signal, signal, ...));
$sformat(output_reg, text", signal, signal, ...);

$swrite and $sformat writes a string to a reg variable. $sformat interprets the string as a format string.


Example:
initial begin
  File = $fopen("Result.dat");
  if (!File)
    $display("Could not open \"result.dat\"");
  else begin
    $display(File, "Result is: %4b", A);
    $fclose(File);
  end
end

reg [7:0] Memory [15:0];
initial begin
  $readmemb("Stimulus.txt", Memory);
end

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值