Unpacked Arrays in SystemVerilog Testbench

There are 4 types of unpacked arrays commonly used in SystemVerilog testbench:

  1. Fixed-size Arrays
  2. Dynamic Arrays
  3. Queues
  4. Associated Arrays

The following shows how to instantiate these unpacked arrays

// Fixed-size arrays
// type array_name[size], for example:
int numbers[5]; // array of 5 integers, with index from 0 to 4
byte byteArr[3]; // array of 3 bytes


// Dynamic arrays
// type array_name[], for example:
logic[7:0] mem[][]; // 2-dimension dynamic array
mem = new[4]; // allocate first dimension
foreach (mem[i])
    mem[i] = new [i*2]; // allocate second dimension


// Queues
// type array_name[$[:bound]], for example:
integer queue[$];
// can use following built-in functions to form a FIFO or stack
// push_back(), push_front(), pop_back(), pop_front(), etc.


// Associated arrays
// type array_name[index_type], for example:
byte opcode[string]; // indexed by string
opcode[“MINUS”] = 7; // create index “MINUS” memory

The table below shows a comparison across all array types above, especially in terms of performance:

TypeMemoryIndexApplication & Performance
Fixed SizeAllocated at compile-time; unchangeable afterwardsNumericalGives the best performance; Use when array size is known and fixed before simulation
DynamicAllocated at run-time; changeable at run-timeNumericalGives good performance; Use when you need random read / write access to any element of the variable sized array
QueuePush-Pop at run-time to change sizeNumericalGives good performance; Use for stack, CAM applications, or Scoreboard queues
AssociativeWrite at run-time to allocate memoryTypedGives moderate performance; Useful for sparse memory applications; Use when creating hash tables

Subscribe

Enter your email to get updates from us. You might need to check the spam folder for the confirmation email.

Leave a comment