认识系统函数$test$plusargs与$value$plusargs

【转】https://blog.csdn.net/kevindas/article/details/80380144

说明:系统函数的介绍参考的是VCS User Guide和IEEE Verilog-2005标准,不同IDE可能不太一样。1、$test$plusargs(string)    在对verilog代码进行编译时,我们会在代码中引入`ifdef, `elsif, `endif等条件编译指令,然后在编译时指定宏来对编译代码范围进行控制。系统函数$test$plusargs的目的就是用来替换这些条件编译指令的。    在使用时,该函数会判断参数string是否定义,从而返回0或非0。VCS User Guide上例子如下:

initial
if ($test$plusargs("postprocess"))
begin
$vcdpluson(0,design_1);
$vcdplusdeltacycleon;
$vcdplusglitchon;
end   

当然,这里所谓的定义就不再是define定义的宏了,而是在VCS运行simv时指定的参数。比如上面的例子,你就需要如下的参数:

% simv +postprocess

   

Q1:那么问题来了,既然已经有了条件编译指令,这样做会不会显得很多余?   

A1:关键就在于条件编译会在编译阶段生效,而系统函数$test$plusargs则会在仿真阶段生效。换句话说,当参数发生变化时,你不需要重新编译代码,只需要改变参数重新运行仿真即可。   

Q2:既然系统函数$test$plusargs这么好,那么是不是条件编译指令就没有用武之地了?   

A2:好处是有代价的,引入系统函数会让你的编译变得更庞大(预编译只对相应代码编译,其它部分即使语法错误也不管),而且simv的运行效率更低。事实上,synopsys官方都不建议我们直接使用该函数:

For this reason, Synopsys recommends that if you use this technique,
you should plan to abandon it fairly early in the development cycle
and switch to either the conditional compilation technique for writing
simulation history files, or a combination of the two techniques.

Q3:到底要不要使用这个函数?   

A3:我认为还是按照官方的建议来,采用$test$plusargs与预编译结合的方式(个中妙处,自行体会):

`ifdef comppostprocess
initial
if ($test$plusargs("postprocess"))
begin
$vcdpluson(0,design_1);
$vcsplusdeltacycleon;
$vcdplusglitchon;
end
`endif

pitfall:
    这个函数在扫描参数list时只顾自己,只要自己匹配上了,就不管参数后半截了,也是就说,当参数是+postprocessabc也是能够匹配上的。

2、$value$plusargs(user_string, variable)   

这个系统函数用于在仿真时将一个参数传递至内部信号。   

example:

module valueplusargs;
reg [31:0] r1;
integer status;
initial
begin
$monitor("r1=%0d at %0t",r1,$time);
#1 r1=0;
#1 status=$value$plusargs("r1=%d",r1);
end
endmodule

user_string格式可以变化,只要vcs能正确提取数据即可。运行时指定参数值:

% simv +r1=10

运行结果:

r1=x at 0
r1=0 at 1
r1=10 at 2

user_string的格式和$display函数一致,支持的数据类型如下:

%d decimal conversion
%o octal conversion
%h hexadecimal conversion
%b binary conversion
%e real exponential conversion
%f real decimal conversion
%g real decimal or exponential conversion
%s string (no conversion)是支持小数类型的,网上其它地方说只支持整型是不对的。
————————————————
版权声明:本文为CSDN博主「kevindas」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kevindas/article/details/80380144

猜你喜欢

转载自www.cnblogs.com/camellia3371----/p/12213216.html