Hi …
I’m getting below error while generating prime numbers
can you please me?
code:
class data_frame;
rand int a;
constraint c1 { a inside {[1:50]}; }
constraint c2 { prime(a) == 1; }
function int prime(int number);
if(number <= 1)
return 0;
for(int i = 2; i < number; i++)begin
if(number % i == 0)
return 0;
end
return 1;
endfunction
endclass
module test;
data_frame d_f1;
initial begin
d_f1 = new();
repeat(10) begin
if (d_f1.randomize()) begin
$display("Value of a=%0d", d_f1.a);
end
end
end
endmodule
Error signature
Error-[CNST-CIF] Constraints inconsistency failure
testbench.sv, 30
Constraints are inconsistent and cannot be solved.
Please check the inconsistent constraints being printed above and rewrite
them.
My guess is that the constraint solver assigns a random value to “a” before it calls the “prime” function.
A way to work around this is to create a list of prime numbers in the range of interest, add them to a queue, then randomly select one of the primes from the queue.
class data_frame;
int unsigned a;
rand int unsigned idx;
int unsigned primes [$];
constraint c2 { idx < (primes.size() - 1); }
function new;
for (int i=0; i<=50; i++) if (is_prime(i)) primes.push_back(i);
endfunction
function void post_randomize;
a = primes[idx];
endfunction
function bit is_prime (int unsigned number);
if(number <= 1)
return 0;
for(int i = 2; i < number; i++)begin
if(number % i == 0)
return 0;
end
return 1;
endfunction
endclass
module test;
data_frame d_f1;
initial begin
d_f1 = new();
repeat(60) begin
if (d_f1.randomize()) begin
$display("Value of a=%0d", d_f1.a);
end else begin
$display("a= randomize failed");
end
end
end
endmodule
That is correct. Inputs to user defined functions get solved first, then the output of the function is used a constant value. The solver is not required to keep trying other input values to get an output value that meets a constraint on it.