Arrays
Multi-dimensional arrays are often used to represented dimensionally structured data. Packtype's syntax allows any type to be arrayed with an arbitrary number of dimensions and dimension sizes. The base type can be a simple scalar, or can reference a more complex type like a struct or union, you can even reference another multi-dimensional array!
Example
The Packtype definition can either use a Python dataclass style or the Packtype custom grammar:
As rendered to SystemVerilog
Warning
The order of dimensions is reversed when compared to declaring a packed
multi-dimensional array in SystemVerilog. For example scalar[4][5][6]
declares a 6x5 array of 4-bit elements, which in SystemVerilog would be
written logic [5:0][4:0][3:0]
. This is done to make it easier to parse the
syntax, as decisions can be made reading left-to-right.
Helper Properties and Methods
Struct definitions expose a collection of helper functions for properties related to the type:
<ARRAY>._pt_width
- property that returns the bit width of the entire array;<ARRAY>._pt_pack()
- packs all values contained within the array into a singular integer value (can also be achieved by casting to an int, e.g.int(<ARRAY>)
);<ARRAY>._pt_unpack(packed: int)
- unpacks an integer value into the entries of the array;len(<ARRAY>)
- returns the size of the outermost dimension of the array;<ARRAY>[X]
- accesses element X within the array, which may return either an instance of the base type or another packed array depending on the number of dimensions.