SystemVerilog Array Ordering methods

Array manipulation methods

SystemVerilog Array manipulation methods provide several built-in methods to operate on arrays.
they are,

  • Array Ordering methods
  • Array Reduction methods
  • Array Locator methods
  • Array Iterator index querying

Array Ordering methods

  • operate on single dimensional arrays or queues.
  • these methods useful for reordering the array elements.

built-in array ordering methods are,

Method Description
reverse() reverses all the elements of the array(packed or unpacked)
sort() sorts the unpacked array in ascending order
rsort() sorts the unpacked array in descending order
shuffle() randomizes the order of the elements in the array

‘with’ clause is allowed for sort and rsort methods.

About ‘with’:
expression specified in “with” clause will be evaluated for each array element and performs the operation on an array.

As mentioned above, these methods operate on all kinds of array types. below are the examples of using array ordering methods.

Array Ordering methods On Fixed Size Array

Below example is for using reverse, sort, rsort and shuffle method on fixed size array.

module fixedsize_array;
  
  //declaration of array’s
  int array_1[4];
  int array_2[4];
  int array_3[4];
  int array_4[4];

  initial begin
    //array initialization
    array_1  = '{0,1,2,3};
    array_2  = '{2,3,1,0};
    array_3  = '{2,3,1,0};
    array_4  = '{0,1,2,3};
    

    $display("-======= reverse =======-");
    $display("Before:\t %p",array_1);
    array_1.reverse();
    $display("After :\t %p",array_1);
    $display("-=======================-");    

    $display("\n-=======   sort   =======-");
    $display("Before:\t %p",array_2);
    array_2.sort();
    $display("After :\t %p",array_2);
    $display("-=======================-"); 
    
    $display("\n-=======   rsort  =======-");
    $display("Before:\t %p",array_3);    
    array_3.rsort();
    $display("After :\t %p",array_3);
    $display("-=======================-");
    
    $display("\n-======= shuffle =======-");
    $display("Before:\t %p",array_4);
    array_4.shuffle();
    $display("After :\t %p",array_4);
    $display("-=======================-");  
    
  end
  
endmodule 

Simulator Output

-======= reverse =======-
Before: '{0, 1, 2, 3} 
After : '{3, 2, 1, 0} 
-=======================-

-======= sort =======-
Before: '{2, 3, 1, 0} 
After : '{0, 1, 2, 3} 
-=======================-

-======= rsort =======-
Before: '{2, 3, 1, 0} 
After : '{3, 2, 1, 0} 
-=======================-

-======= shuffle =======-
Before: '{0, 1, 2, 3} 
After : '{2, 3, 1, 0} 
-=======================-

Click to execute on   

Array Ordering methods On Dynamic Array

Below example is for using reverse, sort, rsort and shuffle method on the dynamic array.

module dynamic_size_array;
  
  //declaration of array’s
  int array_1[];
  int array_2[];
  int array_3[];
  int array_4[];

  initial begin
    array_1 = new[4];
    array_2 = new[4];
    array_3 = new[4];
    array_4 = new[4];
    
    //array initialization
    array_1  = '{0,1,2,3};
    array_2  = '{2,3,1,0};
    array_3  = '{2,3,1,0};
    array_4  = '{0,1,2,3};
    
    $display("-======= reverse =======-");
    $display("Before:\t %p",array_1);
    array_1.reverse();
    $display("After :\t %p",array_1);
    $display("-=======================-");    

    $display("\n-=======   sort   =======-");
    $display("Before:\t %p",array_2);
    array_2.sort();
    $display("After :\t %p",array_2);
    $display("-=======================-"); 
    
    $display("\n-=======   rsort  =======-");
    $display("Before:\t %p",array_3);    
    array_3.rsort();
    $display("After :\t %p",array_3);
    $display("-=======================-");
    
    $display("\n-======= shuffle =======-");
    $display("Before:\t %p",array_4);
    array_4.shuffle();
    $display("After :\t %p",array_4);
    $display("-=======================-");  
    
  end
  
endmodule 

Simulator Output

-======= reverse =======-
Before: '{0, 1, 2, 3} 
After : '{3, 2, 1, 0} 
-=======================-

-======= sort =======-
Before: '{2, 3, 1, 0} 
After : '{0, 1, 2, 3} 
-=======================-

