Lesson 4: Interfaces, Modports and Clocking Blocks

A SystemVerilog interface bundles a design's signals into one named object, so a testbench passes a single handle instead of a dozen wires. A clocking block inside the interface says exactly when the testbench samples and drives those signals relative to a clock edge: inputs are sampled just before the edge and outputs change just after it. That removes the race between testbench code and design code that both wake on the same edge. A modport gives each user, such as the driver, the monitor or the design, its own view with only the directions it is allowed.

Learning objectives

  • Bundle the FIFO's signals in an interface and reach it from classes through a virtual interface
  • Explain the race between testbench and design code that wake on the same clock edge, and show it in a run
  • Write clocking blocks with input and output skews, and use them to drive and sample
  • Restrict each part of the testbench with a modport, and read the compile error when a rule is broken

Prerequisites

  • Lesson 2: the driver and monitor this lesson rewires
  • Lesson 3: classes and handles, which a virtual interface is
  • Blocking vs nonblocking assignments: why the order of code at a clock edge matters
  • The running example is the synchronous FIFO: 16 entries of 8 bits, one clock, flags full and empty and an occupancy count; writes when full are dropped, reads when empty are ignored, and read data appears one clock after the read. Its shared testbench code is in the site's repository under scripts/verification/fifo, and every example runs in AMD Vivado xsim 2025.2.

Interfaces and virtual interfaces

Every earlier lesson already uses one: fifo_if holds the FIFO's pins, the top module connects it to the design, and the driver and monitor classes keep a virtual fifo_if, a handle that points at that one instance. A class cannot contain an interface instance, so the handle is how class-based testbench code reaches the pins.

fifo/fifo_if.sv
interface fifo_if (input logic clk);
  logic       rst, wr_en, rd_en;
  logic [7:0] wr_data, rd_data;
  logic       full, empty;
  logic [4:0] count;
endinterface

The race at the clock edge

At a rising edge the simulator wakes every process waiting for it: the FIFO's always block, the monitor and the driver. The standard does not fix the order they run in. If the driver changes wr_en with a blocking assignment at that edge, the FIFO and the monitor may see the old value or the new one, depending on which ran first. Either way the code compiles and runs; the result just changes when unrelated code moves.

Waveform: driving wr_en on the clock edge races the design; a clocking block drives it after the edge
Top: wr_en driven on the edge itself, so at those edges the design and the monitor may see either value (marked ?). Bottom: driven through a clocking block, wr_en changes 1 ns after the edge and every edge samples the value from just before it.

Clocking blocks and skews

A clocking block names a clock edge and the signals synchronous to it. An input skew of #1step samples each input in the last moment before the edge, so the testbench sees the same values the design saw. An output skew of #1 drives each output 1 ns after the edge, long after the design has sampled. Drives go through the clocking block with <=, and the block itself is the event to wait on.

fifo/fifo_cb_if.sv: the clocking blocks
  // The driver's view: outputs change 1 ns after the rising edge; inputs are the values just before it.
  clocking drv_cb @(posedge clk);
    default input #1step output #1;
    output wr_en, rd_en, wr_data;
    input  full, empty, count;
  endclocking

  // The monitor's view: every signal as it was just before the rising edge.
  clocking mon_cb @(posedge clk);
    default input #1step;
    input wr_en, rd_en, wr_data, full, empty, count, rd_data;
  endclocking

Modports

A modport lists the signals one user may see and the direction of each. The driver's modport holds drv_cb and the reset; the monitor's holds only mon_cb, whose signals are all inputs; the design's lists its ports. A wrapper module puts the FIFO behind its modport. Passing virtual fifo_cb_if.drv to a class instead of the whole interface lets the compiler reject a monitor that tries to drive a pin.

fifo/fifo_cb_if.sv: modports
  modport drv (clocking drv_cb, output rst);
  modport mon (clocking mon_cb);
  modport dut (input clk, rst, wr_en, rd_en, wr_data, output rd_data, full, empty, count);
fifo/fifo_cb_if.sv: the design behind its modport
// The design behind its modport: this wrapper can only read the inputs and drive the outputs the modport lists.
module fifo_wrap (fifo_cb_if.dut p);
  sync_fifo #(.WIDTH(8), .DEPTH(16)) fifo (.clk(p.clk), .rst(p.rst), .wr_en(p.wr_en), .wr_data(p.wr_data),
    .rd_en(p.rd_en), .rd_data(p.rd_data), .full(p.full), .empty(p.empty), .count(p.count));
endmodule
FIFO interface with drv_cb and mon_cb clocking blocks and modports for the driver, monitor and design
The driver, the monitor and the design each see fifo_cb_if through their own modport. drv_cb drives the pins 1 ns after the edge; mon_cb samples them just before it.

Worked example: the race, and the fix

The test is 15 transactions: four writes, four reads, three writes with reads and four reads, checked against Lesson 3's reference model. The driver and the monitor are each written two ways, on the edge itself and through the clocking blocks, and the file is built twice, with the two processes in either order in the source.

