FPGA 与数字 IC 设计 / 系统课时
对多组参数配置执行回归测试
对多组参数配置执行回归测试
参数化 RTL 模块在默认配置下通过仿真,不代表其他配置也正确。本课以同步 FIFO 为例,建立可重复执行的参数回归测试,覆盖不同数据宽度、存储深度、非法参数和指针回绕,并通过自动检查定位失败配置。
学习目标
- 设计覆盖数据宽度、FIFO 深度和边界条件的参数测试矩阵。
- 使用 Icarus Verilog 批量编译、运行和检查不同参数配置。
- 验证非法参数拒绝机制、FIFO 空满行为和指针回绕,识别回归测试中的假通过。
先确定参数矩阵与验收标准
假设待测模块为同步 FIFO,具有两个编译期参数:DATA_W 表示数据宽度,DEPTH 表示存储深度。接口包含 clk、rst_n、wr_en、rd_en、din、dout、full 和 empty;数据写入在时钟上升沿发生,dout 为组合读出的队首数据。
测试环境使用 Icarus Verilog 11.0 或兼容 SystemVerilog 2012 语法的版本。这里采用固定深度数组与指针回绕逻辑,不要求 DEPTH 是 2 的幂。
| 配置 | DATA_W | DEPTH | 检查重点 |
|---|---|---|---|
| A | 1 | 1 | 最小宽度与单槽 FIFO |
| B | 8 | 3 | 非 2 的幂深度与指针回绕 |
| C | 16 | 4 | 常规深度与空满边界 |
| D | 32 | 5 | 较宽数据与非 2 的幂深度 |
| E | 8 | 0 | 非法深度必须失败 |
| F | 0 | 4 | 非法宽度必须失败 |
合法配置统一检查以下条件:复位后 empty=1、full=0;连续写入 DEPTH 次后 full=1;连续读取 DEPTH 次后 empty=1;数据按先进先出顺序输出;多次读写后指针能够正确回绕。
准备可参数化的 FIFO 和自检测试平台
先建立工作目录,将下面两个文件放在同一目录中。示例 RTL 使用占用计数判断空满,满时禁止写入、空时禁止读取;测试平台不会同时拉高读写使能,以便集中检查容量与回绕问题。
fifo.sv 是可以独立综合的参数化模块,采用显式回绕避免非 2 的幂深度出现越界地址。
module fifo #(
parameter int DATA_W = 8,
parameter int DEPTH = 4
)(
input logic clk,
input logic rst_n,
input logic wr_en,
input logic rd_en,
input logic [DATA_W-1:0] din,
output logic [DATA_W-1:0] dout,
output logic full,
output logic empty
);
localparam int PTR_W = (DEPTH > 1) ? $clog2(DEPTH) : 1;
localparam int CNT_W = $clog2(DEPTH + 1);
logic [DATA_W-1:0] mem [0:DEPTH-1];
logic [PTR_W-1:0] wptr, rptr;
logic [CNT_W-1:0] count;
assign full = (count == DEPTH);
assign empty = (count == 0);
assign dout = mem[rptr];
// DEPTH 不必是 2 的幂,必须显式回到地址 0。
function automatic [PTR_W-1:0] next_ptr(
input logic [PTR_W-1:0] ptr
);
next_ptr = (ptr == DEPTH-1) ? '0 : ptr + 1'b1;
endfunction
initial begin
if (DATA_W < 1 || DEPTH < 1)
$fatal(1, "illegal FIFO parameters");
end
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
wptr <= '0;
rptr <= '0;
count <= '0;
end else begin
// 本例仅处理单方向操作,不支持同拍读写。
if (wr_en && !rd_en && !full) begin
mem[wptr] <= din;
wptr <= next_ptr(wptr);
count <= count + 1'b1;
end else if (rd_en && !wr_en && !empty) begin
rptr <= next_ptr(rptr);
count <= count - 1'b1;
end
end
end
endmodule这里的参数检查通过仿真时的 $fatal 实现;综合工具是否接受非法参数属于另一项静态检查,不应把仿真拒绝机制误认为所有综合器都会自动拒绝非法配置。
接着创建 tb_fifo.sv。测试平台使用计分板保存期望读取顺序,连续执行三轮写满、读空,保证读写指针跨过深度边界。非法参数配置不启动正常测试。
module tb_fifo;
parameter int DATA_W = 8;
parameter int DEPTH = 4;
logic clk = 0;
logic rst_n = 0;
logic wr_en = 0, rd_en = 0;
logic [DATA_W-1:0] din = '0;
wire [DATA_W-1:0] dout;
wire full, empty;
logic [DATA_W-1:0] expected [0:DEPTH-1];
fifo #(.DATA_W(DATA_W), .DEPTH(DEPTH)) dut (.*);
always #5 clk = ~clk;
initial begin
if (DATA_W < 1 || DEPTH < 1) begin
// 非法参数应由 DUT 的参数检查终止仿真。
#20;
$fatal(1, "DUT did not reject invalid parameters");
end else begin
repeat (2) @(negedge clk);
rst_n = 1;
#1;
if (!empty || full)
$fatal(1, "reset flags incorrect");
for (int round = 0; round < 3; round++) begin
for (int i = 0; i < DEPTH; i++) begin
@(negedge clk);
wr_en = 1;
din = DATA_W'(round * DEPTH + i + 1);
expected[i] = din;
@(posedge clk);
#1;
if (empty)
$fatal(1, "unexpected empty after write");
end
if (!full)
$fatal(1, "full flag missing");
@(negedge clk);
wr_en = 0;
for (int i = 0; i < DEPTH; i++) begin
#1;
if (dout !== expected[i])
$fatal(1, "data mismatch round=%0d index=%0d", round, i);
rd_en = 1;
@(posedge clk);
#1;
end
if (!empty || full)
$fatal(1, "empty flag incorrect");
@(negedge clk);
rd_en = 0;
end
$display("PASS DATA_W=%0d DEPTH=%0d", DATA_W, DEPTH);
$finish;
end
end
endmodule预期结果:合法配置运行三轮后输出一条 PASS;非法配置由 DUT 的 $fatal(1, ...) 结束仿真,进程返回非零状态。DATA_W=1 时写入数据会发生预期的位宽截断,计分板也使用截断后的值比较。
使用脚本批量执行并判定结果
手动重复修改参数容易漏测。Icarus Verilog 的 -P 选项可以在编译时覆盖顶层模块参数,因此不必为每组配置复制一套 RTL。以下 Python 3.10+ 脚本调用 iverilog 和 vvp,分别运行合法与非法配置。
在当前目录保存为 regress.py:
import subprocess
import tempfile
from pathlib import Path
CASES = [
(1, 1, True), (8, 3, True),
(16, 4, True), (32, 5, True),
(8, 0, False), (0, 4, False),
]
with tempfile.TemporaryDirectory() as directory:
for width, depth, valid in CASES:
executable = str(Path(directory) / "sim.vvp")
compile_cmd = [
"iverilog", "-g2012", "-s", "tb_fifo",
"-Ptb_fifo.DATA_W=" + str(width),
"-Ptb_fifo.DEPTH=" + str(depth),
"-o", executable, "fifo.sv", "tb_fifo.sv",
]
build = subprocess.run(compile_cmd, capture_output=True, text=True)
if build.returncode != 0:
if valid:
print(f"FAIL width={width} depth={depth}: compile error")
print(build.stderr)
raise SystemExit(1)
print(f"REJECT width={width} depth={depth}: compile error")
continue
result = subprocess.run(["vvp", executable], capture_output=True, text=True)
marker = f"PASS DATA_W={width} DEPTH={depth}"
passed = result.returncode == 0 and marker in result.stdout
# 非法参数仅接受明确的 DUT 拒绝信息。
rejected = result.returncode != 0 and "illegal FIFO parameters" in (
result.stdout + result.stderr
)
ok = passed if valid else rejected
label = "PASS" if valid else "REJECT"
print(f"{label if ok else 'FAIL'} width={width} depth={depth}")
if not ok:
print(result.stdout, result.stderr)
raise SystemExit(1)
print("REGRESSION PASS: 6 cases")在安装好 iverilog、vvp 和 Python 后执行:
python3 regress.py预期输出(Icarus Verilog 11.0、Python 3.10+ 且工具位于 PATH 中):
PASS width=1 depth=1
PASS width=8 depth=3
PASS width=16 depth=4
PASS width=32 depth=5
REJECT width=8 depth=0
REJECT width=0 depth=4
REGRESSION PASS: 6 cases脚本除了检查退出码,还要求合法配置打印指定 PASS 标记,避免测试平台提前结束却被误判成功。非法配置则要求明确的参数拒绝信息;意外崩溃不应算作预期拒绝。
专门排查非 2 的幂深度与回绕缺陷
当 DEPTH=3 时,$clog2(3) 得到 2,二进制指针天然可以表示 0、1、2、3 四个地址,但合法存储地址只有 0、1、2。若开发者直接使用 ptr + 1'b1,指针会进入地址 3,造成越界访问。

