Unions & Option
A union is a nominal, optionally parameterized tagged sum type with packed layout [data (MSB), tag (LSB)]. Neo also provides the built-in Option(T) union.
Syntax
neo
pub union Packet(T: LogicType) {
Empty,
Word(Int(32)),
Custom(T),
}Bit Layout ([data (MSB), tag (LSB)])
For a union with N >= 1 variants:
- Tag field (LSB):
tag_width = 0whenN == 1(1-variant union).tag_width = clog2(N)whenN >= 2.- Tag values are assigned
0, 1, ..., N - 1in declaration order. - Occupies bits
[tag_width - 1 : 0].
- Data field (MSB):
data_width = max(payload_i.width)across all variants with payloads (or0for a tag-only union where no variant has a payload).- Occupies bits
[tag_width + data_width - 1 : tag_width]. - Payloads narrower than
data_widthare placed at the LSB of the data region ([tag_width + pw - 1 : tag_width]) and zero-padded in the upper bits.
- Total width:
tag_width + data_width(queryable viaPacket(T).width). - Empty unions (0 variants) are rejected (
empty_union).
Built-In Option(T)
Option(T) is a built-in 2-variant union with fixed encoding:
| Variant | Tag | Payload |
|---|---|---|
Invalid | 0 (1'b0) | None |
Valid(T) | 1 (1'b1) | T |
tag_width = 1,data_width = T.width, total width1 + T.width.- Bit
0is the validity bit; bits[T.width : 1]carry the payloadT.
Validation Rules
- Construction:
- Payload variant:
U.Var(expr)or inferred.Var(expr). Inheritsexpr's timing. - Payload-less variant:
U.VarorU.Var()(and.Var/.Var()). HasConstanttiming.
- Payload variant:
- Pattern matching (
match):- Scrutinee must be a
unionorOption(T). - Each arm matches either a variant (
VarorVar(binding)) or the catch-all wildcard_. - When a payload binding
xis present,xis bound as a read-onlyLogicValwith the variant's payload type and the scrutinee's timing. - Checked for exhaustiveness (
non_exhaustive_match), duplicate arms (duplicate_match_arm), multiple wildcards (multiple_default_cases), and unexpected bindings on payload-less variants (union_variant_no_data). - Inside
comb, allmatcharms must assign the exact same set of signals (inconsistent_conditional_assignments).
- Scrutinee must be a
Elaboration & Code Generation
Example
Source:
neo
union MyUnion(T: LogicType) {
A,
B(Int(32)),
C(T),
}
@top
module UnionMux(t: Timing) {
input in(t): MyUnion(Bits(1)),
output out(t): Int(32),
} {
comb {
out = match in {
A => 1#32,
B(x) => x,
C(x) => if x { 42#32 } else { 0#32 },
};
}
}In neo-ir and SystemVerilog:
inhas width2 + 32 = 34bits:in[1:0]is the tag (A = 0,B = 1,C = 2) andin[33:2]is the data region (B's payload isin[33:2],C's 1-bit payload isin[2]).matchlowers to acase (in[1:0])block insidealways_comb.- Because
UnionMuxhas no sequential logic, no__defaultClockport is emitted.