Verilog基础语法(18)之仿真中显示任务

本文介绍了Verilog语言中用于调试和信息显示的命令,包括$display和$write的使用区别,$strobe的同步特性以及$monitor的实时监控功能。此外,还讨论了格式指定符和特殊字符在打印输出中的作用。
摘要由CSDN通过智能技术生成

显示系统任务主要用于显示信息和调试信息,从日志文件中跟踪仿真的流程,也有助于更快地进行调试。显示任务有不同的组别和格式,它们可以打印数值。

仿真可不是只有波形就完事无忧的,有的时候需要一些显示任务将某些节点的数据打印出来,有利于分析。

Display/Write Tasks

$display$write都会按照参数列表中的顺序显示参数。

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

$write不会在字符串末尾添加换行符,而$display会,从下面的例子可以看出。

module tb;
  initial begin
    $display ("This ends with a new line ");
    $write ("This does not,");
    $write ("like this. To start new line, use newline char
");
    $display ("This always start on a new line !");
  end
endmodule
This ends with a new line 
This does not,like this. To start new line, use newline char 
Hi there !

Strobes

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

需要注意的是,$strobe在时间10ns显示变量b的最终更新值,也就是0x2E,而$display只有在11ns的下一次模拟delta中才会接收到这个值。

[$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

monitor

每当参数列表中的变量或表达式发生变化时,$monitor就会自动打印出变量或表达式的值。它达到的效果与每次更新参数后调用$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

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

    #1 b <= 8'hA4;
    #5 b <= a - 8'h33;
    #10 b <= 8'h1;

  end
endmodule

请注意,monitor 就 像 一 个 在 主 线 程 后 台 生 成 运 行 的 任 务 , 它 监 视 并 显 示 其 参 数 变 量 的 值 变 化 。 新 的 monitor就像一个在主线程后台生成运行的任务,它监视并显示其参数变量的值变化

[$monitor] time=10 a=0x2d b=0x2e
[$monitor] time=11 a=0x2d b=0xa4
[$monitor] time=16 a=0x2d b=0xfa
[$monitor] time=26 a=0x2d b=0x1

格式指定符

符号意义
%h, %H显示十六进制格式
%d, %D显示十进制格式
%b, %B显示二进制格式
%m, %M显示分层名称
%s, %S显示字符串
%t, %T以时间格式显示
%f, %F以十进制格式显示’实数’。
%e, %E以指数格式显示 “实数”。
module tb;
  initial begin
    reg [7:0]  a;
    reg [39:0] str = "Hello";
    time       cur_time;
    real       float_pt;

    a = 8'h0E;
    float_pt = 3.142;

    $display ("a = %h", a);
    $display ("a = %d", a);
    $display ("a = %b", a);

    $display ("str = %s", str);
    #200 cur_time = $time;
    $display ("time = %t", cur_time);
    $display ("float_pt = %f", float_pt);
    $display ("float_pt = %e", float_pt);
  end
endmodule
a = 0e
a =  14
a = 00001110
str = Hello
time =                  200
float_pt = 3.142000
float_pt = 3.142000e+00

特殊字符

有些字符被认为是特殊字符,因为它们代表其他显示目的,如换行、制表符和换页。为了打印这些特殊字符,必须对这些字符的每次出现进行转义。

字符意义
\n新行字
\tTab 制表符
\\符号
‘’''符号
%%%%符号
module tb;
  initial begin
    $write ("Newline character \n");
    $display ("Tab character \tstop");
    $display ("Escaping  " %%");

/*
    // Compilation errors
    $display ("Without escaping ");       // ERROR : Unterminated string
    $display ("Without escaping "");       // ERROR : Unterminated string
*/
  end
endmodule
Newline character 
 
Tab character	stop
Escaping  " %
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值