目录
一、实例分析
1.1源文件
module counter (clk, reset, enable, count);
input clk, reset, enable;
output [3:0] count;
reg [3:0] count;
always @ (posedge clk)
if (reset == 1'b1) begin
count <= 0;
end else if ( enable == 1'b1) begin
count <= count + 1;
end
endmodule
1.2testbench文件
`timescale 1ns/1ps
module counter_tb;
reg clk, reset, enable;
wire [3:0] count;
counter U0 (
.clk (clk_tb),
.reset (reset_tb),
.enable (enable_tb),
.count (count_tb)
);
initial begin
clk_tb = 0;
reset_tb = 0;
enable_tb = 0;
end
always
#5 clk_tb = ! clk_tb;
initial
#100 $finish;
//Rest of testbench code after this line
endmodule
二、分析
对于testbench而言,端口应当和被测试的module一一对应。端口分为input,output和inout类型产生激励信号的时候,input对应的端口应当申明为reg, output对应的端口申明为wire,inout端口比较特殊,下面专门讲解。
一般用initial块给信号赋初值,initial块执行一次,always或者forever表示由事件激发反复执行。
大家应该注意到有个#符号,该符号的意思是指延迟相应的时间单位。该时间单位由timscale决定.一般在testbench的开头定义时间单位和仿真 精度,比如`timescale 1ns/1ps,前面一个是代表时间单位,后面一个代表仿真时间精度。以上面的例子而言,一个时钟周期是5个单位,也就是5ns。而仿真时间精度的概 念就是,你能看到1.001ns时对应的信号值,而假如timescale 1ns/1ns,1.001ns时候的值就无法看到。对于一个设计而言,时间刻度应该统一,如果设计文件和testbench里面的时间刻度不一致,仿真 器默认以testbench为准。一个较好的办法是写一个global.v文件,然后用include的办法,可以防止这个问题。