S3 · Lesson 25 · Verification
Self-checking testbenches, constrained-random stimulus, assertions and coverage
A respin of a modern chip costs millions of dollars and months, so a digital design team spends more of its effort on verification than on design. Exhaustive testing is out of the question: a 32-bit adder alone has input pairs, which would take centuries at a billion tests a second. Instead, a testbench throws constrained-random stimulus at the design, a reference model predicts every output, assertions watch for illegal behavior on every clock, and coverage counts which interesting situations have actually happened. Verification is done when coverage closes, and coverage closes slowly: the last few bins take as long as all the rest.
- Functional coverage against the number of random tests, for a coverpoint with N equally likely bins (solid), and the chance of having hit a rare corner case of probability p at least once (dashed).
- The first bins fill fast; the last ones slowly, because each new test is more and more likely to land in a bin already hit. The marker is the expected number of tests to fill every bin, .
- Lower p: the rare case needs about 1/p tests before it's even likely. That's why verification engineers bias the random stimulus toward corner cases.
Predict
Random tests have covered 32 of a coverpoint's 64 equally likely bins after about 44 tests. Roughly how many tests will it take to cover all 64?
About 300: = 303. This is the coupon collector's problem. The first half of the bins took 44 tests, but when 63 bins are covered, each test has only a 1 in 64 chance of hitting the last one, so the last bin alone takes 64 tests on average. Random stimulus is excellent at finding the bugs nobody thought of and slow at closing the last holes, so teams finish coverage with directed tests or biased constraints.
Name it
The testbench
A testbench is a non-synthesizable module that drives the design under test (DUT) and checks it:
- Stimulus: directed tests for known cases; constrained-random stimulus (random values within legal ranges, weighted toward interesting ones) for everything else.
- Reference model and scoreboard: a simple, obviously correct model (often in a higher-level language) predicts each output; the scoreboard compares and reports mismatches. A self-checking testbench never relies on someone reading waveforms.
- Drivers and monitors translate between transactions ("write 0x3F to address 12") and pin wiggles.
Assertions
An assertion states a property that must always hold and fires the moment it doesn't, close to the bug's cause:
// every request is granted within 1 to 3 cycles
assert property (@(posedge clk) req |-> ##[1:3] gnt);
// the FIFO never reports full and empty together
assert property (@(posedge clk) !(full && empty));
Formal tools can prove such properties for every possible input sequence, for blocks small enough to analyze.
Coverage
- Code coverage: which lines, branches, conditions and toggles were exercised. Necessary, but 100% code coverage can still miss whole features.
- Functional coverage: coverpoints with bins for the situations that matter (each opcode, FIFO full, back-to-back writes) and crosses of coverpoints (each opcode with each addressing mode).
With N equally likely bins and random tests, the expected fraction covered after n tests is , and filling every bin takes on average
tests. A case with probability p per test needs tests to be hit with confidence c.
Worked example
A coverpoint has 64 equally likely bins. What coverage do 200 random tests reach, and how many tests are expected to fill it?
Show the solution
- Coverage after 200 tests. Each bin is missed by a test with probability 63/64: so 95.7%, about 61 of 64 bins.
- Tests to fill all bins. on average, or approximately
- A 2-bin cross. Crossing it with a 2-bin coverpoint doubles the bins to 128 and raises the expected count to 695 tests.
Answer 200 tests reach 95.7% (about 61 bins), and filling all 64 takes 303.6 tests on average.
Worked example
A corner case happens with probability per random test. How many tests give 99% confidence of hitting it, and how does that compare to exhaustively testing a 16-bit and a 32-bit adder?
Show the solution
- Tests for 99% confidence.
- A 16-bit adder. It has input pairs, 72 minutes at a million tests a second (fast simulation).
- A 32-bit adder. It has input pairs, 584 years even at a billion a second.
- What follows. Exhaustive testing is out for anything wide, so stimulus has to be aimed.
Answer 46,050 tests, against 72 minutes to test a 16-bit adder exhaustively and 584 years for a 32-bit one.
Worked example
A cross of 32 opcodes and 8 addressing modes makes 256 equally likely bins. How many random tests are expected to fill it, and what coverage do 1000 tests reach?
Show the solution
- Tests to fill the cross. on average (the exact harmonic sum gives 1568).
- Coverage after 1000 tests.
- The stragglers. The last few bins take as long as all the rest, which is why verification engineers write directed tests for them.
Answer about 1568 tests on average to fill it, and 1000 tests reach 98.0% coverage.
Check
A coverpoint has 100 equally likely bins. What percentage is covered, on average, after 50 random tests?
Check
About how many random tests does it take, on average, to cover all 128 bins of a coverpoint? (Use .)
Check
A cross of an 8-bin and a 3-bin coverpoint is filled by random tests. About how many tests are expected?
Check
A corner case happens with probability 0.0001 per random test. How many tests give a 0.9 probability of hitting it at least once?
Check
How long does it take to test a 16-bit adder exhaustively (every pair of operands) at 1 million tests per second?
Write units the way you normally would: 1.5 s and 1500 ms are the same answer.
Check
The last 2 of 64 bins are still empty. How many more random tests are expected to fill them?
Check
A coverpoint has 100 equally likely bins; 50 random tests are run.
(a)
What coverage do they reach?
(b)
About how many tests are expected to fill every bin?
Check
A corner case occurs with probability p per random test; the goal is to hit it with confidence c.
Step 1
How many tests does that take?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What does that become for small p?
Explain why
Why do verification teams track functional coverage instead of stopping when every line of the design has been executed?
Line coverage shows only that each line ran at least once. It says nothing about whether the line ran in the situations where bugs hide. A FIFO's write logic can execute in a test that never fills the FIFO, so the overflow bug in that same line is never triggered. Functional coverage lists the situations the specification cares about (each opcode, the FIFO full while a write arrives, back-to-back transactions, each pair of modes) and counts which ones the tests actually produced. Gaps in that list point straight at the stimulus that's missing, and the list doubles as a statement of what has been checked, which is what a team needs before committing to fabrication.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.