SystemVerilog Enumerations

 ENUMERATIONS / ENUM 

     An enumerated type defines a set of named values. The simplest enumerated type declaration contains a list of constant names and one or more variables.

In the following example, colors is defined to be variable of the unnamed enumerated int type that includes the members red, green, blue, yellow, white, black.

      enum { red, green, blue, yellow, whiteblack } Colors;

     The actual values are default to integers starting at 0 and then increase. in the above example by default variable will get default value of 0,1,2,3,4,5 respectively from  red. 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.

In the following example value is set for red = 0, blue = 4, white = 10.

     enum { red=0, green, blue=4, yellow, white=10black } Colors;

green, yellow, black automatically assigned to the  increment-value of 1,5,11 respectively.
If an automatically incremented value is assigned elsewhere in the same enumeration, this shall be a syntax error.

In the below example yellow will get the increment-value of 5,value of white is set with 5. this will cause the syntax error.

      enum { red=0, green=0, blue=4, yellow, white=5black=6 } Colors

     Defining new data types as enumerated types: 
   
    A type name can be given so that the same type can be used in many places.

     typedef enum {GOOD, BAD} pkt_type;

     pkt_type  pkt_a; // named type

     Enumerated types methods:
          first()     -- >   returns the value of the first member of the enumeration.
          last()     -- >   returns the value of the last member of the enumeration.
          next()    -- >   returns the value of next member of the enumeration.
          next(N)  -- >   returns the value of next Nth member of the enumeration.
          prev()    -- >   returns the value of previous member of the enumeration.
          prev(N)  -- >  returns the value of previous Nth member of the enumeration.
          num()    -- >   returns the number of elements in the given enumeration.
          name()  -- >   returns the string representation of the given enumeration value.


 Example-1 : Enumeration Type [DataTypes] 


     This example shows how to declare enum.


module enum_datatype;
  //declaration
  enum { red, green, blue, yellow, white, black } Colors;

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


 Simulator Output  

Colors :: Value of red is = 0
Colors :: Value of green is = 1
Colors :: Value of blue is = 2
Colors :: Value of yellow is = 3
Colors :: Value of white is = 4

Colors :: Value of black is = 5

        Execute the above code on 


 Example-2 : Enumeration Type [DataTypes] 


     This example shows how to set other than default values to enum.
  
module enum_datatype; 
  //declaration 
  enum { red=0, green, blue=4, yellow, white=10, black } Colors;

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

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

 Simulator Output  

Colors :: Value of  red is = 0
Colors :: Value of  green is = 1
Colors :: Value of  blue is = 4
Colors :: Value of  yellow is = 5
Colors :: Value of  white is = 10

Colors :: Value of  black is = 11

       Execute the above code on