Chapter 3 Hierarchy Simulation
Chapter 3 Hierarchy Simulation
Binh Tran-Thanh
1 / 39
Module Port List Declaration (Multiple ways)
module Add_half(c_out, sum, a, b);
output sum, c_out;
input a, b;
...
endmodule
//*********************************************//
module Add_half(output c_out, sum, input a, b);
...
endmodule
//*********************************************//
module xor_8bit(out, a, b);
output[7:0] out;
input[7:0] a, b;
...
endmodule
//*********************************************//
module xor_8bit(output[7:0] out, input[7:0] a, b);
...
endmodule
2 / 39
Structural Design Tip
3 / 39
Example: Hierarchy Multiplexer
...
endmodule
//*********[mux2to1 as submudule]**************//
module mux_2to1( output out, input in0, in1, select);
wire n0, n1, n2;
...
endmodule
4 / 39
8to1 Mux from 2to1 Muxs Structure
source: https://vlsiuniverse.blogspot.com 5 / 39
Interface: Hierarchical Multiplexer
module mux_8to1(output out,
input in0, in1, in2, in3, in4, in5, in6, in7,
input[2:0] select);
wire n0, n1, n2, n3, n4, n5;
7 / 39
Zero Delay vs. Unit Delay
8 / 39
Zero/Unit Delay Example
9 / 39
Types Of Delays
10 / 39
Delay Examples
11 / 39
Delays In Testbenches
12 / 39
Simulation
13 / 39
Simulation of Verilog
Stimulus
Inputs Outputs Inputs Outputs
UUT UUT
(Response) (Response)
Testbench Testbench
14 / 39
Simulation [Functionality] Example
endmodule
4/
a[3:0] 4/
sum[3:0]
4/
b[3:0] adder4b
c out
c in
15 / 39
Simulation [Timing and Functionality] Example
‘timescale 1ns /1ns // time_unit/time_precision
module adder4b_delay (sum, c_out, a, b, c_in);
input [3:0] a, b;
input c_in;
output [3:0] sum;
output c_out;
endmodule
4/
a[3:0] 4/
sum[3:0]
4/
b[3:0] adder4b
c out
c in
16 / 39
Simulation Example
adder4b tb
4/
a[3:0] 4/
sum[3:0]
4/
b[3:0] adder4b
c out
c in
17 / 39
Testbench Example
‘timescale 1ns /1ns // time_unit/time_precision
module adder4b_tb;
reg [3:0] in_a, in_b; // inputs to UUT are regs
reg c_in; // inputs to UUT are regs
wire [3:0] sum, sum_d; // outputs of UUT are wires
wire c_out, c_cout_d; // outputs of UUT are wires
// instantiate UUT
adder4b A1 (sum, c_out, in_a, in_b, c_in);
adder4b_delay A2 (sum_d, c_cout_d, in_a, in_b, c_in);
// stimulus generation
initial begin
{in_a, in_b, c_in} = 9’b0000_0000_0; // at 0 ns
#10 {in_a, in_b, c_in} = 9’b1100_0100_1; // at 10 ns
#10 {in_a, in_b, c_in} = 9’b0011_0110_1; // at 20 ns
#10 {in_a, in_b, c_in} = 9’b1111_0001_0; // at 30 ns
#10 {in_a, in_b, c_in} = 9’b0001_1111_0; // at 40 ns
#10 $stop; // at 50 ns, stops simulation
end
endmodule 18 / 39
Testbench Waveform
19 / 39
Testbench Requirements
20 / 39
Output Test Info
21 / 39
Output Format Strings
Formatting string
%h, %H hex
%d, %D decimal
%o, %O octal
%b, %B binary
%t time
22 / 39
Output Example
‘timescale 1ns /1ns // time_unit/time_precision
module adder4b_tb;
reg [3:0] in_a, in_b; // inputs to UUT are regs
reg c_in; // inputs to UUT are regs
wire [3:0] sum; // outputs of UUT are wires
wire c_out; // outputs of UUT are wires
// instantiate UUT
adder4b UUT(sum, c_out, in_a, in_b, c_in);
// monitor statement
initial $monitor("time %t: cout=%b,sum=%h, in_a=%h, in_b
=%h, cin=%b\n", $time, c_out, sum, in_a, in_b, c_in);
// stimulus generation
initial begin
{in_a, in_b, c_in} = 9’b0000_0000_0; // at 0 ns
#10 {in_a, in_b, c_in} = 9’b1100_0100_1; // at 10 ns
#10 {in_a, in_b, c_in} = 9’b0011_0110_1; // at 20 ns
#10 {in_a, in_b, c_in} = 9’b1111_0001_0; // at 30 ns
#10 {in_a, in_b, c_in} = 9’b0001_1111_0; // at 40 ns
#10 $stop; // at 50 ns, stops simulation
end
endmodule
23 / 39
Output Example Output [Text View]
Executed at
https://www.tutorialspoint.com/compile_verilog_online.php
time 0: cout=0, sum=0, in_a=0, in_b=0, cin=0
time 10: cout=1, sum=1, in_a=c, in_b=4, cin=1
time 20: cout=0, sum=a, in_a=3, in_b=6, cin=1
time 30: cout=1, sum=0, in_a=f, in_b=1, cin=0
time 40: cout=1, sum=0, in_a=1, in_b=f, cin=0
24 / 39
Testbench (Read data input from file) Example
‘timescale 1ns /1ns // time_unit/time_precision
module adder4b_read_file_tb();
reg [3:0] in_a, in_b; // inputs to UUT are regs
reg c_in; // inputs to UUT are regs
wire [3:0] sum; // outputs of UUT are wires
wire c_out; // outputs of UUT are wires
integer fd; // file descriptors
// instantiate UUT
adder4b A1 (sum, c_out, in_a, in_b, c_in);
// monitor statement
initial $monitor("time %t: cout=%b,sum=%d, in_a=%d, in_b=%
d, cin=%b", $time, c_out, sum, in_a, in_b, c_in);
// stimulus generation
initial begin
fd = $fopen ("data.in", "r");
if (fd) begin
while ($fscanf (fd, "%h %h %b", in_a, in_b, c_in) != -1)
begin
#5; end
end
$fclose(fd); // close file handler
$stop; // finish simulation
end // end initial
endmodule
25 / 39
DataIn file Example
data.in file
1 5 1
2 6 1
3 7 1
4 8 1
5 9 1
6 10 0
7 11 0
8 12 0
9 13 0
10 14 1
11 15 1
26 / 39
Testbench (Read input file, write output file) Example
‘timescale 1ns /1ns // time_unit/time_precision
module adder4b_read_file_write_output_tb();
reg [3:0] in_a, in_b; // inputs to UUT are regs
reg c_in; // inputs to UUT are regs
wire [3:0] sum; // outputs of UUT are wires
wire c_out; // outputs of UUT are wires
integer read_fd, write_fd; // file descriptors
// instantiate UUT
adder4b A1 (sum, c_out, in_a, in_b, c_in);
// monitor statement
initial $monitor("time %t: cout=%b, sum=%d, in_a=%d, in_b
=%d, cin=%b", $time, c_out, sum, in_a, in_b, c_in);
initial #100 $stop;
// stimulus generation
initial begin
read_fd = $fopen ("data.in", "r");
write_fd = $fopen ("data.out", "w");
if (write_fd ==0 && read_fd ==0) begin
$display("File was NOT opened successfully");
$stop; // stop
end
reg[4:0] x;
initial begin
// Remember to check infinite loop
// This example uses 5-bit counter for 16 samples
for(x = 0; x < 16; x = x + 1)
#5 ; // need a delay here!
end
28 / 39
Example: UUT
// RTL Styles
assign A_eq_B = (A == B)? 1 : 0;
assign A_gt_B = (A > B) ? 1 : 0;
assign A_lt_B = (A < B) ? 1 : 0;
endmodule
29 / 39
Example: Testbench
module Comparator4b_tb;
wire A_gt_B, A_lt_B, A_eq_B;
reg [4:0] A, B; // 5-bit to prevent loop wrap around
// UUT
Comparator4b M1(A_gt_B, A_lt_B, A_eq_B, A[3:0], B[3:0]);
initial $monitor("%t: A=%h, B=%h, AgtB=%b, AltB=%b, AeqB
=%b", $time, A[3:0], B[3:0], A_gt_B, A_lt_B, A_eq_B);
initial #2000 $finish; // end simulation, quit program
initial begin
#5;
/** After #5, exhaustive test of valid inputs **/
for (A = 0; A < 16; A = A + 1) begin
for (B = 0; B < 16; B = B + 1) begin
#5; // every 5 time unit, A, B will be updated
end // first for
end // second for
end // initial
endmodule
30 / 39
Example: Testbench Output [Text view]
Executed at
https://www.tutorialspoint.com/compile_verilog_online.php
0: A=x, B=x, AgtB=x, AltB=x, AeqB=x
5: A=0, B=0, AgtB=0, AltB=0, AeqB=1
10: A=0, B=1, AgtB=0, AltB=1, AeqB=0
15: A=0, B=2, AgtB=0, AltB=1, AeqB=0
......................................
75: A=0, B=e, AgtB=0, AltB=1, AeqB=0
80: A=0, B=f, AgtB=0, AltB=1, AeqB=0
85: A=1, B=0, AgtB=1, AltB=0, AeqB=0
90: A=1, B=1, AgtB=0, AltB=0, AeqB=1
95: A=1, B=2, AgtB=0, AltB=1, AeqB=0
......................................
1275: A=f, B=e, AgtB=1, AltB=0, AeqB=0
1280: A=f, B=f, AgtB=0, AltB=0, AeqB=1
1285: A=0, B=0, AgtB=0, AltB=0, AeqB=1
31 / 39
Combinational Testbench
module comb(output d, e, input a, b, c);
and(d, a, b);
nor(e, a, b, c);
endmodule
module comb_tb;
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
32 / 39
Generating Clocks
Wrong way:
initial begin
#5 clk = 0;
#5 clk = 1;
#5 clk = 0;
... //(repeat hundreds of times)
end
Right way:
34 / 39
Example : 3-bit Counter
// Structural style
always @(posedge clk) begin
if (rst) begin counter_out <= 3’b000; end
else begin counter_out <= counter_out + 1’b1; end
end
endmodule
Initially reset the counter and then test all states, but do not test
reset in each state.
35 / 39
3-bit Counter Testbench
module Counter3b_tb;
wire [2:0] out;
reg clk, rst;
38 / 39
Force/Release Example
A
assign y = a & b; B
assign z = y | c;
Z
initial begin C
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
39 / 39