Chapter 6-Testbench
Chapter 6-Testbench
Chapter 6-Testbench
Component Test
and Verification
Modified from Prof. Navabis Lectures
Testbench
Verilog simulation environments provide two kinds of display of
simulation results:
Graphical
Textual
Some also provide tools for editing input test data to a design module
that is being tested.
These tools are referred to as Waveform Editors.
Waveform editors have 2 problems:
Usually are good only for small designs.
Each simulation environment uses a different procedure for
waveform editing.
This problem can be solved by use of Verilog Testbenches.
Testbench
A Verilog Testbench is:
A Verilog module
Instantiates Module Under Test (MUT).
Applies data to MUT.
Monitors the output.
A module and its testbench forms a Simulation Module in which MUT
is tested for the same data regardless of what simulation environment
is used.
Testbench
Testbench
Combinational
Circuit
Testing
Sequential
Circuit
Testing
Combinational
Combinational
Circuit
Circuit
Testing
Testing
Sequential
Circuit
Testing
Output
Variables
connecting to
Inputs
Variables
connecting to
Outputs
initial begin
Initial
#20 b=4'b1011;
Block
#20 b=4'b1110;
Waits
#20 b=4'b1110;
for
#80 oe=1'b0;
80ns
#20 $finish;
end
always #23 fAfter
= 20ns
f +1;
endmodule
the simulation
is finished
Every 20 ns
A new value is
assigned to b
The $finish statement
reached at 160 ns.
Disablesisthe
At this time all
ALU Output
active
blocks
by setting
oe toprocedural
0
Application
stop and
of simulation
data
Allows
to the f input.
terminates.
effects of the last
f ischange
increment
by 1
input
to be
every
23 ns.
shown
in
simulation results
11
At 140 ns
oe changes to 0
causing the y output
become Z
12
Combinational
Circuit
Testing
Sequential
Sequential
Circuit
Circuit
Testing
Testing
13
14
if( rst )
d_out =4'b0000;
else
d_out = d_in ^ ({4{d_out[0]}} & poly) ^
{1'b0,d_out[3:1]};
endmodule
misr Sequential Circuit
15
...........................
...........................
A Testbench for misr
16
initial begin
#13 rst=1'b1;
#19 d_in=4'b1000;
#31 rst=0'b0;
#330 $finish;
end
new value
every 37 ns.
Generate data on
always #37 d_in = d_in + 3;
clk
always #11 clk = ~clk;
Togglesd_in
everyand
11ns
endmodule
A Testbench for misr (Continued)
17
Testing misr
18
Testbench Techniques
Testbench
Techniques
Test
Data
Simulation
Control
Limiting
Data Sets
Applying
Synchronized Data
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Intervals
Buffered
Data Application
19
Testbench Techniques
module moore_detector (input x, rst, clk, output z );
parameter [1:0] a=0, b=1, c=2, d=3;
reg [1:0] current;
Synchronous Reset Input
20
Testbench Techniques
..........................
if ( rst ) current = a;
else case ( current )
a : current <= x ? b
b : current <= x ? b
c : current <= x ? d
d : current <= x ? b
default : current <=
endcase
assign z = (current==d) ?
endmodule
: a
: c
: a
: c
x;
;
;
;
;
z output becomes 1
in state d when
a sequence of 101
is detected on x
1'b1 : 1'b0;
Test Data
Testbench
Techniques
Test
Data
Simulation
Control
Limiting
Data Sets
Applying
Synchronized Data
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Intervals
Buffered
Data Application
22
Test Data
module test_moore_detector;
reg x, reset, clock;
wire z;
moore_detector MUT ( x, reset, clock, z );
............................
............................
endmodule
Basic Data Generation
23
Test Data
Initial Block
............................
For initializing the reg variables
............................
initial begin
Four
clock=1'b0; x=1'b0; reset=1'b1;
Procedural Blocks
end
initial #24 reset=1'b0;
The waveform generated on x
Generates a signal with a
always #5 clock=~clock;
may or may not be able to test
period of 10ns on clock
our machine for
always #7 x=~x;
endmodule
Generates
signal
x with101 sequence.
a on
correct
Variables
used inathe
left-handperiod of 14ns
sides in theaprocedural
blocks
Periods
are declared as
reg of clock and x can be
changed to make this happen
24
Simulation Control
Testbench
Techniques
Test
Data
Simulation
Simulation
Control
Limiting
Data Sets
Applying
Synchronized Data
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Intervals
Buffered
Data Application
25
Simulation Control
Simulation
Control Tasks
Another testbench
for
are $stop and $finish
moore_detector
which
module test_moore_detector;
reg x=0, reset=1, clock=0;
wire z;
Simulation Control
module test_moore_detector;
reg x=0, reset=1, clock=0;
wire z;
Simulation Control
This testbench combines
the initial blocks of
deactivating reset and
............................
simulation control into
............................
one initial block.
initial begin
#24 reset=1'b0;
Simulation terminates at 165 ns.
#165 $finish;
end
A finished simulation
always #5 clock=~clock;
cannot be resumed.
always #7 x=~x;
endmodule
Testbench with $finish Simulation Control (Continued)
28
Simulation
Control
Limiting
Limiting
Data Sets
Applying
Synchronized Data
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Intervals
Buffered
Data Application
29
module test_moore_detector;
reg x=0, reset=1, clock=0;
wire z;
InCause
large xcircuits,
random
to receive
Generates random data on the x
data
is more
random
datauseful
endmodule
input of the circuit.
for
data inputs
10 times
every 7than
ns for
Testbench Using repeat to Limit Data Sets
control signals.
30
Simulation
Control
Limiting
Data Sets
Applying
Applying
Synchronized Data
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Intervals
Buffered
Data Application
31
32
Testbench
Guarantees
that Delays:
Setup
changing
of and
dataHold
and Time
clock do not coincide.
33
Simulation
Control
Limiting
Data Sets
Applying
Synchronized Data
Synchronized
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Intervals
Buffered
Data Application
34
output iszsupposed
to have
moore_detector MUT ( x, reset, clock,
);
initial
initial
initial
initial
#24 reset=0;
repeat(13) #5 clock=~clock;
forever @(posedge clock) #3 x=$random;
forever @(posedge clock) #1 $displayb(z);
endmodule
Testbench Displaying Output
moore_detector
36
New state is x
Output changes
New state is 0
New state is 1
New state is 2
Output changes
New state is 3
and
at
and
and
and
at
and
occurs at
0
50 to 0
occurs at
occurs at
occurs at
50
250
850
950 to 1
occurs at
950
38
An Interactive Testbench
Testbench
Techniques
Test
Data
Simulation
Control
Limiting
Data Sets
Applying
Synchronized Data
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Intervals
Buffered
Data Application
39
An Interactive Testbench
module moore_detector (input x, start, rst, clk,
output z );
parameter a=0, b=1, c=2, d=3, e=4;
reg [2:0] current;
An Interactive Testbench
if ( rst ) current <= a;
else if ( ~start ) current <= a;
If start becomes 0
else case ( current )
the machine resets to
a : current <= x ? b : a ;
initial state a
b : current <= x ? c : a ;
c : current <= x ? c : d ;
d : current <= x ? e : a ;
e : current <= x ? c : a ;
default: current <= a;
endcase
assign z = (current==e);
Output becomes 1 in state e
endmodule
Moore Sequence Detector Detecting 1101 (Continued)
41
An Interactive Testbench
module test_moore_detector;
reg x=0, start, reset=1, clock=0;
wire z;
moore_detector MUT ( x, start, reset, clock, z );
............................
............................
............................
endmodule
An Interactive Testbench
42
An Interactive Testbench
To get the machine started
initial begin
#24 reset=1'b0; start=1'b1;
wait(z==1'b1);
Waits for z to become 1,
#11 start=1'b0;
After it restarts the machine
#13 start=1'b1;
repeat(3) begin
Repeats the process of
#11 start=1'b0;
starting the machine and
#13 start=1'b1;
waiting for z to become 1
3 more times
wait(z==1'b1);
end
After 50 ns simulation is stopped
#50 $stop;
end
An Interactive Testbench
............................
............................
............................
always #5 clock=~clock;
always blocks to generate
always #7 x=$random;
clock and x input
endmodule
An Interactive Testbench (Continued)
44
An Interactive Testbench
45
An Interactive Testbench
module test_moore_detector;
reg x=0, start, reset=1, clock=0;
wire z;
moore_detector MUT ( x, start, reset, clock, z );
initial begin
#24 reset=1'b0; start=1'b1;
end
............................
endmodule
Interactive Testbench Using Display Tasks
46
An Interactive Testbench
When current becomes e
z is displayed
............................
Hierarchical Naming
Displays as soon as program
always begin Displays
: Output_Display
the old value of z
flow reaches it
To observe wait(MUT.current == MUT.e);
output within$display ("$display task shows: The output is
a state
%b", z);
$strobe ("$strobe task shows: The output is
%b", z);
#2 $stop;
A simulation clock cycle
Waits
for all simulation event
end
after e is detected,
to complete before displaying
always #5 clock=~clock;
z becomes 1 and is displayed
always #7 x=$random;
endmodule
Interactive Testbench Using Display Tasks (Continued)
47
Simulation
Control
Limiting
Data Sets
Applying
Synchronized Data
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Time
Random
Intervals
Intervals
Buffered
Data Application
48
reg [3:0] t;
moore_detector MUT ( x, start, reset, clock, z );
............................
............................
endmodule
Testbench using Random Time Intervals
49
initial begin:running
clock <= 1'b0; x <= 1'b0;
reset <= 1'b1; reset <= #7 1'b0;
start <= 1'b0; start <= #17 1'b1;
repeat (13) begin
Waits for 13 complete clock
@( posedge clock );
pulses before it de-asserts the
@( negedge clock );
start and finishes the simulation
end
start=1'b0;
Waits until the propagation of all
#5;
signals be completed
$finish;
end
Testbench using Random Time Intervals (Continued)
50
Simulation
Control
Limiting
Data Sets
Applying
Synchronized Data
Synchronized
Display of Results
An Interactive
Testbench
Random Time
Intervals
Buffered
Buffered
Data Application
Application
Data
52
rst=1'b1; start=1'b0;
Start and stop control of the state
#29 rst=1'b0;
machine
Each bit of the buffer is shifted
out
#29 start=1'b1;
onto the x input of MUT
#500 $stop;
1ns after the positive edge of clock
end
always @(posedge clk) #1 {x, buffer} = {buffer,x};
always #5 clk = ~clk;
As data is shifted, buffer is rotated in
endmodule
Design Verification
Formal verification:
A way of automating design verification by eliminating testbenches
and problems associated with their data generation and response
observation.
Tools do not perform simulation, but come up with a Yes/No
answer for every property the design is being checked for.
Eliminating data generation and response observation
Assertion verification:
Reduce or eliminate efforts needed for analyzing output responses
While the design is being simulated with its testbench data,
assertion monitors continuously check for correct design behavior.
In conditions that the design is misbehaving, the monitor is said to
fire to alert the designer of the problem.
55
Assertion Verification
Assertion
Verification
Assertion
Verification
Benefits
Open
Verification
Library
Using
Assertion
Monitors
Assertion
Templates
56
Assertion Verification
Unlike simulation, here in-code monitors issue a message if something
happens that is not expected.
In Verilog, monitors are modules.
The present set of assertion monitors are available in a library referred
to as OVL (Open Verification Library)
For using assertions designer compiles OVL and his or her own
assertion library into a simulation library.
If a signal does not have a value expected by a monitor, the assertion
monitor displays a message and the time that the violation of the
property has occurred.
57
Open
Verification
Library
Using
Assertion
Monitors
Assertion
Templates
58
59
Open
Open
Verification
Library
Library
Using
Assertion
Monitors
Assertion
Templates
60
Assertions
61
Assert_name
An Assertion is placed
in code like a module
instantiation.
Followed by static_parameters
like vector size and options
#(static-parameters)
Any Unique name is
allowed
instance-name
(dynamic-arguments);
Reference and monitor signals and
other dynamic arguments
62
Open
Verification
Library
Using
Using
Assertion
Assertion
Monitors
Assertion
Templates
63
assert_always
assert_change
assert_one_hot
assert_
cycle_sequence
assert_next
64
assert_always
Assertion
Monitors
assert_always
assert_change
assert_one_hot
assert_
cycle_sequence
assert_next
65
assert_always
If the test expression fails,
the assertion fires and its
corresponding message
(msg) is displayed
Continuously checks
its test_expr to make
sure it is always
true on the edge of
the
specified clock (clk)
assert_always
#( severity_level, property_type,
msg, coverage_level )
instance_name ( clk, reset_n, test_expr )
66
assert_always
67
assert_always
module BCD_Counter_Tester;
reg r, c;
wire [3:0] count;
BCD_Counter UUT (r, c, count);
initial begin
r = 0; c = 0;
end
initial repeat (200) #17 c= ~c;
initial repeat (03) #807 r= ~r;
endmodule
assert_change
Assertion
Monitors
assert_always
assert_change
assert_one_hot
assert_
cycle_sequence
assert_next
69
assert_change
Verifies that within a
given number of clocks
after the start event, the
test expression changes.
assert_change
#( severity_level, width, num_cks,
action_on_new_start, property_type,
msg, coverage_level )
instance_name ( clk, reset_n, start_event,
test_expr )
General Format for assert_change Assertion Monitor
70
assert_change
A shift register that walks
a 1 with every clock.
module Walking_One (input rst, clk, output reg [7:0] wo);
always @(negedge clk) begin
if (rst) wo <= 8'b10000000;
A 1 is loaded into the left-most
else wo <= {wo[0], wo[7:1]};
bit of wo with the rst signal
end
............................
............................
Check
that @(negedge
from the time that
always
clk) begin
(rst ==
and while
if 0)(rst)
wo(~rst),
<= it8'b10000000;
takes at most 7 negative clock
else wo
<=
{wo[0],
wo[7:1]};
7 for the number of clocks
1
for
the
length
of
the
edges for wo[0] to change.
that change is to occur
end
test expression
assert_change #(1, 1, 7, 0, 0, "Err: Bit 0 is not
changing, 0)
AC1 (~clk, ~rst, (rst==0), wo[0]);
............................
Falling edge of clk
Test Expression
assert_change
module Walking_One_Tester ();
reg rst=0, clk=0;
wire [7:0] walking;
It is the responsibility of
the testbench developer to
make sure enough data is
applied to cause design
errors to trigger assertion
monitors.
73
rst
assert_change
When
becomes 0, this
1 starts walking, it takes 7
clock edges for this 1 to
walk to bit 0 of wo.
Signals connected to
Walking-One Test Results
wo[7] to wo[0]
74
assert_one_hot
Assertion
Monitors
assert_always
assert_change
assert_one_hot
assert_
cycle_sequence
assert_next
75
assert_one_hot
Checks that while the
monitor is active, only
one bit of its n-bit test
expression is 1.
assert_one_hot
#( severity_level, width, property_type,
msg, coverage_level )
instance_name ( clk, reset_n, test_expr )
General Format for assert_one_hot Assertion Monitor
76
assert_one_hot
module Walking_One (input rst, clk, output reg [7:0] wo);
always @(negedge clk) begin
if (rst) wo <= 8'b10000000;
else wo <= {wo[0], wo[7:1]};
end
assert_one_hot
............................
............................
always @(negedge clk) begin
if (rst) wo <= 8'b10000000;
else wo <= {wo[0], wo[7:1]};
end
Test expression width
............................
assert_one_hot #(1, 8, 0, "Err: Multiple active
bits, 0)Test expression
AOH (~clk, ~rst, wo);
Walking One Circuit with Assertions
assert_one_hot
The mem.dat
that
module gray_counter (input [3:0] d_in,
input file
clk,
rst, ld, contains
outputconsecutive
reg [3:0]
q);
Gray code numbers is
read into mem.
reg [3:0] mem[0:15];
reg [3:0] im_q;
initial $readmemb("mem.dat", mem);
assert_one_hot
............................
always @( posedge clk ) begin: register
if( rst )
q <= 4'b0000;
Consecutive Gray code
In order to check for the
else
numbers are only
correct Gray code sequencing,
q
<=
im_q;
different in one bit, their
some auxiliary logic have been
end XOR must be one-hot.
assert_cycle_sequence
Assertion
Monitors
assert_always
assert_change
assert_one_hot
assert_
cycle_sequence
assert_next
81
assert_cycle_sequence
Checks for a sequence of
events in a given number
of clocks.
assert_cycle_sequence
#( severity_level, num_cks,
necessary_condition,
property_type,
msg, coverage_level )
instance_name ( clk, reset_n, event_sequence)
Like
other assertion
monitors, this
General Format for assert_cycle_sequence
Assertion
Monitor
assert_cycle_sequence
After the reset state,
the machine searches for
110 sequence.
When 110 is
received the next
2 clock cycles
take the machine
back to state a
e=4
a=0
0
1
1
1 0
b=1
0
d=3
1
0
0
c=2
0
83
assert_cycle_sequence
module Sequencing_Machine (input x, start, rst, clk,
output z );
parameter a=0, b=1, c=2, d=3, e=4;
84
assert_cycle_sequence
............................
always @( posedge clk )
if ( rst ) current <= a;
else if ( ~start ) current <= a;
else case ( current )
a : current <= x ? b : a ;
b : current <= x ? c : a ;
c : current <= x ? c : d ;
d : current <= e ;
e : current <= a ;
default: current <= a;
endcase
Verilog Code of Sequencing_Machine (Continued)
85
assert_cycle_sequence
assert_cycle_sequence
#(1, 3, 0, 0, "Err: State sequence not
followed, 0)
ACS (clk, ~rst, {(current==d),
(current==e), (current==a)});
assert_next #(1, 2, 1, 0, 0, "Err: Output state
The reached,
sequence of states
not
0) to verify
AN1 (clk, ~rst, (current==c && x==0),
(z==1));
endmodule
Verilog Code of Sequencing_Machine (Continued)
86
assert_cycle_sequence
87
assert_next
Assertion
Monitors
assert_always
assert_change
assert_one_hot
assert_
cycle_sequence
assert_next
88
assert_next
Verifies that starting and
an ending events occur
with a specified number
of clocks in between.
assert_next
#( severity_level, num_cks,
check_overlapping,
check_missing_start, property_type,
msg, coverage_level )
instance_name ( clk, reset_n, start_event,
test_expr)
General Format for assert_next Assertion Monitor
89
assert_next
Verifying that there are
two clock cycles between
............................
the time that current
assign z = (current==e);
becomes c while x is 0,
assert_cycle_sequence
and the time that z
#(1, 3, 0, 0, "Err: State sequence
not 1.
becomes
0)
Numberfollowed,
of clock
ACS
~rst,
cycles (clk,
between the
events{(current==d),
(current==e), (current==a)});
assert_next #(1, 2, 1, 0, 0, "Err: Output state
not reached, 0)
AN1 (clk, ~rst, (current==c && x==0),
(z==1));
Start Expression
endmodule
Test Expression
Assertion Templates
Assertion
Verification
Assertion
Verification
Benefits
Open
Verification
Library
Using
Assertion
Monitors
Assertion
Assertion
Templates
Templates
91
Assertion Templates
92
Assertion Templates
Assertion
Templates
Reset
Sequence
Initial
Resetting
Implication
Valid States
93
Reset Sequence
Assertion
Templates
Reset
Reset
Sequence
Initial
Resetting
Implication
Valid States
94
Reset Sequence
Often controllers have a
resetting sequence that
with certain sequence of
module Sequencing_Machine (input x,inputs,
start,
rst, ends
clk,
the machine
Verifies output
that if in three
z state
); it is in.
of what
clocks, x is 0,
parameter a=0, b=1, c=2, consecutive
d=3, e=4;
in the fourth clock the
// . . .
current state of the
machine becomes a.
assert_cycle_sequence
#(1, 4, 0, 0, "Err:Resetting does not occur")
ACS2 (clk, ~rst, {(x==0), (x==0), (x==0),
(current==a)});
// . . .
endmodule
Assertion Reset Sequence
95
Initial Resetting
Assertion
Templates
Reset
Sequence
Initial
Resetting
Resetting
Implication
Valid States
96
Initial Resetting
For verification of many
sequential circuits becomes
A Mealy Machine
necessary to check for resetting the
circuit using a synchronous or
module mealy_detector (input x, rst, clk, output z);
asynchronous reset input.
localparam [1:0]
reset = 0, // 0 = 0 0
got1 = 1, // 1 = 0 1
got10 = 2; // 2 = 1 0
reg [1:0] current;
............................
............................
............................
endmodule
Hard-Reset Assertion
97
Initial Resetting
............................
always @( posedge clk ) begin
if (rst) current <= reset;
else case ( current )
reset: if( x==1b1 ) current <= got1;
else current <= reset;
got1: if( x==1b0 ) current <= got10;
else current <= got1;
got10: if( x==1b1 ) current <= got1;
else current <= reset;
default: current <= reset;
endcase end
Hard-Reset Assertion (Continued)
98
Initial Resetting
............................
............................
assign z = ( current==got10 && x==1b1 ) ? 1b1 :
1b0;
Checks if rst is 1
assert_next
the next
current does not
#(1, 1, 1, 0, 0, then
Err:
Machine
state becomes
reset properly,
0)reset
AN1 (clk, 1b1, rst, (current==reset));
endmodule
Assertion Monitor is
Hard-Reset Assertion (Continued)
always active.
99
Implication
Assertion
Templates
Reset
Sequence
Initial
Resetting
Implication
Valid States
100
Implication
assert_implication
#( severity_level, properety_type,
msg, coverage_level )
instance_name ( clk, reset_n,
antecedent_expr,
consequence_expr )
101
Implication
Checks the output value in the
got10 state while (input
x is 1
module mealy_detector2
x, rst, clk, output z);
// . . .
assert_implication
#(1, 0, Err: Output not asserted, 0)
AI1 (clk, 1b1, (current==got10 && x),
(z==1));
// . . .
Always Active
endmodule
Asserting Implication
102
Valid States
Assertion
Templates
Reset
Sequence
Initial
Resetting
Implication
Valid States
103
Valid States
If the states of the machine
being tested are consecutive
binary numbers
assert_no_overflow
#( severity_level, width, min, max,
property_type,
msg, coverage_level )
instance_name ( clk, reset_n, test_expr )
General Format for assert_no_overflow Assertion Monitor
104
Valid States
The assertion fires, if the
Of the four possible states,
Mealy machine ever
it is using only three:
enters, the machines
reset, got1, got10.
invalid
The range
of state.
values the test
expression can take.
module mealy_detector2 (input x, rst, clk, output z);
(Min and Max)
// . . .
assert_no_overflow #(1, 2, 0, 2, 0, Err: Invalid
state, 0)
The size of vector being tested
ANV1 (clk, 1b1, current);
// . . .
endmodule
Checking for Invalid States
105
106
Summary
This chapter discussed:
The use of Verilog constructs for developing testbenches
Data generation and response analysis by use of Verilog
How assertion monitors could be used for reducing efforts needed
for response analysis of a unit-under-test.
Developing good testbenches for complex designs requires design
observability given to designers by use of assertions.
107