S3 · Lesson 29 · Verification lab
Coverage by simulation, a scoreboard against a reference model, and LFSR stimulus, in Python
A testbench is software, and the habits of lessons 25 and 26 are easiest to learn by writing one. This lab measures random coverage by simulation and checks it against the coupon-collector formulas, builds a scoreboard that compares a FIFO against a reference model and reports the first mismatch, and generates stimulus with a linear-feedback shift register whose period shows whether its taps are maximal. Each piece is small, and each catches a different kind of mistake.
- The line is the expected coverage of a coverpoint with N equally likely bins after n random tests, ; the dashed line marks n = , the expected number of tests to fill every bin.
- Coverage climbs quickly at first: half the bins take well under N tests. The last few bins take as long as all the others.
- The lab simulates this and compares.
Predict
A FIFO of depth 8 accepts a push when it's full, overwriting its oldest entry instead of refusing. A scoreboard compares its pops with a reference model's. When does the scoreboard first complain?
A push doesn't produce output, so the overflow itself is invisible to a scoreboard that compares outputs. The corruption shows up when the overwritten entry would have been popped: the DUT returns newer data where the model returns the old value. That delay is why designers also add assertions (never push while full) that fire at the cause.
Name it
The pieces
- Coverage: sample bins at random; the covered fraction should track , and filling all bins takes tests on average.
- Reference model and scoreboard: run the same operations through a simple, obviously correct model (here a deque) and compare each output with the design's; report the first index where they differ.
- LFSR stimulus: a Fibonacci LFSR of n bits shifts right and feeds back the XOR of its tap bits; with maximal taps (such as 16, 14, 13, 11) it cycles through all nonzero states.
Check
Write coverage(n_bins, tests, trials, seed): in each trial draw tests bins uniformly with random.Random(seed) and record the fraction of bins hit; return the average over the trials. Write fill(n_bins, trials, seed): the average number of draws needed to hit every bin.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write scoreboard(ops, outs, depth): ops is a list of ('push', value) or ('pop', 0); outs is the design's output for each op (None for pushes and for pops of an empty FIFO). Model a FIFO of the given depth that refuses pushes when full, and return the index of the first op whose output differs from the model's, or None.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write lfsr_period(taps, n): start an n-bit Fibonacci LFSR at state 1; each step compute the XOR of bits (n − t) for each tap t (bit 0 the least significant), shift right by one and put that bit in at the top. Return the number of steps until the state returns to 1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a testbench need both a scoreboard and assertions?
A scoreboard compares what the design outputs with what a reference model predicts, so it catches any bug that eventually produces a wrong output, whatever its cause. But it only notices when the wrong data comes out, which can be long after the mistake happened, and it says nothing about internal rules that don't immediately affect outputs. Assertions check those rules where they apply, such as never pushing into a full FIFO, and fire in the cycle the rule is broken, pointing straight at the cause. The scoreboard gives broad end-to-end checking; assertions give early, precise diagnosis.