Dynamic Arrays, Queues, and Associative Arrays
Choose a testbench collection for variable batches, transaction queues, and indexed results.
Collections for the testbench
Dynamic collections manage a number of transactions that is unknown at compile time. They belong mainly in verification and high-level models rather than synthesizable RTL.
Three forms serve different needs:
- a dynamic array for a size allocated at run time;
- a queue for easy insertion and removal at its ends;
- an associative array for elements indexed by sparse keys.
Dynamic array
int samples[];
samples = new[128];
foreach (samples[i])
samples[i] = i * 2;
$display("count=%0d", samples.size());
samples.delete();new[128] allocates 128 elements. A new allocation can preserve part of the old content with new[new_size](samples). delete() releases the array.
This format fits a data block whose size is known at the beginning of an operation and which needs frequent direct indexing.
Queue