Project: How Do You Verify an APB Slave in SystemVerilog?

Verify an APB slave the way a verification team would: write the plan, then build a layered testbench around it. A generator makes random transfers, a driver turns them into APB pin activity, a monitor turns the pins back into transfers, and a scoreboard compares every read with a reference model while a covergroup records which registers and directions were hit. Assertions on the interface check the protocol itself. Then plant a bug and confirm the testbench finds it.

How this was checked

Everything below was compiled and run in AMD Vivado xsim 2025.2, with and without the planted bug; the outputs shown are copied from those runs. The files are in the site's repository under scripts/apb.

What you'll learn

  • How APB transfers work: setup, access and wait states
  • A verification plan for a register slave
  • Each layer of the testbench and what it may touch
  • Protocol assertions on the interface
  • Why coverage alone can't find a bug

The design under test

The slave has eight 32-bit registers at offsets 0x00 to 0x1C. Writes complete in the access cycle; reads take one wait state, with PREADY low for one clock. Any address from 0x20 up completes with PSLVERR and changes nothing.

APB write and readA write: setup with PSEL high and PENABLE low, then access with PENABLE and PREADY high. A read: setup, then access with PREADY low for one wait state, then PREADY high as PRDATA is returned.waitclkPSELPENABLEPWRITEPADDR0410PREADYPRDATAdata
A write completes in two clocks; this slave's reads take three, with one wait state.

Step 1: the plan

FeatureChecked by
Every register stores and returns what was writtenScoreboard with a model of the eight registers
Out-of-range addresses give PSLVERR and change nothingScoreboard
PENABLE only after a setup phaseAssertion a_setup_first
Address, direction and data stable while waitingAssertion a_stable
PSLVERR only on a completing transferAssertion a_err_valid
Every register read and written, and the error range hitCovergroup: register x direction
The layered APB testbenchA generator sends random transfers to a driver, which drives the APB interface and the slave. A monitor on the same interface sends completed transfers to a scoreboard that checks them and samples coverage. Assertions sit on the interface.GeneratorDriverapb_regs8 registersScoreboard+ coverageMonitormailboxpinspinsmailboxAssertions in the interface check every transfer
Only the driver and the monitor touch pins; the assertions watch the interface for every transfer, whoever drives it.

Step 2: the interface and its assertions

apb_tb.sv (interface)
interface apb_if (input logic pclk);
  logic presetn, psel, penable, pwrite, pready, pslverr;
  logic [7:0] paddr; logic [31:0] pwdata, prdata;

  // Protocol assertions (explicit disable iff: xsim has no default form).
  // 1. PENABLE rises only in the clock after a setup phase (PSEL without PENABLE).
  a_setup_first: assert property (@(posedge pclk) disable iff (!presetn) $rose(penable) |-> $past(psel && !penable))
    else $error("PENABLE rose without a setup phase");
  // 2. While a transfer waits, its address, direction and data hold still.
  a_stable: assert property (@(posedge pclk) disable iff (!presetn) psel && penable && !pready |=> $stable({paddr, pwrite, pwdata}))
    else $error("transfer changed while waiting for PREADY");
  // 3. PSLVERR only on the completing clock of a transfer.
  a_err_valid: assert property (@(posedge pclk) disable iff (!presetn) pslverr |-> psel && penable && pready)
    else $error("PSLVERR outside a completing transfer");
endinterface

Step 3: the transaction

Addresses are word aligned, and nine transfers in ten are in range. Lesson 3 found that xsim ignores dist weights, so the split is chosen with $urandom in pre_randomize and applied as an ordinary constraint.

apb_tb.sv (transaction)
class apb_txn;
  rand bit        write;
  rand bit [7:0]  addr;
  rand bit [31:0] data;
  bit      [31:0] rdata;
  bit             err;
  bit             in_range;          // chosen before randomizing, 9 times in 10
  constraint aligned { addr[1:0] == 0; }
  // xsim 2025.2 ignores dist weights (Lesson 3), so the 90/10 split is made
  // with $urandom in pre_randomize and applied as a plain constraint.
  constraint range_split { in_range -> addr < 8'h20; !in_range -> addr >= 8'h20; }
  function void pre_randomize(); in_range = ($urandom_range(0, 9) != 0); endfunction
  function string show(); return $sformatf("%s 0x%02h data=%h rdata=%h err=%0b", write ? "WR" : "RD", addr, data, rdata, err); endfunction
endclass

Step 4: driver, monitor and scoreboard

