I would like to control no.of ones and zeros in a particular dynamic array by using constraint?

Hi,

rand bit [3:0] [3:0] p
randc bit [31:0] x;
randc bit [31:0] y;

constraint con1 {p.size == (x + y)/4;}

know i want to control the no.of ones in p( $countones(p)== x*4 ) for this how can i write constraint.

Thanks&regards
Vamshi Krishna B

In reply to vamshikrishnaboosam:

constraint con2 { p.sum() with ($countones(item) ) == x*4; }

In reply to dave_59:

Hi dave,

Thank you very much,but in this scenario i am setting the “x” value from the sequence to particular number and my constraint become getting failed.

ex :
My requirement is in p we have suppose 200 bit positions in that i want “x” no.of ones and “y” no.of zeros.

Thanks,
Vamshi Krishna B.

In reply to vamshikrishnaboosam:

You are going to have to show the complete set of constraint so that we can try to help you. I’m sure you do not want to have x be a 32-bit randc number.

In reply to dave_59:

rand bit [3:0] [3:0] p[];
rand bit [3:0] [31:0] q[]
rand bit [31:0] x;
rand bit [31:0] y;

constraint con1 {p.size == (x+y)/4 ;} //these x and y are fixed values setting from the sequence as 200 and 40 (by using `uvm_do_with(req,x==200;y==40;))
constraint con2 {q.size == x+y)/4 ;}

constraint con3 {foreach(p[i,j,k])
{
if(p[i][j][k] == 1'b1)
{
q[i][j][((k+1)*8 - 1)-:8] inside {0,1,2,4,8,16,32,64,128} ;
}else
{
!(q[i][j][((k+1)*8 - 1)-:8] inside {0,1,2,4,8,16,32,64,128});
}
}
constraint con4 {solve p before q;}
constraint con5 {p.sum with ($countones(item) == x*4);}

//randomization got failed becausep.sum means it will give sum of all the elements but not the number of ones in the array.

The above constraints are wrote in my transaction packet.please help me out to generate no.of ones and no.of zero’s in array should be as per my requirement

ex : bit position 0 1 2 3
                  0 1 1 0 --> 0
                    .
                    .
                    .
                  1 0 0 1 --> 59

the number of ones in the above array is equal to x value it is same for rest of 3 packed arrays.

Please any body can help me to resolve this problem?

Thanks,
Vamshi

In reply to vamshikrishnaboosam:

Hi Vamshi,

First of all i thing " p.sum with some_condition " will not return the sum of the array.
for example :

int count,d = '{5,4,9,1,8,7};
count = d.sum with (item > 4); // output → 4 = sum{1,0,1,0,1,1}

so its return the some of the “TRUE” condition.

Thanks
Anu


rand bit [3:0] [3:0] p;
rand bit [3:0] [31:0] q
rand bit [31:0] x;
rand bit [31:0] y;

constraint con1 {p.size == (x+y)/4 ;} //these x and y are fixed values setting from the sequence as 200 and 40 (by using `uvm_do_with(req,x==200;y==40;))
constraint con2 {q.size == x+y)/4 ;}

constraint con3 {foreach(p[i,j,k])
{
if(p[i][j][k] == 1’b1)
{
q[i][j][((k+1)*8 - 1)-:8] inside {0,1,2,4,8,16,32,64,128} ;
}else
{
!(q[i][j][((k+1)8 - 1)-:8] inside {0,1,2,4,8,16,32,64,128});
}
}
constraint con4 {solve p before q;}
constraint con5 {p.sum with ($countones(item) == x
4);}

//randomization got failed becausep.sum means it will give sum of all the elements but not the number of ones in the array.

The above constraints are wrote in my transaction packet.please help me out to generate no.of ones and no.of zero’s in array should be as per my requirement

ex : bit position 0 1 2 3
0 1 1 0 → 0
.
.
.
1 0 0 1 → 59

the number of ones in the above array is equal to x value it is same for rest of 3 packed arrays.

Please any body can help me to resolve this problem?

Thanks,
Vamshi

Vamshi
you have to cast bit to interger to get total some of 1’s
try below
constraint c2 { p.sum() with (int’(item) ) == x*4; }

Good Luck !
Neel