systemverilog/verilog文件操作

1、Verilog文件操作

        Verilog具有系统任务和功能,可以打开文件、将值输出到文件、从文件中读取值并加载到其他变量和关闭文件。

1.1 、Verilog文件操作

1.1.1、打开和关闭文件

module tb;
// 声明一个变量存储 file handler
integer fd;
initial begin
// 以写权限打开一个文件名为 "my_file.txt" 的新文件,并将文件柄指针存储在变量"fd"中
    fd = $fopen("my_file.txt", "w");
// 关闭指向 "fd"的文件柄
    $fclose(fd);
end
endmodule

文件操作选项:

1.1.2、写文件

        每个系统函数都以基数十进制打印值。它们还有其他三个版本,可以以二进制、八进制和十六进制打印。

integer handle1;
initial begin
handel1=$fopen("picture_out.txt","w");
forever @(posedge clk) begin
    if(PicInHEnd &PicInVEnd) begin
        $fclose(handle1);
        $finish;
    end
end
end
always@(posedge clk) begin
    if(  PicOutVld  ) begin
        if(PicInHEnd)
            $fwrite(handle1,"%h",PicInData,"\n");
        else
            $fwrite(handle1,"%h",PicInData,"\n");
    end
end

使用方法如下:
 

module tb;
integer fd;
integer i;
reg [7:0] my_var;
initial begin
  // Create a new file
  fd = $fopen("my_file.txt", "w");
  my_var = 0;
   $fdisplay(fd, "Value displayed with $fdisplay");
  #10 my_var = 8'h1A;
  $fdisplay(fd, my_var); // Displays in decimal
  $fdisplayb(fd, my_var); // Displays in binary
  $fdisplayo(fd, my_var); // Displays in octal
  $fdisplayh(fd, my_var); // Displays in hex
  // $fwrite does not print the newline char '' automatically at
   // the end of each line; So we can predict all the values printed
   // below to appear on the same line
   $fdisplay(fd, "Value displayed with $fwrite");
  #10 my_var = 8'h2B;
  $fwrite(fd, my_var);
  $fwriteb(fd, my_var);
  $fwriteo(fd, my_var);
  $fwriteh(fd, my_var);
   // Jump to new line with '', and print with strobe which takes
   // the final value of the variable after non-blocking assignments
   // are done
   $fdisplay(fd, "Value displayed with $fstrobe");
  #10 my_var <= 8'h3C;
  $fstrobe(fd, my_var);
  $fstrobeb(fd, my_var);
  $fstrobeo(fd, my_var);
  $fstrobeh(fd, my_var);
   #10 $fdisplay(fd, "Value displayed with $fmonitor");
   $fmonitor(fd, my_var);
  for(i = 0; i < 5; i= i+1) begin
    #5 my_var <= i;
  end
   #10 $fclose(fd);
end
endmodule

1.1.3、读取文件

1.1.3.1、文件读取:$fscanf和$readmemh

其中$readmemh的作用是一次性将文件中的数据读取到某个数组中,然后一次从数组中读取1个数组进行处理,而$fscanf的作用是从文件中读取一行数据。

        在verilog中,$fscanf函数用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值