System Verilog : Queues

In your system verilog code, if extraction and insertion order of array elements are important, queue would be the best option. A queue is a variable-size, ordered collection of homogeneous elements. It is declared using the same syntax as unpacked arrays, but specifying $ as the array size. It is analogous to a one-dimensional unpacked array that grows and shrinks automatically. So like arrays, queues can be manipulated using concatenation, slicing, indexing and quality operators.

Each element in a queue is identified by an ordinal number that represents its position within the queue. In this 0 represents the first and $ represents the last. Insertion and deletion of elements from random locations using an index are also possible with queues.

Bounded queues
Queue size can be limited by giving the last index (upper bound) as follows

These are called bounded queues.It will not have an element whose index is higher than the queue’s declared upper bound. Operators on bounded queue can be applied exactly the same way as unbounded queues, except that, result should fall in the upper bound limit.

There are some built in methods available in queue to do insertion, deletion, pushing, popping etc, without using the above methods.

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

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

If the index argument has any bits with unknown (x/z) value, or is negative, or is greater than the current size of the queue, then the method call shall have no effect on the queue and may cause a warning to be issued.

delete():
The delete() method deletes the item at the specified index. Index is optional. If index is not specified, entire elements in the queue will get deleted leaving the queue empty.

If the index argument has any bits with unknown (x/z) value, or is negative, or is greater than or equal to the current size of the queue, then the method call shall have no effect on the queue and may cause a warning to be issued.

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.

3 Comments

  1. Ramya

    Hi,
    I’m trying to use struct as data type in queues. Like as shown below:

    typedef struct {bit[31:0] addr, bit[3:0] id, bit[63:0] data; bit flag}
    packet_t;

    packet_t q1 [$];

    I want to populate the addr everytime a new addr comes in, like:

    if (intf.a && intf.b)begin
    q1.push_front.addr = intf.address;
    q1.push_front.id = intf.id;
    end

    I would like to store the addr and id in the same item (index) of the queue.
    (On the next cycle, if intf.a and intf.b evaluate to True, I want to store the id and addr as the second queue item)

    Will the above code achieve the intent?
    Are there examples of using struct as data type in queues and manipulating them?

    When data arrives(along with data id), I plan to use the “find_first with” to find the queue item that matches with the id to store the data into.

    Please let me know your inputs.

    Thanks!
    Ramya

Leave a Reply

Your email address will not be published. Required fields are marked *