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

D10 · Lesson 7 · Testbenches and waveforms

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

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.

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

Predict

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

Name it

A testbench

`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 2n 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.

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

Show the solution
  1. Number of tests. Two 16-bit inputs make 232=4.29×109tests
  2. Time at a million tests per second. t=232106s=4295s=71.6minutes
  3. The clock. It toggles every 4 ns, a period of 8 ns: 18ns=125MHz

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

Worked example

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

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

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

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

Show the solution
  1. One test. Each test misses the corner value with probability 1−2−16.
  2. All of them miss. P=(1−2−16)100000=e−1.53=0.22
  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

A block has 24 input bits in all. How long does exhaustive testing take at 1000000 tests per second?

Write units the way you normally would: 1.5 s and 1500 ms are the same answer.

Check

A testbench toggles its clock with always #4 clk = ~clk; and a timescale unit of 1 ns. What frequency is the clock?

Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.

Check

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

Check

A 12-bit × 12-bit multiplier is tested exhaustively at 1 thousand tests per second.

(a)

How many tests are there?

(b)

How long do they take?

Write units the way you normally would: 1.5 s and 1500 ms are the same answer.

Check

A random testbench drives a 12-bit input with 1000 random values.

(a)

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

(b)

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

Check

A testbench checks a 24+24-bit adder exhaustively, every pair of inputs, at 1 million vectors per second.

(a)

How many vectors is that?

(b)

How many days does it take?

Explain why

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

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.