Friday, November 8, 2013

Queue in SystemVerilog - Very fancy

Power of Queues in System Verilog Test benches - 
- are very fancy to use in Test Benches - 
- Queue can increase/decrease the number of elements in it as per need basis.
- Methods Available - size(), insert(), delete(), push_front(), push_back(), pop_front(), pop_back()
- Check the usage of these methods in below program.
- Powerful to use as per need basis 
- Multiple ways to edit (to insert or delete) an element of a queue

Here is a sample code which explores the features of the Queue...!!!
#####################################################################
module test;

 integer IntQueue[$] = {0,1,2,3,4}; //Queue Declaration with initialization
 integer i, temp;
  
 initial begin
   $display("Size of the queue: %0d",IntQueue.size());       //Size of The Queue
   $display("First Element of the queue: %0d",IntQueue[0]); //1st Element Access
   $display("Last Element of the queue: %0d",IntQueue[$]); //Last Element Access
   
   print_queue("INITIAL QUEUE");
   
   IntQueue.push_front(9);     //Insert, push 9 in front of the queue - push_front()
   print_queue("PUSH_FRONT()");
   
   temp=IntQueue.pop_front();  //Get, pop 1st element of the queue - pop_front()
   print_queue("POP_FRONT()");
   
   IntQueue.insert(0,9);       //Insert 9 before 1st element of the queue - instert()
   print_queue("INSERT()");
   
   IntQueue.delete(0);         //Delete 1st element of the queue - delete()
   print_queue("DELETE()");
   
   IntQueue.push_back(9);      //Insert 9 at the end of the queue - push_back()
   print_queue("PUSH_BACK()");
   
   IntQueue.pop_back();        //Delete Last element of the queue - pop_back()
   print_queue("POP_BACK()");
 end

 //Task to Print Queue
 task print_queue(string fname);
   $display("IntQueue: After Operation:%s",fname);
   for (i=0; i<IntQueue.size(); i++)
     $display("I=%0d IntQueue[%0d]=%0d ",i,i,IntQueue[i]);
 endtask

endmodule //module test
#####################################################################
Run Log follows as ...:
#####################################################################
Size of the queue: 5
First Element of the queue: 0
Last Element of the queue: 4
Queue: From Function: After Operation:INITIAL QUEUE
I=0 IntQueue[0]=0 
I=1 IntQueue[1]=1 
I=2 IntQueue[2]=2 
I=3 IntQueue[3]=3 
I=4 IntQueue[4]=4 
Queue: From Function: After Operation:PUSH_FRONT()
I=0 IntQueue[0]=9 
I=1 IntQueue[1]=0 
I=2 IntQueue[2]=1 
I=3 IntQueue[3]=2 
I=4 IntQueue[4]=3 
I=5 IntQueue[5]=4 
Queue: From Function: After Operation:POP_FRONT()
I=0 IntQueue[0]=0 
I=1 IntQueue[1]=1 
I=2 IntQueue[2]=2 
I=3 IntQueue[3]=3 
I=4 IntQueue[4]=4 
Queue: From Function: After Operation:INSERT()
I=0 IntQueue[0]=9 
I=1 IntQueue[1]=0 
I=2 IntQueue[2]=1 
I=3 IntQueue[3]=2 
I=4 IntQueue[4]=3 
I=5 IntQueue[5]=4 
Queue: From Function: After Operation:DELETE()
I=0 IntQueue[0]=0 
I=1 IntQueue[1]=1 
I=2 IntQueue[2]=2 
I=3 IntQueue[3]=3 
I=4 IntQueue[4]=4 
Queue: From Function: After Operation:PUSH_BACK()
I=0 IntQueue[0]=0 
I=1 IntQueue[1]=1 
I=2 IntQueue[2]=2 
I=3 IntQueue[3]=3 
I=4 IntQueue[4]=4 
I=5 IntQueue[5]=9 
Queue: From Function: After Operation:POP_BACK()
I=0 IntQueue[0]=0 
I=1 IntQueue[1]=1 
I=2 IntQueue[2]=2 
I=3 IntQueue[3]=3 
I=4 IntQueue[4]=4 
#####################################################################

Yaa... seen above are multiple ways to add or delete an element from a queue. 
Here are some more operations on queues ... 

IntQueue = IntQueue[1:$];    //Delete 1st Element
IntQueue = IntQueue[0:$-1]; //Delete Last Element
IntQueue = IntQueue[1:$-1]; //Delete 1st & Last Elements


Hope with this you've got to know the SV Queues Usage ... !

No comments:

Post a Comment