In reply to dave_59:
Thanks Dave, I got what you mean.
Yes the question indeed is a “Y” part of a solution, so let me try to explain the original “X” problem.
In UVM command line, you can set enumeration value by “+uvm_set_config_int”, like below.
// trivial code omitted
typedef enum{MIN, LOW, MEDIUM, HIGH, MAX} level_t;
rand level_t traffic_level;
// then in command line, set traffic level as you wish
+uvm_set_config_int=uvm_test_top,traffic_level,4
My goal is to make it intuitive, when you set a enumeration type, you can use its literal name rather than a number. So in this case user won’t need to look up the enumeration definition to understand what the number stands for.
And here is my plan:
- instead of setting integer value, let’s set string value on command line, with the enum type explicitly typed out
- interpret the special strings, extract the component, field and target variable’s enum type and literal value
- use uvm_enum_wrapper to get the integer value from literal name, and then set it
So far I was able to achieve the first two steps, and stuck in step3, which was my previous “Y” problem.
And the code is something like this:
// in command line
+uvm_set_config_string=uvm_test_top,traffic_level,level_t::MAX
// a intermediate function to convert string to enum
function void string_to_enum();
string comp, field, enum_type, value;
// use UVM command line processor to get the special string and store each part
// after processing the string, I get:
// comp = "uvm_test_top"
// field = "traffic_level"
// enum_type = "level_t"
// value = "MAX"
endfunction : string_to_enum
Now I get everything I needed, but cannot use uvm_enum_wrapper#(type T)::from_name to convert the string to a integer, because I was unable to convert the string to a type as I originally described in the post.
Having said so, I suddenly realize my plan could be impossible because as you said, statically typed language cannot create variable on a dynamic string.
However there might be a way of doing this.