FPGA代码仿真(功能仿真或者时序仿真)是必不可少的环节,合理的测试代码架构可以帮助我们快速编写测试代码节约测试代码的编写时间,本节将自己实际使用的测试代码最小架构进行整理(见下图),方便以后改进及使用。
1.待测试代码
module TaskTest(
input i_clk ,
input i_rst ,
input [7:0] a ,
input [7:0] b ,
output [8:0] c
);
assign c = a + b;
endmodule
2.TestBench代码
module tb_TaskTest();
reg i_clk ;
reg i_rst ;
reg [7:0] a ;
reg [7:0] b ;
wire [8:0] c ;
/********************************************
Definition parameter
*********************************************/
localparam C_Period = 20 ;//the clock period
/********************************************
Generate i_clk
*********************************************/
always begin
#(C_Period/2)
i_clk = 0;
#(C_Period/2)
i_clk = 1;
end
/********************************************
Main function
*********************************************/
initial begin
/**** Initialization variable ****/
rst;
a = 0;
b = 0;
/**** Main ****/
task0(1,1);
/**** Verify the result ****/
@(posedge i_clk);
if(c == 2)begin
$display("The result is right!!!");
end
else begin
$display("The result is wrong!!!");
end
end
/********************************************
Tasks or Functions
*********************************************/
task rst;
begin
i_rst = 0;
#(C_Period)
i_rst = 1;
end
endtask
task task0;
input [7:0] idata0;
input [7:0] idata1;
begin
@(posedge i_clk);
a = idata0;
b = idata1;
end
endtask
TaskTest TaskTest_inst(
.i_clk (i_clk ),
.i_rst (i_rst ),
.a (a ),
.b (b ),
.c (c )
);
endmodule