---
title: "Verification: testbenches, assertions and coverage"
heading: Self-checking testbenches, constrained-random stimulus, assertions and coverage
eyebrow: S3 · Lesson 25 · Verification
xp: 50
---

::: hook
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 $2^{64}$ 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.
:::

::: play
- 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, $N(\ln N + 0.577)$.
- 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.
:::
:::

```sim plot
view: graph
x: n = 1..100000 log
y: c = 0..100
slider: N = 8..2000 = 64 log
slider: p = 0.00001..0.01 = 0.0001 log
plot: 100*(1 - (1 - 1/N)^n) | samples: 400 | label: bins covered, %
plot: 100*(1 - (1 - p)^n) | samples: 400 | dashed | color: ch2 | label: rare case hit, %
marker: x = N*(ln(N) + 0.5772) | label: all bins expected
readout: tests to fill every bin = N*(ln(N) + 0.5772)
readout: tests for 99% confidence of the rare case = ln(0.01)/ln(1 - p)
```

::: 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 88
- [ ] About 130
- [x] About 300
- [ ] About 4000

::: reveal
About 300: $64(\ln 64 + 0.577)$ = 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.
:::
:::

::: nameit
## 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:

```verilog
// 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 $1 - (1 - 1/N)^n$, and filling every bin takes on average

$$N H_N \approx N(\ln N + 0.577)$$

tests. A case with probability p per test needs $n = \frac{\ln(1 - c)}{\ln(1 - p)}$ tests to be hit with confidence c.
:::

::: example
## A coverpoint has 64 equally likely bins. What coverage do 200 random tests reach, and how many tests are expected to fill it?

::: solution
1. **Coverage after 200 tests.** Each bin is missed by a test with probability 63/64:
   $$\begin{aligned} C &= 1 - (63/64)^{200} \\ &= 0.957 \end{aligned}$$
   so 95.7%, about 61 of 64 bins.
2. **Tests to fill all bins.**
   $$64H_{64} = 303.6\ \text{tests}$$
   on average, or approximately
   $$64(\ln 64 + 0.577) = 303.1$$
3. **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.
:::
:::

::: example
## A corner case happens with probability $10^{-4}$ 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?

::: solution
1. **Tests for 99% confidence.**
   $$\begin{aligned} n &= \frac{\ln 0.01}{\ln(1 - 10^{-4})} \\ &= 46{,}050\ \text{tests} \end{aligned}$$
2. **A 16-bit adder.** It has $2^{32} = 4.3 \times 10^9$ input pairs, 72 minutes at a million tests a second (fast simulation).
3. **A 32-bit adder.** It has $2^{64}$ input pairs, 584 years even at a billion a second.
4. **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.
:::
:::

::: 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?

::: solution
1. **Tests to fill the cross.**
   $$\begin{aligned} n &= 256H_{256} \\ &\approx 256(\ln 256 + 0.577) \\ &= 1567 \end{aligned}$$
   on average (the exact harmonic sum gives 1568).
2. **Coverage after 1000 tests.**
   $$1 - (1 - 1/256)^{1000} = 98.0\%$$
3. **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
given: N = 32 | 100 | 400; n = 50 | 300 | 1000
answer: 100*(1 - (1 - 1/N)^n)
unit: %
tolerance: 0.5%

## A coverpoint has {N} equally likely bins. What percentage is covered, on average, after {n} random tests?

::: right
Right. $1 - (1 - 1/N)^n$ = {answer}%.
:::

::: misses
- [100*n/N] Tests often land in bins already covered.
- [any] Not yet. Find the chance a given bin is still empty.
:::
:::

::: check
given: N = 50 | 128 | 500 | 2000
answer: N*(ln(N) + 0.5772)
tolerance: 1%

## About how many random tests does it take, on average, to cover all {N} bins of a coverpoint? (Use $N(\ln N + 0.577)$.)

::: right
Right. {answer} tests.
:::

::: misses
- [N] One test per bin would need every test to land in a new bin.
- [N*(log(N) + 0.5772)] Use the natural logarithm.
- [any] Not yet. The coupon collector's count.
:::
:::

::: check
given: a = 4 | 8 | 16; b = 3 | 5 | 12
answer: a*b*(ln(a*b) + 0.5772)
tolerance: 1%

## A cross of a {a}-bin and a {b}-bin coverpoint is filled by random tests. About how many tests are expected?

::: right
Right. The cross has {a}·{b} bins: $ab(\ln ab + 0.577)$ = {answer}.
:::

::: misses
- [(a + b)*(ln(a + b) + 0.5772)] A cross has a bin for every pair.
- [any] Not yet. Count the cross bins first.
:::
:::

::: check
given: p = 0.001 | 0.0001 | 0.00001; c = 0.9 | 0.99 | 0.999
answer: ln(1 - c)/ln(1 - p)
tolerance: 0.5%

## A corner case happens with probability {p} per random test. How many tests give a {c} probability of hitting it at least once?

::: right
Right. $\frac{\ln(1 - c)}{\ln(1 - p)}$ = {answer} tests.
:::

::: misses
- [c/p] The chance of a miss compounds: solve $(1 - p)^n = 1 - c$.
- [1/p] That's the expected wait, about 63% confidence.
- [any] Not yet. All n tests must miss for the case to go unseen.
:::
:::

::: check
given: w = 12 | 16 | 20 | 24; R = 1 | 100
answer: 2^(2*w)/(R*1e6)
unit: s
tolerance: 0.5%

## How long does it take to test a {w}-bit adder exhaustively (every pair of operands) at {R} million tests per second?

::: right
Right. $2^{2w}$ pairs: {answer} s.
:::

::: misses
- [2^w/(R*1e6)] Two operands of w bits make $2^{2w}$ pairs.
- [any] Not yet. Count every pair of inputs.
:::
:::

::: check
given: N = 64 | 256; k = 2 | 3 | 4
answer: N*(1 + 1/2 + step(k - 2.5)/3 + step(k - 3.5)/4)
tolerance: 1%

## The last {k} of {N} bins are still empty. How many more random tests are expected to fill them?

::: right
Right. $N\left(\frac{1}{k} + \frac{1}{k-1} + \dots + 1\right)$ = {answer}: the last few bins cost as much as the first half.
:::

::: misses
- [N*k] The empty bins get harder to hit as they run out: add N/k, N/(k − 1), and so on down to N/1.
- [N/k] That's only the wait for the first of them.
- [any] Not yet. With j bins left, each test fills one with probability j/N.
:::
:::

::: check
kind: parts
given: N = 16 | 100 | 1000; n = 50 | 500 | 5000

## A coverpoint has {N} equally likely bins; {n} random tests are run.

::: part
answer: (1 - (1 - 1/N)^n)*100
unit: %
tolerance: 0.5%

### What coverage do they reach?

::: misses
- [any] $1 - (1 - 1/N)^n$.
:::
:::

::: part
answer: N*(ln(N) + 0.577)
tolerance: 1%

### About how many tests are expected to fill every bin?

::: misses
- [N*ln(N)] Add Euler's constant: $N(\ln N + 0.577)$.
- [any] $NH_N \approx N(\ln N + 0.577)$.
:::
:::
:::

::: check
kind: derivation
vars: p = 1e-6..0.1, c = 0.5..0.9999

## A corner case occurs with probability p per random test; the goal is to hit it with confidence c.

::: step
answer: ln(1 - c)/ln(1 - p)

### How many tests does that take?
:::

::: step
answer: -ln(1 - c)/p

### What does that become for small p?
:::

::: right
Right. For 99% confidence, about 4.6/p tests: rare cases need directed tests or formal proofs.
:::
:::

::: explain
## Why do verification teams track functional coverage instead of stopping when every line of the design has been executed?

::: model
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

::: check
given: N = 40 | 160; n = 80 | 600
answer: 100*(1 - (1 - 1/N)^n)
unit: %
tolerance: 0.5%

## Coverage of {N} bins after {n} random tests?

::: misses
- [any] $1 - (1 - 1/N)^n$.
:::
:::

::: check
given: N = 16 | 64 | 100 | 256 | 1000 | 4096
answer: N*(ln(N) + 0.5772)
tolerance: 1%

## Tests to fill {N} bins?

::: misses
- [any] $N(\ln N + 0.577)$.
:::
:::

::: check
given: p = 0.002 | 0.00005; c = 0.95 | 0.999
answer: ln(1 - c)/ln(1 - p)
tolerance: 0.5%

## Tests to hit a {p} case with confidence {c}?

::: misses
- [any] $\ln(1 - c)/\ln(1 - p)$.
:::
:::
:::
