[SV]Verilog系统任务及应用实例 ---- 文件讀寫任务及应用案例(File I/O tasks and example)

                       Verilog系统任务及应用实例

                              ---- 文件讀寫任务及应用案例(File I/O tasks and example)

一、文件讀寫任務列表

 1.1、File I/O task list

No.                    Task                                 Description
1 $fopen("file_name" [ , type ] ) 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. 
2 $fclose(mcd); closes a disk file that was opened by $fopen.
3 $readmemb("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 @.
4 $readmemh("file_name", memory_name [ , start_address [ , end_address ]] );  
6 $fgetc(mcd); $fgetc reads a byte (a character) from the file.
7 ungetc(c, mcd); $ungetc inserts a specified character into a buffer specified by the file.
8  $fgets(str, mcd); $fgets reads a line from the file.
9 $fscanf(mcd, "text", signal, signal, ...); $fscanf and $sscanf reads data and interprets the data according to a format. $fscanf reads the data from a file
10 $sscanf(str, "text", signal, signal, ...); $sscanf reads the data from a reg variable.
11 $fread(reg_or_mem, mcd [ , start_address [ , end_address ]] ); $fread reads binary data from the file.
12 $ftell(mcd); $ftell returns the offset from the beginning of the file.
13 $fseek(mcd, offset, operation); $fseek sets the position of the next input or output operation on the file.
14 $rewind(mcd); $rewind is the same as $fseek(0,0).
15 $fflush( [ mcd ] );  $fflush writes any buffered output to the file.
16 $ferror(mcd, str); $ferror can be used to obtain more information about an error.
17 $swrite(output_reg, signal, signal, ...)); $swrite and $sformat writes a string to a reg variable.
18 $sformat(output_reg, text", signal, signal, ...);  $sformat interprets the string as a format string.
  • mcd = expression       // multi-channel descriptor; in Verilog-2001 file descriptor

 1.2、File I/O Type Lise

No.                    Type                                                                           Description
1 "r" or "rb" Open for reading,打開文件,返回文件指針
2 "w" or "wb" Create for writing,創建文件并寫入
3 "a" or "ab" Append,以追加模式打開
4 "r+" or "rb+" or "r+b" Open for update (reading and writing)
5 "w+" or "wb+" or "w+b" Create for update
6 "a+" or "ab+" or "a+b" Append; open or create for update at end-of-file

 1.3、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

二、SystemVerilog讀寫文件實例

 2.1、讀寫程序

module openfile#(
  parameter FILE    = "txt",
  parameter DW      = 32,
  parameter PN      = 32
)
(
  input bit            rst_b,
  input bit            clk,
  input bit            pop_str,
  input bit [DW-1:0]   out_i
)

  integer              int_ifptr, int_count;
  reg signed [31:0]    data_i_scan[0:PN-1];
  integer              data_i_tmp[$];
  reg                  dout_i;

  initial begin : open_file
    repeat(10) @(posedge clk);
    int_ifptr = $fopen($sformatf(FILE), "r");
    for(int i = 0; i < PN; i = i + 1) begin
      int_count = $fscanf(int_ifptr, "%0h\n", data_i_scan[i]);
      data_i_tmp.push_back(data_i_scan[i]);
    end
    $fclose(int_ifptr);
  end

  always @(posedge clk or negedge rst_n) begin
    if(!rst_n) begin
      dout_i <= {32{1'b0}};
    end
    else if(data_i_tmp.size() != 0) begin
      if(pop_str) begin
        dout_i <= data_i_tmp.pop_front();
      end
    end
  end

  assign out_i = d_out_i[DW-1:0];

endmodule

 2.2、仿真程序

program read_file();
  integer    f;
  string     key;

  reg [31:0]    value;
  reg [31:0]    hash [string];

  initial begin : file_read
    f = $fopen("file.txt", "r");
    if(f == 0) disable file_read;

    while(!feof(f)) begin
      $fscanf(f, "%s %h\n", key, value);   //$fscanf裡面的格式控制符與文件格式要Match
      hash[key] = value;
    end

    foeeach(hash[i]) begin
      $display("Hash Key %0s --> %0h", i hash[i])
    end

endprogram

三、參考資料

 3.1、https://blog.csdn.net/gsjthxy/article/details/103887334

 3.1、Link

 3.2、Link

原创文章 228 获赞 180 访问量 6万+

猜你喜欢

转载自blog.csdn.net/gsjthxy/article/details/105748166