The Parts of a Testbench

A testbench is not one long block of code. It has parts: a generator, a driver, a monitor, a checker and coverage. Learn what each part is for, what breaks when one is missing, and see all of them in one small bench.

You Will Be Able To

  • Name the parts of a testbench and the one job of each.
  • Say what goes wrong when one part is missing.
  • Find each part in a small bench and read what it reports.
Lesson
B2.2
Time
20 min
Runs on
Your browser
Check
5 questions

Every test of a FIFO passes, and the checker reports zero errors. Then a reviewer asks one question: "Did any test ever fill it?" Nobody knows, because no part of the bench was counting. The bench checked well and measured nothing.

Five Parts Around the Design, One Job EachStep through the parts of a testbench. Each has one job, and each passes its work to the next as whole transactions, not wires.
  • Control / FSM
  • Driver
  • DUT
  • Monitor
  • Test
  • Report
  • Control
  • Data
  • Analysis
CallValueReturnWaiting
Five Parts Around the Design, One Job EachStep through the parts of a testbench. Each has one job, and each passes its work to the next as whole transactions, not wires. control Generator decides what to donext driver Driver turns a decision intopin wiggles dut Design under test fifo4 monitor Monitor turns pin wiggles intotransactions test Checker is each result right? report Coverage which situationshappened? push 3cpinspinsin 3c, out 3cwhat happened
  1. control Generatordecides what to do next
    • push 3c → Driver
  2. driver Driverturns a decision into pin wiggles
    • pins → Design under test
  3. dut Design under testfifo4
    • pins → Monitor
  4. monitor Monitorturns pin wiggles into transactions
    • in 3c, out 3c → Checker
    • what happened → Coverage
  5. test Checkeris each result right?
  6. report Coveragewhich situations happened?
  1. Generator

    It decides the next operation, such as "push the byte 3c", without knowing how pins move.

  2. Driver

    It turns that decision into pin values on the right clock edges.

    Follow the calls, values and waits
    1. call push 3c: Generator → Driver
  3. Monitor

    It watches the pins, never drives them, and reports whole transactions: "3c went in", "3c came out".

  4. Checker and coverage

    The checker decides whether each result is right. Coverage counts which situations the run reached.

Read the diagram as text
  • Generator: decides what to do next
  • Driver: turns a decision into pin wiggles
  • Design under test: fifo4
  • Monitor: turns pin wiggles into transactions
  • Checker: is each result right?
  • Coverage: which situations happened?
  • Generator → Driver: push 3c
  • Driver → Design under test: pins
  • Design under test → Monitor: pins
  • Monitor → Checker: in 3c, out 3c
  • Monitor → Coverage: what happened

The parts of a testbench for a FIFO. In bigger benches each box grows, but the jobs stay the same.

Before You Start

Read The Simplest Test That Can Fail first: every test drives, observes and decides. A FIFO hands out bytes in the order they came in; The Levels: Block, Subsystem, Chip, System used one.

Five questions this page answers
Question
1What are the parts of a testbench?
2What is the one job of each part?
3What goes wrong when a part is missing?
4Why do the parts pass transactions instead of wires?
5What can the coverage part tell you that the checker cannot?

The Idea: Split the Work by Job

The test in the last lesson did everything in one task. It chose inputs, set pins, read outputs and compared. That is fine for an adder with eight cases. A FIFO with clocks, flags and random traffic needs more, and one long block of code becomes impossible to change.

So a testbench is split by job. One part decides what to do. One part moves pins. One part watches pins. One part decides right or wrong. One part counts what happened. Each part can be written, fixed and reused without touching the others.

The parts talk in transactions: whole operations such as "push the byte 3c". Only the driver and the monitor deal with pins and clock edges. Everything above them thinks in operations, which is how the specification thinks too.

New Words

New words on this page
WordMeaning
TransactionOne whole operation, such as "push 3c" or "3c came out", instead of the pin values that carry it.
GeneratorThe part that decides which transactions to send next.
DriverThe part that turns a transaction into pin values on the right clock edges.
MonitorThe part that watches pins, never drives them, and reports transactions.
CheckerThe part that decides whether each result is right. A scoreboard is a common kind.
CoverageThe part that counts which situations the run reached, such as "the FIFO was full".
SeedThe starting number of a pseudo-random sequence. The same seed always gives the same sequence.

What Breaks When a Part Is Missing

Take one part away
Missing partWhat happens
GeneratorEvery input is written by hand, so tests cover only what someone thought of.
DriverPin timing is copied into every test; one change of protocol breaks them all.
MonitorEach check reads pins itself, and one of them samples at the wrong moment.
CheckerNobody decides. The bench prints and a person must decide, as in the last lesson.
CoverageEvery check passes, and nobody knows which situations were never reached.

