Verification:Writing Testbenches
前言
今天更新HDLBits最后一章的习题:编写Testbench。
题库
Clock
提供了如下描述的模块:
module dut ( input clk ) ;
要求传入频率为10ps的时钟,初始为0,如下图所示。
Solution:
module top_module ();
reg clk;
initial begin
clk = 1'b0;
end
always #5 clk = ~clk;
dut u0(clk);
endmodule
Testbench 1
产生如下图所示的A,B激励。
Solution:
module top_module ( output reg A, output reg B );//
// generate input patterns here
initial begin
A = 1'b0;
B = 1'b0;
#10 A = 1'b1;
#5 B = 1'b1;
#5 A = 1'b0;
#20 B = 1'b0;
end
endmodule
AND gate
写测试激励测试and模块,波形图如下图所示。
提供的AND模块声明如下:
module andgate (
input [1:0] in,
output out
);
Solution:
module top_module();
reg in_0,in_1;
reg out;
initial begin
in_0 = 1'b0;
in_1 = 1'b0;
#10 in_0 = 1'b1;
#10
in_0 = 1'b0;
in_1 = 1'b1;
#10 in_0 = 1'b1;
end
andgate u0(.in({in_1,in_0}),.out(out));
endmodule
Testbench 2
产生如下图波形图所示的激励信号,激励模块q7;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gJoCkqof-1613628105042)(http://ww1.sinaimg.cn/large/006AXXmQgy1gnrn2vzxtrj311q06f0st.jpg)]
模块q7的描述如下:
module q7 (
input clk,
input in,
input [2:0] s,
output out
);
Solution:
module top_module();
reg clk,in,out;
reg [2:0] s;
initial begin
clk = 1'b0;
in = 1'b0;
s = 3'd2;
#10 s = 3'd6;
#10
s = 3'd2;
in = 1'b1;
#10
s = 3'd7;
in = 1'b0;
#10
s = 3'd0;
in = 1'b1;
#30 in = 1'b0;
end
always #5 clk = ~clk;
q7 u0(.clk(clk),
.in(in),
.s(s),
.out(out));
endmodule
T flip-flop
该题作者给出了一个T触发器的module,我们仅需将其复位,然后切回到状态1就可以了。
Solution:
module top_module ();
reg clk,reset,t;
wire q;
tff u0(
.clk (clk ),
.reset (reset ),
.t (t ),
.q (q )
);
initial begin
clk = 1'b0;
reset = 1'b0;
#3;
reset = 1'b1;
#10;
reset = 1'b0;
end
always #5 clk = ~clk;
always@(posedge clk)begin
if(reset)begin
t <= 1'b0;
end
else begin
t <= 1'b1;
end
end
endmodule
结语
HDLBits系列总算是更新结束了,非常感谢该网站的作者!自己立的flag总算完成了,在寒假收假前刷完了。如果代码有错误的地方欢迎大家指正。