Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท SystemVerilog Features

Interfaces, Modports and Clocking Blocks

Bundle signals with SystemVerilog interfaces, restrict direction with modports, share tasks, connect testbenches with virtual interfaces and avoid races with clocking blocks.

Look at the port list of a bus master in plain Verilog: address, write data, read data, valid, ready, write enable, byte enables, response, and so on, 15 or 20 signals. Every module on the bus repeats the same list, every connection repeats it again, and one swapped wire is a day of debugging. An interface bundles those signals into a single named object you declare once and connect with one port.

This lesson covers interfaces, modports, tasks inside interfaces, virtual interfaces for testbenches and clocking blocks. SystemVerilog code is read-only here (no simulator on this site); the C programs are labelled stand-ins that model a bundle of signals and clock-edge sampling.

A simple interface

SYSTEMVERILOG
interface bus_if (input logic clk);
  logic        valid;
  logic        ready;
  logic        write;
  logic [15:0] addr;
  logic [31:0] wdata;
  logic [31:0] rdata;
endinterface

Modules take it as a single port:

SYSTEMVERILOG
module memory (bus_if bus);
  logic [31:0] mem [1024];

  always_ff @(posedge bus.clk) begin
    bus.ready <= 1'b0;
    if (bus.valid && !bus.ready) begin
      if (bus.write) mem[bus.addr[9:0]] <= bus.wdata;
      else           bus.rdata <= mem[bus.addr[9:0]];
      bus.ready <= 1'b1;
    end
  end
endmodule

And the top level creates one instance and hands it to both sides:

SYSTEMVERILOG
module top;
  logic clk = 0;
  always #5 clk = ~clk;

  bus_if bus (clk);
  cpu    u_cpu (.bus(bus));
  memory u_mem (.bus(bus));
endmodule

Adding a signal means editing the interface only, not every module and every connection.

Modports: who drives what

A raw interface lets any module drive any signal. Modports state the direction of each signal from the point of view of a particular user:

SYSTEMVERILOG
interface bus_if (input logic clk);
  logic        valid, ready, write;
  logic [15:0] addr;
  logic [31:0] wdata, rdata;

  modport master (output valid, write, addr, wdata, input  ready, rdata, clk);
  modport slave  (input  valid, write, addr, wdata, output ready, rdata, input clk);
endinterface

module memory (bus_if.slave bus);   // can only drive ready and rdata
  // ...
endmodule

Now an accidental bus.valid = 1; in the memory is a compile error. Modports are access control; they cost nothing in hardware.

The first stand-in models this in C: the bundle is a struct, and the two "modports" are two views of the same struct. The slave function receives only a pointer to a const view of the master's signals plus pointers to the signals it may drive, so writing the wrong field is a compiler error.

C Stand-in: a signal bundle with read-only and drive views

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The stand-in prints the write acknowledged, the read returning 99, and ready dropping when the bus is idle. A mistaken *valid = 0; inside slave_step would not compile, which is the protection modports give you.

Tasks and functions in interfaces

An interface can contain behaviour, so every user shares one correct implementation of the protocol:

SYSTEMVERILOG
interface bus_if (input logic clk);
  // ... signals ...
  task automatic write_word(input logic [15:0] a, input logic [31:0] d);
    @(posedge clk);
    valid <= 1; write <= 1; addr <= a; wdata <= d;
    do @(posedge clk); while (!ready);
    valid <= 0;
  endtask
endinterface

A testbench then calls bus.write_word(16'h10, 32'hDEAD_BEEF); and does not need to know the handshake.

Virtual interfaces: connecting classes

A module can have an interface port, but a class (see classes and randomization) lives in the dynamic world and cannot have ports. A virtual interface is a variable that holds a reference to a real interface instance:

SYSTEMVERILOG
class driver;
  virtual bus_if vif;               // handle to a bus_if

  function new(virtual bus_if vif);
    this.vif = vif;
  endfunction

  task run();
    vif.write_word(16'h4, 32'h1234);
  endtask
endclass

module tb;
  bus_if bus (clk);
  initial begin
    driver d = new(bus);
    d.run();
  end
endmodule

This is the bridge between a static hardware design and an object-oriented verification environment (UVM uses virtual interfaces in every driver and monitor).

Clocking blocks

In the testbench lesson on clocks you saw races at the clock edge. A clocking block removes them systematically: it samples inputs just before the clock edge and drives outputs just after it, with configurable skews.

SYSTEMVERILOG
interface bus_if (input logic clk);
  // ... signals ...
  clocking cb @(posedge clk);
    default input #1step output #1;     // sample 1 step before, drive 1 time unit after
    output valid, write, addr, wdata;
    input  ready, rdata;
  endclocking
  modport tb (clocking cb);
endinterface

In the testbench you write bus.cb.valid <= 1; and x = bus.cb.rdata; and the tool takes care of the timing. ##3; inside a program or clocking context means "wait three clock cycles" (##1 is the next edge).

The second stand-in demonstrates the sampling idea: the register output count changes at an edge, and a clocking-block-style monitor reads the value from before the edge ("preponed" sampling), while a naive monitor might see the new value.

C Stand-in: sampling before the clock edge

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The "sampled" column is what a flip-flop that captured count at that edge would have seen. Clocking blocks guarantee this behaviour, so tests behave identically in every simulator.

Generic interfaces with parameters

Interfaces can have parameters just like modules:

SYSTEMVERILOG
interface stream_if #(parameter int WIDTH = 8) (input logic clk);
  logic [WIDTH-1:0] data;
  logic             valid, ready;
endinterface

stream_if #(.WIDTH(32)) s32 (clk);

Modules that accept a stream_if use $bits(bus.data) to adapt.

When not to use interfaces

Interfaces shine for buses, memories and protocols with many wires. For two or three signals a plain port list is simpler. Some synthesis flows (older versions of tools, ASIC netlist handoff) support interfaces unevenly, so check your tool, and keep the top-level boundary of a chip in ordinary ports.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What problem do interfaces solve?

    An interface bundles related signals so you declare them once and connect one port, instead of repeating each signal everywhere.
  2. 2. What does a modport do?

    Modports are direction views of the interface. Driving a signal declared as an input in your modport is a compile error.
  3. 3. Why does a class need a virtual interface?

    Classes live outside the static module hierarchy; a virtual interface gives them access to the signals of an instantiated interface.
  4. 4. What do clocking blocks provide?

    Inputs are sampled just before the edge and outputs driven just after it, which removes testbench/design races.

Summary

  • An interface groups signals (and shared tasks) so modules connect with one port.
  • Modports restrict direction for each role and catch wiring mistakes at compile time.
  • Virtual interfaces connect classes and testbenches to real interface instances.
  • Clocking blocks sample before and drive after the clock edge, removing races.

Next: Classes and Constrained Randomization