HI All,
I want to write a constraint to randomize an array with the following requirements:
a) Sum of all array elements must be 17
b) Array must have at least one element which is multiple of 4.
c) Size of an array could be random
class c;
rand bit[7:0] arr[];
constraint c1_arr_sz { arr.sum() with (int'(item)) == 17;
arr.size() inside {[2:10]}; }
constraint c2_arr_m4 { arr.or() with (item % 4 == 0); }
endclass
module m1;
c obj;
initial
begin
obj=new();
repeat(5)
begin
obj.randomize();
$display("array is %p",obj);
end
end
endmodule
Output of the code:
array is β{arr:β{'h0, 'h3, 'h0, 'h0, 'h0, 'he, 'h0, 'h0, 'h0}}
array is β{arr:β{'h8, 'h8, 'h0, 'h0, 'h0, 'h1}}
array is β{arr:β{'h5, 'h7, 'h1, 'h4}}
array is β{arr:β{'h4, 'h1, 'h7, 'h3, 'h2}}
array is β{arr:β{'h1, 'h0, 'h3, 'h1, 'h0, 'h0, 'hb, 'h0, 'h0, 'h1}}
Multiple of 4, is being satisifed only in 2nd, 3rd and 4th iteration.
But it should be satisfied in 1st and last iteration also, may I know the reason?
How to update the code to get all requirements in all iterations.
Kindly share your opinion
Thank You