Structs & Unions

Packed structs for headers and registers, unpacked structs as records, unions for two views of the same bits.

A struct groups related fields under one name. A packed struct is laid out as a single bit vector, so it can be assigned from a bus, compared and sent through a port like any vector — ideal for packet headers, register maps and pipeline stage contents.

Parsing a header with a packed struct

typedef struct packed { ... } header_t; — 4 + 4 + 8 + 16 = 32 bits.

h.version is raw[31:28], h.checksum is raw[15:0].

Packed vs unpacked, and unions

typedef struct { logic [7:0] a; logic [7:0] b; } s_t; input s_t in_port // unpacked across a module port typedef struct packed { logic [7:0] a; logic [7:0] b; } s_t;

Packed structs are plain vectors and connect and synthesize everywhere. Unpacked structs across ports work in some tools but not all; use packed ones in RTL.

In typedef struct packed { logic [3:0] hi; logic [3:0] lo; } p_t; which field holds bits 7:4?

hi: the first field occupies the most significant bits.

Can a packed struct contain a string or an int array?

No. Packed structs may contain only packed (bit-vector) types. Use an unpacked struct for records with strings, reals or unpacked arrays.