typedef struct packed {
logic valid;
logic [3:0] opcode;
logic [7:0] tag;
logic [31:0] data;
} request_t;
request_t req;
logic [$bits(request_t)-1:0] req_bits;
assign req_bits = req; // exact packed representation
assign req.data = payload_i;A packed request_t can travel through storage, ports and debug views. $bits(request_t) gives a stable width; an explicit conversion function can own external byte-order mapping. This fragment demonstrates layout and field access, not a fully driven request producer.
The width is 1 + 4 + 8 + 32 = 45 bits. The first declared field occupies the most significant bits: valid[44], opcode[43:40], tag[39:32], data[31:0]. These positions describe a packed value; they do not select which byte an external bus transmits first.
Read the strip left to right, from bit 44 down to bit 0. Each hex digit of the worked values lines up with its field.
Valid is a single bit, so it takes the top bit alone: the leading 1 of 1A3C12345678.
Opcode and tag are whole hex digits (4 and 8 bits), so A and 3C read straight off the value.
Data fills the bottom 32 bits: the last eight hex digits.
// snippet: equal-width views, using request_t above
typedef union packed {
request_t fields;
logic [44:0] bits;
} request_view_t;
function automatic logic [44:0] worked_request();
request_view_t view;
view.fields.valid = 1'b1;
view.fields.opcode = 4'hA;
view.fields.tag = 8'h3C;
view.fields.data = 32'h12345678;
return view.bits; // {1'b1, 4'hA, 8'h3C, 32'h12345678}
endfunction // returns 45'h1A3C12345678The first value is 45'h1A3C12345678. The second decodes to valid=0, opcode=5, tag=E7, data=DEADBEEF. Slice [44], [43:40], [39:32] and [31:0]; no external byte-order assumption is needed.
Type | Synthesizable use | Caution |
|---|---|---|
Packed struct | Named fields over one contiguous vector | Field order defines bit positions |
Packed union | Alternate views of identical bits | No run-time tag; designer owns interpretation |
Unpacked struct/array | Grouped storage or arrays | Port/tool support varies; layout is not a flat bus |
Enum | Named encoded state/opcode | Specify base width and illegal-value behavior |
At IP boundaries, publish the bit layout and use conversion functions if external standards define endianness independently from the struct declaration.
No. All members are views of the same bits; RTL must carry an explicit tag if interpretation changes dynamically.