简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM