Reversing the array elements in a array without using reverse keyword

Hi SV Forum,

I am trying to generate the reversing of the array elements in the same array which i have generated. But not able to do . I am not getting what is the wrong i am doing in the code. My code is as follows

class unique_array_example;
rand bit [7:0] array_1[];
constraint addr_size{array_1.size()==5;}
constraint addr_unique_values{ foreach(array_1[i])
array_1[i]==i;}
endclass:unique_array_example

module abc();
unique_array_example uaeg_1;
initial 
begin
   uaeg_1 =new();

   //This loop is for the unique array values 
   repeat(1)
   begin
      uaeg_1.randomize();
      foreach(uaeg_1.array_1[i])
      begin
      $display("the value of array_size is %d ",uaeg_1.array_1.size());
      $display("the value of a is %d and the value of is %d ",uaeg_1.array_1[i],i);
      end
   end

   //This loop is for the unique reverse array values 
   repeat(1)
   begin
      uaeg_1.randomize() with{ foreach(array_1[i])
      array_1[i] == array_1[(array_1.size()-1) - i];
      };
      foreach(uaeg_1.array_1[i])
      begin
      $display("the value of array_size is %d ",uaeg_1.array_1.size());
      $display("the value of  in reverse array a is %d and the value of is %d ",uaeg_1.array_1[i],i);
      end
   end

end
endmodule

Simuation results :
Compiler version L-2016.06-SP1_Full64; Runtime version L-2016.06-SP1_Full64; Aug 30 14:01 2021
the value of array_size is 5
the value of a is 0 and the value of is 0
the value of array_size is 5
the value of a is 1 and the value of is 1
the value of array_size is 5
the value of a is 2 and the value of is 2
the value of array_size is 5
the value of a is 3 and the value of is 3
the value of array_size is 5
the value of a is 4 and the value of is 4

=======================================================

Solver failed when solving following set of constraints

rand bit[7:0] array_1[0]; // rand_mode = ON 
rand bit[7:0] array_1[4]; // rand_mode = ON 

constraint addr_unique_values    // (from this) (constraint_mode = ON) (arrays_example.sv:118)
{
   (array_1[0] == 0);
   (array_1[4] == 4);
}
constraint WITH_CONSTRAINT    // (from this) (constraint_mode = ON) (arrays_example.sv:142)
{
   (array_1[4] == array_1[0]);

}

=======================================================

Can anyone help regarding this.

Thanks,
Manikanta K.

In reply to Manikanta Kopparapu:

Please, use code tag. It will provide easy visibility over code.
Btw, Your constraints are conflict due to below 2 condition contradicting each other.


  // this constraints assign array_1[0] = 0, array_1[1] = 1 ... array_1[4] = 4; 
  constraint addr_unique_values{ 
    foreach(array_1[i]) array_1[i]==i;
  }
  
  // in this line you are doing
  // array_1[0] = array_1[4] ==> array_1[0] = 4 which is contradicting with array_1[0] = 0;
  // So, your constraints is conficting.
  uaeg_1.randomize() with { 
    foreach(array_1[i]) array_1[i] == array_1[(array_1.size()-1) - i];
  };

And also in order to reverse array you not need to randomize again.
Simple streaming operator is enough to archive this. ( See below code )


class unique_array_example;
  
  rand bit [7:0] array_1[];
  
  constraint addr_size{
    array_1.size() == 5;
  }
  
  constraint addr_unique_values{ 
    foreach(array_1[i]) array_1[i] == i;
  }
  
endclass : unique_array_example

module abc();
  
unique_array_example uaeg_1;
  
initial begin
uaeg_1 = new();

//This loop is for the unique array values
// Note : - I dont know why you use repeat 1. It has not meaning.  
repeat(1) begin
  uaeg_1.randomize();
  foreach(uaeg_1.array_1[i]) begin
    $display("the value of a is %d and the value of is %d ",uaeg_1.array_1[i],i);
  end
end

$display("Before:- \n%p",uaeg_1.array_1);
uaeg_1.array_1 = {<<8{uaeg_1.array_1}};
$display("After:- \n%p",uaeg_1.array_1);
                
//uaeg_1.randomize() with{ foreach(array_1[i])
//  array_1[i] == array_1[ (array_1.size()-1) - i];
//};
   
foreach(uaeg_1.array_1[i]) begin
  $display("the value of in reverse array a is %d and the value of is %d ",uaeg_1.array_1[i],i);
end

end
endmodule

Thanks!

In reply to harsh pandya:

In reply to Manikanta Kopparapu:
Hi Harsh,
Thanks for the response. But still i have some queries which I have commented
Please, use code tag. It will provide easy visibility over code.
Btw, Your constraints are conflict due to below 2 condition contradicting each other.


// this constraints assign array_1[0] = 0, array_1[1] = 1 ... array_1[4] = 4; 
constraint addr_unique_values{ 
foreach(array_1[i]) array_1[i]==i;
}
// in this line you are doing
// array_1[0] = array_1[4] ==> array_1[0] = 4 which is contradicting with array_1[0] = 0;
// So, your constraints is conficting.
<font size=20> **//May I know why the conflict occurs I am not facing any issues 
//while assigning array_1[1] =array_1[3]
//array_1[2]= array_1[2]
//array_1[3] =array_1[1]
// But constraint solver issue is only when assigning the 
//first and last locations of the array
//array_1[0]= array_1[4]
//array_1[4]=array_1[0]</font>**
uaeg_1.randomize() with { 
foreach(array_1[i]) array_1[i] == array_1[(array_1.size()-1) - i];
};