fifo/fifo_cb_drv.svh: the driver
  // The driver: one transaction per clock.
  initial begin
    wait (running);
    while (plan.size() > 0) begin
      fifo_txn t = plan.pop_front();
      if (mode == "naive") begin
        @(posedge clk);                                   // on the edge itself: races the design
        ifc.wr_en = t.wr_en; ifc.rd_en = t.rd_en; ifc.wr_data = t.data;
      end else begin
        @(ifc.drv_cb);                                    // through the clocking block: 1 ns after the edge
        ifc.drv_cb.wr_en <= t.wr_en; ifc.drv_cb.rd_en <= t.rd_en; ifc.drv_cb.wr_data <= t.data;
      end
    end
    if (mode == "naive") begin @(posedge clk); ifc.wr_en = 0; ifc.rd_en = 0; end
    else begin @(ifc.drv_cb); ifc.drv_cb.wr_en <= 0; ifc.drv_cb.rd_en <= 0; end
  end
fifo/fifo_cb_mon.svh: the monitor
  // The monitor: what the FIFO saw and showed at each rising edge out of reset.
  always @(posedge clk) if (running && mode == "naive")
    check(ifc.wr_en, ifc.rd_en, ifc.wr_data, ifc.full, ifc.empty, ifc.count, ifc.rd_data);
  always @(ifc.mon_cb) if (running && mode == "cb")
    check(ifc.mon_cb.wr_en, ifc.mon_cb.rd_en, ifc.mon_cb.wr_data, ifc.mon_cb.full, ifc.mon_cb.empty,
          ifc.mon_cb.count, ifc.mon_cb.rd_data);
fifo_cb_tb.sv: the process order, swapped by -d MON_FIRST
`ifdef MON_FIRST
  `include "fifo_cb_mon.svh"
  `include "fifo_cb_drv.svh"
`else
  `include "fifo_cb_drv.svh"
  `include "fifo_cb_mon.svh"
`endif
xsim output: the four builds
naive: clock 2: empty is 1 with 1 entries
naive: clock 3: count is 1, model has 2
naive, driver first: 17 clocks checked, 11 errors
naive, monitor first: 17 clocks checked, 0 errors
cb, driver first: 17 clocks checked, 0 errors
cb, monitor first: 17 clocks checked, 0 errors

Driving on the edge gave 11 errors with the driver's process first in the source and 0 with the monitor's first. Neither number means anything about the FIFO: with the driver first, the monitor saw each new request at an edge where the design still saw the old one, so the model applied every request one clock before the FIFO did (the first error: empty is 1 while the model holds 1 entry). Through the clocking blocks, both orders give 0 errors, because every sample is taken before the edge and every drive lands after it. A test whose result depends on the order of unrelated code is a race; clocking blocks make the timing explicit.

Common mistakes

  • Driving with a blocking assignment on the same edge the design samples. It may pass today and fail when code moves, as the run above shows.
  • Mixing clocking-block drives and direct pin assignments to the same signal, so two processes fight over it.
  • Reading ifc.count instead of ifc.mon_cb.count in the monitor: the pin can already show the new value, the sample cannot.
  • Forgetting that a clocking-block drive takes effect after the output skew, so a check right after the drive still sees the old value.
  • Passing the whole interface where a modport would do, so nothing stops a monitor from driving a pin.

Quick check

1. What does input #1step mean in a clocking block?

Each input is sampled in the last moment before the clock edge, so the testbench sees the value the design saw at that edge, never one the edge is changing.

2. Why did swapping two blocks of testbench code change the result from 11 errors to 0?

Both woke on the same rising edge as the design. The order they run in is not defined, so whether the monitor saw the driver's new value depended on which one ran first.

3. What does a modport add that a clocking block does not?

Direction rules for one user. The clocking block fixes timing; the modport fixes who may drive what, and the compiler enforces it.

Exercises

Graded from easy to hard. Try each one before you open the solution; every solution was run in xsim 2025.2, and its output is shown.

E1 (easy). Write a monitor module that tries to drive wr_en through its modport. What does the tool say?

mon_cb only has inputs, so the elaborator rejects the drive. The rule is caught before the simulation starts.

fifo_cb_ex_tb.sv: E1, built with -d E1
`ifdef E1
// E1: a monitor that tries to drive a pin through its modport. mon_cb only has inputs.
module bad_monitor (fifo_cb_if.mon m);
  initial m.mon_cb.wr_en <= 1;
