Lec4 Slides Testbenches
Lec4 Slides Testbenches
Lecture 4:
Testbenches
Introduction
Testbenches
Verilog description of environment
Apply test inputs
Look at outputs
Testbenches
nand g1(c,b,a);
endmodule
A simulation example from the lab
in1
a G1
in2 c out1
b
in1
a G1
in2 c out1
b
endmodule
A test bench for the NAND gate
Test bench
in1
a G1
in2 c out1
b
reg in1,in2;
wire out1;
A test bench for the NAND gate
Test bench
in1
a G1
in2 c out1
b
nandgate n1(.a(in1),.b(in2),.c(out1));
A test bench for the NAND gate
Test bench
in1
a G1
in2 c out1
b
initial
begin
in1= 0; in2=0;
#10 in1=1;
#30 in1=0; in2=1;
end
A test bench for the NAND gate
module nand_test;
reg in1,in2;
wire out1;
nandgate n1(.a(in1),.b(in2),.c(out1));
initial
begin
in1= 0; in2=0;
#10ns in1=1;
#10ns in2=1;
#30ns in1=0; in2=1;
end
endmodule
Test bench
in1
a G1
in2 c out1
b
A test bench for the fulladder
module fa_tb();
reg cin,x,y ;
wire s,cout;
fulladd fa0(cin,x,y,s,cout);
initial
begin
{cin,x,y} = 3'b000;
repeat(7)
#10ns {cin,x,y} = {cin,x,y} + 3'b001;
end
endmodule
A Test Bench for our 4-bit Adder
module adder4_tb; Open libraries
cin
x sum
+
y
cout
A Test Bench for our 4-bit Adder
Declare signals used in
our test bench
reg carry_in;
reg [3:0] in1,in2;
wire carry_out;
wire [3:0] sum1;
carry_out
A Test Bench for our 4-bit Adder
Instantiate the adder
from the library
adder4 u1
carry_out
A Test Bench for our 4-bit Adder
Wire up the inputs & outputs
adder4
u1(.carryin(carry_in), .X(in1), .Y(in2), .S(sum1)
, .carryout(carry_out));
carry_out
A Test Bench for our 4-bit Adder
initial Give some values to inputs
begin and outputs
in1= 0; in2=0; carry_in=0;
#10ns in1=3;
#10ns in2=4;
#20ns in1=5;
#40ns in2=1;
end
Testbench Work library
carry_in
cin
x sum
cin +
in1 x sum y
in2 + sum1 cout
y
cout
carry_out
Verilog System Tasks
The response to the stimulus generated by the initial and
always blocks will appear at the output of the simulator as
timing diagrams.
It is also possible to display numerical outputs using Verilog
system tasks.
$display – display one-time value of variables or strings
with end-of-line return,
$write – same $display but without going to next line.
$monitor – display variables whenever a value changes
during simulation run.
$time – displays simulation time
$finish – terminates the simulation
{TS,TA,TB} = 3'b000;
repeat(7)
#10 {TS,TA,TB} = {TS,TA,TB} + 3'b001;
end
endmodule
MUX2x1 Testbench
initial begin
$display (" Select A B Out");
$monitor("Time %0d select=%b A=%b B=%b OUT=%b",
$time,TS,TA,TB,Y);
TS= 0; TA = 0; TB = 0;
end