string to enum in systemverilog

the syntax of ENUM

  enum {red, yellow, green } color;  

  1. default of data type is “int”
  2. default data value is 0, or set the customized value by user, eg: enum {red = 1, yellow = 100, green=1000} color.
  3. typedef: typedef enum {red, yellow, green}  colors;
  method:
  1. function enum first(): 
  2. function enum last();
  3. function enum next(int unsigned N=1);
  4. function int num(); return the number of enum value
  5. name(), return the string name of data, eg, colors c; c=c.first(); c.name() or c.name: ->  "red"
data typr convert 
  1. if enum data typr is same with converted data type, colors c; c =green; or c = color`(2)
string to enum(data type is int):
there are two ways, but the first is recommended;
1.  use the associative array , eg:
     colors color_a[string];
     colors c;
     
     // initial the array on build phase(new is ok as well)
     function void build_phase(uvm_phase phase);
     super.build_phase(phase);
     c = c.first();
     foreach (c.num()) begin
       color_a[c.name] = c;
       c=c.next();
     end
     endfunction: build_phase

     task run_phase(uvm_phase phase);
        string str = "red";
        `uvm_report_info(get_type_name(), $sformatf("the string is %0s, the enum is %0p", str, color_a[str]),UVM_LOW);
       
     endtask: run_phase

2. redefine the string2enum function;
    virtual function colors string2enum(string str);
       if(str  == null ) return ;
       c = c.first();
       foreach(c.num()) begin
         if(c.name == str) return c;
         else c = c.next(); 
       end
       `uvm_report_info(get_typr_name(), $sformatf("Could not match, the string is %0s", str), UVM_LOW);
    endfunction : string2enum

Please let me know if any problem or any question.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值