The last row is the hook's story. A checker can only rule on what happened. If the run never filled the FIFO, the checker never saw a full FIFO, and it has nothing to complain about.

The Rules

  1. Rule 1: One part, one job.

    Deciding, driving, watching, judging and counting are separate parts.

  2. Rule 2: Only the driver and the monitor touch pins.

    Everything above them works in transactions.

  3. Rule 3: A monitor never drives.

    It only watches, so it can be reused wherever the same pins appear.

  4. Rule 4: The checker rules on what happened; coverage records what happened.

    A clean checker with empty coverage proves nothing about the empty cases.

Sources for These Rules

Spear and Tumbush (2012), section 1.9: the testbench wraps the DUT and is made of components that look like real neighbours. Section 1.10: the layered testbench. It has a signal layer and a command layer (driver, monitor, assertions). Above them sit a functional layer (agent, scoreboard, checker), a scenario layer (generator) and a test layer with functional coverage.

Spear and Tumbush (2012), section 1.11: the driver is a transactor, a loop that turns commands into signal changes.

Mehta (2018), section 15.5.2: the testbench instruments the DUT to provide stimulus and measure response.

The Verification Academy UVM Cookbook, "Testbench Architecture", draws the same parts in UVM. Only the idea of the picture is used here.

The Lab: Every Part, Labelled

The lab is one small bench around fifo4, with each part marked by a comment in capitals. Read it part by part. The generator decides each clock's operation from a pseudo-random sequence. Its numbers look random but follow a fixed rule from a starting number, the seed. The same seed gives the same traffic on every run. Random and Constrained-Random Stimulus says more about seeds.

GENERATOR: a shift register makes the next pseudo-random byte; two of its bits choose push and pop.
task automatic next_op(output bit p, q, output logic [7:0] d);
  lfsr = {lfsr[6:0], lfsr[7] ^ lfsr[5] ^ lfsr[4] ^ lfsr[3]};
  p = lfsr[0]; q = lfsr[1]; d = lfsr;
endtask

The monitor watches the pins at each rising clock edge and records two lists: bytes accepted in, and bytes handed out. seen_in [$] is a queue, a list that grows at the back and is read from the front.

MONITOR: it reads pins and never drives them.
always @(posedge clk) if (!rst) begin
  if (push && !full) seen_in.push_back(din);
  if (pop && !empty) seen_out.push_back(dout);
end

The checker takes the two lists and compares them in order: what went in first must come out first. Coverage counts three situations the plan cares about. They are a push while full, a pop while empty, and a push and a pop in the same clock.

Printed by the lab (Icarus), after 40 clocks of traffic.
checker: 23 bytes matched, 0 errors
coverage: push while full 0, pop while empty 0, push and pop together 13

The checker line is clean: 23 bytes matched, 0 errors. Read the coverage line, though. A push while full happened 0 times, and so did a pop while empty. The run never tested what the FIFO does at either limit. Rule 4 says what that means: the checker's zero errors say nothing about full or empty.

This is why the coverage part exists. Without it, this run would look finished. With it, you know the next job: steer the generator so the FIFO fills and drains.

Why Transactions Between the Parts

Why not let the checker read the pins? Because then every check repeats the monitor's timing rules, and one of them gets it wrong. Worse, the checker would break whenever the pin protocol changed.

With transactions, the protocol lives in two places only: the driver and the monitor. Change the pins, and you change those two parts. The generator, the checker and coverage keep working, because "push 3c" means the same thing on any pins.

Traps

The first trap is the monitor that peeks at the driver. It is tempting to tell the checker what the driver sent, instead of what the pins showed. Then a driver bug and a design bug look the same, and some are never seen. A monitor reads the pins.

The second trap is the checker that samples at the wrong moment. Read a FIFO output just before the clock edge, or just after, and you get different bytes. Putting all pin timing in the monitor makes that mistake once, in one place, where it is easy to fix.

The third trap is the bench with no coverage, shown in the lab. Every number it prints is true, and none of them is about the cases it skipped.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. Methodologies such as UVM give each part a standard shape.

  1. Agents

    A driver, a monitor and the part that feeds the driver are packaged together for one interface. The package is reused on every bench with that interface.

  2. Layers

    Large benches stack generators: one makes a scenario, the next breaks it into transactions, the driver turns those into pins.

Where This Goes Next

This lesson named the parts. These lessons build each one.

Where each idea is taught in depth
The idea from this pageWhere it is taught
Writing the generator and the driverBuild the Generator and Driver
Writing the monitor and the scoreboardBuild the Monitor and Scoreboard
Letting the generator choose random inputs, and seedsRandom and Constrained-Random Stimulus
Counting the situations a plan asks forFunctional Coverage: What Was Tested
Writing coverage for the FIFOCoverage and Tests for the Checker
The same parts as UVM componentsFrom a Layered Bench to UVM
A monitor that reports what the design acceptedMonitor the Event the DUT Accepts

