30 bit number with constraints

Consider a 30bit number and using constraints, it needs to have 10 1’s at any bit and other bits can be random numbers or the same at any index.

The following or similar constraints shouldn’t be used:

constraint c1 {
	
		       foreach (var1 [i]) {
					
				        if (i inside {[20:29]}) {
											
						var1 [i] == 1;
																	
								}
																	
					if (i inside {[0:19]})  {
											
						var1 [i] == 0;
																		
								}
																	
					}
										
		   }

In reply to Shashank Gurijala:

Your question isn’t clear.
other bits can be random numbers → it can be either 0 or 1, if any bit 1 among them then your requirement of only 10 1’s won’t meet.


 // any 10 bits are high at random position between 29 to 0
 rand int var1;
 constraint c1 { $countones(var1) == 10; var1[31:30] == 0; }


 //consecutive 10bits need to high 
 rand int var1;
 rand int N;
 constraint c1 { var1 == { ((1<<10)-1)<<N}; N inside {[0:20]}; var1[31:30] == 0; }



https://www.linkedin.com/in/patel-rahulkumar/

In reply to Rahulkumar:

Sorry for the confusion.

Consider a number, say rand bit [29:0] var1.

Using constraint, we have to generate a number such that it has 10 1’s (any order any index) in it and at other indices, the values have to be 0.

It is a 30 bit number but not a number b/w 0 to 30.

In reply to Shashank Gurijala:


 // any 10 bits are high at random position between 29 to 0
 rand int var1;
 constraint c1 { $countones(var1) == 10; var1[31:30] == 0; }

In reply to Rahulkumar:

can you try it without using $countones?

I got it using $countones.

Thanks!

In reply to Shashank Gurijala:

class cls;
rand bit [29:0] a, b, c;
constraint cons1 {$countbits(a, 1) == 10;}
endclass

In reply to VC_45:


 //10 consecutive bits will be high
 rand int var1;
 rand int N;
 constraint c1 { var1 == { ((1<<10)-1)<<N}; N inside {[0:20]}; var1[31:30] == 0; }


 //10 random bits will be high
    rand int var1;
    rand bit arr[30];
 
    constraint c1 { arr.sum() with (int'(item))== 10; }

    function void post_randomize();
        var1 = {2'b00,{>>{arr}}};
    endfunction


https://www.linkedin.com/in/patel-rahulkumar/

In reply to Rahulkumar:
Hi Rahul Could you please explain the consecutive concept with using shift operator? minus 1 concept?
constraint c1 { var1 == { ((1<<10)-1)<<N}; N inside {[0:20]}; var1[31:30] == 0;

In reply to Rahulkumar:

In general i am looking to understand how to write constraints for consecutive things? One problem statement i have is following. any idea for below?
100 bits width number.
Write a constraint such a way that,

  1. Number of continuous zeros will be between 2 to 5.
  2. number of continuous ones will be between 2 to 5.

In reply to DVtrojan:

In reply to Rahulkumar:
Hi Rahul Could you please explain the consecutive concept with using shift operator? minus 1 concept?
constraint c1 { var1 == { ((1<<10)-1)<< N}; N inside {[0:20]}; var1[31:30] == 0; }


var1 == { ((1<<10)-1)<< N};

// want to generate 10 consecutive one 
(1 << 10) => 100_0000_0000
(1 << 10)-1 => 011_1111_1111

((1<<10)-1)<< N  => Now will left shift this data by N 



https://www.linkedin.com/in/patel-rahulkumar/

In reply to DVtrojan:

In reply to Rahulkumar:
In general i am looking to understand how to write constraints for consecutive things? One problem statement i have is following. any idea for below?
100 bits width number.
Write a constraint such a way that,

  1. Number of continuous zeros will be between 2 to 5.
  2. number of continuous ones will be between 2 to 5.


        rand bit [99:0] var1; 
        rand bit [2:0] arr[];

        constraint c1 {
                        arr.size() inside {[20:50]}; 
                        arr.sum with (int'(item)) == 100;
                        foreach(arr[i]) arr[i] inside {[2:5]};
                    }

     function void post_randomize();
            bit n;
            n = $urandom;
            foreach(arr[i])begin
                var1 = var1 << arr[i]; 
                if(n) var1 = var1 | ((n<<arr[i])-1);
                n = ~n;
            end 
     endfunction 

/*
//output : 
arr : '{'h2, 'h5, 'h4, 'h2, 'h2, 'h3, 'h5, 'h5, 'h5, 'h3, 'h5, 'h5, 'h4, 'h3, 'h5, 'h5, 'h3, 'h4, 'h4, 'h5, 'h5, 'h4, 'h3, 'h4, 'h5} 
var1 : 0011111000011001110000011111000001110000011111000011100000111110001111000011111000001111000111100000

arr : '{'h2, 'h5, 'h3, 'h3, 'h5, 'h4, 'h5, 'h4, 'h5, 'h5, 'h2, 'h5, 'h5, 'h5, 'h5, 'h5, 'h3, 'h4, 'h5, 'h5, 'h3, 'h2, 'h5, 'h5} 
var1 : 1100000111000111110000111110000111110000011000001111100000111110000011100001111100000111001111100000

arr : '{'h2, 'h2, 'h2, 'h2, 'h2, 'h2, 'h2, 'h2, 'h5, 'h2, 'h2, 'h2, 'h2, 'h2, 'h2, 'h2, 'h2, 'h3, 'h2, 'h2, 'h2, 'h4, 'h2, 'h2, 'h2, 'h2, 'h3, 'h2, 'h4, 'h2, 'h4, 'h2, 'h2, 'h2, 'h2, 'h4, 'h2, 'h4, 'h3, 'h2, 'h2, 'h2} 
var1 : 0011001100110011000001100110011001100111001100111100110011000110000110000110011001111001111000110011
*/


https://www.linkedin.com/in/patel-rahulkumar/

In reply to Rahulkumar:

Thank you so much Rahul! This is great!