How to access registers/fields by name

In my DUT I have multiple instances of the same register types (i.e. control_reg_0, control_reg_1,…) and I want to use a loop construct in my test to access the registers (and in some cases fields). I have tried using the following syntax, but get compile errors:

for (int i=0; i < 8; i++) begin
slave_rm.get_reg_by_name($sformatf(“control_reg_%0d”, i)).write(status, data, .parent(this));
end

Similarly, if I want to loop through different fields in the same register, I get errors:

for (int i=0; i < 8; i++) begin
slave_rm.status.($sformatf(“m_active%0d”, i)).read(status, data, .parent(this));
end

As a workaround to this error, do I need to get the registers and fields and put them in an array?:

uvm_reg control_regs;
uvm_reg_field status_fields;
for (int i=0; i < 8; i++) begin
control_regs[i] = slave_rm.get_reg_by_name($sformatf(“control_reg_%0d”, i));
control_regs[i].write(status, data, .parent(this));
status_fields[i] = slave_rm.get_field_by_name($sformatf(“m_active%0d”, i));
status_fields[i].read(status, data, .parent(this));
end

Are there any other ways to do this?

I’ve not tried this, but I think you’ll find that the first approach would work if you modified it to:

uvm_reg tmp;

for (int i=0; i < 8; i++) begin
tmp = slave_rm.get_reg_by_name($sformatf(“control_reg_%0d”, i));
tmp.write(status, data, .parent(this));
end

This should work for the fields as well, although you will need to use uvm_reg_field as the type for tmp.

In reply to mperyer:

I ended up declaring arrays for all the registers and fields that I need to access in my base sequence class. This actually worked out quite well because I have numerous tests/sequences that require accessing registers/fields by name. My DUT has multiple instances of the same register type, and using the arrays makes it easy to loop through any number of register instances.