endmodule
`endif
xsim output
ERROR: [VRFC 10-3225] clocking block input signal 'wr_en' cannot be driven [fifo_cb_ex_tb.sv:9]
ERROR: [XSIM 43-3322] Static elaboration of top level Verilog design unit(s) in library work failed.
E2 (easy). After a write, compare the count pin with mon_cb.count 1 ns after the next edge.

The pin already shows 1; the clocking block still holds 0, the value sampled before the edge. The monitor must use the sample.

fifo_cb_ex_tb.sv: E2
        // E2: one write, then compare the pin with the clocking block's sample, 1 ns after the next edge.
        @(ifc.drv_cb); ifc.drv_cb.wr_en <= 1; ifc.drv_cb.wr_data <= 5;
        @(ifc.drv_cb); ifc.drv_cb.wr_en <= 0;
        #1 $display("E2: at %0d ns: count pin %0d, mon_cb.count %0d", $time, ifc.count, ifc.mon_cb.count);
      end
xsim output
E2: at 36 ns: count pin 1, mon_cb.count 0
E3 (medium). Issue a write through drv_cb at an edge. When does the pin change, and when does the FIFO act on it?

The drive is issued at the 25 ns edge, reaches the pin at 26 ns (the output skew) and the FIFO acts at the 35 ns edge, one clock later, with no race.

fifo_cb_ex_tb.sv: E3
        // E3: when does a clocking-block drive reach the pin, and when does the design act on it?
        fork
          @(ifc.wr_en) $display("E3: wr_en pin changed at %0d ns", $time);
          @(ifc.count) $display("E3: count changed at %0d ns", $time);
        join_none
        @(ifc.drv_cb) $display("E3: drive issued at the %0d ns edge", $time);
        ifc.drv_cb.wr_en <= 1; ifc.drv_cb.wr_data <= 9;
        @(ifc.drv_cb); ifc.drv_cb.wr_en <= 0;
        repeat (2) @(ifc.drv_cb);
      end
xsim output
E3: drive issued at the 25 ns edge
E3: wr_en pin changed at 26 ns
E3: count changed at 35 ns
E4 (hard). Rewrite Lesson 2's driver and monitor classes to use the clocking blocks through modports, and run 200 random transactions through Lesson 2's scoreboard.

Each class keeps a virtual interface with a modport, waits on its clocking block and drives or samples through it. The generator and scoreboard are unchanged.

fifo_cb_ex_tb.sv: E4
// E4: Lesson 2's driver and monitor, rewritten to use the clocking blocks through modports.
class cb_driver;
  virtual fifo_cb_if.drv vif;
  mailbox #(fifo_txn) from_gen;
  function new(virtual fifo_cb_if.drv v, mailbox #(fifo_txn) m); vif = v; from_gen = m; endfunction
  task run();
    fifo_txn t;
    forever begin
      @(vif.drv_cb);
      if (from_gen.try_get(t)) begin
        vif.drv_cb.wr_en <= t.wr_en; vif.drv_cb.rd_en <= t.rd_en; vif.drv_cb.wr_data <= t.data;
      end else begin
        vif.drv_cb.wr_en <= 0; vif.drv_cb.rd_en <= 0;
      end
    end
  endtask
endclass

class cb_monitor;
  virtual fifo_cb_if.mon vif;
  mailbox #(fifo_txn) to_sb;
  function new(virtual fifo_cb_if.mon v, mailbox #(fifo_txn) m); vif = v; to_sb = m; endfunction
  task run();
    forever begin
      @(vif.mon_cb);
      begin
        fifo_txn t = new(vif.mon_cb.wr_en, vif.mon_cb.rd_en, vif.mon_cb.wr_data);
        t.full = vif.mon_cb.full; t.empty = vif.mon_cb.empty; t.count = vif.mon_cb.count; t.rd_data = vif.mon_cb.rd_data;
        to_sb.put(t);
      end
    end
  endtask
endclass
xsim output
E4: 203 clocks checked, 90 writes, 89 reads, 0 errors: PASS

Interview questions

  • Q1. What race does a clocking block remove? Answer: the race between testbench code and design code that both wake on the same clock edge. Inputs are sampled before the edge and outputs driven after it, so the result no longer depends on which process the simulator runs first. Here it turned 11 errors or 0, depending on code order, into 0 either way.
  • Q2. Why does a class use a virtual interface? Answer: a class cannot contain an interface instance, which is part of the module hierarchy. A virtual interface is a handle to one instance, set when the testbench is built, so the same class can drive any instance.
  • Q3. What is a modport for? Answer: it gives one user of an interface a view with fixed directions. A monitor given the mon modport cannot drive a pin; the compiler rejects it, as in E1.

Summary

  • An interface bundles the FIFO's pins; classes reach it through a virtual interface handle.
  • Testbench and design code that wake on the same edge race; driving on the edge gave 11 errors or 0 depending only on code order.
  • A clocking block samples inputs just before the edge (#1step) and drives outputs after it (#1), and the race disappears.
  • A modport gives each user only the directions it needs, and the compiler enforces them.

Related

Sources

Last verified

Run in Vivado xsim 2025.2 on 26 November 2026: the four builds of the race example and all four exercise solutions. Outputs are copied from those runs and kept in scripts/verification/results; lib/verificationCourse.test.js checks them against this page.

Keep going