---
title: Testbenches and waveforms
heading: Verifying a design before it's built, a testbench module that generates a clock and stimulus and checks the outputs, delays and the simulator's timescale, reading waveforms to find bugs, self-checking tests, and why exhaustive testing runs out of time so quickly
eyebrow: D10 · Lesson 7 · Testbenches and waveforms
xp: 40
---

::: hook
Hardware can't be debugged with print statements once it's built, so it's tested in simulation first, with a **testbench**: a Verilog module with no ports that creates the clock, drives the design's inputs, and checks its outputs. Engineers then read the result as **waveforms**, every signal plotted against time, the way a logic analyzer would show real hardware. Most of a digital project's effort goes here, because a bug found in simulation costs minutes and one found in silicon costs months.
:::

::: play
- A two-bit counter: Q0 toggles on every clock edge, and Q1 toggles whenever Q0 was 1. Together they count 00, 01, 10, 11, 00, ...
- The timing diagram is the waveform a testbench would record. Reading it, you can check each clock's value against what the counter should hold.
- A self-checking testbench compares those values automatically, clock by clock.
:::

```sim logic
clock CLK 8
output Q0, Q1
always @(posedge CLK) begin
  Q0 <= ~Q0;
  Q1 <= Q1 ^ Q0;
end
wave: CLK, Q0, Q1
time: 72
```

::: predict
## A testbench checks a 32-bit adder exhaustively, every pair of inputs, at a billion tests per second. How long does it take?

- [ ] About 4 seconds
- [ ] About a day
- [x] About 584 years
- [ ] About a minute

::: reveal
Two 32-bit inputs make $2^{64}$ = 1.8 × 10¹⁹ combinations; at 10⁹ per second that's 1.8 × 10¹⁰ s, 584 years. Exhaustive testing works only for small blocks (a 16-bit adder takes 4.3 s), so larger designs rely on directed tests, random tests, coverage and formal proofs (D7).
:::
:::

::: nameit
## A testbench

```verilog
`timescale 1ns/1ps
module tb;
  reg clk = 0, reset = 1;
  wire [1:0] q;
  counter dut(.clk(clk), .reset(reset), .q(q));

  always #5 clk = ~clk;          // 10 ns period: 100 MHz

  initial begin
    #12 reset = 0;
    repeat (8) begin
      @(posedge clk); #1;
      $display("%t q=%b", $time, q);
    end
    $finish;
  end
endmodule
```

- `timescale` sets the unit of `#` delays; `always #5 clk = ~clk` toggles every 5 units, a period of 10.
- The **design under test** (dut) is instantiated with its ports connected by name.
- `initial` blocks run once, stepping through the test with delays and waits for clock edges.
- `$display` prints, and `$dumpvars` records waveforms for a viewer.

## Self-checking

A good testbench computes the expected output, from a simple model or a table, and compares: `if (q !== expected) $error(...)`. It then runs unattended in regression after every change.

## How much to test

An n-input combinational block has $2^n$ input combinations. Sequential designs have far more, since their behavior depends on history. Beyond about 30 to 40 input bits, exhaustive simulation is hopeless, and verification turns to constrained-random stimulus and coverage measures (D7) and formal methods.
:::

::: example
## A 16-bit multiplier is tested exhaustively by a simulator managing a million tests per second. How long does it take? What clock does `always #4 clk = ~clk;` make with a 1 ns timescale?

::: solution
1. **Number of tests.** Two 16-bit inputs make
   $$2^{32} = 4.29 \times 10^9\ \text{tests}$$
2. **Time at a million tests per second.**
   $$\begin{aligned} t &= \frac{2^{32}}{10^6}\ \text{s} \\ &= 4295\ \text{s} \\ &= 71.6\ \text{minutes} \end{aligned}$$
3. **The clock.** It toggles every 4 ns, a period of 8 ns:
   $$\frac{1}{8\ \text{ns}} = 125\ \text{MHz}$$

**Answer:** 71.6 minutes to test it exhaustively, and a 125 MHz clock.
:::
:::

::: example
## The two-bit counter starts at 00. What does it read after 7 clocks?

::: solution
1. **Count modulo 4.** A two-bit counter wraps every 4 clocks:
   $$7 \bmod 4 = 3$$
2. **Read the bits.** 3 is binary 11, so Q1 = 1 and Q0 = 1.

**Answer:** it reads 11: Q1 = 1 and Q0 = 1.
:::
:::

::: example
## A random testbench drives a 16-bit input with 100,000 random values. What is the chance that it hits one particular corner value?

::: solution
1. **One test.** Each test misses the corner value with probability $1 - 2^{-16}$.
2. **All of them miss.**
   $$\begin{aligned} P &= (1 - 2^{-16})^{100\,000} \\ &= e^{-1.53} \\ &= 0.22 \end{aligned}$$
