1. 问题一:$value $plusarg 用处
注意:解答来自 IEEE Standard for SystemVerilog书籍中的第 21章 Input/output system tasks and system functions的第21.6 Command line input。
1.1 问题解决
读取文件以获取用于模拟的信息的替代方法是使用命令指定信息以调用模拟器。 此信息采用提供给模拟的可选参数的形式。 这些参数以加号 (+) 字符开头,在视觉上与其他模拟器参数区分开来。
这些参数(以下称为 plusargs)可通过以下系统函数访问:
$test $plusargs ( string )
$value $plusargs ( user_string, variable )
1.1.1 $test $plusarg
系统函数$test $plusarg搜索用户指定的plusarg_string的plusargs列表。字符串在系统函数的参数中指定为字符串或被解释为字符串的非实数变量。这个字符串不包含命令行参数的前导加号(+)。按照提供的顺序搜索命令行上的plusargs。如果提供的一个plusargs的前缀匹配提供的字符串中的所有字符,则函数返回一个非零整数。如果命令行中没有匹配提供的字符串的plusarg,函数将返回整数值0。
例如:
运行仿真器时添加命令: +HELLO
有如下Verilog代码:
initial begin
if ($test$plusargs("HELLO")) $display("Hello argument found.");
if ($test$plusargs("HE")) $display("The HE subset string is detected.");
if ($test$plusargs("H")) $display("Argument starting with H found.");
if ($test$plusargs("HELLO_HERE"))$display("Long argument.");
if ($test$plusargs("HI")) $display("Simple greeting.");
if ($test$plusargs("LO")) $display("Does not match.");
end
有如下输出:
Hello argument found.
The HE subset string is detected.
Argument starting with H found.
结论:$test $plusargs执行的是 从输入字符串开头开始的部分匹配。输入的是“HELLO”,从H开始匹配,所以“HELLO”、“HE”、“H”对应的 $test $plusargs()返回值为1,所以会打印出来,其余的返回值为0,不打印。
1.1.2 $value $plusargs
系统函数 $value $ plusarg搜索用户指定的plusargs列表(类似于 $test $plusargs系统函数)。字符串在系统函数的第一个参数中指定为字符串或被解释为字符串的非实数变量。这个字符串不包含命令行参数的前导加号。按照提供的顺序搜索命令行上的plusargs。如果提供的一个plusargs的前缀匹配提供的字符串中的所有字符,函数将返回一个非零整数,字符串的其余部分将转换为user_string中指定的类型,并将结果值存储在提供的变量中。如果没有找到匹配的字符串,函数将返回整数值0,并且不修改提供的变量。当函数返回0(0)时不会产生任何警告。
user_string的格式如下:“plusarg_string format_string”。格式字符串与$display系统任务相同。这些是唯一有效的(大写和小写以及前导0形式是有效的):
%d 转换为十进制数
%o 转换为八进制数
%h 转换为十六进制数
%b 转换为二进制数
%e 转换为实指数
%f 转换为实数
%g 转换为实数小数或指数
%s 字符串(不转换)
提供给仿真器的plusargs列表中的第一个字符串,它与指定的user_string的plusarg_string部分相匹配,应该是可用于转换的plusarg字符串。匹配的plusarg的剩余部分字符串(剩余部分是plusarg字符串中与用户的plusarg_string匹配的部分后面的部分)将从字符串转换为格式字符串指定的格式,并存储在提供的变量中。如果没有剩余的字符串,则存储到变量中的值应该是0或空字符串值。
如果变量的大小大于转换后的值,则存储的值将被填零到变量的宽度。如果变量不能包含转换后的值,则该值将被截断。如果该值为负数,则认为该值大于所提供的变量。如果字符串中存在可用于转换的字符,而该字符对指定的转换是非法的,则该变量应写入值’bx。
有如下Verilog代码:
`define STRING logic [1024 * 8:1]
module goodtasks;
`STRING str;
integer i1;
logic
real realvar;
[31:0] vect;
initial
begin
if ($value$plusargs("TEST=%d", i1))
else
$display("value was %d", i1);
$display("+TEST= not found");
end
#100 $finish;
endmodule
module ieee1364_example;
real frequency;
logic [8*32:1] testname;
logic [64*8:1] pstring;
logic clk;
initial
begin
if ($value$plusargs("TESTNAME=%s",testname))
begin
$display(" TESTNAME= %s.",testname);
end
$finish;
if (!($value$plusargs("FREQ+%0F",frequency)))
frequency = 8.33333; // 166 MHz
$display("frequency = %f",frequency);
pstring = "TEST%d";
if ($value$plusargs(pstring, testname))
$display("Running test number %0d.",testname);
end
endmodule
结果:
and adding to the tool’s command line the plusarg
+TEST=5
will result in the following output:
value was 5
frequency = 8.333330
Running text number x.
Adding to the tool’s command line the plusarg
+TESTNAME=t1
will result in the following output:
+TEST= not found
TESTNAME= t1.
Adding to the tool’s command line the plusarg
+FREQ+9.234
will result in the following output:
+TEST= not found
frequency = 9.234000
Adding to the tool’s command line the plusarg
+TEST23
will result in the following output:
+TEST= not found
frequency = 8.333330
Running test number 23.
声明
本人所有系列的文章,仅供学习,不可商用,如有侵权,请告知,立删!!!
本人主要是记录学习过程,以供自己回头复习,再就是提供给后人参考,不喜勿喷!!!
如果觉得对你有用的话,记得收藏+评论!!!