-======= rsort =======-
Before: '{2, 3, 1, 0} 
After : '{3, 2, 1, 0} 
-=======================-

-======= shuffle =======-
Before: '{0, 1, 2, 3} 
After : '{2, 3, 1, 0} 
-=======================-

Click to execute on   

Array Ordering methods On Associative Array

Below example is for using reverse, sort, rsort and shuffle method on the associative array.

Note:
whereas in fixed/dynamic/queue array types index will be incremental but in associative array index shall be random.

In associative array, based on ordering methods elements will be stored to available different index locations.

module assoc_size_array;
  
  //declaration of array’s
  int array_1[*];
  int array_2[*];
  int array_3[*];
  int array_4[*];

  initial begin
    
    //array initialization
    array_1[3] = 8;
    array_1[5] = 2;
    array_1[7] = 6;
    array_1[9] = 1;
    
    array_2[3] = 8;
    array_2[5] = 2;
    array_2[7] = 6;
    array_2[9] = 1;
    
    array_3[3] = 8;
    array_3[5] = 2;
    array_3[7] = 6;
    array_3[9] = 1;
    
    array_4[3] = 8;
    array_4[5] = 2;
    array_4[7] = 6;
    array_4[9] = 1;
    
    $display("-======= reverse =======-");
    $display("Before:\t %p",array_1);
    array_1.reverse();
    $display("After :\t %p",array_1);
    $display("-=======================-");    

    $display("\n-=======   sort   =======-");
    $display("Before:\t %p",array_2);
    array_2.sort();
    $display("After :\t %p",array_2);
    $display("-=======================-"); 
    
    $display("\n-=======   rsort  =======-");
    $display("Before:\t %p",array_3);    
    array_3.rsort();
    $display("After :\t %p",array_3);
    $display("-=======================-");
    
    $display("\n-======= shuffle =======-");
    $display("Before:\t %p",array_4);
    array_4.shuffle();
    $display("After :\t %p",array_4);
    $display("-=======================-");  
    
  end
  
endmodule 

Simulator Output

-======= reverse =======-
Before: '{0x3:8, 0x5:2, 0x7:6, 0x9:1}
After : '{0x3:1, 0x5:6, 0x7:2, 0x9:8}
-=======================-
-======= sort =======-
Before: '{0x3:8, 0x5:2, 0x7:6, 0x9:1}
After : '{0x3:1, 0x5:2, 0x7:6, 0x9:8}
-=======================-
-======= rsort =======-
Before: '{0x3:8, 0x5:2, 0x7:6, 0x9:1}
After : '{0x3:8, 0x5:6, 0x7:2, 0x9:1}
-=======================-
-======= shuffle =======-
Before: '{0x3:8, 0x5:2, 0x7:6, 0x9:1}
After : '{0x3:6, 0x5:1, 0x7:2, 0x9:8}
-=======================-

Click to execute on   

Array Ordering methods On Queue

Below example is for using reverse, sort, rsort and shuffle method on the queue.

module queue_manipulation;
  
  //declaration of array’s
  int queue_1[$];
  int queue_2[$];
  int queue_3[$];
  int queue_4[$];
  
  initial begin
    queue_1.push_back(8);
    queue_1.push_back(2);
    queue_1.push_back(6);
    queue_1.push_back(1);

    queue_2.push_back(8);
    queue_2.push_back(2);
    queue_2.push_back(6);
    queue_2.push_back(1);
    
    queue_3.push_back(8);
    queue_3.push_back(2);
    queue_3.push_back(6);
    queue_3.push_back(1);
    
    queue_4.push_back(8);
    queue_4.push_back(2);
    queue_4.push_back(6);
    queue_4.push_back(1);
    
    //array initialization
    $display("-======= reverse =======-");
    $display("Before:\t %p",queue_1);
    queue_1.reverse();
    $display("After :\t %p",queue_1);
    $display("-=======================-");    

    $display("\n-=======   sort   =======-");
    $display("Before:\t %p",queue_2);
    queue_2.sort();
    $display("After :\t %p",queue_2);
    $display("-=======================-"); 
    
    $display("\n-=======   rsort  =======-");
    $display("Before:\t %p",queue_3);    
    queue_3.rsort();
    $display("After :\t %p",queue_3);
    $display("-=======================-");
    
    $display("\n-======= shuffle =======-");
    $display("Before:\t %p",queue_4);
    queue_4.shuffle();
    $display("After :\t %p",queue_4);
    $display("-=======================-");  
    
  end
