FPGA基础 -- Verilog 命令行参数变量(Command-line Plusargs)

Verilog 命令行参数变量(Command-line Plusargs)

一、概述:什么是 Plusargs?

Plusargs 是仿真运行时通过命令行传入的键值对参数,用于控制 Verilog/SystemVerilog 仿真行为,例如:控制仿真模式、设置参数值、指定波形路径等。

典型形式如下:

vsim +MODE=debug +dumpfile=wave.vcd

在仿真时通过 $test$plusargs()$value$plusargs() 在 RTL 中读取。


二、两种读取方式

方法功能使用场景
$test$plusargs("key")检测某个 plusarg 是否存在,返回布尔值标志位、模式选择
$value$plusargs("key=%d", var)获取 plusarg 的数值赋给变量配置数值、参数传递

2.1 $test$plusargs 示例

initial begin
  if ($test$plusargs("DEBUG")) begin
    $display("Debug mode enabled.");
  end
end

运行命令:

vsim +DEBUG

2.2 $value$plusargs 示例

integer freq;
initial begin
  if (!$value$plusargs("FREQ=%d", freq)) begin
    freq = 100; // 默认值
  end
  $display("Frequency is %d MHz", freq);
end

运行命令:

vsim +FREQ=200

结果:

Frequency is 200 MHz

三、常见应用场景

场景示例
配置寄存器宽度+DATA_WIDTH=32,设置仿真参数
选择测试场景+CASE=tx_only,选择测试模块或行为
指定波形文件名+dumpfile=output.vcd
调试开关+DEBUG,启用调试输出
多测试用例复用通过 plusargs 指定 DUT 配置

四、Verilog 示例:模块参数可配置化

module test;

  integer mode, depth;

  initial begin
    if (!$value$plusargs("MODE=%d", mode))
      mode = 0;

    if (!$value$plusargs("DEPTH=%d", depth))
      depth = 16;

    $display("Running in mode %0d with depth %0d", mode, depth);
  end

endmodule

命令行运行:

vsim test +MODE=2 +DEPTH=64

输出:

Running in mode 2 with depth 64

五、工具支持差异

仿真器支持 plusargs说明
ModelSim支持 $test$plusargs $value$plusargs
VCS同上
Verilator✅(不同写法)C++ 接口中读取 plusargs
XSIM (Vivado)通过 Tcl 脚本传入

六、使用技巧

  1. 默认值保护:使用 if (!$value$plusargs(...)) 写法防止未传参时异常。
  2. 组合多个参数构建测试平台
  3. 配合参数化模块(parameter)灵活仿真多个配置组合
  4. 避免在合成代码中使用$value$plusargs$test$plusargs系统函数,不能综合

七、进阶拓展(SystemVerilog)

SystemVerilog 支持结构体封装参数:

typedef struct {
  int mode;
  int freq;
} config_t;

config_t cfg;

initial begin
  void'($value$plusargs("MODE=%d", cfg.mode));
  void'($value$plusargs("FREQ=%d", cfg.freq));
end

八、实战建议

  • 开发验证平台时,使用 plusargs 控制模块行为(如 AXI 配置、传输模式、波形选项)。
  • 约定命名规则:如 +TESTCASE=xxx+DUMP=1 等提升项目一致性。
  • 集成自动化仿真脚本(Makefile/Tcl) 中自动拼接参数,构建可移植的仿真平台。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值