SystemVerilog plusargs 与文件 I/O

plusargs 让同一个仿真可执行文件通过命令行切换测试、种子和配置;文件 I/O 让 testbench 读取向量、写出日志和比对结果。两者都是验证环境的“外部接口”,出错时往往不会报错,只会静默使用默认值或空数据。

本文先讲 plusargs 和文件任务的语法,再给回归配置、向量加载和日志输出的例子,最后集中列出参数、EOF、文件描述符和缓冲方面的异常。

1. 语法与用法

1.1 $test$plusargs:只判断存在

1
2
if ($test$plusargs("DEBUG"))
$display("debug mode enabled");

命令行:

1
simv +DEBUG

$test$plusargs 返回 1 表示找到匹配的 plusarg,返回 0 表示没有。它按前缀匹配,因此 $test$plusargs("TEST") 对 +TEST=smoke 也会返回 1。它只适合判断开关,不负责取值。

1.2 $value$plusargs:读取值

1
2
3
4
5
6
7
8
int    seed;
string test_name;

if ($value$plusargs("SEED=%d", seed))
$display("seed = %0d", seed);

if ($value$plusargs("TEST=%s", test_name))
$display("test = %s", test_name);

命令行:

1
simv +TEST=smoke +SEED=12345

格式串决定解析类型:

格式 含义
%d 十进制整数
%h 十六进制整数
%b 二进制整数
%s 字符串
%f 实数

$value$plusargs 返回 1 表示找到并成功解析,返回 0 表示没找到或格式不匹配。返回 0 时目标变量保持原值,不会自动清零。

1.3 必需参数与可选参数

1
2
3
4
5
6
7
8
string test_name;
int seed = 1;

if (!$value$plusargs("TEST=%s", test_name))
$fatal(1, "+TEST=<name> is required");

if (!$value$plusargs("SEED=%d", seed))
$display("SEED not given, use default %0d", seed);

必需参数用 $fatal 直接终止,可选参数给默认值。回归脚本最容易犯的错误,是必需参数拼错后静默使用默认值,跑完才发现测的不是目标用例。

1.4 $fopen 与 $fclose

1
2
3
4
5
6
7
8
9
10
11
int fd;

initial begin
fd = $fopen("vectors.txt", "r");
if (fd == 0)
$fatal(1, "cannot open vectors.txt");

// ...

$fclose(fd);
end

常用模式:

模式 含义
"r" 只读,文件必须存在
"w" 只写,截断或创建
"a" 追加
"r+" 读写,文件必须存在
"w+" 读写,截断或创建
"a+" 读写,追加

$fopen 失败返回 0。文件路径、权限、目录是否存在都要检查。

1.5 写文件

1
2
3
4
5
6
int log_fd = $fopen("result.log", "w");

$fdisplay(log_fd, "time=%0t addr=%0h data=%0h", $time, addr, data);
$fwrite(log_fd, "no newline");
$fflush(log_fd);
$fclose(log_fd);
  • $fdisplay 自动换行。
  • $fwrite 不换行。
  • $fflush 立即刷新缓冲区。
  • $fclose 关闭文件并刷新。

1.6 读文件

1
2
3
4
5
6
7
8
int fd = $fopen("vectors.txt", "r");
int addr, data;

while ($fscanf(fd, "%h %h", addr, data) == 2) begin
drive(addr, data);
end

$fclose(fd);

$fscanf 返回成功匹配的字段数。上例只有在两个字段都读到时才继续,读到文件尾或格式不符时循环结束。

也可以按行读:

1
2
3
4
5
6
7
string line;
int addr, data;

while ($fgets(line, fd)) begin
if ($sscanf(line, "%h %h", addr, data) == 2)
drive(addr, data);
end

$fgets 返回读到的字符数,读到 EOF 返回 0。

1.7 MCD:一次写多个文件

1
2
3
4
5
6
7
int mcd;

mcd = $fopen("a.log"); // 不带模式,返回单通道描述符
mcd = mcd | $fopen("b.log"); // 按位或,组合成多通道

$fdisplay(mcd, "written to both files");
$fclose(mcd);

不带模式的 $fopen 返回一个只有单一位被置起的描述符。要把多个文件组合成一个 MCD,必须对返回值按位或;连续直接赋值只会保留最后一个文件。$fdisplay 会写入掩码中的每个文件。MCD 适合同时输出到屏幕和日志,但调试时不如单文件描述符直观。

1.8 $readmemh 与 $readmemb

1
2
3
4
5
logic [7:0] mem[0:255];

initial begin
$readmemh("mem.hex", mem);
end

$readmemh 按十六进制加载,$readmemb 按二进制加载。文件中的数据按地址顺序填入数组,适合初始化 ROM/RAM 模型。

2. 示例

2.1 回归配置:测试名与种子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module tb_top;

string test_name;
int seed = 1;
bit debug;

initial begin
if (!$value$plusargs("TEST=%s", test_name))
$fatal(1, "usage: simv +TEST=<name> [+SEED=<n>] [+DEBUG]");

void'($value$plusargs("SEED=%d", seed));
debug = $test$plusargs("DEBUG");

$display("test=%s seed=%0d debug=%0d", test_name, seed, debug);
$urandom(seed);

run_test(test_name);
end

endmodule

void'() 表示可选参数,忽略返回值。必需参数仍然用 if 检查。

2.2 从文件加载激励

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module vector_driver;

int fd;
int addr, data;
int line_no = 0;

