Skip to main content

SystemVerilog Enum Data Type

· loading · loading · ·
Hardware Design Verification SystemVerilog Enum FSM RTL Design Testbench Debugging
Hardware Design Verification
Axolot Logic
Author
Axolot Logic
Digital Design Engineer
Table of Contents
SystemVerilog Design Series - This article is part of a series.
Part 5: This Article

๐ŸŽฏ What Is enum?
#

In SystemVerilog, an enum is a data type used to group named constant values. It improves code readability, reduces error risk, and simplifies debugging.

enum {RED, GREEN, BLUE} led_color;

๐Ÿง  Why Use enum?
#

With parameterWith enum
parameter RED = 0;enum {RED, GREEN, BLUE}
Only numeric values visibleNamed constants improve clarity
No type safetyVariable only accepts enum values
Hard to manage defaultEasier to detect invalid cases

โœ… Basic Usage
#

module led_ctrl;
  enum {RED, GREEN, BLUE} color;

  initial begin
    color = GREEN;
    if (color == RED)
      $display("Red selected!");
  end
endmodule

๐Ÿ”„ By default, values are assigned as RED = 0, GREEN = 1, BLUE = 2. You can assign your values : enum {RED = 5, GREEN =7 , BLUE} color; // blue default get 1 after value befor so 8.


๐Ÿงฐ Enum Methods (Built-in)
#

SystemVerilog enums support several helpful built-in methods:

MethodDescription
first()Returns the first enum value
last()Returns the last enum value
next(value)Returns the next value after value
prev(value)Returns the previous value before value
num()Returns the number of enum elements
name(value)Returns the string name of the enum constant

๐Ÿง  These methods are simulation-only and not synthesizable.


๐Ÿงช Example: Printing All Enum Constants
#

module enum_list_example;
  enum {IDLE, READ, WRITE, ERROR} state;

  initial begin
    repeat(state.num()) begin
      $display("State = %s", state.name());
    end
  end
endmodule

๐Ÿ“ค Output:
#

State = IDLE
State = READ
State = WRITE
State = ERROR

This technique allows printing all enum constant names in a readable way to the terminal or log file.


โš ๏ธ Things to Keep in Mind
#

  • Methods like name() are only for simulation and not synthesizable.
  • For broader reuse, defining enum types with typedef is recommended โ€” to be covered in the next article.

๐Ÿ“Œ Summary
#

  • enum allows you to define constants that are safer and more readable.
  • A better alternative to raw parameter definitions.
  • Methods like name() enhance testbench logging and debugging.
  • Usable in RTL, but some features are only supported during simulation.
module enum_demo_tb;

  typedef enum logic [1:0] {
    RED,
    GREEN,
    BLUE
  } color_t;

  color_t led_color;
  color_t temp;

  initial begin
    // Basic usage
    led_color = GREEN;
    if (led_color == RED)
      $display("๐Ÿ”ด Red selected!");
    else if (led_color == GREEN)
      $display("๐ŸŸข Green selected!");
    else
      $display("๐Ÿ”ต Blue selected!");

    // Enum methods
    $display("\n๐Ÿงช Enum Built-in Methods:");
    temp = led_color.first();
    $display("First value  : %s", temp.name());
    temp = led_color.last();
    $display("Last value   : %s", temp.name());
    $display("Total enums  : %0d", led_color.num());

    temp = led_color.next(RED);
    $display("Next of RED  : %s", temp.name());

    temp = led_color.prev(BLUE);
    $display("Prev of BLUE : %s", temp.name());

    // Print all enum constants
    $display("\n๐Ÿ“‹ Listing all enum constants:");
    for (int i = 0; i < led_color.num(); i++) begin
      temp = color_t'(i);  // Explicit cast required
      $display("Color %0d = %s", i, temp.name());
    end
  end

endmodule
# ? Green selected!
# 
# ? Enum Built-in Methods:
# First value  : RED
# Last value   : BLUE
# Total enums  : 3
# Next of RED  : GREEN
# Prev of BLUE : BLUE
# 
# ? Listing all enum constants:
# Color 0 = RED
# Color 1 = GREEN
# Color 2 = BLUE

๐Ÿ”– Note
#

When used with methods like name(), enums are extremely useful in debugging and logging โ€” especially in FSMs and control signal definitions.

SystemVerilog Design Series - This article is part of a series.
Part 5: This Article

Related

SystemVerilog Tasks and Functions
· loading · loading
Hardware Design Verification SystemVerilog Tasks Functions RTL Design Testbench Reusability
Hardware Design Verification
SystemVerilog Loops and Control Flow โ€“ for, while, foreach, repeat, break
· loading · loading
Hardware Design Verification SystemVerilog Loops Control Flow Testbench RTL Design Break/Continue
Hardware Design Verification
SystemVerilog Clocking Block โ€“ Timing Control for Testbenches
· loading · loading
Verification Hardware Design SystemVerilog Clocking Block RTL Design Testbench UVM Timing
Verification Hardware Design
SystemVerilog Data Types
· loading · loading
Hardware Design Verification SystemVerilog Verilog RTL Design Data Types Synthesis Simulation
Hardware Design Verification
SystemVerilog Logic Data Type
· loading · loading
Hardware Design Verification SystemVerilog Logic RTL Design Verilog Synthesis Net vs Variable
Hardware Design Verification
SystemVerilog Arrays
· loading · loading
Hardware Design Verification SystemVerilog Arrays Packed Dynamic Arrays Queues Testbench
Hardware Design Verification