verilog学习——系统任务和函数/system tasks and functions

本文介绍了Verilog中的系统任务,如显示任务(display/write/strobe)用于调试,连续监控器(monitor)自动打印变量变化,以及数学函数如对数、指数等。此外,还涵盖了时间格式、timescale指令和文件I/O操作,如打开、写入和读取文件的方法。
摘要由CSDN通过智能技术生成

verilog学习——系统任务和函数/system tasks and functions

(一)Verilog显示任务/task

显示系统任务(display system tasks)用于显示信息和调试信息,以跟踪日志文件中的模拟流程,有助于更快的调试。可以通过不同的显示任务组和格式打印值。
1.显示/写入任务(tasks)
语法:

$display(<list_of_arguments>);
$write(<list_of_arguments>);

d i s p l a y 和 display 和 displaywrite都按照参数列表中的出现顺序显示参数。
$write不会将换行符添加到其字符串的末尾。
2.verilog storbes
s t r o b e 在当前增量时间步长的末尾打印变量的最终值,并与 strobe 在当前增量时间步长的末尾打印变量的最终值,并与 strobe在当前增量时间步长的末尾打印变量的最终值,并与display具有类似的格式,

module tb;
  initial begin
    reg [7:0] a;
    reg [7:0] b;

    a = 8'h2D;
    b = 8'h2D;

    #10;                  // Wait till simulation reaches 10ns
    b <= a + 1;           // Assign a+1 value to b

    $display ("[$display] time=%0t a=0x%0h b=0x%0h", $time, a, b);
    $strobe  ("[$strobe]  time=%0t a=0x%0h b=0x%0h", $time, a, b);

    #1;
    $display ("[$display] time=%0t a=0x%0h b=0x%0h", $time, a, b);
    $strobe  ("[$strobe]  time=%0t a=0x%0h b=0x%0h", $time, a, b);

  end
endmodule
[$display] time=10 a=0x2d b=0x2d
[$strobe]  time=10 a=0x2d b=0x2e
[$display] time=11 a=0x2d b=0x2e
[$strobe]  time=11 a=0x2d b=0x2e

3.verilog continuous monitors
m o n i t o r 帮助每当参数列表中的变量或表达式发生更改时自动打印出变量或表达式值。它实现了类似 monitor 帮助每当参数列表中的变量或表达式发生更改时自动打印出变量或表达式值。它实现了类似 monitor帮助每当参数列表中的变量或表达式发生更改时自动打印出变量或表达式值。它实现了类似display的效果,会在每次更新任何参数后调用。
4.verilog format specifiers/Verilog格式说明符
为了在显示函数中打印变量,需要为变量提供适当的格式说明符。
在这里插入图片描述
5.verilog转义字符

Verilog数学函数

1.整数数学函数
函数 $clog2 返回给定参数的 log 2 上限。这通常用于计算寻址给定大小的内存所需的最小宽度。
2.实数数学函数
系统函数接收实数参数并返回实数参数。

在这里插入图片描述

module tb;
  real x, y;

  initial begin
    x = 10000;
    $display("$log10(%0.3f) = %0.3f", x, $log10(x));

    x = 1;
    $display("$ln(%0.3f) = %0.3f", x, $ln(x));

    x = 2;
    $display("$exp(%0.3f) = %0.3f", x, $exp(x));

    x = 25;
    $display("$sqrt(%0.3f) = %0.3f", x, $sqrt(x));

    x = 5;
    y = 3;
    $display("$pow(%0.3f, %0.3f) = %0.3f", x, y, $pow(x, y));

    x = 2.7813;
    $display("$floor(%0.3f) = %0.3f", x, $floor(x));

    x = 7.1111;
    $display("$ceil(%0.3f) = %0.3f", x, $ceil(x));

  end
endmodule
$log10(10000.000) = 4.000
$ln(1.000) = 0.000
$exp(2.000) = 7.389
$sqrt(25.000) = 5.000
$pow(5.000, 3.000) = 125.000
$floor(2.781) = 2.000
$ceil(7.111) = 8.000

(三)Verilog的时间格式

语法:
Verilog timescale 指令指定模拟的时间单位和精度。Verilog $timeformat 系统函数在显示语句(如 $display 和 $strobe )中指定 %t 格式说明符报告样式。

$timeformat(<unit_number>, <precision>, <suffix_string>, <minimum field width>);

