Handoutverilog5testing 250101 232413
Handoutverilog5testing 250101 232413
TESTING
xyz.v
add.v
© B. Baas
109
Verilog Code in Testbenches
add.v
© B. Baas
111
Verilog Code in Testbenches
© B. Baas
112
Verilog Code in Testbenches
© B. Baas
114
Testbench Design: Approach 1
Basic Flow
• An Approach 1 example top_tb.v OR top.vt
testbench including $write( )
initial begin test
in = 4'b0000;
clk = 1’b0; generator
reset = 1’b1;
in2 = 8’hA5;
code data
#100; clk=1’b1; #100; clk=1’b0; // 1 clk clk
$write("in = %b, out = %b\n", in, out); inputs
reset = 0;
#100; clk=1’b1; #100; clk=1’b0; // 1 clk
top.v
$write("in = %b, out = %b\n", in, out);
abc.v
in = 4'b0001;
#100; clk=1’b1; #100; clk=1’b0; // 1 clk
$write("in = %b, out = %b\n", in, out); xyz.v
in = 4'b1010;
#100; clk=1’b1; #100; clk=1’b0; // 1 clk
$write("in = %b, out = %b\n", in, out);
add.v
...
$stop;
end
© B. Baas
115
Testbench Design: Approach 2
Basic Flow
• Both the “test” block and the
top_tb.v OR top.vt
“hardware” block are
coordinated by the same
clock signal which is test
generated by an independent generator
clock oscillator module in the code
clock
test module
osc
• Better for more complex top.v
systems abc.v
• Adds some realism in the
timing of input and output xyz.v
signals add.v
© B. Baas
116
Testbench Design: Approach 2
Example Clock Oscillator
reg clock;
initial begin
reset = 1’b1; // assert reset
top_tb.v OR top.vt
#1000; // perhaps a few cycles
reset = 1’b0; // de-assert reset
...
#10000; // main simulation test
...
$stop; // stop simulation clock generator
end
code
// osc inverts clock value every #100
always begin clock
if (reset == 1’b1) begin
clock = 1’b0; osc
#1;
end
// let time advance when reset == 1'b1 top.v
else begin
#100; // cycle time = #200 abc.v
clock = ~clock; // invert clock
end
end
xyz.v
• This code more cleanly sets clock in only add.v
one always block
• In this implementation, reset takes effect
only at the end of a clock phase, not in
the middle of one
© B. Baas
118
Testbench Design: Approach 2
Example Test Generator
• Here is an example of how top_tb.v OR top.vt
code in the test generator
could look: test
initial begin generator
reset = 1’b1; // set reset
data = 8’h00; code
#500; // wait > 1 clk period
reset = 1’b0; // clear reset clock
// #10 after clk edge is clk-to-Q delay osc
@(posedge clock); #10; // next clock edge top.v
data = 8’hB3;
© B. Baas
119
Verifying Hardware Correctness
© B. Baas 121
Verifying Hardware Correctness
© B. Baas 124
Golden Reference Approach:
Example with Matlab Ref. and Checker
verilog,
matlab,
or file,....
input input
data matlab
verilog copy
for HW
testbench reference
testing match?
model
matlab
verilog checker
HW out
verilog
Verilog Matlab
© B. Baas 125
simulator simulator
Sources of Input Test Data
1) From a data file on disk
– For example, from a video sequence
2) From data generated by a script
– For example, all values 0–65,535 for a block with 16 binary
inputs
% data.m
– Input data may be generated from either matlab or a(1) = 23;
verilog. I think it's a little easier to generate input a(2) = 456;
a(3) = 92;
data in verilog and then print both input and output a(4) = 4738;
to a matlab-readable *.m file and test and compare ...
in matlab
• It can sometimes be a little awkward to read data from a file in
verilog
– You may find it handy to declare variables as signed in
verilog and print them using $fwrite so both positive and
© B. Baas
negative numbers print correctly 126
Sources of Input Test Data
• Example data generated by verilog, then imported into matlab
% verilog_data.m >> verilog_data
% >> whos
% Data file printed by a verilog simulation test bench. Name Size Bytes Class Attributes
%
% The data is printed in such a manner that the data a 4x3 96 double
% may be loaded into matlab by simply typing in matlab: input 1x5 40 double
%
% >> verilog_data >> input
a(1,1) = 5; a =
a(1,2) = 9;
a(1,3) = -1; 5 9 -1
a(2,1) = 0; 0 5 -8
a(2,2) = 5; 4 -2 5
a(2,3) = -8; 0 -1 -5
a(3,1) = 4;
a(3,2) = -2; >>
a(3,3) = 5;
a(4,1) = 0;
a(4,2) = -1;
a(4,3) = -5;
© B. Baas 127
Generating Test Cases
1) Exhaustive
– Example: a 16-bit adder has 32 inputs, so 2^32 (4.3 billion)
possible inputs. 71 minutes @ 1 million tests/sec
– Example: a 32-bit adder would require 584,942 years
@ 1 million tests/sec!
– On the positive side, when you are done, you know your
circuit is 100.000% correct
2) Directed—choose corner or edge cases by hand
– Example: 8-bit + 8-bit signed 2’s complement adder
• 0+0, 0+1, 1+0, 0+(–1), (–1)+0
• (–1)+(–1) = 11111111 + 11111111
• 127+127 = 01111111 + 01111111
• (–128)+(–128) = 10000000 + 10000000
© B. Baas 128
Generating Test Cases
3) Random
– The test environment automatically generates random input
test cases, possibly with some direction
– It is almost always a good idea to make results repeatable to
permit debugging of errors
• Avoid: “it failed after a week of testing but now it works fine
and I can not find the case that failed!”
• Run short batches of tests
• The random input data of each batch is determined by a
random seed
– Run random tests on as much hardware as you can afford
– Run random tests as long as the schedule permits
© B. Baas 129
Recommended Directory and
File Layout (EEC 180B)
© B. Baas 130