ENUMERATIONS / ENUM Data Type

The simplest enumerated type declaration contains a list of constant names and one or more variables. Which similar like parameter in Verilog in case if declare more parameter are difficult we can use Enum.

By default the labels of an enum are int

The first label in the enum list is represented with a value 0, the second label with 1 and so on.Label values must be unique

Example :

enum { add, sub, mul , div } Opcode;

The default data type for enum is int so the variables declare start from 0; in the above example add=0; sub=1 ,mul=2, div=3, and so on...

Example :

enum { ZERO ,ONE, TWO, THREE , FOUR, FIVE , SIX, SEVEN , EIGHT , NINE ,TEN } Count ;

where the first variable value is 0 and increment by 1
where ZERO = 0 , ONE = 1 , TWO =2 , THREE=3 , FOUR=4, FIVE =5 , SIX=6, SEVEN=7, EIGHT =8 , NINE=9 ,TEN=10 , is called auto assign value to variable

Manul assign values

The values can be set for the names and also values can be set for some of the names and not set for other names. A name without a value is automatically assigned an increment of the value of the previous name.

Example :

enum { ZERO ,ONE, TWO, FIVE =5 , SIX, SEVEN , NINE =9,TEN } Count ;

In the following example value is set for ZERO =0 ,ONE =1 , TWO=2, FIVE =5 , SIX=6, SEVEN=7 , NINE =9,TEN=10

Example :

enum { ZERO ,ONE, TWO, FIVE =5 , SIX, SEVEN=6 , NINE =9,TEN } Count ;

In the below following yellow will get the increment-value of SIX, , value of while is set with SEVEN =6 . this will cause the syntax error.

Enumerated Types

Advantages Of Using Enum :

  • Makes a code portable,since changing a type name is easier at one place of the code rather than all the places.
  • Reduces the length of the complex declaration and hence improves readability.
  • Typecasting is easier.
  • Can be used through pointers and also useful in declaration of arrays.


  • Example 01: Enum_datatype

    module enum_types;
    typedef enum {red, green, blue, yellow, white, black} colors ;
    colors my_colors;
    initial begin
    my_colors = green;
    // my_colors = 1; // invalid need to typecast
    // my_colors = colors'(2); // cast as blue
    case (my_colors)
    red: $display ("my_color is red");
    blue: $display ("my_color is blue");
    default: $display ("my_color is not blue or red");
    endcase
    end
    endmodule

    OUT PUT :

    # ----------------OUTPUT RESULT -----------------------
    # my_color is not blue or red
    # ------------------------------------------------------

    Example 02: Enum_datatype

    module enum_datatype;
    //declaration
    enum { ZERO, ONE, TWO , THREE , FOUR, FIVE,SIX, SEVEN , EIGHT , NINE ,TEN} count;

    //display members of Colors
    initial begin
    count =count.first;

    for(int i=0;i<11;i++) begin
    $display("count:: Value of Enum List %0s \t is = %0d", count.name, count);
    count = count.next;
    end
    end
    endmodule

    OUT PUT :

    # ----------------OUTPUT RESULT -----------------------
    # count:: Value of Enum List ZERO is = 0
    # count:: Value of Enum List ONE is = 1
    # count:: Value of Enum List TWO is = 2
    # count:: Value of Enum List THREE is = 3
    # count:: Value of Enum List FOUR is = 4
    # count:: Value of Enum List FIVE is = 5
    # count:: Value of Enum List SIX is = 6
    # count:: Value of Enum List SEVEN is = 7
    # count:: Value of Enum List EIGHT is = 8
    # count:: Value of Enum List NINE is = 9
    # count:: Value of Enum List TEN is = 10
    # ------------------------------------------------------

    Example 04: Enum_datatype

    module enum_datatype;
    //declaration
    enum { ZERO=0, ONE, FOUR=4, FIVE=5, TEN =10, ElVEN } manuall_assign_to_enum;

    //display members of Colors
    initial begin
    manuall_assign_to_enum = manuall_assign_to_enum.first;
    for(int i=0;i<6;i++) begin
    $display("manuall_assign_to_enum:: Value of %0s \t is = %0d",manuall_assign_to_enum.name,manuall_assign_to_enum);
    manuall_assign_to_enum= manuall_assign_to_enum.next;
    end
    end
    endmodule

    OUT PUT :

    # ----------------OUTPUT RESULT -----------------------
    # manuall_assign_to_enum:: Value of ZERO is = 0
    # manuall_assign_to_enum:: Value of ONE is = 1
    # manuall_assign_to_enum:: Value of FOUR is = 4
    # manuall_assign_to_enum:: Value of FIVE is = 5
    # manuall_assign_to_enum:: Value of TEN is = 10
    # manuall_assign_to_enum:: Value of ElVEN is = 11
    # ------------------------------------------------------

    Example 04: Enum_datatype [ compile error ]

    module enum_datatype;
    //declaration
    enum { ZERO=0, ONE, FOUR=1, FIVE=5, TEN =10, ElVEN } manuall_assign_to_enum;

    //display members of Colors
    initial begin
    manuall_assign_to_enum = manuall_assign_to_enum.first;
    for(int i=0;i<6;i++) begin
    $display("manuall_assign_to_enum:: Value of %0s \t is = %0d",manuall_assign_to_enum.name,manuall_assign_to_enum);
    manuall_assign_to_enum= manuall_assign_to_enum.next;
    end
    end
    endmodule

    OUT PUT :

    # ----------------OUTPUT RESULT -----------------------
    # enum_datatype.sv(5): Enum member 'FOUR' does not have unique value.
    # // compile error
    # ------------------------------------------------------