And also in order to reverse array you not need to randomize again.
Simple streaming operator is enough to archive this. ( See below code )


class unique_array_example;
rand bit [7:0] array_1[];
constraint addr_size{
array_1.size() == 5;
}
constraint addr_unique_values{ 
foreach(array_1[i]) array_1[i] == i;
}
endclass : unique_array_example
module abc();
unique_array_example uaeg_1;
initial begin
uaeg_1 = new();
//This loop is for the unique array values
// Note : - I dont know why you use repeat 1. It has not meaning.  
repeat(1) begin
uaeg_1.randomize();
foreach(uaeg_1.array_1[i]) begin
$display("the value of a is %d and the value of is %d ",uaeg_1.array_1[i],i);
end
end
$display("Before:- \n%p",uaeg_1.array_1);
uaeg_1.array_1 = {<<8{uaeg_1.array_1}};
<font size=20>//Can you tell me the significance of the <<8 which is nothing left shifted ?</font>****
$display("After:- \n%p",uaeg_1.array_1);
//uaeg_1.randomize() with{ foreach(array_1[i])
//  array_1[i] == array_1[ (array_1.size()-1) - i];
//};
foreach(uaeg_1.array_1[i]) begin
$display("the value of in reverse array a is %d and the value of is %d ",uaeg_1.array_1[i],i);
end
end
endmodule

Thanks!

In reply to Manikanta Kopparapu:

With below print it does not mean, only first and last index generating conflict.
This is just showing list of constrains/condition involved for constraints conflict.


rand bit[7:0] array_1[0]; // rand_mode = ON 
rand bit[7:0] array_1[4]; // rand_mode = ON 
 
constraint addr_unique_values    // (from this) (constraint_mode = ON) (arrays_example.sv:118)
{
   (array_1[0] == 0);
   (array_1[4] == 4);
}
constraint WITH_CONSTRAINT    // (from this) (constraint_mode = ON) (arrays_example.sv:142)
{
   (array_1[4] == array_1[0]);

See, below result in this i have just changed array from 5 to 10. It shows only array[0] and [9].



rand bit[7:0] array_1[0]; // rand_mode = ON 
rand bit[7:0] array_1[9]; // rand_mode = ON 

constraint addr_unique_values    // (from this) (constraint_mode = ON) (testbench.sv:4)
{
   (array_1[0] == 0);
   (array_1[9] == 9);
}
constraint WITH_CONSTRAINT    // (from this) (constraint_mode = ON) (testbench.sv:27)
{
   (array_1[9] == array_1[0]);
}

So, as per above example it does not mean only array[0] and array[9] has conflict only.
As, per my understanding during randomization any condition is not satisfying/contradicting randomization fail will come.

In your case original constraints was like below.


 constraint addr_unique_values{ 
    foreach(array_1[i]) array_1[i] == i;
  }
 
  //Equivalent
   constraint addr_unique_values{ 
    array_1[0] == 0;
    array_1[1] == 1;
    array_1[2] == 2;
    array_1[3] == 3;
    array_1[4] == 4;
  }

However, during second randomization by using in-line constraints you are trying to apply different value ( EX:- array[0] = array[4] ).
which is contradicting to original hard constraint.

I hope now its clear to you.

Thanks!

In reply to harsh pandya:

In reply to Manikanta Kopparapu:
With below print it does not mean, only first and last index generating conflict.
This is just showing list of constrains/condition involved for constraints conflict.


rand bit[7:0] array_1[0]; // rand_mode = ON 
rand bit[7:0] array_1[4]; // rand_mode = ON 
constraint addr_unique_values    // (from this) (constraint_mode = ON) (arrays_example.sv:118)
{
(array_1[0] == 0);
(array_1[4] == 4);
}
constraint WITH_CONSTRAINT    // (from this) (constraint_mode = ON) (arrays_example.sv:142)
{
(array_1[4] == array_1[0]);

See, below result in this i have just changed array from 5 to 10. It shows only array[0] and [9].


rand bit[7:0] array_1[0]; // rand_mode = ON 
rand bit[7:0] array_1[9]; // rand_mode = ON 
constraint addr_unique_values    // (from this) (constraint_mode = ON) (testbench.sv:4)
{
(array_1[0] == 0);
(array_1[9] == 9);
}
constraint WITH_CONSTRAINT    // (from this) (constraint_mode = ON) (testbench.sv:27)
{
(array_1[9] == array_1[0]);
}

So, as per above example it does not mean only array[0] and array[9] has conflict only.
As, per my understanding during randomization any condition is not satisfying/contradicting randomization fail will come.
In your case original constraints was like below.


constraint addr_unique_values{ 
foreach(array_1[i]) array_1[i] == i;
}
//Equivalent
constraint addr_unique_values{ 
array_1[0] == 0;
array_1[1] == 1;
array_1[2] == 2;
array_1[3] == 3;
array_1[4] == 4;
}

However, during second randomization by using in-line constraints you are trying to apply different value ( EX:- array[0] = array[4] ).
which is contradicting to original hard constraint.
I hope now its clear to you.
Thanks!

Hi Harsh,

Thanks for the solution. Now its clear for me . Whenever there is a constraint failure then randomization won’t work .

In reply to Manikanta Kopparapu:

Correct.
And it is always recommend to check whether randomized get success or fail. In order to catch wrong configuration at early.
Like


  if(!uaeg_1.randomize()) `uvm_fatal("RANDOMIZE_ERROR","Randomization Fail");

Thanks!