how to constraint the following?
1)constraint for size of queue to be between a to b. Also write a constraint for the another queue values being in range m to n.
also randomize the same.
module da;
class cons;
//rand used to randomize the properties
rand bit [7:0]que1[$];
rand int que2[$];
//writing constraint for que1 & que2
constraint constraint_1{1<que1.size()<10;}
constraint constraint_2{foreach(que2[i]) inside {[150:200]};}
//print all randomized que elements with constraint passing
task print();
$display("que1 is %0p",que1);
$display("que2 is %0p",que2);
endtask:print
endclass:cons
initial
begin
cons cons_h;
cons_h=new();
//do call print after randomization
cons_h.randomize();
cons_h.print();
end
endmodule:da
i tried this but i got error saying that {near “inside”: syntax error, unexpected “SystemVerilog keyword ‘inside’”}
how to reso;ve this.
In reply to Nitin Jalibenchi:
class test;
rand int q1[$];
constraint c1 {
q1.size() > 5 && q1.size < 10;
}
constraint c2 {
foreach(q1[i]) {
q1[i] > 20 && q1[i] < 30;
}
}
function void print();
$display(q1);
endfunction
endclass
module abc;
test t1 = new();
initial begin
t1.randomize();
t1.print();
end
endmodule
I am using the same queue (q1) to demonstrate the constraint on size & each element. Hope this helps.
In reply to Nitin Jalibenchi:
Please use code tags making your code readable. I have added them for you.
The constraint {1 < que1.size() < 10;} does not do what you think it does. Please see this link. And without a constraint on the size of que2, it remains an empty queue after randomization.
The syntax of your foreach constraint is incorrect. See how it should be written below.
module da;
class cons;
//rand used to randomize the properties
rand bit [7:0] que1[$];
rand int que2[$];
//writing constraint for que1 & que2
constraint constraint_1{que1.size() inside {[1:10]};}
constraint constraint_2{que2.size() inside {[1:10]};}
constraint constraint_3{foreach(que2[i]) que2[i] inside {[150:200]};}
//print all randomized que elements with constraint passing
function void print();
$display("que1 is %0p",que1);
$display("que2 is %0p",que2);
endfunction:print
endclass:cons
initial repeat(10)
begin
cons cons_h;
cons_h=new();
//do call print after randomization
assert(cons_h.randomize());
cons_h.print();
end
endmodule:da