SystemVerilog Reference

interface and modport

An interface bundles the signals of a connection. A modport gives each side its directions.

Syntax

interface bus_if (input logic clk);
  logic valid, ready;
  logic [7:0] data;
  modport src (output valid, data, input  ready);
  modport dst (input  valid, data, output ready);
endinterface

Rules

  • A module takes an interface as one port: module tx (bus_if.src bus);.
  • Inside, reach the signals with a dot: bus.valid.
  • A modport lists directions as seen from that side.
  • Driving a modport input is a compile error. That is the point of a modport.
  • An interface can also hold tasks, functions, assertions and clocking blocks.

Example

interface bus_if;
  logic valid;
  logic [7:0] data;
  modport src (output valid, data);
  modport dst (input  valid, data);
endinterface

module tx (bus_if.src bus);
  initial begin
    bus.valid = 0;
    #5 bus.data = 8'hA5; bus.valid = 1;
  end
endmodule

module rx (bus_if.dst bus);
  always @(posedge bus.valid) $display("t=%0t rx got %h", $time, bus.data);
endmodule

module tb;
  bus_if bus();
  tx u_tx (bus);
  rx u_rx (bus);
endmodule
Output on Verilator 5.052
t=5 rx got a5

Run It in the Playground

Common Mistake

Forgetting the parentheses on the instance

bus_if bus;     // error: this declares nothing usable
bus_if bus();   // correct: an interface instance

An interface is instantiated like a module. Without () the line is not an instance.

Fix: Write bus_if bus();, with port connections inside the parentheses when the interface has ports.

Learn It Properly

These lessons teach it step by step, with practice problems.

Standard

IEEE 1800-2023, 25.5.