S329 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, 1−(1−1/N)n; the dashed line marks n = NHN, 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?

Name it

The pieces

  • Coverage: sample bins at random; the covered fraction should track 1−(1−1/N)n, and filling all bins takes NHN 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 2n−1 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?