3. **At least one hits.**
   $$1 - 0.22 = 0.78$$
   a 78% chance of hitting it.
4. **A directed test.** A directed test for the corner costs one test and makes it certain.

**Answer:** a 78% chance of hitting the corner value.
:::
:::

::: check
given: n = 16 | 24 | 32 | 40; r = 1e6 | 1e9
answer: 2^n/r
unit: s
tolerance: 0.5%

## A block has {n} input bits in all. How long does exhaustive testing take at {r} tests per second?

::: right
Right. $2^n/r$ = {answer} s.
:::

::: misses
- [n/r] There are $2^n$ combinations of n bits.
- [any] Not yet. Count the combinations.
:::
:::

::: check
given: h = 2 | 4 | 5 | 10; u = 1 | 10
answer: 1000/(2*h*u)
unit: MHz
tolerance: 0.5%

## A testbench toggles its clock with `always #{h} clk = ~clk;` and a timescale unit of {u} ns. What frequency is the clock?

::: right
Right. A period of 2 × {h} × {u} ns: {answer} MHz.
:::

::: misses
- [1000/(h*u)] Each toggle is half a period: the period is twice the delay.
- [any] Not yet. Two toggles per period.
:::
:::

::: check
given: k = 5 | 6 | 9 | 14
answer: k - 4*floor(k/4)
tolerance: 0.4

## A two-bit counter starts at 0. What value, in decimal, does it hold after {k} clocks?

::: right
Right. {k} mod 4 = {answer}.
:::

::: misses
- [k] A two-bit counter wraps around after 3.
- [any] Not yet. It counts modulo 4.
:::
:::

::: check
kind: parts
given: b = 8 | 12 | 16; r = 1 | 10 | 1000

## A {b}-bit × {b}-bit multiplier is tested exhaustively at {r} thousand tests per second.

::: part
answer: 2^(2*b)
tolerance: 0.5%

### How many tests are there?

::: misses
- [2^b] Both inputs vary: $2^{2b}$.
- [any] $2^{2b}$.
:::
:::

::: part
answer: 2^(2*b)/(r*1000)
unit: s
tolerance: 0.5%

### How long do they take?

::: misses
- [any] Tests ÷ rate.
:::
:::
:::

::: check
kind: parts
given: b = 8 | 12 | 16; N = 1000 | 100000

## A random testbench drives a {b}-bit input with {N} random values.

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

### What is the chance it hits one particular value at least once?

::: misses
- [any] $1 - (1 - 2^{-b})^N \approx 1 - e^{-N/2^b}$.
:::
:::

::: part
answer: N/2^b
tolerance: 0.5%

### On average, how many times does it hit that value?

::: misses
- [any] $N/2^b$.
:::
:::
:::

::: check
kind: parts
given: n = 16 | 24 | 32; r = 1 | 100 | 1000

## A testbench checks an {n}+{n}-bit adder exhaustively, every pair of inputs, at {r} million vectors per second.

::: part
answer: 2^(2*n)
tolerance: 0.5%

### How many vectors is that?

::: misses
- [2^n] There are two {n}-bit inputs: $2^{2n}$ combinations.
- [any] Not yet. $2^{\given{n}} \times 2^{\given{n}}$.
:::
:::

::: part
answer: 2^(2*n)/(r*1e6)/86400
tolerance: 0.5%

### How many days does it take?

::: misses
- [2^(2*n)/(r*1e6)] That's in seconds; divide by 86 400.
- [any] Not yet. Vectors ÷ rate, in days.
:::
:::

::: right
Right. Small blocks can be tested exhaustively; wide ones need constrained-random tests and formal tools.
:::
:::

::: explain
## Why does a testbench check its design's outputs automatically instead of relying on someone reading the waveforms?

::: model
Waveforms are invaluable for understanding a failure once you know where to look, but a design is retested after every change, often with thousands of cycles of stimulus across hundreds of tests, and no one can inspect all of that by eye or notice a single wrong bit among millions. A self-checking testbench computes what each output should be and compares automatically, so it can run every test unattended, report exactly which cycle and signal went wrong, and confirm that an earlier bug hasn't come back. People then read the waveforms only around the reported failures, which is where they're most useful.
:::
:::

::: practice

::: check
given: b = 8 | 12 | 16 | 20 | 24 | 32 | 40 | 48; r = 1e3 | 1e9
answer: 2^b/r
unit: s
tolerance: 0.5%

## Exhaustive time: {b} bits at {r} tests/s?

::: misses
- [any] $2^n/r$.
:::
:::

::: check
given: k = 1 | 2 | 3 | 7 | 10 | 13 | 22 | 31
answer: k - 4*floor(k/4)
tolerance: 0.4

## Two-bit counter after {k} clocks?

::: misses
- [any] k mod 4.
:::
:::
:::
