Systemverilog Queues

 Queues 


A queue is a variable-size, ordered collection of homogeneous elements. like dynamic array, queues can grow and shrink, queue supports adding and removing elements anywhere. 

Queues are declared using the same syntax as unpacked arrays, but specifying $ as the array size. In queue 0 represents the first, and $ representing the last entree.

Queue Declaration:
    data_type queue_name[$];
        where:        
        data_type        -  data type of the queue elements.        
        queue_name    -  name of the queue.

Example:  
   bit       queue_1[$];          // queue of bits  
   int       queue_2[$];          //  queue of int  
   byte    queue_3[$:255];   // queue of byte with max size 256 entries  
   string  queue_4[$];          // queue of strings


Queue Initialization:  
   queue_1  = {0,1,2,3};  
   queue_4  = {“Red”,"Blue”,"Green”};

Queue Methods:  
    size()            -->    returns the number of items in the queue.
    insert()          -->    inserts the given item at the specified index position.
    delete()         -->    deletes the item at the specified index position.
    push_front()  -->    inserts the given element at the front of the queue.
    push_back() -->    inserts the given element at the end of the queue.
    pop_front()   -->    removes and returns the first element of the queue.
    pop_back()   -->    removes and returns the last element of the queue.

 Example-1 : Queue Declaration, Initialization, Size, Insert and Delete Method. 

     This example shows declaration and usage Queue methods.

module queues_array;
  //declaration
  bit    [31:0] queue_1[$]; 
  string  queue_2[$];  
  
  initial begin
    //Queue Initialization:
    queue_1 = {0,1,2,3};
    queue_2 = {"Red","Blue","Green"};
    
    //Size-Method
    $display("----- Queue_1 size is %0d  -----",queue_1.size());
    foreach(queue_1[i]) $display("\tqueue_1[%0d] = %0d",i,queue_1[i]);    
    $display("----- Queue_2 size is %0d  -----",queue_2.size());
    foreach(queue_2[i]) $display("\tqueue_2[%0d] = %0s",i,queue_2[i]);
    
    //Insert-Method
    queue_2.insert(1,"Orange");
    $display("----- Queue_2 size  after inserting Orange is %0d  -----",queue_2.size());
    foreach(queue_2[i]) $display("\tqueue_2[%0d] = %0s",i,queue_2[i]);
    
    //Delete Method
    queue_2.delete(3);
    $display("----- Queue_2 size after Delete is %0d  -----",queue_2.size());
    foreach(queue_2[i])$display("\tqueue_2[%0d] = %0s",i,queue_2[i]);
  end

endmodule

 Simulator Output  


----- Queue_1 size is 4 -----
queue_1[0] = 0
queue_1[1] = 1
queue_1[2] = 2
queue_1[3] = 3
----- Queue_2 size is 3 -----
queue_2[0] = Red
queue_2[1] = Blue
queue_2[2] = Green
----- Queue_2 size after inserting Orange is 4 -----
queue_2[0] = Red
queue_2[1] = Orange
queue_2[2] = Blue
queue_2[3] = Green
----- Queue_2 size after Delete is 3 -----
queue_2[0] = Red
queue_2[1] = Orange
queue_2[2] = Blue
Execute the above code on 

 Example-2 : Queue, push_front(), push_back(), pop_front() and pop_back() Method. 

module queues_array;
  //declaration
  bit    [31:0] queue_1[$]; 
  int    lvar;  
  
  initial begin
    //Queue Initialization:
    queue_1 = {0,1,2,3};
    
    //Size-Method
    $display("\tQueue_1 size is %0d",queue_1.size());   
    
    //Push_front Method
    queue_1.push_front(22);
    $display("\tQueue_1 size after push_front is %0d",queue_1.size());
    
    //Push_back  Method
    queue_1.push_back(44);
    $display("\tQueue_1 size after push_back is %0d",queue_1.size());

 //Pop_front Method
 lvar = queue_1.pop_front();
    $display("\tQueue_1 pop_front value is %0d",lvar);

 //Pop_back Method
 lvar = queue_1.pop_back();
    $display("\tQueue_1 pop_back value is %0d",lvar);
  end
endmodule

 Simulator Output  

Queue_1 size is 4
Queue_1 size after push_front is 5
Queue_1 size after push_back is 6
Queue_1 pop_front value is 22
Queue_1 pop_back value is 44
Execute the above code on