String Search within Hierarchy ? Is it possible in SV?

Hello All,

a. Say I have two strings one is register name and other is field name.

Eg string reg_name = CONTROL;
string field_name = MODE;

Is it possible to search any hierarchy using strings i.e. hello_uvm_reg_model.$reg_name.get_address();

Thought it would be easy if I just pass the register name and the field name, so that I can access the register/field details from the UVM register model using the string name !

Appreciate your help !!

Thanks !

The above is not possible in SV, but class hierarchies like the UVM reg provide string-based lookup for you, by implementing an associated array of class handles keyed by a string under the hood, and normally providing an accessor method:

UVM API:

virtual function uvm_reg uvm_reg_block::get_reg_by_name(string name)
virtual function uvm_reg_field uvm_reg::get_field_by_name(string name)

UVM Usage:

hello_uvm_reg_model.get_reg_by_name(reg_name).get_address();
hello_uvm_reg_model.get_reg_by_name(reg_name).get_field_by_name(field_name).foo();

If you need to use this technique in your own code, something like this would work:

class my_reg_model;
  my_reg regs[string];
  ...
class my_reg;
  my_reg_field fields[string];
  ...
hello_uvm_reg_model.regs[reg_name].get_address() ...
hello_uvm_reg_model.regs[reg_name].fields[field_name].foo() ...

… but to add protection so that you can ensure your data structure does not get altered from the outside, make it protected or local and provide an accessor. That’s what the UVM register model has done here:

class my_reg_model;
  local my_reg regs[string];
  function my_reg get_reg(string name); return regs[name]; endfunction
  ...
class my_reg;
  local my_reg_field fields[string];
  function my_reg_field get_field(string name); return fields[name]; endfunction
  ...
hello_uvm_reg_model.get_reg(reg_name).get_address() ...
hello_uvm_reg_model.get_reg(reg_name).get_field(field_name).foo() ...