Chapter 3: Hierarchy & Simulation: Digital Design With The Verilog HDL
Chapter 3: Hierarchy & Simulation: Digital Design With The Verilog HDL
Binh Tran-Thanh
January 5, 2022
1 / 34
Module Port List
endmodule
endmodule
2 / 34
Module Port List
endmodule
endmodule
3 / 34
Structural Design Tip
4 / 34
Example: Hierarchy Multiplexer
mux_8_to_1(output out,
input in0, in1, in2, in3, in4, in5, in6, in7,
input[2:0] select);
5 / 34
Example: Hierarchy Multiplexer
endmodule
6 / 34
Interface: Hierarchical Multiplexer
endmodule
7 / 34
Timing Controls For Simulation
8 / 34
Zero Delay vs. Unit Delay
9 / 34
Zero/Unit Delay Example
10 / 34
Types Of Delays
11 / 34
Delay Examples
12 / 34
Delays In Testbenches
13 / 34
Simulation
14 / 34
Simulation of Verilog
Stimulus
Inputs Outputs Inputs Outputs
UUT UUT
(Response) (Response)
Testbench Testbench
15 / 34
Simulation Example
endmodule
4
a[3:0] 4
4 sum[3:0]
b[3:0] adder4b
Cout
Cin
16 / 34
Simulation Example
t adder4b
4
a[3:0] 4
4 sum[3:0]
b[3:0] adder4b
Cout
Cin
17 / 34
Example
not an apostrophe!
// stimulus generation
initial begin
stim = 9'b000000000; // at 0 ns
#10 stim = 9'b111100001; // at 10 ns see “response” to
Behav. #10 stim = 9'b000011111; // at 20 ns each of these input
Verilog: #10 stim = 9'b111100010; // at 30 ns vectors
“do this #10 stim = 9'b000111110; // at 40 ns
once” #10 $stop; // at 50 ns – stops simulation
end timing control for
endmodule simulation
18 / 34
Testbench Requirements
19 / 34
Output Test Info
20 / 34
Output Format Strings
Formatting string
%h, %H hex
%d, %D decimal
%o, %O octal
%b, %B binary
%t time
21 / 34
Output Example
`timescale 1ns /1ns // time_unit/time_precision
module t_adder4b;
reg[8:0] stim; // inputs to UUT are regs
wire[3:0] S; // outputs of UUT are wires
wire C4;
All values will run together,
// instantiate UUT easier to read with formatting
adder4b(S, C4, stim[8:5], stim[4:1], stim[0]); string
// monitor statement
initial $monitor(“%t: %b %h %h %h %b\n”, $time, C4, S, stim[8:5],
stim[4:1], stim[0]);
// stimulus generation
initial begin
stim = 9'b000000000; // at 0 ns
#10 stim = 9'b111100001; // at 10 ns
#10 stim = 9'b000011111; // at 20 ns
#10 stim = 9'b111100010; // at 30 ns
#10 stim = 9'b000111110; // at 40 ns
#10 $stop; // at 50 ns – stops simulation
end
endmodule
22 / 34
Exhaustive Testing
reg[4:0] x;
initial begin
for(x = 0; x < 16; x = x + 1)
#5// need a delay here!
end
23 / 34
Why Loop Vector Has Extra Bit
24 / 34
Example: UUT
// Code to compare A to B
25 / 34
Example: Testbench
module t_Comp_4_str();
wire A_gt_B, A_lt_B, A_eq_B;
reg [4:0] A, B; // sized to prevent loop wrap around
wire [3:0] A_bus, B_bus;
assign A_bus = A[3:0]; // display 4 bit values
assign B_bus = B[3:0];
initial begin
#5 for (A = 0; A < 16; A = A + 1) begin // exhaustive test of valid inputs
for (B = 0; B < 16; B = B + 1) begin #5; // may want to test x’s and z’s
end // first for
end // second for note multiple initial
end // initial blocks
endmodule
26 / 34
Combinational Testbench
module comb(output d, e, input a, b, c);
and(d, a, b);
nor(e, a, b, c);
endmodule
module t_comb();
wire d, e;
reg [3:0] abc;
comb CMD(d, e, abc[2], abc[1], abc[0]); // UUT
initial $monitor("%t a: %b b: %b c: %b d: %b e: %b",
$time, abc[2], abc[1], abc[0], d, e);
initial #2000 $finish;// end simulation, quit program
// exhaustive test of valid inputs
initial begin
for(abc= 0; abc< 8; abc= abc+ 1) begin #5; end// for
end// initial
endmodule
27 / 34
Generating Clocks
Wrong way:
initial begin
#5 clk= 0;
#5 clk= 1;
#5 clk= 0;
... //(repeat hundreds of times)
end
Right way:
initial begin
initial clk= 0;
clk= 0;
always @ (clk)
forever #5 clk= ˜clk;
clk= #5 ˜clk;
end
LESS TYPING
Easier to read, harder to make mistake
28 / 34
FSM Testing
29 / 34
Example : Gray Code Counter –Test1
30 / 34
Solution: Gray Code Counter –Test1
module t1_gray_counter();
wire[2:0] out;
reg clk, rst;
gray_counter GC(out, clk, rst); // UUT
31 / 34
Solution: Gray Code Counter –Test1
32 / 34
Force/Release In Testbenches
33 / 34
Force/Release Example
C
assign y = a & b; B
assign z = y | c;
Z
initial begin A
Y
a = 0; b = 0; c = 0;
#5 a = 0; b = 1; c = 0; Time a b c y z
#5 force y = 1;
0 0 0 0 0 0
#5 b = 0;
5 0 1 0 0 0
#5 release y;
10 0 1 0 1 1
#5 $stop;
15 0 0 0 1 1
end
20 0 0 0 0 0
34 / 34