Try It

Run the lab. Read the checker line, then the coverage line.

Change the seed to 8'hc3. Predict the coverage line before you run it.

Make the generator push more often than it pops (for example p = lfsr[0] | lfsr[2];). Does the full count change?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: checker: 23 bytes matched, 0 errors coverage: push while full 0, pop while empty 0, push and pop together 13

Check Yourself

Predict the outputThe same bench runs with the seed changed to 8'hc3. What is the last line printed?
// fifo4: a first-in first-out buffer, four entries deep. dout shows the oldest entry.
module fifo4 (input logic clk, rst, push, pop, input logic [7:0] din,
              output logic [7:0] dout, output logic full, empty);
  logic [7:0] mem [0:3];
  logic [1:0] wp, rp;           // write and read pointers
  logic [2:0] count;            // entries held
  assign full  = (count == 4);
  assign empty = (count == 0);
  assign dout  = mem[rp];
  always_ff @(posedge clk)
    if (rst) begin wp <= 0; rp <= 0; count <= 0; end
    else begin
      if (push && !full)  begin mem[wp] <= din; wp <= wp + 1; end
      if (pop  && !empty) rp <= rp + 1;
      count <= count + (push && !full) - (pop && !empty);
    end
endmodule

module tb;
  logic clk = 0, rst = 1, push = 0, pop = 0, full, empty;
  logic [7:0] din = 0, dout;
  fifo4 dut (.clk, .rst, .push, .pop, .din, .dout, .full, .empty);
  always #1 clk = ~clk;

  // GENERATOR: decides what to do next. Here: a little random pattern from a seed.
  logic [7:0] lfsr = 8'hc3;
  task automatic next_op(output bit p, q, output logic [7:0] d);
    lfsr = {lfsr[6:0], lfsr[7] ^ lfsr[5] ^ lfsr[4] ^ lfsr[3]};
    p = lfsr[0]; q = lfsr[1]; d = lfsr;
  endtask

  // MONITOR: watches the pins and turns them into transactions; it never drives anything.
  logic [7:0] seen_in [$], seen_out [$];
  always @(posedge clk) if (!rst) begin
    if (push && !full) seen_in.push_back(din);
    if (pop && !empty) seen_out.push_back(dout);
  end

  // CHECKER (a scoreboard): what goes in must come out, in order.
  int matched = 0, errors = 0;
  always @(negedge clk) while (seen_out.size() > 0) begin
    if (seen_in.size() == 0) begin errors++; void'(seen_out.pop_front()); end
    else if (seen_in.pop_front() === seen_out.pop_front()) matched++;
    else errors++;
  end

  // COVERAGE: counts the situations the plan asked for.
  int saw_full = 0, saw_pop_empty = 0, saw_both = 0;
  always @(posedge clk) if (!rst) begin
    if (push && full) saw_full++;
    if (pop && empty) saw_pop_empty++;
    if (push && pop && !full && !empty) saw_both++;
  end

  // DRIVER: turns each decision into pin wiggles, one per clock.
  initial begin
    bit p, q;
    logic [7:0] d;
    @(negedge clk) rst = 0;
    repeat (40) begin
      next_op(p, q, d);
      push = p; pop = q; din = d;
      @(negedge clk);
    end
    push = 0; pop = 0;
    @(negedge clk);
    $display("checker: %0d bytes matched, %0d errors", matched, errors);
    $display("coverage: push while full %0d, pop while empty %0d, push and pop together %0d", saw_full, saw_pop_empty, saw_both);
    $finish;
  end
endmodule
ConceptWhat is the one job of a monitor?
ConceptWhich parts of a bench deal with pins and clock edges?
ConceptIn the lab, what does the coverage line show that the checker line does not?
ConceptWhat goes wrong when a bench has no driver, and every test sets pins itself?

Common Mistakes

Wrong Write the whole test as one block that sets pins and compares.

Every change to the protocol or the checks means rewriting every test.

Fix Split the bench into generator, driver, monitor, checker and coverage.

Wrong Let the checker learn what was sent from the driver.

A driver bug then hides a design bug, because both sides share the mistake.

Fix Let the monitor report what the pins really showed.

Wrong Leave out coverage because the checker is clean.

The checker rules only on what happened; skipped cases leave no trace.

Fix Count the situations the plan asks for, and read the counts.

Remember

  • A testbench has parts: generator, driver, monitor, checker and coverage, one job each.
  • Only the driver and the monitor touch pins; the rest works in transactions.
  • A monitor never drives; a checker rules on what happened; coverage records what happened.
  • A clean checker with empty coverage says nothing about the cases that never ran.

Practice

Report a problem with this page