SystemVerilog命令行参数

您偶尔会遇到需要避免testbench重新编译的情况,而是能够像bash或perl之类的脚本语言那样从命令行接受值。在SystemVerilog中,此信息作为一个可选参数提供给模拟,始终以+字符开头。从命令行传入的这些参数可以通过以下称为plusargs的系统函数在SV代码中访问。

Syntax

$test$plusargs (user_string)
 
$value$plusargs (user_string, variable)

函数$ test $plusargs通常在不需要参数值时使用。它在plusargs列表中搜索用户指定的字符串。变量也可以用来指定字符串,任何空字符都将被忽略。如果提供的一个plusargs的前缀与提供的字符串中的所有字符匹配,则该函数将返回一个非零整数,否则返回零。

Example
 
module tb;
  initial begin
    if ($test$plusargs ("STANDBY"))
      $display ("STANDBY argument is found ...");
 
    if ($test$plusargs ("Standby"))
      $display ("Standby argument is also found ...");
 
    if ($test$plusargs ("STAND"))
      $display ("STAND substring is found ...");
 
    if ($test$plusargs ("S"))
      $display ("Some string starting with S found ...");
 
    if ($test$plusargs ("T"))
      $display ("Some string containing T found ...");
 
    if ($test$plusargs ("STAND_AT_EASE"))
      $display ("Can't stand any longer ...");
 
    if ($test$plusargs ("SUNSHADE"))
      $display ("It's too hot today ...");
 
    if ($test$plusargs ("WINTER"))
      $display ("No match.. ");
  end
endmodule

当使用运行时参数+ STANDBY编译并模拟上面显示的代码时,其中STANDBY是提供给模拟工具的字符串plusarg,我们得到如下所示的输出。 请注意,plusarg区分大小写,并且即使提供的字符串为“ STANDBY”,也匹配“ S”和“ STAND”。

Simulation Log
ncsim> run
STANDBY argument is found ...
STAND substring is found ...
Some string starting with S found ...
ncsim: *W,RNQUIE: Simulation is complete.

$ value $ plusargs

$ value $ plusargs系统函数还会搜索plusargs列表,例如$ test $ plusargs,但是它具有获取指定用户字符串的值的功能。 如果提供的plusargs之一的前缀与给定用户字符串中的所有字符匹配,则该函数将返回非零值并将结果值存储在提供的变量中。 如果找不到用户字符串,则该函数将返回非零值,并且不会修改该变量。

user_string的格式应为“ plusarg_string format_string”,其中格式字符串与$ display任务相同。 这些格式标识符用于将通过命令行提供的值转换为给定格式并存储在变量中。
在这里插入图片描述

Example
 
// Note FS : Format Specifier
 
module tb;
  initial begin
    string    var1, var2;
    bit [31:0]   data;
 
    if ($value$plusargs ("STRING=%s", var1))
      $display ("STRING with FS has a value %s", var1);
 
    if ($value$plusargs ("NUMBER=%0d", data))
      $display ("NUMBER with %%0d has a value %0d", data);
 
    if ($value$plusargs ("NUMBER=%0h", data))
      $display ("NUMBER with %%0h has a value %0d", data);
 
    if ($value$plusargs ("NUMBER=%s", data))
      $display ("NUMBER with %%s has a value %0d", data);
 
    if ($value$plusargs ("STRING=", var1))
      $display ("STRING without FS has a value %s", var1);
 
    if ($value$plusargs ("+STRING=%s", var1))
      $display ("STRING with + char has a value %s", var1);
 
`ifdef RUNTIME_ERR    
    if ($value$plusargs ("STRING=%0d", var2))
      $display ("STRING with %%0d has a value %s", var2);
`endif
  end
endmodule
 

对于不同的输入参数,我们将获得不同的输出。还要注意,用户字符串=和命令行表达式中的值之间不应有任何空格。
+STRING=Joey or +STRING=“Joey”
Joey”可以带有或不带有双引号。

ncsim> run
STRING with FS has a value Joey
STRING without FS has a value Joey
ncsim: *W,RNQUIE: Simulation is complete.

+ NUMBER = 100

+ 值100根据不同的格式说明符转换并存储在给定的变量中。

ncsim> run
NUMBER with %0d has a value 100
NUMBER with %0h has a value 256
NUMBER with %s has a value 3223600
ncsim: *W,RNQUIE: Simulation is complete.

+ STRING

  • 缺少=符号,因此无法识别。
    ncsim> run ncsim: *W,RNQUIE: Simulation is complete.

  • STRING = 参数值是一个空字符串,因此显示为空。

参考文献:
【1】https://www.chipverify.com/systemverilog/systemverilog-command-line-input#example-2

发布了71 篇原创文章 · 获赞 8 · 访问量 7371

猜你喜欢

转载自blog.csdn.net/qq_43042339/article/details/104590854
今日推荐