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

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 264 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, 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.

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?

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

NHN≈N(ln⁡N+0.577)

tests. A case with probability p per test needs n=ln⁡(1−c)ln⁡(1−p) 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
  1. Coverage after 200 tests. Each bin is missed by a test with probability 63/64: C=1−(63/64)200=0.957 so 95.7%, about 61 of 64 bins.
  2. Tests to fill all bins. 64H64=303.6tests 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.

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

Show the solution
  1. Tests for 99% confidence. n=ln⁡0.01ln⁡(1−10−4)=46,050tests
  2. A 16-bit adder. It has 232=4.3×109 input pairs, 72 minutes at a million tests a second (fast simulation).
  3. A 32-bit adder. It has 264 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.

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
  1. Tests to fill the cross. n=256H256≈256(ln⁡256+0.577)=1567 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

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 N(ln⁡N+0.577).)

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°.

Explain why

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

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.