Skip to content

Parameterization ​

Modules, structs, unions, and interfaces can all be parameterized at compile time. Because Neo's frontend checks every parameterized declaration symbolically with Z3, a declaration that passes neo check is verified for all valid parameter combinations—not just the ones instantiated in a testbench.

Compile-Time Parameter Types ​

Declarations accept parameters of the following compile-time meta types:

Parameter TypeMeaningTypical Uses
UIntNon-negative integer (>= 0)Zero-based indices, Bits(w), Array(T, n), 0#0
PosStrictly positive integer (>= 1)Non-empty widths/lengths (data[len - 1], clog2(n), .repeat(n))
BoolCompile-time boolean (true / false)Conditional hardware generation (if use_reg { ... } else { ... })
LogicTypeFirst-class hardware type valueGeneric data containers, FIFOs, arbiters, pipelines
TimingSymbolic single-cycle timing triggerReusable cycle-accurate pipeline stages

UInt vs Pos ​

  • UInt is assumed >= 0. Subtraction a - b is Z3-checked against underflow (a >= b, else uint_underflow), and / and % are Z3-checked against division by zero (b >= 1, else division_by_zero). UInt values also provide .toBits(w) and .toInt(w) to materialize hardware constants.
  • Pos is assumed >= 1 inside the declaration body and verified >= 1 at every call site (argument_invariant_error). Using Pos lets Z3 prove expressions like len - 1, clog2(len), and x.repeat(n) are always safe without extra where clauses.
neo
module ShiftReg(T: LogicType, width: Pos, t: Timing) {
  input data(t): Array(T, width),
  output head(t): T,
  output tail(t): T,
} {
  assign head = data[0];
  assign tail = data[width - 1]; // Safe: width >= 1
}

Bool Parameters and Compile-Time if ​

Bool supports true, false, !, &&, ||, ^, ==, !=, and .toBits() (converting to a 1-bit Constant Bits(1)). Comparing two UInt values with <, <=, >, >=, ==, or != also produces a Bool.

When an if condition is a compile-time Bool, Neo evaluates it at compile time (and pushes the branch condition into Z3 during symbolic checking):

neo
module OptionalInvert(invert: Bool, w: Pos, t: Timing) {
  input x(t): Bits(w),
  output y(t): Bits(w),
} {
  assign y = if invert { ~x } else { x };
}

where Invariants ​

When parameter constraints go beyond UInt (>= 0) and Pos (>= 1), attach a where clause to any parameterized module, struct, union, or interface:

neo
module SliceHigh(w: Pos, lo: UInt, t: Timing)
where lo: lo < w {
  input x(t): Bits(w),
  output y(t): Bits(w - lo),
} {
  assign y = x{w - 1 : lo};
}
  • Multiple constraints on the same parameter can be grouped with brackets: where lo: [lo < w, lo % 2 == 0].

  • Inside the body of SliceHigh, Z3 assumes lo < w, so w - lo cannot underflow and x{w - 1 : lo} is proven in bounds.

  • At every call site (e.g., SliceHigh(16, 8, t)), Z3 proves the where invariant holds and reports argument_invariant_error (with a related span pointing to the invariant declaration) if it can be violated.

LogicType Parameters and .width ​

Types in Neo are first-class compile-time values (LogicType). Passing Bits(32), Int(w), MyStruct, or Option(T) works just like passing any other argument:

neo
struct Tagged(T: LogicType, tag_w: Pos) {
  tag: Bits(tag_w),
  payload: T,
}

module Serializer(T: LogicType, t: Timing) {
  input in(t): T,
  output raw(t): Bits(T.width),
} {
  assign raw = in.castTo(Bits(T.width));
}

Compile-Time Lambdas and Logic Functions ​

  • Compile-time lambdas (a, b) => a + b abstract over compile-time values (UInt, Bool, LogicType, Timing).
  • Hardware logic functions #[captures](input x(t): T) -> output y(t): U => expr abstract over hardware logic expressions and can be invoked with fn#(arg) or passed to Array.map(fn).

Monomorphization ​

Parameters are evaluated completely during elaboration (neo-elab). Each distinct set of concrete non-timing arguments produces a specialized concrete module in neo-ir, SystemVerilog, and FIRRTL, with no SystemVerilog runtime parameters required.