亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Verilog

Verilog 文件 IO 操作

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

打开和关闭文件

  
  
module tb;
	// Declare a variable to store the file handler
	integer fd;
	
	initial begin
		// Open a new file by the name "my_file.txt" 
		// with "write" permissions, and store the file
		// handler pointer in variable "fd"
		fd = $fopen("my_file.txt", "w");
		
		// Close the file handle pointed to by "fd"
		$fclose(fd);
	end
endmodule

  

打开文件模式

参数 说明
“r”或“rb” 开放阅读
“w”或“wb” 创建一个用于写入的新文件。如果文件存在,将其截断为零长度并覆盖它
“a”或“ab” 如果文件存在,则追加(在 EOF 处打开写入),否则创建一个新文件
“r+”、“r+b”或“rb+” 可读可写
“w+”、“w+b”或“wb+” 截断或创建更新
“a+”、“a+b”或“ab+” 追加或创建新文件以在 EOF 更新

如何写入文件

函数 说明
$fdisplay 类似于$display,改为写入文件
$fwrite 类似于 $write,改为写入文件
$fstrobe 类似于 $strobe,改为写入文件
$fmonitor 类似于 $monitor,改为写入文件

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

函数 说明
$fdisplay() 默认以十进制打印
$fdisplayb() 二进制打印
$fdisplayo() 八进制打印
$fdisplayh() 以十六进制打印
  
  
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

  
模拟日志
Value displayed with $fdisplay
26
00011010
032
1a
Value displayed with $fwrite
 43001010110532b
Value displayed with $fstrobe
 60
00111100
074
3c
Value displayed with $fmonitor
 60
  0
  1
  2
  3
  4

如何读取文件

读一行

系统函数$fgets 从 [hl]fd[/hd] 指定的文件中读取字符到变量 str 中,直到 str 被填满,或者读取换行符并传输到 str,或者遇到 EOF 条件。

如果在读取过程中发生错误,则返回代码零。否则返回读取的字符数。

检测EOF

系统函数$feof 当找到 EOF 时返回一个非零值,否则对于给定的文件描述符作为参数返回零。

  
  
module tb;
	reg[8*45:1] str;
	integer  	fd;
	
	initial begin
	  fd = $fopen("my_file.txt", "r");
	  
	  // Keep reading lines until EOF is found
      while (! $feof(fd)) begin
      
      	// Get current line into the variable 'str'
        $fgets(str, fd);
        
        // Display contents of the variable
        $display("%0s", str);
      end
      $fclose(fd);
	end
endmodule

  

fdisplay 的多个参数

当给$fdisplay多个变量时 ,它只是按给定的顺序一个接一个地打印所有变量,没有空格。

  
  
module tb;
  reg [3:0] a, b, c, d;
  reg [8*30:0] str;
  integer fd;
  
  initial begin
    a = 4'ha;
    b = 4'hb;
    c = 4'hc;
    d = 4'hd;
    
    fd = $fopen("my_file.txt", "w");
    $fdisplay(fd, a, b, c, d);
    $fclose(fd);
  end
endmodule

  
模拟日志
10111213

将数据格式化为字符串

$sformat 中的第一个参数 系统函数是放置结果的变量名。第二个参数是 format_string 它告诉如何将以下参数格式化为字符串。

  
  
module tb;
	reg [8*19:0] str;
	reg [3:0] a, b;
	
	
	initial begin
		a = 4'hA;
		b = 4'hB;
		
		// Format 'a' and 'b' into a string given
		// by the format, and store into 'str' variable
		$sformat(str, "a=%0d b=0x%0h", a, b);
		$display("%0s", str);
	end
endmodule

  
模拟日志
xcelium> run
a=10 b=0xb
xmsim: *W,RNQUIE: Simulation is complete.


Verilog

  1. Verilog 简介
  2. Verilog 教程
  3. Verilog 连接
  4. Verilog 作业
  5. Verilog 阻塞和非阻塞
  6. Verilog 函数
  7. Verilog 任务
  8. Verilog 时钟发生器
  9. Verilog 数学函数
  10. Verilog 时间格式
  11. Verilog 时间刻度范围
  12. Verilog Hello World