SystemVerilog枚举可以为null吗?

问题描述 投票:2回答:2

我正在使用枚举方法name()来打印枚举变量的字符串值。

我应该先进行null检查吗?

enums system-verilog
2个回答
2
投票

name()方法不能返回null。如果值与枚举值不对应,它将返回空字符串。建议进行空字符串检查。

引自IEEE std 1800-2012§6.19.5.6名称()

The name() method returns the string representation of the given enumeration value. If the given value is not a member of the enumeration, the name() method returns the empty string.

1
投票

不,你不需要担心null枚举。但是,如果枚举没有与0对应的状态,则可以取消初始化枚举

例如:

typedef enum {STATE0, STATE1} plain_enum;
typedef enum {VAL0=1, VAL1} special_enum;

module test;

  initial begin
    plain_enum plain;
    special_enum special;
    $display("plain: %s", plain.name());
    $display("special: %s", special.name());
  end

endmodule

返回:

# plain: STATE0
# special: 

因为special_enum没有对应0的状态

在这里编辑/重新运行代码:http://www.edaplayground.com/s/4/647

© www.soinside.com 2019 - 2024. All rights reserved.