I have the code for the following constraint:
- Sum of all array elements must be 17
- Array must have at least one element which is multiple of 4.
- Size of an array can be anything
class Example;
rand bit [15:0] my_arr[]; //if I change to 31 here, I see an incorrect array
rand int inx_num;
constraint c1 {my_arr.size() inside {[10:15]};}
constraint c1a {inx_num inside {[0:my_arr.size()]};}
constraint c2 {foreach (my_arr[i]) {
i == inx_num -> my_arr[i] % 4 == 0 && my_arr[i] > 4;
}};
constraint c3 {my_arr.sum() with(int'(item))== 17;}
function void post_randomize();
$display("my_arr = %p \n", my_arr);
endfunction
endclass
module TB;
Example E;
initial begin
E = new();
if(!(E.randomize()))
$display("error");
end
endmodule
In line #2:
rand bit [15:0] my_arr[]
→ gives me the correct array; like this:
my_arr = '{'h0, 'h1, 'h1, 'h2, 'h1, 'h0, 'h0, 'h1, 'h3, 'h0, 'h8}
rand bit [31:0] my_arr[]
→ gives me an incorrect array (constraint c3 not honored)
my_arr = '{'h1420568f, 'h5031fd43, 'h730cfe86, 'h105454e8, 'h4583b052, 'h1c167bce, 'h4b2f6faa, 'h7e643336, 'hd532e50b, 'h3fbca2b6, 'hd82f0210}
Any idea, why?