Interfaces and Modports
Group protocol signals, define each block's role, and reduce connection mistakes.
Group a protocol
An interface collects the signals that form one connection. It can also contain types, parameters, functions, tasks, and assertions related to the protocol.
interface simple_stream_if #(
parameter int unsigned WIDTH = 32
) (
input logic clk
);
logic valid;
logic ready;
logic [WIDTH-1:0] data;
logic last;
modport source (
input clk, ready,
output valid, data, last
);
modport sink (
input clk, valid, data, last,
output ready
);
endinterfaceInstead of repeating four ports at every level, a module receives an interface instance.
module stream_register (
simple_stream_if.sink upstream,
simple_stream_if.source downstream
);
always_ff @(posedge upstream.clk) begin
if (upstream.valid && upstream.ready) begin
downstream.data <= upstream.data;
downstream.last <= upstream.last;
downstream.valid <= 1'b1;
end else if (downstream.ready) begin
downstream.valid <= 1'b0;
end
end
assign upstream.ready = downstream.ready || !downstream.valid;
endmoduleThe role of modports
A modport describes one participant's view. For source, valid, data, and last are outputs, while ready is an input. For sink, directions are reversed.
This information helps the compiler catch a signal driven from the wrong side. It also documents the protocol without requiring readers to inspect every connected module.
Modport directions are viewed from the module that uses the modport. This is a common source of confusion when writing the first interface.
Top-level instantiation
simple_stream_if #(.WIDTH(16)) link (.clk(i_clk));
producer u_producer (.bus(link.source));
consumer u_consumer (.bus(link.sink));One link instance contains the signals. Modports restrict the view supplied to each module.
What an interface does not solve
An interface does not define timing behavior by itself. You must still specify when valid may change, when a transfer occurs, how long data remains stable, and how reset behaves.
Many modern tools synthesize interfaces, but limits can exist around tasks, parameters, arrays of interfaces, or IP boundaries. A simple flat interface is usually the most portable form.
Key points
- An interface groups protocol signals and declarations.
- A modport gives each role a view and directions.
- Directions are expressed from the using module's point of view.
- Interfaces reduce repeated wiring, not the need to specify a protocol.
- Check synthesis support at project boundaries.
📝 Test your knowledge - Chapter quiz