I am generating 5 unique elements in an array using values 1,2,3…9 whose sum is greater than 20
I use the constrain “data_sum_c” to constraint the sum of elements and with this constraint all values of data are 0;
Without the constraint “data_sum_c” i am able to generate unique data values. Is my code wrong ?
class pkt;
rand int unsigned data[];
//data elements constraint
constraint data_ele_c {
data.size == 5;
foreach (data[i]) {
data[i] inside {[1:9]};
}
}// data_c
//data elements sum constraint
constraint data_sum_c{
data.sum() >20;
}
constraint data_unique_c {
foreach(data[i])
foreach (data[j])
if(i!=j)
data[i]!=data[j];
}
function void print();
foreach(data[i])
$write("\n DATA[i]=%0d, ", i,data[i]);
$display("\n =============================");
endfunction
endclass : pkt
//===============================================
module top;
pkt pkt1;
int count;
initial begin
repeat(3) begin
pkt1 = new;
if(!pkt1.randomize()) $display("\n ****FAILED**** \n");
pkt1.print();
end //repeat
end
endmodule
Hi georgean,
I would like to share my knowledge when you use a constraint on sum().
For ex:
rand bit [2:0] a;
Constraint on the sum of a is less than or equal to 7 and size of a is 3.
now there is no wonder if you see the following values of a:
a[0] = 'h7, a[1] = 'h7, a[2] = 'h7
Reason is the lower 3 bits of a.sum() is 5 and the constraint is still valid!
Solution would be to use one extra bit when declaring “a” i.e. declare it as, “rand bit [3:0] a” and constrain the MSB of a i.e. a[3] = 0. Now you can see valid values on a.
And you would have to add at least 2 bits to catch the error with the given constraints.
What you are trying to explain is that the result of the array.sum() method only has the precision of the individual array elements. But there is no need to add extra bits to the array, you can use the with clause to cast the elements to a greater width.
In reply to georgean:
Your code worked on EDAPlayground on VCS. You should contact your tool vendor.
P.S. You could have also used unique for your uniqueness constraint:
constraint data_unique_c {
unique { data };
}
Hi Timi:
I have used the unique constraint as your advice
but I got error log as blow:
*System verilog keyword ‘unique’ is not expected to be used in this context.
*
And I check the [SystemVerilog 3.1a
Language Reference Manual] , did not find discription about this.
Could you tell me where I can get any more detail about this unique constraint?
In reply to naveensv:
Naveen,
I think you meant to declare a dynamic array:
rand bit [2:0] a[];
And you would have to add at least 2 bits to catch the error with the given constraints.
What you are trying to explain is that the result of the array.sum() method only has the precision of the individual array elements. But there is no need to add extra bits to the array, you can use the with clause to cast the elements to a greater width.
a.sum(item) with ( 32'(item) )
Hi Dave,
My requirement is:
array size should be 4
Each element should be divisible by 8
Sum of all elements should be equal to 40
When I run the below code I am getting constraint conflicting error:
module tb;
class sum_array;
rand int unsigned arr[];
constraint arr_c { arr.size() == 4;
foreach(arr[i]) { (arr[i]%8) == 0; }
constraint sum_c { arr.sum() with (32'(item)) == 40 ; }
}
endclass
initial
begin
sum_array c1;
c1 = new;
void'(c1.randomize());
end
endmodule
I am not seeing any constraint conflicts. I changed the code a little, as below
module tb;
class sum_array;
rand int unsigned arr[];
constraint arr_c { arr.size() == 4;
foreach(arr[i]) { (arr[i]%8) == 0; } }
constraint sum_c { arr.sum() with (36'(item)) == 36'h0_0000_0028 ; } //hex equivalent of dec 40
//added extra bits to make sure, it is the sum that is 40, not the sum after overflow
endclass
initial
begin
sum_array c1;
c1 = new;
void'(c1.randomize());
$display("%p",c1.arr);
end
endmodule
I see following output over diff runs
'{'h18,'h8,'h8,'h0}
'{'h0,'h0,h28,'h0}
'{'h0,'h28,'h0,'h0}
'{'h0,'h8,'h20,'h0}
In reply to muneebullashariff:
I am not seeing any constraint conflicts. I changed the code a little, as below
module tb;
class sum_array;
rand int unsigned arr[];
constraint arr_c { arr.size() == 4;
foreach(arr[i]) { (arr[i]%8) == 0; } }
constraint sum_c { arr.sum() with (36'(item)) == 36'h0_0000_0028 ; } //hex equivalent of dec 40
//added extra bits to make sure, it is the sum that is 40, not the sum after overflow
endclass
initial
begin
sum_array c1;
c1 = new;
void'(c1.randomize());
$display("%p",c1.arr);
end
endmodule
I see following output over diff runs
'{'h18,'h8,'h8,'h0}
'{'h0,'h0,h28,'h0}
'{'h0,'h28,'h0,'h0}
'{'h0,'h8,'h20,'h0}
Hi,
Many thanks for your reply.
The above mentioned solution is working perfect with questa and Synopsis tools.
The constraint conflict is seen in Cadence tool.