Example 1:: by Default, Structures Are Unpacked. Unpacked Structures Can Contain Any Data Type
Example 1:: by Default, Structures Are Unpacked. Unpacked Structures Can Contain Any Data Type
Structures
A structure represents a collection of data types that can be referenced as a whole, or
the individual data types that make up the structure can be referenced by name.
By default, structures are unpacked. Unpacked structures can contain any data type.
Structure declarations follow the C syntax, but without the optional structure tags before the “{”. The syntax
for structure declarations is
Eg
struct { bit [7:0] opcode; bit [23:0] addr; }IR; // anonymous structure
// defines variable IR
IR.opcode = 1; // set field in IR.
typedef struct {
bit [7:0] opcode;
bit [23:0] addr;
} instruction; // named structure type
instruction IR; // define variable
Unions
A union is a data type that represents a single piece of storage that can be accessed using one of the
named member data types. Only one of the data types in the union can be used at a time.
By default, a union is unpacked, meaning there is no required representation for how members of the
union are stored.
Dynamic types and chandle types can only be used in tagged unions
Examples:
typedef union { int i; shortreal f; } num; // named union type
num n;
n.f = 0.0; // set n in floating point format
typedef struct {
bit isfloat;
union { int i; shortreal f; } n; // anonymous union type
} tagged_st; // named structure
The term packed array is used to refer to the dimensions declared before the data identifier name.
The term unpacked array is used to refer to the dimensions declared after the data identifier name.
Packed arrays
A packed array is a mechanism for subdividing a vector into subfields, which can be conveniently
accessed as array elements.
packed array is guaranteed to be represented as a contiguous set of bits. An unpacked array may or may
not be so represented
Unpacked arrays
Unpacked arrays can be made of any data type.
Arrays whose elements are themselves arrays are declared as multidimensional arrays