1.Writing Testbenches
1.1 Clock [Tb/clock]
问题描述
你将获得一个带有以下声明的模块:
module dut (input clk);
编写一个testbench,创建一个模块dut的实例(可以使用任何实例名称),并创建一个时钟信号来驱动模块的时钟输入。时钟的周期为10ps,时钟应初始化为0,其第一次转换为0变1.
代码
`timescale 1 ps/1 ps
module top_module ( );
reg clk;
parameter CYCLE = 10;
dut test(
.clk (clk )
);
initial begin
clk = 0;
forever #(CYCLE/2)
clk = ~clk;
end
endmodule
1.2 Testbench1 [Tb/tb1]
问题描述
创建一个Verilog测试平台,为输出A和输出B产生以下波形:
代码
`timescale 1 ps/1 ps
module top_module ( output reg A, output reg B );//
// generate input patterns here
initial begin
A = 0;
#10;
A = 1;
#10;
A = 0;
end
initial begin
B = 0;
#15;
B = 1;
#25;
B = 0;
end
endmodule
1.3 AND gate [Tb/and]
问题描述
你将获得如下一个需要测试的AND门:
module andgate(
input [1:0] in,
output out
);
编写一个testbench,通过生成如下时序图从而实例化这个与门并测试所有的4中输入情况:
代码
`timescale 1 ps/1 ps
module top_module();
reg [1:0] in;
wire out;
andgate testAnd(
.in(in),
.out(out)
);
initial begin
in = 2'b00;
#10;
in = 2'b01;
#10;
in = 2'b10;
#10;
in = 2'b11;
end
endmodule
一开始想的是写成一个时钟循环,如下:
initial begin
in[0] = 0;
forever #(10) in[0] = ~in[0];
end
initial begin
in[1] = 0;
forever #(20) in[1] = ~in[1];
end
不过不符合题目要求。
1.4 Testbench2 [Tb/tb2]
问题描述
下面的波形设置了clk,in和s:
q7模块具有以下声明:
module q7(
input clk,
input in,
input [2:0] s,
output out
);
编写一个testbench,实例化模块q7并生成上述输入信号。
代码
`timescale 1 ps/1 ps
module top_module();
reg clk;
reg in;
reg [2:0] s;
reg out;
q7 testQ7(
.clk (clk ),
.in (in ),
.s (s ),
.out (out )
);
initial begin
clk = 0;
forever #5 clk = ~clk;
end
initial begin
in = 0;
#20;
in = 1;
#10;
in = 0;
#10;
in = 1;
#30;
in = 0;
end
initial begin
s = 3'h2;
#10;
s = 3'h6;
#10;
s = 3'h2;
#10;
s = 3'h7;
#10;
s = 3'h0;
end
endmodule
1.5 T filp-flop [Tb/tff]
问题描述
你将获得一个T触发器模块,其声明如下:
module tff(
input clk,
input reset,
input t,
output q
);
编写一个测试平台,实例化一个T触发器,并将重置T触发器,然后将其切换到1状态。
分析
我的理解是要求testbench一开始应对T触发器进行重置,然后在上升沿时输出1。
代码
`timescale 1 ps/1 ps
module top_module ();
reg clk;
reg reset;
reg t;
reg q;
tff testTff(
.clk (clk ),
.reset (reset ),
.t (t ),
.q (q )
);
initial begin
clk = 0;
reset = 1;
t = 0;
#2;
reset = 0;
t = 1;
end
always #1 clk = ~clk;
endmodule
这个系列主要是记录一下自己的学习过程和简单的思考过程,参考了许多他人的思路。题目均为HDLBits上的题目,借助翻译器与自己的理解组织了题目描述,如果有问题欢迎批评指正。