Interfaces & Decoupled
Interfaces bundle related directional wires (inputs and outputs) into a reusable port contract that modules can provides or consumes. Neo supports both user-defined interface declarations and the built-in Decoupled(T) ready/valid interface.
Defining Custom Interfaces
An interface lists input and output fields from the provider's perspective. Like structs and modules, interfaces can be parameterized with UInt, Pos, Bool, LogicType, and Timing (plus where invariants):
pub interface MemBus(addr_w: Pos, data_w: Pos, t: Timing) {
output req_valid(t), // Defaults to Bits(1)
output addr(t): Bits(addr_w),
output wdata(t): Bits(data_w),
input rdata(t + 1): Bits(data_w),
}provides and consumes
A module exposes an interface in its I/O block using provides or consumes:
provides: The module is the provider—interfaceoutputfields are assignable inside the module, andinputfields are read-only.consumes: Directions are flipped—interfaceinputfields are assignable inside the module, andoutputfields are read-only.
module Producer(t: Timing) {
provides bus: MemBus(16, 32, t),
} {
assign {
bus.req_valid = 1;
bus.addr = 0x1000#16;
bus.wdata = 42#32;
}
}
module Consumer(t: Timing) {
consumes bus: MemBus(16, 32, t),
} {
seq {
bus.rdata = bus.wdata;
}
}Connecting Interfaces with connect(a, b)
At the top level of a module body, connect(a, b) wires every field between two compatible interfaces of opposite effective direction:
- Peer-to-peer: Connect a submodule's
providesinterface to another submodule'sconsumesinterface. - Passthrough: Connect a submodule's interface directly to the enclosing module's matching
providesorconsumesinterface.
@top
module System(t: Timing) {
provides ext_bus: MemBus(16, 32, t),
} {
mod prod = Producer(t);
mod cons = Consumer(t);
connect(prod.bus, cons.bus);
// Passthrough from a submodule to the parent module's own port:
mod ext_prod = Producer(t);
connect(ext_prod.bus, ext_bus);
}Neo verifies that:
- Both interfaces have the same nominal interface kind and Z3-equal parameter arguments (
connect_type_mismatch). - The two sides have opposite effective directions (
connect_same_direction). - None of the interface's assignable fields have already been driven (
multi_assignment). - Any interface field left neither explicitly assigned nor connected via
connect(...)triggers anundriven_interface_fieldwarning.
Built-In Decoupled(T) Interface
For ready/valid handshakes and dynamically stalled pipelines, Neo provides the built-in Decoupled(T) interface. From the provider's perspective, Decoupled(T) is equivalent to:
interface Decoupled(T: LogicType) {
input ready(*): Bits(1), // Driven by consumer; dynamic (*) timing
output req(*): Option(T), // Driven by provider; Valid(data) or Invalid
}Using Option(T) for req binds the valid bit and payload together in a single tagged union (Invalid = tag 0, Valid(T) = tag 1), so consumers must match on req before accessing the data.
Example: Register-Sliced / Combinational Pass-Through Stage
module IncrementStage {
consumes in: Decoupled(Int(32)),
provides out: Decoupled(Int(32)),
} {
assign in.ready = out.ready;
comb {
out.req = match in.req {
Valid(x) => .Valid(x + 1#32),
Invalid => .Invalid,
};
}
}Example: Priority Arbiter
module Arbiter(T: LogicType) {
consumes a: Decoupled(T),
consumes b: Decoupled(T),
provides out: Decoupled(T),
} {
comb {
match a.req {
Valid(x) => {
out.req = .Valid(x);
a.ready = out.ready;
b.ready = 0;
},
Invalid => {
out.req = b.req;
a.ready = 0;
b.ready = out.ready;
},
}
}
}Because comb enforces consistent branch assignments across all match arms, forgetting to drive a.ready, b.ready, or out.req in either branch is caught immediately at compile time.
Generated Port Flattening
During elaboration, interface ports are flattened into deterministic, readable hardware ports prefixed with the interface instance name (e.g., in_ready, in_req, out_ready, out_req).