Enumerations
Enums are defined using the @<PACKAGE>.enum()
decorator or enum
keyword.
Entries of an enum can be given explicit values or enumerated automatically with
various different value patterns. The width of an enum can be defined explicitly
or inferred from the largest value encoded within it.
Example
The Packtype definition can either use a Python dataclass style or the Packtype custom grammar:
As rendered to SystemVerilog:
Declaration Options
The decorator accepts options that modify the enum declaration:
Name | Default | Description |
---|---|---|
mode |
EnumMode.INDEXED |
Defines how inferred values are assigned |
width |
Inferred by max value | Specifies the bit-width of the enum type |
prefix |
None |
Customise the prefix in rendered languages |
The mode
parameter may be set to one of the following values:
EnumMode.INDEXED
- values start from0
and are chosen sequentially (default);EnumMode.ONE_HOT
- values start from1
and are chosen to have exactly one active bit (i.e powers of 2) so follow the sequence1
,2
,4
,8
, ...;EnumMode.GRAY
- values start from0
and are assigned following a Gray code (only one bit changes between sequentially assigned values), anEnumError
will be raised where not enough named constants are declared to fill the Gray code sequence.
The width
parameter may be omitted, in which case it is automatically inferred
as the ceiling log2 of the maximum enumerated value. Where it is specified, an
EnumError
will be raised if the maximum enumerated value exceeds what can be
expressed within the enum's bit width.
The prefix
may be used when rendering to languages that do not support name
spacing of enumerated types - for example in SystemVerilog the names of the
enumerated values will be <PREFIX>_<VALUE_NAME>
. If omitted, the prefix will
be inferred as the shouty snake case version of the enumeration's name.
The enum
declaration accepts options that modify the enum declaration,
the full syntax is as follows:
For example:
The <MODE>
option may omitted or set to one of the follow values:
indexed
- values start from0
and are chosen sequentially (default);onehot
- values start from1
and are chosen to have exactly one active bit (i.e powers of 2) so follow the sequence1
,2
,4
,8
, ...;gray
- values start from0
and are assigned following a Gray code (only one bit changes between sequentially assigned values), anEnumError
will be raised where not enough named constants are declared to fill the Gray code sequence.
The <WIDTH>
parameter may be omitted, in which case it is automatically inferred
as the ceiling log2 of the maximum enumerated value. Where it is specified, an
EnumError
will be raised if the maximum enumerated value exceeds what can be
expressed within the enum's bit width.
The <MODIFIER>
syntax allows standard behaviours of Packtype to be altered,
in the Python syntax these are given as keyword arguments to the decorator:
@prefix=...
may be used when rendering to languages that do not support name spacing of enumerated types - for example in SystemVerilog the names of the enumerated values will be<PREFIX>_<VALUE_NAME>
. If omitted, the prefix will be inferred as the shouty snake case version of the enumeration's name (.e.g entryA
ofmy_onehot_enum_t
will becomeMY_ONEHOT_ENUM_T_A
).
Explicit Values
Values within an enumeration can be assigned explicit values, these may be interspersed with implicitly assigned values and the tool will adopt explicit values and continue to enumerate from that point:
Helper Properties and Methods
Enum definitions expose a collection of helper functions for properties related to the enumeration:
<ENUM>._pt_width
- property that returns the bit width of the enumerated values;<ENUM>._pt_mask
- property that returns a bit mask matched to the width of the enumerated value (i.e.(1 << MyEnum._pt_width) - 1
);<ENUM>._pt_as_dict()
- function that returns a dictionary of the values of the enumeration with the key as the name as the value as the integer value;