Lesson 2: How Is a Layered Testbench Organized?

A layered testbench splits the work into components that each do one job: a generator creates transactions, a driver turns them into pin wiggles on the design's interface, a monitor watches the pins and rebuilds transactions, and a scoreboard compares what came out against a reference model. Mailboxes pass transactions between them, and an environment class builds and starts them all. Each piece can then be reused or replaced without touching the others, which is the structure UVM formalizes.

What you'll learn

  • The job of each component and why they are separate
  • Interfaces and virtual interfaces
  • Mailboxes for passing transactions between threads
  • Forking the components and ending the test
  • Adding a protocol assertion beside the scoreboard

Conventions and assumptions

Every lesson tests the same small design: a registered 8-bit ALU (add, sub, and, or, xor) whose 9-bit result appears one clock after in_valid. All code was compiled and run in AMD Vivado xsim 2025.2, and the output shown is copied from those runs. The complete files are in the site's repository under scripts/verification.

The pieces

ComponentInputOutputKnows about pins?
GeneratorNothing (or a test's settings)TransactionsNo
DriverTransactionsPin activityYes
MonitorPin activityObserved transactionsYes
ScoreboardObserved transactionsPass or failNo
EnvironmentConfigurationBuilds and starts the restHolds the interface
Layered testbenchA generator sends transactions to a driver, which drives the DUT. A monitor watches the DUT's pins and sends what it sees to a scoreboard, which checks it.GeneratorDriverDUTthe ALUScoreboardMonitormailboxpinspinsmailboxThe environment builds and connects all of these
Stimulus flows across the top, observation back along the bottom; only the driver and monitor touch pins.

The key separation is that only the driver and monitor know the pin-level protocol. The generator and scoreboard work purely in transactions, so a new protocol means a new driver and monitor, and a new test usually means only a new generator.

Driver and monitor

The driver waits for a transaction from its mailbox and applies it on the falling edge. The monitor never looks at what the driver sent: it rebuilds each transaction from the pins, capturing the inputs when in_valid is high and the result when out_valid follows. That independence is what lets it catch a driver bug too.

lesson2_tb.sv: driver and monitor
class driver;
  virtual alu_if vif;
  mailbox #(alu_txn) from_gen;
  function new(virtual alu_if v, mailbox #(alu_txn) m); vif = v; from_gen = m; endfunction
  task run();
    forever begin
      alu_txn t;
      from_gen.get(t);
      @(negedge vif.clk);
      vif.in_valid = 1; vif.op = t.op; vif.a = t.a; vif.b = t.b;
      @(negedge vif.clk);
      vif.in_valid = 0;
    end
  endtask
endclass

class monitor;
  virtual alu_if vif;
  mailbox #(alu_txn) to_sb;
  function new(virtual alu_if v, mailbox #(alu_txn) m); vif = v; to_sb = m; endfunction
  task run();
    alu_txn t;
    forever begin
      @(posedge vif.clk);
      if (vif.in_valid) begin            // capture the inputs as the DUT samples them
        t = new();
        t.op = vif.op; t.a = vif.a; t.b = vif.b;
      end
      if (vif.out_valid && t != null) begin
        t.y = vif.y;                    // and the result one clock later
        to_sb.put(t);
        t = null;
      end
    end
  endtask
endclass

Environment and test

lesson2_tb.sv: environment and top level
class environment;
  generator gen; driver drv; monitor mon; scoreboard sb;
  function new(virtual alu_if vif, int count);
    mailbox #(alu_txn) g2d = new(), m2s = new();
    gen = new(g2d, count); drv = new(vif, g2d); mon = new(vif, m2s); sb = new(m2s);
  endfunction
  task run();
    fork gen.run(); drv.run(); mon.run(); sb.run(); join_none
  endtask
endclass

  initial begin
    vif.rst_n = 0; vif.in_valid = 0;
    repeat (2) @(negedge clk); vif.rst_n = 1;
    env = new(vif, 200);
    env.run();
    wait (env.sb.checked == 200);
    $display("%0d transactions checked, %0d errors: %s", env.sb.checked, env.sb.errors, env.sb.errors ? "FAIL" : "PASS");
    $finish;
  end

What the run prints

xsim output
200 transactions checked, 0 errors: PASS

Proving it can fail

A testbench that has never failed proves little. Changing the ALU's subtraction to b − a and rerunning, the scoreboard reported 53 errors out of 200 transactions, every one of them a subtraction, for example "op 1 a 249 b 66: got 329, expected 183". Subtractions where a equals b still passed, since b − a = a − b = 0 then.

Exercises

  • E1. Add an assertion that out_valid follows in_valid by exactly one clock.
  • E2. The test waits for 200 checked transactions. What happens if the DUT drops one?
  • E3. Make the generator's transaction count a plusarg so it can be set on the command line.

Solutions

  • E1. valid_follows: assert property (@(posedge clk) disable iff (!vif.rst_n) vif.in_valid |=> vif.out_valid); This is in lesson2_tb.sv and passes on the correct ALU.
  • E2. wait (env.sb.checked == 200) never finishes and the simulation hangs. Add a timeout: fork the wait with a #100us and fail if the timeout wins.
  • E3. In the top level: int n = 200; void'($value$plusargs("N=%d", n)); env = new(vif, n); then run with -testplusarg N=1000.

Common mistakes

  • Letting the monitor reuse the driver's transaction object instead of rebuilding from the pins, which hides driver bugs.
  • Sharing one transaction object across mailbox puts, so later changes overwrite earlier entries. Create a new object for each.
  • Ending the test with a fixed delay rather than when the expected number of checks is done.
  • Putting protocol knowledge in the scoreboard, so it can't be reused.

Check your understanding

  • Q1. Which components need the virtual interface? Answer: the driver and the monitor.
  • Q2. Why use a mailbox rather than a shared variable? Answer: it queues transactions and blocks the reader until one is available, so producer and consumer threads stay in step.
  • Q3. How did the injected bug show up? Answer: 53 scoreboard errors, all on subtractions.

The course

Related

Sources

Last verified

Run in Vivado xsim 2025.2 on 14 November 2026; outputs copied from that run. Language rules checked against IEEE 1800-2023.