apb_tb.sv (checking layers)
class driver;
  virtual apb_if vif;
  mailbox #(apb_txn) in;
  function new(virtual apb_if v, mailbox #(apb_txn) m); vif = v; in = m; endfunction
  task run();
    apb_txn t;
    vif.psel <= 0; vif.penable <= 0;
    forever begin
      in.get(t);
      @(posedge vif.pclk);                       // setup phase
      vif.psel <= 1; vif.penable <= 0; vif.pwrite <= t.write; vif.paddr <= t.addr; vif.pwdata <= t.data;
      @(posedge vif.pclk);                       // access phase
      vif.penable <= 1;
      do @(posedge vif.pclk); while (!vif.pready);
      vif.psel <= 0; vif.penable <= 0;
    end
  endtask
endclass

class monitor;
  virtual apb_if vif;
  mailbox #(apb_txn) out;
  function new(virtual apb_if v, mailbox #(apb_txn) m); vif = v; out = m; endfunction
  task run();
    forever begin
      @(posedge vif.pclk);
      if (vif.presetn && vif.psel && vif.penable && vif.pready) begin
        apb_txn t = new();
        t.write = vif.pwrite; t.addr = vif.paddr; t.data = vif.pwdata; t.rdata = vif.prdata; t.err = vif.pslverr;
        out.put(t);
      end
    end
  endtask
endclass

class scoreboard;
  mailbox #(apb_txn) in;
  bit [31:0] model [8];
  int checked, errors, n_err;
  apb_txn cur;
  covergroup cg;
    cp_reg: coverpoint cur.addr { bins regs[] = {[8'h00:8'h1C]} with (item % 4 == 0); bins out_of_range = {[8'h20:8'hFC]}; }
    cp_dir: coverpoint cur.write { bins rd = {0}; bins wr = {1}; }
    reg_x_dir: cross cp_reg, cp_dir;
  endgroup
  function new(mailbox #(apb_txn) m); in = m; cg = new(); foreach (model[i]) model[i] = 0; endfunction
  task run();
    forever begin
      in.get(cur);
      cg.sample();
      checked++;
      if (cur.addr >= 8'h20) begin
        if (!cur.err) begin errors++; if (errors <= 3) $display("MISMATCH no PSLVERR: %s", cur.show()); end
      end else if (cur.err) begin
        errors++; if (errors <= 3) $display("MISMATCH unexpected PSLVERR: %s", cur.show());
      end else if (cur.write) model[cur.addr[4:2]] = cur.data;
      else if (cur.rdata !== model[cur.addr[4:2]]) begin
        errors++; if (errors <= 3) $display("MISMATCH %s, expected %h", cur.show(), model[cur.addr[4:2]]);
      end
      if (cur.err) n_err++;
    end
  endtask
endclass

What the runs showed

DesignTransfersPSLVERRMismatchesCoverage
Correct slave400470100%
Planted bug: decoder ignores address bit 44004787100%
xsim output with +define+BUG (first lines)
MISMATCH RD 0x08 data=ce9d2708 rdata=f0868e72 err=0, expected 00000000
MISMATCH RD 0x08 data=f7dddc6e rdata=f0868e72 err=0, expected 00000000
MISMATCH RD 0x08 data=cf88dd19 rdata=f0868e72 err=0, expected 00000000
400 transfers checked, 47 with PSLVERR, 87 mismatches, coverage 100.0% (registers 100.0%, register x direction 100.0%)

The bug makes registers 4 to 7 alias registers 0 to 3, so a write to 0x18 overwrites 0x08. The scoreboard caught it on the first read of 0x08 after such a write. Coverage reached 100% in both runs: it shows that every register was read and written, not that the results were right. The assertions stayed quiet too, correctly, because the bug breaks the register map, not the protocol. Each check finds a different kind of bug, which is why a testbench needs all three.

Extend the project

  • Add a sequence of back-to-back transfers with no idle clock between them.
  • Add a cross of register with PSLVERR and check the error range for both directions.
  • Rebuild it in UVM, using Lesson 6's structure.
  • Plant a protocol bug, such as PREADY high in the setup phase, and confirm which assertion fires.

Common mistakes

  • Checking reads in the driver instead of a scoreboard fed by the monitor.
  • Sampling PRDATA in the setup phase or before PREADY.
  • Trusting 100% coverage as proof of correctness.
  • Forgetting that a write to an out-of-range address must change nothing.

Check your understanding

  • Q1. How many clocks does this slave's read take? Answer: three: setup, a wait state, and the completing access.
  • Q2. Why didn't coverage reveal the bug? Answer: it records what was exercised, not whether the results were right.
  • Q3. Which layer would notice a write landing in the wrong register? Answer: the scoreboard, on the next read of the register that was overwritten.

Related

Sources

Last verified

Run in Vivado xsim 2025.2 on 17 November 2026, with and without +define+BUG; outputs copied from those runs.