I'm trying to create an array with a constraint on the total number of array elements less than given value. For example, I want to generate a random array where the total number of elements less than 4 is no more than 6. The way I implement it is:
function automatic int get_small_vals(input bit [3 : 0] bit_vector [64]);
automatic bit [3 : 0] q [$];
q = bit_vector.find(x) with (x < 4'd4);
return q.size();
endfunction
class InputData;
rand bit [3 : 0] abs_vals [64];
constraint no_more_than_p_small_values {
get_small_vals(abs_vals) < 6;
}
function void display();
foreach (abs_vals[i]) $write("%1h", abs_vals[i]);
$write("\n");
endfunction
endclass
initial begin
InputData rels;
rels = new();
rels.randomize();
rels.display();
#100;
$stop;
end
I see in the console (Vivado 2018.3 64bit) that there are more than 6 values which are less than 4:
WARNING: File: C:/my_path/unit_tb.sv Line: 69 : randomize failed to meet constraint.
902ed8fb3af155c14a5402c092709a999d7f0b2adbd6e7d6d698a7d5c2b5baa7
I can't understand what's the problem. It works with 'equal', but not with 'less than':
function automatic int get_small_vals(input bit [3 : 0] bit_vector [64]);
automatic bit [3 : 0] q [$];
q = bit_vector.find(x) with (x == 4'd4);
return q.size();
endfunction