验证这种缺陷,可以临时将 next_ptr 的函数体替换为普通加法,重新执行回归。预期 DEPTH=3 或 DEPTH=5 的配置在后续访问时出现数据不匹配或仿真异常,原实现则全部通过。
不过,越界访问结果受仿真器语义和测试激励影响,不能把某一种报错文本当作固定结果。正确做法是检查计分板是否检测到失配,并在更完整的测试中加入指针合法性断言。
还应特别覆盖 DEPTH=1:此时指针必须始终停留在地址 0,单槽写满和读空都应正确更新 full、empty。不能直接把 $clog2(1) 的结果作为未经保护的寄存器位宽。
将参数回归加入持续集成
在项目中,建议将每次提交的最小回归设置为 6 组参数;合并前再扩展到 DATA_W 为 1、8、16、32,DEPTH 为 1、2、3、4、5、8 的 24 种合法组合。每组应独立编译、独立运行,失败立即显示对应参数与仿真日志。
CI 可直接运行以下命令,依靠退出码判断是否允许合并:
python3 regress.py预期结果:全部配置通过时退出码为 0,显示 REGRESSION PASS: 6 cases;任一合法配置失败或非法配置未被正确拒绝时,脚本退出码为 1,CI 应将本次任务标记为失败。
最后需要明确示例的验证边界:当前测试覆盖复位后的顺序读写、空满判断和多轮回绕,但尚未覆盖同时读写、满时写入、空时读取、异步复位中断及随机操作序列。真实产品如果支持这些行为,应继续扩充激励、参考模型和覆盖点,而不能仅凭这 6 组配置通过就宣称 FIFO 已完成全部功能验证。