initial begin
fd = $fopen("vectors.txt", "r");
if (fd == 0)
$fatal(1, "cannot open vectors.txt");

while ($fscanf(fd, "%h %h", addr, data) == 2) begin
line_no++;
drive(addr, data);
end

$display("loaded %0d vectors", line_no);
$fclose(fd);
end

endmodule

每行两个十六进制数。$fscanf 返回值检查保证格式错误或文件结束时不会继续使用旧值。

2.3 写事务日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class logger;
int fd;

function new(string path);
fd = $fopen(path, "w");
if (fd == 0)
$fatal(1, "cannot open %s", path);
endfunction

function void write(transaction tr);
$fdisplay(fd, "%0t,%0h,%0d,%0d",
$time, tr.addr, tr.len, tr.is_write);
endfunction

function void close();
$fclose(fd);
endfunction
endclass

日志格式用逗号分隔,后续可以直接用脚本或表格工具分析。每行带时间戳,便于和波形对齐。

2.4 同时写屏幕和文件

1
2
3
4
5
6
7
8
9
10
int mcd;

initial begin
mcd = $fopen("run.log");
mcd = mcd | $fopen("errors.log");

$fdisplay(mcd, "test started at %0t", $time);
$fdisplay(mcd, "test finished at %0t", $time);
$fclose(mcd);
end

MCD 把同一条消息写到两个文件。若只想写一个文件,用带模式的 $fopen 和普通 fd。

3. 异常情况

3.1 plusarg 拼写或大小写不一致

命令行 +test=smoke 与代码里的 "TEST=%s" 不匹配,$value$plusargs 返回 0,变量保持默认值。plusarg 名称通常区分大小写,回归脚本和 TB 要统一约定。

3.2 把 $test$plusargs 当取值函数

1
2
if ($test$plusargs("SEED=%d"))   // 错误:它只判断存在
...

$test$plusargs 不解析值。取值必须用 $value$plusargs。

3.3 必需参数不检查返回值

1
2
$value$plusargs("TEST=%s", test_name);
run_test(test_name); // 参数缺失时 test_name 可能是空串

必需参数用 if (!...) $fatal,不要让默认值掩盖配置错误。

3.4 格式串与命令行不匹配

1
$value$plusargs("SEED=%d", seed);   // 命令行给 +SEED=0x10

%d 只按十进制解析,0x10 可能只匹配到 0 或直接失败。十六进制用 %h,字符串用 %s,类型要一致。

3.5 $fopen 返回 0 未检查

路径错误、目录不存在、权限不足都会返回 0。后续 $fdisplay(0, ...) 可能写到标准输出或直接无效,日志看起来“少了内容”。打开后立即检查。

3.6 while (!$feof(fd)) 的重复读

1
2
3
4
while (!$feof(fd)) begin
$fscanf(fd, "%h %h", addr, data);
drive(addr, data); // EOF 时可能重复驱动上一行
end

$feof 只有在尝试读过文件尾之后才为真。最后一次 $fscanf 失败时,addr/data 仍是上一行的值,循环体可能多执行一次。用 $fscanf 返回值控制循环,或读完立即检查。

3.7 $fscanf 返回值不检查

格式不符、字段缺失、文件结束时,$fscanf 返回小于期望的字段数。继续使用未更新的变量会把旧数据当成新数据。

3.8 $fwrite 与 $fdisplay 混用

$fwrite 不换行,$fdisplay 换行。日志里所有记录粘成一行,通常是混用了两者。需要行式日志时统一用 $fdisplay。

3.9 忘记 $fclose

缓冲区未刷新时,仿真异常结束可能丢最后几行。长回归应在 final 或收尾函数中关闭所有文件。

3.10 MCD 与普通 fd 混用

1
2
int fd = $fopen("a.log");       // MCD
int fd2 = $fopen("b.log", "w"); // 普通 fd

两者都返回整数,但语义不同。把普通 fd 当 MCD 组合,或把 MCD 传给只接受单通道的函数,结果可能不符合预期。MCD 只用于 $fdisplay/$fwrite 等多通道输出。

3.11 文件描述符耗尽

每个 $fopen 都占用描述符。循环里反复打开不关闭,最终会返回 0。打开的文件应在使用完后及时关闭。

3.12 相对路径依赖启动目录

$fopen("vectors.txt", "r") 相对于仿真启动目录,而不是源文件目录。回归脚本切换工作目录后可能找不到文件。使用绝对路径、+INC 约定或统一在运行目录下执行。

3.13 $readmemh 数据不足或格式错误

$readmemh 遇到非法字符、数据行不足或地址越界时通常只给警告,不会终止仿真。加载后检查数组内容或使用返回状态(工具相关),关键向量不要静默缺失。

3.14 文件 I/O 不可综合

$fopen、$fscanf、$fdisplay、$readmemh 都只用于仿真。不要把它们放进 RTL 的可综合分支;需要初始化 RAM 时使用综合工具支持的 $readmemh 子集或独立的初始化文件。

4. 小结

plusargs 和文件 I/O 都可以按“打开检查、读取检查、关闭刷新”处理:

  1. plusargs:$test$plusargs 判存在,$value$plusargs 取值;必需参数必须检查返回值。
  2. 文件打开:$fopen 返回 0 立即报错,模式要与读写意图一致。
  3. 文件读取:用 $fscanf/$fgets 的返回值控制循环,不要只依赖 $feof。
  4. 文件写入:区分 $fwrite 与 $fdisplay,结束时 $fflush/$fclose。