ch2-Queues

2019-10-24

SystemVerilog Queues

​ A SystemVerilog queue is a First In First Out scheme which can have a variable size to store elements of the same data type.

Queue Syntax

A queue is distinguished by it’s specification of the size using $ operator.

SystemVerilog Queue Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module tb;
// Create a queue that can store "string" values
string fruits[$] = { "orange", "apple", "kiwi" };

initial begin
// Iterate and access each queue element
foreach (fruits[i])
$display ("fruits[%0d] = %s", i, fruits[i]);

// Display elements in a queue
$display ("fruits = %p", fruits);

// Delete all elements in the queue
fruits = {};
$display ("After deletion, fruits = %p", fruits);
end
endmodule

What are queue slice expressions ?

A slice expression selects a subset of the existing variable. Queue elements can be selected using slice expressions as shown in the example below.

Some simulators provide different results, hence using queue methods is recommended.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module tb;
// Create a queue that can store "string" values
string fruits[$] = { "orange", "apple", "lemon", "kiwi" };

initial begin
// Select a subset of the queue
$display ("citrus fruits = %p", fruits[1:2]);

// Get elements from index 1 to end of queue
$display ("fruits = %p", fruits[1:$]);

// Add element to the end of queue
fruits[$+1] = "pineapple";
$display ("fruits = %p", fruits);

// Delete first element
$display ("Remove orange, fruits = %p", fruits[1:$]);
end
endmodule

Queue Methods Example

queue-methods

In addition to array operators, queues provide several built-in methods.

Function Description
function int size (); Returns the number of items in the queue, 0 if empty
function void insert (input integer index, input element_t item); Inserts the given item at the specified index position
function void delete ( [input integer index] ); Deletes the element at the specified index, and if not provided all elements will be deleted
function element_t pop_front (); Removes and returns the first element of the queue
function element_t pop_back (); Removes and returns the last element of the queue
function void push_front (input element_t item); Inserts the given element at the front of the queue
function void push_back (input element_t item); Inserts the given element at the end of the queues

How to create a queue of dynamic arrays in SystemVerilog ?

queue of dynamic arrays

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Declare a dynamic array to store strings as a datatype
typedef string str_da [];

module tb;
// This is a queue of dynamic arrays
str_da list [$];

initial begin
// Initialize separate dynamic arrays with some values
str_da marvel = '{"Spiderman", "Hulk", "Captain America", "Iron Man"};
str_da dcWorld = '{"Batman", "Superman" };

// Push the previously created dynamic arrays to queue
list.push_back (marvel);
list.push_back (dcWorld);

// Iterate through the queue and access dynamic array elements
foreach (list[i])
foreach (list[i][j])
$display ("list[%0d][%0d] = %s", i, j, list[i][j]);

// Simply print the queue
$display ("list = %p", list);
end

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!