在 Systemverilog 中访问枚举名称

Access enum name in Systemverilog

我希望能够检索枚举中类型的名称,而不必实际为它们分配变量。因此,给定这样的枚举

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

我知道我可以像这样访问已声明变量的名称:

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

是否可以访问枚举的名称而无需实际声明和分配变量?我的意思是这样的:

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

不,不能对类型调用内置方法。

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

通过简单地在您的代码中“使用”枚举,如果它消失了,您将得到一个编译错误。这似乎是你正在复制的东西。更实用的重复是对每个枚举进行值检查:

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

编辑:或为枚举

创建一个包装器class
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

打印:

ENUM without variable: THREE