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

说明:系统函数的介绍参考的是VCS User GuideIEEE 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)
支持小数类型的,网上其它地方说只支持整型是不对的。

附:总是觉得函数的名字好奇怪,当初为什么要起这样的名字,希望懂的人来解答下。。。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值