typedef, struct, union and enum
Classic Verilog models all data as flat bit vectors or loose scalar wires.
This approach lacks semantic grouping and increases the likelihood of wiring mistakes.
SystemVerilog provides structured data types: enum, struct, union, and typedef.
These constructs provide strict type checking and self-documenting RTL code.
Enumerations: Strong Type Safety
Section 5.6 used localparam declarations to assign symbolic names to binary state values.
SystemVerilog replaces manual parameters with the enum construct:
typedef enum logic [1:0] {
IDLE = 2'b00,
ACTIVE = 2'b01,
DONE = 2'b10,
ERR_ST = 2'b11
} state_t;
state_t state, next_state;
The enum keyword associates symbolic names with specific binary encodings.
SystemVerilog enforces strict type safety on enumerated variables.
Assigning an arbitrary bit vector such as state <= 2'b11 triggers a compile error.
Only defined enumeration labels can be assigned without explicit type casting.
Designers retain full control over physical hardware encodings. You can specify binary, one-hot, or Gray encodings:
typedef enum logic [3:0] {
S_RED = 4'b0001,
S_GREEN = 4'b0010,
S_YELLOW = 4'b0100,
S_PED = 4'b1000
} traffic_state_t;
always_comb begin
next_state = state;
unique case (state)
S_RED: if (timer_done) next_state = S_GREEN;
S_GREEN: if (timer_done) next_state = S_YELLOW;
S_YELLOW: if (timer_done) next_state = S_PED;
S_PED: if (timer_done) next_state = S_RED;
default: next_state = S_RED;
endcase
end
The unique case construct verifies that exactly one branch executes.
Simulators generate runtime warnings if conditions overlap or remain unhandled.
Packed Structures: Grouping Related Signals
Use struct to group logically related signals into a single named record:
typedef struct packed {
logic [7:0] opcode;
logic [7:0] operand_a;
logic [7:0] operand_b;
logic valid;
} instruction_t;
instruction_t current_instr;
// Access individual fields using dot notation
assign alu_op = current_instr.opcode;
assign valid_now = current_instr.valid;
The packed keyword guarantees that fields form a single contiguous bit vector.
Synthesizers map packed structures directly to hardware buses and registers.
Connecting a packed structure to a module port replaces multiple individual wire connections:
module decode_stage (
input instruction_t instr,
output logic [7:0] alu_result
);
// Reference fields directly as instr.opcode and instr.operand_a
endmodule
Unions: Alternative Bit Representations
A struct allocates separate bits for each declared field.
In contrast, a union overlays multiple fields onto the same underlying storage bits:
typedef union packed {
logic [31:0] raw_word;
struct packed {
logic [15:0] high_half;
logic [15:0] low_half;
} halves;
} status_reg_t;
status_reg_t status;
assign status.raw_word = 32'hDEAD_BEEF;
// status.halves.high_half reads 16'hDEAD
// status.halves.low_half reads 16'hBEEF
Use packed unions when a register requires access as a whole word or as individual bitfields.
Reusable Type Definitions with typedef
The typedef keyword creates custom named types that can be shared across modules.
Declare project-wide types inside SystemVerilog packages.
Modules import these packages to ensure identical bus definitions throughout the hierarchy.
This practice eliminates integration defects caused by mismatched bit-slicing indices.
Practice Exercise
Convert the following state declarations to a SystemVerilog enum.
Preserve the specified one-hot binary encoding.
localparam [2:0] S_IDLE = 3'b001, S_LOAD = 3'b010, S_DONE = 3'b100;
reg [2:0] state, next_state;
Solution
typedef enum logic [2:0] {
S_IDLE = 3'b001,
S_LOAD = 3'b010,
S_DONE = 3'b100
} state_t;
state_t state, next_state;
This code preserves the one-hot bit patterns while applying strong type safety.
The compiler prevents arbitrary numeric assignments to state and next_state.
Enumerations ensure type safety and eliminate arbitrary integer assignments.
Packed structures group related signals into coherent hardware bit vectors.
The typedef construct enables modular type reuse across complex designs.
The next article introduces SystemVerilog interfaces and modports for interconnect management.