【问题标题】:Can SystemVerilog enums be null?SystemVerilog 枚举可以为空吗?
【发布时间】:2013-12-17 23:49:18
【问题描述】:

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

我应该先检查null 吗?

【问题讨论】:

    标签: enums system-verilog


    【解决方案1】:

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

    引用IEEE std 1800-2012 § 6.19.5.6 Name()

    name() 方法返回给定枚举值的字符串表示形式。如果给定值不是枚举成员,name() 方法返回空字符串。

    【讨论】:

      【解决方案2】:

      不,您无需担心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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-27
        • 1970-01-01
        • 2012-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多