Unions
Unions are defined using the @<PACKAGE>.union()
decorator, which processes
the definition and attaches it to the package. Members of a union
may be declared as scalars or reference enums,
structs, or other unions.
Example
The Packtype definition can either use a Python dataclass style or the Packtype custom grammar:
As rendered to SystemVerilog
Note
All members of a union must have the same bit width, otherwise a UnionError
will be raised for the first field that differs.
Helper Properties and Methods
Union definitions expose a collection of helper functions for properties related to the type:
<UNION>._pt_width
- property that returns the bit width of the union;<UNION>._pt_mask
- property that returns a bit mask matched to the width of the union (i.e.(1 << <UNION>._pt_width) - 1
);<UNION>._pt_fields
- property that returns a dictionary of fields within the union with the key being the field instance and the value being the field's name;<UNION>._pt_pack()
- packs all values contained within the union into a singular integer value (can also be achieved by casting to an int, e.g.int(<UNION>)
);<UNION>._pt_unpack(packed: int)
- unpacks an integer value into the fields of the union.