RTL Studio Open IDE

1.1 · SYSTEMVERILOG CLASS

typedef and enum

Declare and use typedef enums for FSM state names instead of raw integers.

Watch on YouTube

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 enum improves code readability and maintainability.

Theory

SystemVerilog allows defining custom types for better abstraction and type safety.

FeatureSyntaxPurpose
typedeftypedef <type> <alias>;Create an alias for an existing type
enumenum {A, B, C} type_name;Define a set of named constants
Combinedtypedef 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

  1. Open this chapter in RtlStudio; files are preloaded in the workspace.
  2. Edit student/rtl/design.sv to complete the typedef enum declaration.
  3. Run Simulation and check the Log panel for state transition logs.
  4. 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

  1. Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
  2. Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
  3. Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
  4. 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.