quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bullet_green_ball.gif Queues

A queue is a variable-size, ordered collection of homogeneous elements. Each element in a queue is identified by an ordinal number that represents its position within the queue, with 0 representing the first, and $ representing the last. A queue is analogous to a one-dimensional unpacked array that grows and shrinks automatically. Thus, like arrays, queues can be manipulated using the indexing, concatenation, slicing operator syntax, and equality operators.

   

space.gif

Syntax

   

space.gif

variable_dimension ::= 
      { sized_or_unsized_dimension } 
   | associative_dimension 
   | queue_dimension 
queue_dimension ::= [ $ [ : constant_expression ] ]
   

space.gif

  ../images/main/bulllet_4dots_orange.gif Queue Operators
   

space.gif

Following operators are used in working with queues

   

space.gif

  • 0 : Used to access first element of queue
  • $ : Used to access last element of queue
  • {} : Used along with first and last operator to add/delete elements
   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Queues methods

SystemVerilog provides following methods to work with queues

   

space.gif

Method

Description

insert()

The insert() method inserts the given item at the specified index position.

delete()

The delete() method deletes the item at the specified index position.

pop_front()

The pop_front() method removes and returns the first element of the queue.

pop_back()

The pop_back() method removes and returns the last element of the queue.

push_front()

The push_front() method inserts the given element at the front of the queue.

push_back()

The push_back() method inserts the given element at the end of the queue.

size()

The size() method returns the number of items in the queue. If the queue is empty, it returns 0.

   

space.gif

  ../images/main/bulllet_4dots_orange.gif Example - Queues
   

space.gif


  1 module queue_data();
  2 
  3 // Queue is declated with $ in array size
  4 integer queue[$] = { 0, 1, 2, 3, 4 };
  5 integer i;
  6 
  7 initial begin
  8   $display ("Initial value of queue");
  9   print_queue;
 10   // Insert new element at begin of queue
 11   queue = {5, queue};
 12   $display ("new element added using concate");
 13   print_queue;
 14   // Insert using method at begining
 15   queue.push_front(6);
 16   $display ("new element added using push_front");
 17   print_queue;
 18   // Insert using method at end
 19   queue.push_back(7);
 20   $display ("new element added using push_back");
 21   print_queue;
 22   // Using insert to insert, here 4 is index
 23   // and 8 is value
 24   queue.insert(4,8);
 25   $display ("new element added using insert(index,value)");
 26   print_queue;
 27   // get first queue element method at begining
 28   i = queue.pop_front();
 29   $display ("element poped using pop_front");
 30   print_queue;
 31   // get last queue element method at end
 32   i = queue.pop_back();
 33   $display ("element poped using pop_end");
 34   print_queue;
 35   // Use delete method to delete element at index 4 in queue
 36   queue.delete(4);
 37   $display ("deleted element at index 4");
 38   print_queue;
 39    #1  $finish;
 40 end
 41 
 42 task print_queue;
 43   integer i;
 44   $write("Queue contains ");
 45   for (i = 0; i < queue.size(); i ++) begin
 46     $write (" %g", queue[i]);
 47   end
 48   $write("\n");
 49 endtask
 50 
 51 endmodule
You could download file queue_data.sv here
   

space.gif

  ../images/main/bullet_star_pink.gif Simulation Output - Queues
   

space.gif

 Initial value of queue
 Queue contains  0 1 2 3 4
 new element added using concate
 Queue contains  5 0 1 2 3 4
 new element added using push_front
 Queue contains  6 5 0 1 2 3 4
 new element added using push_back
 Queue contains  6 5 0 1 2 3 4 7
 new element added using insert(index,value)
 Queue contains  6 5 0 1 8 2 3 4 7
 element poped using pop_front
 Queue contains  5 0 1 8 2 3 4 7
 element poped using pop_end
 Queue contains  5 0 1 8 2 3 4
 deleted element at index 4
 Queue contains  5 0 1 8 3 4
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com