Queues, Arrays, and Mailboxes
Learn the SystemVerilog data structures and communication mechanisms you will use to store, organize, and move verification data.
Learning objectives
- Understand the difference between fixed-size arrays, dynamic arrays, and queues.
- Use queues to store and process transactions in FIFO or LIFO order.
- Understand when associative arrays are useful in verification.
- Use mailboxes to safely pass objects between concurrent processes.
- Recognize where these constructs appear in real UVM environments.
Step 1: Why verification needs collections
Verification environments rarely work with only one piece of data. A testbench may need to store hundreds of transactions, keep track of expected results, remember previously seen addresses, or pass work from one process to another.
SystemVerilog provides several tools for this:
- Arrays organize multiple values using indexes.
- Queues provide a flexible ordered collection that can grow and shrink.
- Associative arrays store data using a key instead of a fixed numeric index.
- Mailboxes provide communication between concurrent processes.
Step 2: Fixed-size arrays
A fixed-size array has a size that is known when the array is declared. The indexes are typically numeric and have a fixed range.
bit [7:0] data[4];
data[0] = 8'h10;
data[1] = 8'h20;
data[2] = 8'h30;
data[3] = 8'h40;
This creates four 8-bit elements. The valid indexes are
0 through 3.
Fixed-size arrays are useful when the number of elements is known, such as a register file, a set of protocol fields, or a fixed number of channels.
Step 3: Dynamic arrays
Sometimes the testbench does not know the required number of elements until runtime. A dynamic array lets you choose its size during simulation.
int data[];
data = new[5];
foreach (data[i]) begin
data[i] = i * 10;
end
The array starts without an allocated size. The
new[] operation allocates the requested number of elements.
Dynamic arrays are particularly useful when the size depends on a configuration value or a test scenario.
int packet_length;
byte packet[];
packet_length = 64;
packet = new[packet_length];
Here the test can decide the packet size at runtime.
Step 4: Queues
A queue is one of the most useful SystemVerilog collections for verification. Unlike a fixed-size array, a queue can grow and shrink during simulation.
int queue[$];
queue.push_back(10);
queue.push_back(20);
queue.push_back(30);
The queue now contains:
10 20 30
You can remove the first element with pop_front():
int value;
value = queue.pop_front();
After the operation, value is 10 and the
queue contains:
20 30
Common queue operations
| Operation | Purpose |
|---|---|
push_back() |
Add an item to the end. |
push_front() |
Add an item to the beginning. |
pop_front() |
Remove and return the first item. |
pop_back() |
Remove and return the last item. |
size() |
Return the number of elements. |
delete() |
Remove elements from the queue. |
Step 5: Queues of objects
In UVM, queues often contain transaction objects rather than simple integers.
class bus_item;
rand bit [7:0] addr;
rand bit [31:0] data;
rand bit write;
endclass
bus_item transactions[$];
bus_item item;
item = new();
item.addr = 8'h10;
item.data = 32'h1234_5678;
item.write = 1;
transactions.push_back(item);
The queue now stores a handle to the transaction object. You can retrieve it later:
item = transactions.pop_front();
This pattern becomes important later when you build monitors and scoreboards that collect and compare transactions.
Step 6: Associative arrays
Sometimes a numeric index is not the best way to identify data. An associative array lets you use a key.
int expected_data[int];
expected_data[100] = 32'hAAAA_BBBB;
expected_data[200] = 32'hCCCC_DDDD;
In this example, the address is being used as the key. This is useful when the testbench needs to remember information associated with a particular address or identifier.
Associative arrays are especially useful for verification structures such as:
- Expected values indexed by address.
- Transactions indexed by transaction ID.
- Tracking which addresses have been accessed.
- Maintaining sparse data where most possible indexes are unused.
if (expected_data.exists(100)) begin
$display("Address 100 has an expected value");
end
Step 7: Mailboxes
A mailbox is different from an array or queue. Its primary purpose is communication between concurrent processes.
Think of a mailbox as a container with a producer on one side and a consumer on the other side.
mailbox mb = new();
task producer();
mb.put(10);
endtask
task consumer();
int value;
mb.get(value);
$display("Received %0d", value);
endtask
The producer places data into the mailbox using put().
The consumer retrieves data using get().
A mailbox can therefore synchronize two processes that operate concurrently.
Step 8: Bounded mailboxes
A mailbox can optionally have a maximum capacity.
mailbox mb = new(4);
This mailbox can hold up to four items. If the mailbox is full,
a blocking put() waits until space becomes available.
Similarly, a blocking get() waits when there is no
item available.
mb.put(item);
mb.get(item);
This blocking behavior makes mailboxes useful for coordinating concurrent verification processes.
Step 9: Choose the right structure
A common beginner mistake is trying to use the same data structure for every problem. Instead, ask what behavior the testbench needs.
| Need | Good choice |
|---|---|
| Known number of elements | Fixed-size array |
| Runtime-sized collection | Dynamic array |
| Ordered collection that grows/shrinks | Queue |
| Lookup using a key | Associative array |
| Communication between concurrent processes | Mailbox |
Step 10: A small verification example
Consider a simple scoreboard-like problem. The testbench receives expected values and wants to process them in the same order they arrived.
int expected[$];
expected.push_back(100);
expected.push_back(200);
expected.push_back(300);
while (expected.size() > 0) begin
int value = expected.pop_front();
$display("Checking expected value: %0d", value);
end
The queue acts as a simple FIFO:
100 -> 200 -> 300
This is a pattern you should recognize when you later study UVM scoreboards and transaction streams.
Step 11: What happens when you mix objects and queues?
Verification environments commonly store transaction objects in queues. This is powerful, but it introduces an important concept: object handles.
bus_item item;
bus_item queue[$];
item = new();
item.addr = 8'h20;
queue.push_back(item);
The queue contains a handle to item. It does not contain
an independent copy of every field.
This matters when the same object is reused:
item.addr = 8'h30;
If you stored the same object handle in the queue, the object itself has changed. Understanding handles, copies, and object lifetime will become critical when you build real UVM scoreboards and sequences.
Try it yourself
Build a small SystemVerilog program that models a stream of bus transactions.
- Create a
bus_itemclass with address, data, and write fields. - Create a queue of
bus_itemobjects. - Create five transactions and add them to the queue.
- Print the queue size.
- Remove the transactions one at a time with
pop_front(). - Print each transaction as it is removed.
Bonus: Create an associative array that stores an expected data value for each address.
Quick check
- Which structure should you use when the number of elements is fixed at compile time?
- Which structure is best for a collection that grows and shrinks and should preserve insertion order?
- Which structure allows you to look up information using an address or transaction ID?
- What is the primary purpose of a mailbox?
- Does a queue containing class objects automatically make copies of those objects?
Show answers
- Fixed-size array.
- Queue.
- Associative array.
- Communication and synchronization between concurrent processes.
- No. It stores object handles unless you explicitly create/copy objects.
Key takeaways
- Arrays organize collections using indexes.
- Dynamic arrays allow the size to be selected at runtime.
- Queues are excellent for ordered transaction streams and FIFO-style processing.
- Associative arrays are useful for key-based lookup, such as address or transaction ID.
- Mailboxes provide communication and synchronization between concurrent processes.
- UVM uses these SystemVerilog concepts extensively, especially when building transaction-based verification components.
- Understanding object handles is essential when storing transactions in queues.