๐ฏ 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 parameter | With enum |
---|---|
parameter RED = 0; | enum {RED, GREEN, BLUE} |
Only numeric values visible | Named constants improve clarity |
No type safety | Variable only accepts enum values |
Hard to manage default | Easier 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:
Method | Description |
---|---|
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.