Hi,
I have tried to code for the scenario and found below is not working (always first 5 bits are set to 1) when set_consctv is 1. Please let me know the issue.
class a;
rand bit[99:0] var1;
rand bit set_consctv;
constraint c_var1{
$countones(var1) == 5;
foreach(var1[i]){
if(var1[i] && set_consctv && i<=95){
{var1[i],var1[i+1],var1[i+2],var1[i+3],var1[i+4]} == 5'b11111;
}else if(var1[i] && !set_consctv && i<=95){
{var1[i],var1[i+1],var1[i+2],var1[i+3],var1[i+4]} != 5'b11111;
}
}
}
constraint c_set_consctv{
set_consctv dist {1:=80, 0:=20};
}
endclass
module test;
a a_h;
initial
begin
a_h = new;
repeat(20) begin
void'(a_h.randomize());
$display("a_h.set_consctv:%0b a_h.var1:%25h",a_h.set_consctv,a_h.var1);
end
end
endmodule
class cards;
rand bit[99:0] ab;
rand int N;
bit [4:0] cd = 5'b11111;
constraint ab_c {N inside {[0:90]};
ab == cd << N;
}
function void post_randomize();
ab[99:95] = 5'b11111;
endfunction
endclass
module tb;
cards c1;
initial begin
c1 = new();
repeat (5) begin
assert (c1.randomize());
$display ("%b ***** N value is %d",c1.ab,c1.N);
end
end
endmodule
It might help to shrink your code into something more manageable, like 3 consecutive bits in a 6-bit vector. Then unroll the foreach constraints manually.
class a;
rand bit[5:0] var1;
constraint c_var1{
$countones(var1) == 3;
foreach(var1[i]){
if(var1[i] && i<=3)
{var1[i],var1[i+1],var1[i+2]} == 3'b111;
}
}
endclass
module test;
a a_h;
initial
begin
a_h = new;
repeat(20) begin
void'(a_h.randomize());
$display("a_h.var1:%6b",a_h.var1);
end
end
endmodule
Then unroll the foreach loop into 6 set of constraints
Hi, Could you explain the question?
I got the first part like it needs to have 5 consecutive bits as set-bits. But what about 80%? What does that mean?
In reply to dave_59:
Thanks. I got it and I tried to make a few changes in verif_eng_20’s code.
class a;
rand bit[99:0] var1;
rand bit set_consctv;
rand int start_point;
constraint c_var1{
$countones(var1) == 5;
start_point inside {[0:95]};
foreach(var1[i]){
if(set_consctv && i==start_point && i<=95){
{var1[i],var1[i+1],var1[i+2],var1[i+3],var1[i+4]} == 5'b11111;
}else if(!set_consctv && i<=95){
{var1[i],var1[i+1],var1[i+2],var1[i+3],var1[i+4]} != 5'b11111;
}
}
}
constraint c_set_consctv{
set_consctv dist {1:=80, 0:=20};
}
endclass
module test;
a a_h;
initial
begin
a_h = new;
repeat(20) begin
void'(a_h.randomize());
$display("a_h.set_consctv:%0d , a_h.start_point:%0d , a_h.var1:%b",a_h.set_consctv,a_h.start_point,a_h.var1);
end
end
endmodule
Here I have used a rand property start_point which will decide from where We’ll be getting 1s. In the constrait section, I made it very clear that the value of start_point lies between 0 and 95. Coming to the foreach loop, in the if-condition, I had to mention i<=95. Or it was giving garbage value.
I am not understanding why it happened.
Hello Sir, My doubt is not regarding what you explained. If you analyze the following code you will see that I have used a start property and the value is between 0 to 3. Still, I had to mention i<=3 along with i==start.
class a;
rand bit[5:0] var1;
randc int start;
constraint condition{start inside{[0:3]};}
constraint c_var1{
$countones(var1) == 3;
foreach(var1[i]){
if(i==start) // Here I will have to use i<=3 to get proper output
{var1[i],var1[i+1],var1[i+2]} == 3'b111;
else if(i<start)var1[i] == 0;
else if(i>start && i-start > 2)var1[i] == 0;
}
}
endclass
module test;
a a_h;
initial
begin
a_h = new;
repeat(20) begin
void'(a_h.randomize());
$display("a_h.var1:%6b",a_h.var1);
end
end
endmodule
Here for var1[4+2], it’s getting out of bound error. Now my question is when the start is inside 0-3, for i=4 why will the statement be executed?
I hope I explained where I am having doubts pretty well.
This is because the foreach loop gets unrolled before a value for start gets chosen. And an if expression in a constraint is not procedural code, it is another way of writing the Boolean expression
In order for this to work (in Dave’s solution to generate var1), I think following code change is needed
i <=95 → var1 != 5’b11111 << N;
change to:
i <=95 → var1 != 5’b11111 << i;
Individual array elements can be constrained, in which case the index expression may include iterative constraint loop variables, constants, and state variables.