Hi,
I try to add a function in a constraint, but it doesn’t work. the output is always 0.
class random_a;
rand bit [31:0] a;
function int sum_count(bit[31:0] b);
int count = 0;
for (int i=0; i<31; i++) begin
if (b[i] != b[i+1])
count ++;
end
return count;
endfunction
constraint con_a { sum_count(a) <= 3; }
endclass
if I change the function sum_count to a built_in function, such as $onehot() or $countones(), the constraint can work.
if I change another way, like below
class random_a;
rand bit [31:0] a;
rand int b;
function int sum_count(bit[31:0] b);
int count = 0;
for (int i=0; i<31; i++) begin
if (b[i] != b[i+1])
count ++;
end
return count;
endfunction
constraint con_a { b == sum_count(a); }
endclass
it can display random a and the correct b.
can anyone tell me why I cannot use a sum_count directly in the first example?