Hi,
I have the below code where i am expecting the sum of the arrays to be 20 but when i run the sum is not 20.
class A;
rand bit [2:0] a [10];
// int sum =100;
//constraint c {
//(( (0<3)? a[0]:0) + (1<3)? a[1]:0) + (2<3)? a[2]:0) + ... (9<3)? a[8]:0) )) == 10;
//}
// constraint c {
// a.sum with ((item.index<4)? int'(item):0) ==10;
//}
constraint b { foreach (a[i])
a.sum with (item.index(i) ? int'(item) : 0) == 20;
}
endclass
module tb;
A a1;
initial begin
a1=new();
// a1.c.constraint_mode(0);
if (!a1.randomize())
$display("Error");
$display ("%p",a1.a);
end
endmodule
Actual output: '{3, 7, 1, 0, 1, 0, 2, 5, 3, 1}
Question 1: what does the item.index(i) consider true to select item in this case?
Question 2: why is the output not 20?
But if i change the constraint to this i am getting the expected output.
constraint b { foreach (a[i])
a.sum with (1 ? int'(item) : 0) == 20;
}
constraint b { foreach (a[i])
a.sum with (item.index(i) ? int'(item) : 0) == 20;
}
in the above constraint you dont have to pass “i” to the item.index() method. The input to the item.index is ‘dimension’. As you are dealing with single dimension array you dont have to pass the value ‘i’.
constraint b { foreach (a[i])
a.sum with (item.index() ? int'(item) : 0) == 20;
}
constraint b { foreach (a[i])
a.sum with (int'(item)) == 20;
}
Also the difference between these two constraints is the second constraint is summing all the elements, where as the first one is summing all except the zeroth.