Choose a testbench collection for variable batches, transaction queues, and indexed results.
A testbench handles a variable number of transactions. Choosing a dynamic collection that matches the protocol avoids oversized fixed arrays and unnecessary matching logic.
Allocate a known-size block
A dynamic array provides dense indices:
int samples[];samples = new[128];foreach (samples[i]) samples[i] = i * 2;$display("count=%0d", samples.size());samples.delete();
new[128] allocates 128 elements. new[new_size](samples) can retain existing elements that fit the new size. delete() removes the contents. This suits a packet whose length is known before processing.
[$] declares an unbounded queue. push_back and pop_front provide FIFO behavior; push_front, pop_back, insert, and delete provide other operations.
Check for an empty queue before removal. For ordered responses, it directly represents scoreboard expectations.
If a response has an identifier but requires a queue search:
typedef struct { int unsigned id; logic [31:0] data;} expected_t;expected_t pending_q[$];int matches[$];matches = pending_q.find_index(item) with (item.id == response_id);case (matches.size()) 0: $error("No expected item for id=%0d", response_id); 1: begin check_word(pending_q[matches[0]].data, observed_data); pending_q.delete(matches[0]); end default: $error("Multiple expected items for id=%0d", response_id);endcase
find_index returns a queue of int indices. No match means an unexpected response; multiple matches mean ambiguous expectations. Compare the single match against observed data before removing it.
Find a response by key
logic [31:0] expected_by_id[int unsigned];expected_by_id[transaction_id] = predicted_value;if (expected_by_id.exists(response_id)) begin assert (observed === expected_by_id[response_id]) else $error("Response mismatch for id %0d", response_id); expected_by_id.delete(response_id);end else begin $error("Unexpected response id %0d", response_id);end
An associative array contains only created keys. It suits reordered responses with unique identifiers. Also check that a new command does not overwrite a still-pending key.
first, next, last, and prev visit keys according to index type rather than insertion order.
Choose and drain
Use queues for ordered streams, associative arrays for keyed lookup, and dynamic arrays for resizable dense blocks. These constructs mainly belong in simulation models.
Explain any remaining entries at test completion: they may indicate missing responses. An empty collection does not establish correctness if entries were removed without data comparison.