#学习记录#
目录
Abstract
我们在进行verilog仿真时,经常喜欢采用宏定义,来做条件判断,但是通过宏定义做条件判断的这种方法,存在很大的弊端,就是条件改变的时候,需要重新编译,这样会导致,在项目后期进行回归测试和后仿的时候,很多时间都浪费在重复编译上面,效率非常低下。
$test$plusargs和$value$plusargs作为进行Verilog和SystemVerilog仿真运行时调用的系统函数,可以在仿真命令中直接通过进行赋值的方式将参数传递进入到设计中,并且不局限于不同仿真器对于参数在仿真命令中定义格式不同的限制,也避免了调换参数带来的频繁编译等问题。
1 使用宏定义的条件编译
initial
begin
`ifdef dump_fsdb
$dumpfile("test.fsdb");
$dumpvars;
`endif
end
如果要能够成功调用$dump等函数,需要在编译(compile)时指定`define的宏定义,其使用方法如下:
<compile-option> -define dump_fsdb
但是这种宏定义的方式,实现条件编译,当条件改变时,需要重新编译,这样会造成仿真效率底下,特别在项目后期,对测试用例进行回归的时候,会有相当一部分时间浪费在编译上。
2 $test$plusargs
使用条件编译函数$test$plusargs的代码如下:
initial
begin
if($test$plusargs("test1"))
$readmemh("test1.dat",mem1);
if($test$plusargs("test2"))
$readmemh("test2.dat",mem2);
end
仿真运行命令如下,如果仿真不需要test1,只需要在运行命令中去掉test1。
<run-options> +test1 +test2
3 $value$plusargs
$value$plusargs可以将运行命令(run-options)中的参数值,传递给指定的信号或者字符,其语法格式如下:
$value$plusargs(“string”,signalname);
其中string="plusarg_format"+"format_string","plusarg_format"指明了用户定义的要进行传递的值。"format_string"指定了要传递的值的格式(类似$display中定义的%s、%h等)。并且在string中"plusarg_format和"format_string"的格式应该为"plusarg_format"=/+"plusarg_format"。如果转换后的位宽和传递的值不一致,则按照如下规则转换:
plusarg位宽与sigalname的关系 | signalname值 |
< | plusarg左补零 |
> | plusarg截位 |
plusarg为负数 | 按照正数处理 |
不匹配 | 若为指定默认值,则reg类型为x |
代码如下:
if($value$plusargs("finish=%d", finish))
begin
repeat(finish);
$display("finish=%d", finish);
$finish;
end
if($value$plusargs("freq=%f",frequency))
begin
$display("freq=%f", frequency);
end
if($value$plusargs("testname=%s"testname))
begin
$display("testname=%s",testname);
end
运行命令:
<run-options> +finish=100 +freq=1.23 +testname=test1
结果:
finish:100
freq:1.23
testname=test1
参考文献
[1] verilog仿真测试中的参数传递——$test$plusargs和$value$plusargs-CSDN博客
[2] Verilog之“$test$plusargs和$value$plusargs用法小结“ - 知乎 (zhihu.com)