2013-07-17 342 views
2

有没有办法将字符串强制枚举在系统verilog中?铸造字符串枚举

typedef enum {ABC1,ABC2,ABC3} abc; 

program Test ; 
    abc x1; 
    string teststring; 
    initial 
    begin 
     teststring="ABC2"; 
     x1=abc'(teststring); // Static cast doesn't work 
     $display("[%s][%0d]",teststring,x1); 
    end 
endprogram 

回答

2

铸造是由价值,而不是名称。假设您使用默认的枚举数据类型(int)并且不将值分配给任何标识符,那么ABC1,ABC2ABC3的值分别为0,12(所有类型为int)。

施放此效果的enum与将其施放到int大致相同。 int'("ABC2") == 32'h41424332,并且与任何枚举标识符的值不匹配。

选项,从而获得所需的功能:

  1. 使通过这样的例子不胜枚举,并比较名称的功能:

    function abc a2e(input string s); 
        a2e = a2e.first; 
        repeat(a2e.num) begin 
         if(a2e.name == s) return a2e; 
         else a2e = a2e.next; 
        end 
        assert(0) else $error("Identifier '%s' not in enum abc",s); 
    endfunction 
    

    更多关于枚举的方法:IEEE Std 1800-2012部分6.19.5

  2. 关联数组查找:(请参见IEEE Std 1800-2012第7.8节& 7.9)

    abc lookup[string]; 
    ... 
    x1 = abc.first; 
    repeat(x1.num) begin 
        lookup[x1.name] = x1; 
        x1 = x1.next; 
    end 
    ... 
    teststring="ABC2"; 
    /* Without protection: Non-match creates new entry for with the value 
        of abc.first (or '{default:???} if specified) and returns the value */ 
    x1 = lookup[teststring]; 
    
    // With protection 
    if (lookup.exists(teststring)) 
        x1= lookup[teststring]; 
    else 
        assert(0) else $error("Identifier '%s' not in enum abc",teststring); 
    
  3. 如果枚举标识符长度为1到4个字符,那么值无关紧要,请将名称作为值。 typedef enum {ABC1="ABC1",ABC2="ABC2",ABC3="ABC3"} abc;

    • 要长写?试试等值的:

      typedef enum {ABC[1:3]="ABC1"} abc; 
      
    • 需要更多4个字符?分配数据类型。

      typedef enum bit[5*8-1:0] {ABC[10:19]="ABC10", ABC[20:29]="ABC20"} abc; 
      
    • 更多关于枚举类型范围:IEEE Std 1800-2012部分6.19.2


注:上述全部功能,因为IEEE标准1800至2005年,这个版本的现有必须购买LRM才能阅读。 2012版本免费提供IEEE,因此引用了该版本。