I am getting below error when I try to use rand variable in a loop.
Method setup_cfg::get_index contains a construct
not supported in constraint functions: reference to a rand variable
‘num_elements’
How to re-write this code to avoid this problem?
Thanks
class setup_cfg extends uvm_object;
`uvm_object_utils(setup_cfg)
rand int unsigned num_elements;
rand bit[2:0] id[];
function new(string name = "");
super.new(name);
endfunction
function int unsigned get_index(bit[2:0] input_id);
for(int i = 0; i < num_elements; i++) begin <-------------- this line gives error
if( (id[i] == input_id) ) begin
return i;
end
end
endfunction
endclass
In reply to S2011:
You didn’t show us how your function was used in a constraint so we can’t show you how to rewrite it. Nevertheless, functions have to have all their input as arguments to the function and their output becomes a state variable. You might have to rewrite your function as a for each loop with implications.
In reply to dave_59:
Here is a simpler version of the constraint where the function is called…
class base_test extends uvm_test;
`uvm_component_utils(base_test)
table_cfg t_cfg[];
setup_cfg s_cfg[];
function new(string name_= "base_test", uvm_component parent = null);
super.new(name_,parent);
endfunction // new
virtual function void build_phase(uvm_phase phase);
super.build_phase(phase);
t_cfg = new[6];
s_cfg = new[7];
endfunction // build_phase
virtual function create_t_cfg(int num);
int t_num;
t_num = num + 1;
if(!t_cfg[num].randomize() with {
t_id[2] == s_cfg[t_num].id[s_cfg[t_num].get_index({3'b010})]; <----- call to function
})
begin
`uvm_fatal(get_type_name(),"randomization failed")
end
endfunction
endclass