Hellzzar
Hellzzar

Reputation: 195

Access enum name in Systemverilog

I want to be able to retrieve the names for the types in an enumeration without having to actually assign a variable to them. Hence, given an enumeration like this

class my_class;
   typedef enum bit {
      ONE,
      TWO
   } fsm_state_t;
endclass

I know I can access the name of a declared variable like this:

class another_class;
...
my_class::fsm_state_t state = my_class::ONE;
   print(state.name());
...
endclass

Is it possible to access the names of the enum without actually having to declare and assign a variable? What I mean is something like this:

class another_class;
...
   print(my_class::ONE);
   print(my_class::TWO);
...
endclass

Upvotes: 0

Views: 4180

Answers (2)

bcassell
bcassell

Reputation: 46

if someday the type is changed, the compiler notifies that the print must be changed as well.

By simply "using" the enumeration within your code, if it goes away you'll get a compile error. That seems to be what you're duplicating. A more practical duplication would be to value check every enum:

class another_class;
...
   if (my_class::ONE!=0) print("ONE has changed!");
   if (my_class::TWO!=1) print("TWO has changed!");
...
endclass

EDIT: or create a wrapper class for enums

virtual class enum_wrap#(type T);
  static function string name(T obj);
    return obj.name();
  endfunction
endclass

program testbench;
  initial begin
    typedef enum {ZERO, ONE, TWO, THREE} numbers_t;
    $display("ENUM without variable: %s", enum_wrap#(numbers_t)::name(THREE));
  end
endprogram

prints:

ENUM without variable: THREE

Upvotes: 0

dave_59
dave_59

Reputation: 42698

No, built-in methods cannot be called on types.

Upvotes: 2

Related Questions