typedef, Enumerations, and Packages
Give types a clear meaning, encode states cleanly, and share declarations without hidden dependencies.
Name a format once
typedef creates a type name. That name can represent a vector, integer, structure, or enumeration.
typedef logic [11:0] adc_sample_t;
typedef logic signed [23:0] accumulator_t;
adc_sample_t sample_a;
adc_sample_t sample_b;
accumulator_t sum;The type communicates more than a repeated [11:0] range. If resolution changes, one declaration needs review. Two type names with the same width can still represent different ideas to the reader.
Readable states
An enumeration associates names with a finite set of values.
typedef enum logic [1:0] {
IDLE,
LOAD,
RUN,
DONE
} state_t;
state_t state_q, state_d;The logic [1:0] base type fixes the representation. Enumeration variables are strongly typed, so an arbitrary assignment can require an explicit cast. This prevents a random value from silently becoming a valid-looking state.
Methods such as first(), last(), next(), prev(), and name() are useful in simulation. Check tool support before relying on them in synthesized RTL.
Share declarations with a package
A package groups common types, constants, functions, and declarations.
package stream_pkg;
parameter int unsigned DATA_WIDTH = 32;
typedef struct packed {
logic valid;
logic last;
logic [DATA_WIDTH-1:0] data;
} stream_word_t;
endpackageUse its content with explicit qualification:
stream_pkg::stream_word_t word;You can also import one name:
import stream_pkg::stream_word_t;
stream_word_t word;The wildcard form import stream_pkg::*; is convenient, but it hides name origins and can create ambiguity. Targeted imports or the :: prefix are often easier to review in a shared codebase.
Compilation order
A package must be compiled before the modules that use it. The build script or file list must reflect this dependency.
Global declarations in $unit depend more heavily on file ordering and on how a tool groups compilation units. A named package is clearer and more portable.
Key points
typedefgives a stable name to a data format.- An enumeration restricts values and makes states readable.
- Choose an enumeration base type deliberately.
- A package shares types, constants, and functions between modules.
package::nameand targeted imports keep dependencies visible.
📝 Test your knowledge - Chapter quiz