D10 · Lesson 7 · Testbenches and waveforms
Verifying a design before it's built, a testbench module that generates a clock and stimulus and checks the outputs, delays and the simulator's timescale, reading waveforms to find bugs, self-checking tests, and why exhaustive testing runs out of time so quickly
Hardware can't be debugged with print statements once it's built, so it's tested in simulation first, with a testbench: a Verilog module with no ports that creates the clock, drives the design's inputs, and checks its outputs. Engineers then read the result as waveforms, every signal plotted against time, the way a logic analyzer would show real hardware. Most of a digital project's effort goes here, because a bug found in simulation costs minutes and one found in silicon costs months.
- A two-bit counter: Q0 toggles on every clock edge, and Q1 toggles whenever Q0 was 1. Together they count 00, 01, 10, 11, 00, ...
- The timing diagram is the waveform a testbench would record. Reading it, you can check each clock's value against what the counter should hold.
- A self-checking testbench compares those values automatically, clock by clock.
Predict
A testbench checks a 32-bit adder exhaustively, every pair of inputs, at a billion tests per second. How long does it take?
Two 32-bit inputs make = 1.8 × 10¹⁹ combinations; at 10⁹ per second that's 1.8 × 10¹⁰ s, 584 years. Exhaustive testing works only for small blocks (a 16-bit adder takes 4.3 s), so larger designs rely on directed tests, random tests, coverage and formal proofs (D7).
Name it
A testbench
`timescale 1ns/1ps
module tb;
reg clk = 0, reset = 1;
wire [1:0] q;
counter dut(.clk(clk), .reset(reset), .q(q));
always #5 clk = ~clk; // 10 ns period: 100 MHz
initial begin
#12 reset = 0;
repeat (8) begin
@(posedge clk); #1;
$display("%t q=%b", $time, q);
end
$finish;
end
endmodule
timescalesets the unit of#delays;always #5 clk = ~clktoggles every 5 units, a period of 10.- The design under test (dut) is instantiated with its ports connected by name.
initialblocks run once, stepping through the test with delays and waits for clock edges.$displayprints, and$dumpvarsrecords waveforms for a viewer.
Self-checking
A good testbench computes the expected output, from a simple model or a table, and compares: if (q !== expected) $error(...). It then runs unattended in regression after every change.
How much to test
An n-input combinational block has input combinations. Sequential designs have far more, since their behavior depends on history. Beyond about 30 to 40 input bits, exhaustive simulation is hopeless, and verification turns to constrained-random stimulus and coverage measures (D7) and formal methods.
Worked example
A 16-bit multiplier is tested exhaustively by a simulator managing a million tests per second. How long does it take? What clock does always #4 clk = ~clk; make with a 1 ns timescale?
Show the solution
- Number of tests. Two 16-bit inputs make
- Time at a million tests per second.
- The clock. It toggles every 4 ns, a period of 8 ns:
Answer 71.6 minutes to test it exhaustively, and a 125 MHz clock.
Worked example
The two-bit counter starts at 00. What does it read after 7 clocks?
Show the solution
- Count modulo 4. A two-bit counter wraps every 4 clocks:
- Read the bits. 3 is binary 11, so Q1 = 1 and Q0 = 1.
Answer it reads 11: Q1 = 1 and Q0 = 1.
Worked example
A random testbench drives a 16-bit input with 100,000 random values. What is the chance that it hits one particular corner value?
Show the solution
- One test. Each test misses the corner value with probability .
- All of them miss.
- At least one hits. a 78% chance of hitting it.
- A directed test. A directed test for the corner costs one test and makes it certain.
Answer a 78% chance of hitting the corner value.
Check
A block has 24 input bits in all. How long does exhaustive testing take at 1000000 tests per second?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
A testbench toggles its clock with always #4 clk = ~clk; and a timescale unit of 1 ns. What frequency is the clock?
Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.
Check
A two-bit counter starts at 0. What value, in decimal, does it hold after 6 clocks?
Check
A 12-bit × 12-bit multiplier is tested exhaustively at 1 thousand tests per second.
(a)
How many tests are there?
(b)
How long do they take?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
A random testbench drives a 12-bit input with 1000 random values.
(a)
What is the chance it hits one particular value at least once?
(b)
On average, how many times does it hit that value?
Check
A testbench checks a 24+24-bit adder exhaustively, every pair of inputs, at 1 million vectors per second.
(a)
How many vectors is that?
(b)
How many days does it take?
Explain why
Why does a testbench check its design's outputs automatically instead of relying on someone reading the waveforms?
Waveforms are invaluable for understanding a failure once you know where to look, but a design is retested after every change, often with thousands of cycles of stimulus across hundreds of tests, and no one can inspect all of that by eye or notice a single wrong bit among millions. A self-checking testbench computes what each output should be and compares automatically, so it can run every test unattended, report exactly which cycle and signal went wrong, and confirm that an earlier bug hasn't come back. People then read the waveforms only around the reported failures, which is where they're most useful.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.