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 beginif ($test$plusargs("DEBUG")) begin$display("Debug mode enabled.");end
end
运行命令:
vsim +DEBUG
2.2 $value$plusargs
示例
integer freq;
initial beginif (!$value$plusargs("FREQ=%d", freq)) beginfreq = 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 beginif (!$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);endendmodule
命令行运行:
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 脚本传入 |
六、使用技巧
- 默认值保护:使用
if (!$value$plusargs(...))
写法防止未传参时异常。 - 组合多个参数构建测试平台。
- 配合参数化模块(parameter)灵活仿真多个配置组合。
- 避免在合成代码中使用:
$value$plusargs
、$test$plusargs
是系统函数,不能综合!
七、进阶拓展(SystemVerilog)
SystemVerilog 支持结构体封装参数:
typedef struct {int mode;int freq;
} config_t;config_t cfg;initial beginvoid'($value$plusargs("MODE=%d", cfg.mode));void'($value$plusargs("FREQ=%d", cfg.freq));
end
八、实战建议
- 开发验证平台时,使用 plusargs 控制模块行为(如 AXI 配置、传输模式、波形选项)。
- 约定命名规则:如
+TESTCASE=xxx
、+DUMP=1
等提升项目一致性。 - 集成自动化仿真脚本(Makefile/Tcl) 中自动拼接参数,构建可移植的仿真平台。