What you practice
typedef and enum (1.1). Declare and use typedef enums for FSM state names instead of raw integers. It is in Aggregate Data Types — Part 1 · Design in SV of the SystemVerilog Masterclass. Synthesizable SystemVerilog that removes Verilog ambiguity and cuts boilerplate.
The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.
What you will learn
This lesson teaches how to declare user-defined types and strongly typed enumerations in SystemVerilog to replace error-prone magic numbers in state machines.
Learning objectives
- Declare a user-defined type using
typedef. - Define a strongly typed enumeration using
enum. - Use descriptive state names instead of raw bit patterns.
- Understand how
typedef enumimproves code readability and maintainability.
Theory
SystemVerilog allows defining custom types for better abstraction and type safety.
| Feature | Syntax | Purpose |
|---|---|---|
typedef | typedef <type> <alias>; | Create an alias for an existing type |
enum | enum {A, B, C} type_name; | Define a set of named constants |
| Combined | typedef enum {A,B,C} type_name_t; | Create a named enum type |
Using typedef enum ensures all state variables share the same type and prevents accidental assignment of invalid values.
Practice
- Open this chapter in RtlStudio; files are preloaded in the workspace.
- Edit
student/rtl/design.svto complete thetypedef enumdeclaration. - Run Simulation and check the Log panel for state transition logs.
- Confirm the output matches expected
INFO:lines.
Summary
You practiced declaring a strongly typed enum using typedef enum {STATE_A, STATE_B, STATE_C, STATE_D} state_t; to replace ambiguous numeric literals with descriptive state names. By using the .name() method in simulation, you observed human-readable state transitions in the log output, improving code clarity and reducing the risk of invalid state assignments. This approach enforces type safety and makes the design self-documenting by clearly expressing intent through named constants. Synthesis preserved the enum structure, confirming that the RTL correctly maps descriptive states to hardware logic.
Run it in the Verilog IDE
- Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
- Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
- Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
- Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.
The lesson is done when the simulation matches typedef and enum. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.