I’m trying to call a virtual function on an array of parametrized uvm_components. The final step of the puzzle here is to cast the components as their actual type so I can call the function I want on all of them. The components in the array are all from the same class, just parametrized with different values. Some pseudocode to hopefully illustrate what I want to do:
uvm_component components[3] = '{comp_1, comp_2, comp_3};
foreach components[i] begin
// Get parameters somehow
my_type#(parameters) cast_component;
$cast(cast_component,components[i]);
// Call function I want on all components in array
components[i].print();
end
I am using UVM, so perhaps there’s a way to pull the parameters from that, but I don’t know. Perhaps there’s a better way of doing this entirely (introspection?).
I don't think the pseudocode illustrated will work because the "parameters" must not be a variable. "// Get parameters somehow" will only work at run-time however what you need here is to get "parameters" at elaboration-time which is before run-time.
You need not to use a casting operation if "parameters" is not used in the virtual function, otherwise, I am not quite sure what will happen if you do not use a casting opertion.
You should read my DVCon paper that explains how to deal with parameterized classes.
Basically you need to create a common base class that has the (pure) virtual method print. Then each parameterized extension of that base class can have a print method that has the correct parametrization.
Now I remember why I didn’t do that: the component is actually a UVM comparator class, which is defined as a parametrized class. I suppose I could create my own comparator base class to get around this (extending uvm_component directly and ignoring the built-in UVM comparators), but it certainly is a pity that there isn’t another way.