Constraint for array of 10 elements in which first 5 elements are in increment in nature and next 5 elements are in decrementing nature

Writing a constraint for array of elements in which first 5 elements are in increment in nature and next 5 elements are in decrementing nature

In reply to shashikanth123:


case 1: 
// arr[0] < arr[1] < ... < arr[4]
// arr[5] > arr[6] > ....> arr[9]
    constraint c1 {
            foreach(arr[i]) { if(i < 5) arr[i] < arr[i+1];
                              if((i > 4) & (i != 9))  arr[i] > arr[i+1];
                            }
        }

 
case 2:
// arr[0] < arr[1] < ... < arr[4] > arr[5] > arr[6] > ....> arr[9] 
    constraint c1 {
            foreach(arr[i]) { if(i < 4) arr[i] < arr[i+1];
                              if((i > 3) & (i != 9))  arr[i] > arr[i+1];
                            }
        }

In reply to Rahulkumar:

Thanks Rahul, for the logic and also i tried to modify with if - else case from your logic which is also working.For which i have taken fixed array of 10 elements so i tried with 5 elements increment and next 5 elements with decrement order.

like : constraint c1 {
foreach(arr[i]) { if(i < 5)
arr[i] < arr[i+1];
else
arr[i] > arr[i+1];}
}

In reply to shashikanth123:


constraint c1 {
foreach(arr[i]) { if(i < 5)
arr[i] < arr[i+1];
else
arr[i] > arr[i+1];}   // for i = 9 this arr[i+1]=arr[10] will go outside of array size 
}

In reply to Rahulkumar:

Hello! Here is my code:

class sample;

	rand bit [3:0] array [10];
	
	rand int i;
	
	constraint elements {		
		             foreach (array[i])
				      {
					  if (i >= 5 && i <= 9)
									
						array [i] > array [i+1];	
								
		  			  else if (i == 0 && i <= 4 )
						array [i] < array [i+1];
				       }
				}					
							
endclass

module array_elements;
	
	initial begin
	
		sample s = new;
		
		s.randomize();
		
		foreach (s.array[i])
		
		$display("array[%0d] = %0d",i,i);
				
	end
	
endmodule

Here is the output:

array[0] = 0
array[1] = 1
array[2] = 2
array[3] = 3
array[4] = 4
array[5] = 5
array[6] = 6
array[7] = 7
array[8] = 8
array[9] = 9

I removed the cconstraint and the result still holds.

In reply to Shashank Gurijala:

You are not displaying the contents of the array, just i. You did not check the result of the randomization, it is failing.

  1. From the below, when i is 9, it results into out of bound access error.
    so, you need to restrict the index not to fall outside the array limit.

    if (i >= 5 && i <= 9)
    array [i] > array [i+1];

Error-[CNST-IAIE] Constraint illegal array index error
test1.v, 25
Constrained variable array[10] is outside of the array size.
Please make sure variable array[10] is within the array size.

  1. You need to print s.array[i], not ‘i’ in the display statement to get the results.
    $display(“array[%0d] = %0d”,i,i);

Writing a constraint for array of elements in which first 5 elements are in increment in nature and next 5 elements are in decrementing nature

class Ascend; 
  rand  byte unsigned d[10]; 
    
  constraint c { foreach (d[i]){ 
    if (i>0 && i<5) 					
     d[i] > d[i-1];
    if(i>=5 && i<10)
                  d[i] < d[i-1];}
 						
} endclass 

module sample_6_61;
  Ascend a1;
  initial begin
    a1=new();
     a1.randomize();
    foreach(a1.d[i])begin
 
      if(i==5)
        repeat(20)
          $write("=");
      $display(" \n%0d \t %0d",i,a1.d[i]);
      
    end
  end
endmodule