Learning BSV‎ > ‎Data Types‎ > ‎



An enum defines a new type with a set of scalar values with symbolic names.


 typedef enum {Identifier1, Identifier2, ... IdentifierN} EnumName
     deriving (Eq, Bits);

  • Enum label identifiers must begin with Uppercase letters
  • Enum labels can be repeated in different enum definitions
  • The default decoding of labels is 0,1,2...,  using just enough bits to encode the entire set
  • deriving Bits allows this structure to be used and converted to type Bits.  This means it can be used by Reg#() and all data functions and types that use Bit as an input.
  • deriving Eq allows this type to be compared with ==.  Just do a straight compare of all the bits in each data bus.


 typedef enum{Green, Yellow, Red} TrafficLight deriving (Eq, Bits);
 typedef enum{Reset, Count, Decision} States deriving (Eq, Bits);

Using Enums

  • A defined enum type is used like any other type: to declare variables, function/module parameters, etc.
  • The enum labels are used as constant values of that type

Example: Comparing Enum values

 TrafficLight t1 = Red;
 TrafficLight t2 = Yellow;
   if (c1 == Red)
   if (c1 == c2)