Testbench 自动生成流程
Verilua 中,Testbench 是全自动生成的 testbench_gen
工具实现(安装 Verilua 的时候会编译生成 testbench_gen
),其核心功能包括顶层模块的例化、时钟驱动、波形控制等。具体工作流程如下图所示:

testbench_gen
底层使用的是 slang 进行 RTL 代码的解析,下面是 testbench_gen
的命令行参数(高亮的为常用的选项):
Testbench 只在 HVL 场景下才需要自动生成,可以在 xmake.lua 文件中通过 add_values("cfg.tb_gen_flags", ...)
来添加其他的 flags 参数,例如:
默认情况下,testbench_gen
会生成两个文件,一个是 tb_top.sv
,另一个是 other.sv
,其中 tb_top.sv
是 Testbench 文件,other.sv
是留给用户自定义的文件。用户可以在 others.sv
中自行添加其他的内容,每次重新生成 Testbench 的时候如果 others.sv
存在,就不会重新生成或者覆盖,因此用户可以安全地在其中添加用户自定义的内容。 部分内容如下(假设 DUT 为 Design
):
tb_top.sv
// -----------------------------------------
// user custom code
// use `--custom-code-outer/-cco <file>` to pass in the custom code file.
// |_ e.g. `testbench_gen [...] --custom-code-outer path/to/file`
// use `--custom-code-str-outer/-ccso <string>` to pass in the custom code string.
// |_ e.g. `testbench_gen [...] --custom-code-str-outer "`define a 1"`
// -----------------------------------------
module tb_top;
// ...
reg clock;
reg reset;
initial begin
clock = 0;
reset = 1;
end
always #10 clock = ~clock;
// ...
// -----------------------------------------
// reg/wire declaration
// -----------------------------------------
reg inc ; // Input
reg[7:0] test ; // Input
wire[7:0] value ; // Output
// ...
// -----------------------------------------
// DUT module instantiate
// -----------------------------------------
Design u_Design (
.clock (clock ), // direction: In dataType: logic
.reset (reset ), // direction: In dataType: logic
.inc (inc ), // direction: In dataType: logic
.test (test ), // direction: In dataType: reg[7:0]
.value (value ) // direction: Out dataType: logic[7:0
); // u_Design
// ...
// -----------------------------------------
// other user code...
// -----------------------------------------
Others u_others(
.clock(clock),
.reset(reset)
);
// -----------------------------------------
// user custom code
// use `--custom-code/-cc <file>` to pass in the custom code file.
// |_ e.g. `testbench_gen [...] --custom-code path/to/file`
// use `--custom-code-str/-ccs <string>` to pass in the custom code string.
// |_ e.g. `testbench_gen [...] --custom-code-str "reg a; initial a = 1;"`
// -----------------------------------------
endmodule