the syntax of ENUM
enum {red, yellow, green } color;
- default of data type is “int”
- default data value is 0, or set the customized value by user, eg: enum {red = 1, yellow = 100, green=1000} color.
- typedef: typedef enum {red, yellow, green} colors;
method:
- function enum first():
- function enum last();
- function enum next(int unsigned N=1);
- function int num(); return the number of enum value
- name(), return the string name of data, eg, colors c; c=c.first(); c.name() or c.name: -> "red"
data typr convert
- 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.