endmodule

Simulator Output

-======= reverse =======-
Before: '{8, 2, 6, 1}
After : '{1, 6, 2, 8}
-=======================-
-======= sort =======-
Before: '{8, 2, 6, 1}
After : '{1, 2, 6, 8}
-=======================-
-======= rsort =======-
Before: '{8, 2, 6, 1}
After : '{8, 6, 2, 1}
-=======================-
-======= shuffle =======-
Before: '{8, 2, 6, 1}
After : '{6, 1, 2, 8}
-=======================-

Click to execute on   

Array Ordering methods ‘SORT’ Associative Array using ‘with’ clause

In the below example, Objects of type packet are stored in an associative array. elements of an array can be sorted for particular type on using sort along with ‘with‘ clause.

On sort method, an item with the lower value of ‘a’ will move into a lower index position of the array, the same will be repeated for all the array elements.

class packet;
  int a;
  int b;
  
  function void display();
    $display("\tValue of a = %0d",a);
    $display("\tValue of b = %0d",b);
  endfunction
endclass


module assoc_array;
  //declaration of array’s
  packet array_1[*];

  packet pkt;
  
  initial begin
    
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    array_1[3] = pkt;
    
    pkt = new();
    pkt.a = 0;
    pkt.b = 6;
    array_1[7] = pkt;
    
    pkt = new();
    pkt.a = 2;
    pkt.b = 1;
    array_1[9] = pkt;

    $display("-======= sort =======-");
    $display("Before:");
    foreach(array_1[i]) begin //{
      $display("array_1[%0d]:",i);
      array_1[i].display();
    end //}
    
    array_1.sort with (item.a);
    
    $display("\nAfter:");
    foreach(array_1[i]) begin //{
      $display("array_1[%0d]:",i);
      array_1[i].display();
    end //}
    $display("-=======================-");        
  end
  
endmodule 

Simulator Output

-======= sort =======-
Before:
array_1[3]:
 Value of a = 8
 Value of b = 3
array_1[7]:
 Value of a = 0
 Value of b = 6
array_1[9]:
 Value of a = 2
 Value of b = 1

After:
array_1[3]:
 Value of a = 0
 Value of b = 6
array_1[7]:
 Value of a = 2
 Value of b = 1
array_1[9]:
 Value of a = 8
 Value of b = 3
-=======================-

Description: 
As explained above, on calling sort method. items are sorted w.r.t value of 'a' and stored in 
different index location. item with a=0 stored to index 3,
a=2 to next index 7 and a=8 to next index 9 

Click to execute on   

Array Ordering methods ‘RSORT’ Associative Array using ‘with’ clause

class packet;
  int a;
  int b;
  
  function void display();
    $display("\tValue of a = %0d",a);
    $display("\tValue of b = %0d",b);
  endfunction
endclass


module assoc_array;
  //declaration of array’s
  packet array_1[*];

  packet pkt;
  
  initial begin
    
    pkt = new();
    pkt.a = 8;
    pkt.b = 3;
    array_1[3] = pkt;
    
    pkt = new();
    pkt.a = 0;
    pkt.b = 6;
    array_1[7] = pkt;
    
    pkt = new();
    pkt.a = 2;
    pkt.b = 1;
    array_1[9] = pkt;

    $display("-======= rsort =======-");
    $display("Before:");
    foreach(array_1[i]) begin //{
      $display("array_1[%0d]:",i);
      array_1[i].display();
    end //}
    
    array_1.rsort with (item.a);
    
    $display("\nAfter:");
    foreach(array_1[i]) begin //{
      $display("array_1[%0d]:",i);
      array_1[i].display();
    end //}
    $display("-=======================-");        
  end
  
endmodule 

Simulator Output

-======= rsort =======-
Before:
array_1[3]:
Value of a = 8
Value of b = 3
array_1[7]:
Value of a = 0
Value of b = 6
array_1[9]:
Value of a = 2
Value of b = 1

After:
array_1[3]:
Value of a = 8
Value of b = 3
array_1[7]:
Value of a = 2
Value of b = 1
array_1[9]:
Value of a = 0
Value of b = 6
-=======================-

Click to execute on   

❮ Previous Next ❯