unit_number是代码中使用的所有 ‘timescale 指令中最小的时间精度。
Precision表示当前timescale的小数位数。
suffix_string是一个选项,用于将刻度与实时值一起显示。
在这里插入图片描述

`timescale 1ns/1ps

module tb;
  bit 	a;

  initial begin

    // Wait for some time - note that because precision is 1/1000 of
    // the main scale (1ns), this delay will be truncated by the 3rd
    // position
    #10.512351;

    // Display current time with default timeformat parameters
    $display("[T=%0t] a=%0b", $realtime, a);

    // Change timeformat parameters and display again
    $timeformat(-9, 2, " ns");
    $display("[T=%0t] a=%0b", $realtime, a);

    // Remove the space in suffix, and extend fractional digits to 5
    $timeformat(-9, 5, "ns");
    $display("[T=%0t] a=%0b", $realtime, a);

    // Here suffix is wrong, it should not be "ns" because we are
    // setting display in "ps" (-12)
    $timeformat(-12, 3, " ns");
    $display("[T=%0t] a=%0b", $realtime, a);

    // Correct the suffix to ps
    $timeformat(-12, 2, " ps");
    $display("[T=%0t] a=%0b", $realtime, a);
  end
endmodule
[T=10512] a=0
[T=10.51 ns] a=0
[T=10.51200ns] a=0
[T=10512.000 ns] a=0
[T=10512.00 ps] a=0
修改:`timescale 1ns/100ps
[T=105] a=0
[T=10.50 ns] a=0
[T=10.50000ns] a=0
[T=10500.000 ns] a=0
[T=10500.00 ps] a=0
修改:`timescale 100ns/1ns
[T=1051] a=0
[T=1051.00 ns] a=0
[T=1051.00000ns] a=0
[T=1051000.000 ns] a=0
[T=1051000.00 ps] a=0

(四)Verilog timescale scope/Verilog时间刻度范围

1.默认时间刻度/default timescale
Verilog应在模块之前定义事件刻度,但模拟器可以有默认时间刻度。可以使用接受范围作为参数的系统任务 $printtimescale 来打印在Verilog详细层次结构中任何作用域应用的实际时间刻度。

module tb;
	initial begin
		// Print timescale of this module
		$printtimescale(tb);
		// $printtimescale($root);
	end
endmodule

2.标准时间刻度范围
默认情况下,文件中的timescale指令将应用于遵循该指令的所有模块,直到定义另一个timescal指令。
3.scope between Verilog files/Verilog文件之间的范围
‘include 指令是预处理指令,可以使用该指令将其他文件包含在当前文件中,在编译之前将文件内容放置到包含处。

// main.v
`timescale 1ns/1ps

module tb;
  des m_des();
  alu m_alu();

  initial begin
    $printtimescale(tb);
    $printtimescale(tb.m_alu);
	$printtimescale(tb.m_des);
  end
endmodule

`include "file_alu.v"
`include "file_des.v"

// file_alu.v
module alu;
endmodule

// file_des.v
`timescale 1ns/10ps

module des;

endmodule
Time scale of (tb) is  1ns /  1ps
Time scale of (tb.m_alu) is  1ns /  1ps
Time scale of (tb.m_des) is  1ns /  10ps

交换文件可以更改时间刻度/timescale:
文件的包含顺序在时间刻度指令的重新定义中起着重要作用。

(五)verilogg文件IO操作

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

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

打开文件模式:
在这里插入图片描述
2.如何写入文件
在这里插入图片描述
上述每个系统功能都以基数十进制打印值。他们还有另外三个版本,可以打印二进制、八进制和十六进制的值。

在这里插入图片描述
3.如何读取文件
读出一行:
系统函数 $fgets 将 [hl]fd[/hd] 指定的文件中的字符读取到变量 str 中,直到填充 str,或者读取换行符并将其传输到 str,或者遇到 EOF 条件。
如果在读取过程中发生错误,则返回代码零,否则,它将返回读取的字符数。
检测EOF:
当找到 EOF 时,系统函数 $ feof 返回非零值,否则对于给定的文件描述符作为参数返回零。
要显示多个参数:
当多个变量传入$fdisplay时,只是按给定的顺序一个接一个地打印所有变量,没有空格。
4.将数据格式化为字符串
$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
a=10 b=0xb
  • 28
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值