Hi,
Is it possible to use user defined functions in a constraint? I tried this simple example below where the system function works while user defined function is not. Why is the difference in behavior since user defined function is not consuming time either?
Example below
class power_of_2;
typedef int unsigned uint;
rand uint p;
int count;
//power of 2 will only have a one set bit
constraint power_of_2{
p >0; p < 40000;
//$countones(p)==1; //This generates random numbers correctly while the setones function do not.
setones(p) == 1;
}
//user defined function to find number of set bits
function uint setones(int a);
uint count=0;
while (a) begin
if (a & 1) begin
count++;
a=a>>1;
end
else
a=a>>1;
end
return count;
endfunction
endclass
module test ();
power_of_2 p;
initial begin
p=new();
repeat (10) begin
p.randomize();
$display(p.p);
end
$display (p.setones(15));
end
endmodule