Skip to main content

SystemVerilog Arrays

· loading · loading · · ·
HDL SystemVerilog HDL
Axolot Logic
Author
Axolot Logic
Digital Design Engineer
Table of Contents
SystemVerilog - This article is part of a series.
Part 3: This Article

📦 SystemVerilog Arrays – Complete Guide
#

SystemVerilog extends the concept of arrays far beyond Verilog’s basic vectors. It introduces multiple powerful array types that allow for better data organization, random access, and dynamic behavior during simulation and testbench development.


🧱 1. Packed Arrays (Bit Vectors)
#

Packed arrays represent contiguous bits stored together — they are used like traditional Verilog vectors.

logic [7:0] byte_data;  // 8-bit packed vector
  • Always declared left-to-right ([MSB:LSB])
  • Treated as a single value in arithmetic and bitwise operations
  • Can be multi-dimensional:
logic [3:0][7:0] memory_word;  // 4 elements, each 8 bits

📚 2. Unpacked Arrays
#

Unpacked arrays are true arrays of elements, each stored separately.

logic [7:0] memory_array [0:15];  // 16 elements, each 8 bits
  • Declared to the right of the variable name
  • Useful for RAMs, register files, lookup tables, etc.
  • Multidimensional versions are supported:
logic [7:0] matrix [0:3][0:3];  // 4x4 byte matrix

🔁 3. Dynamic Arrays
#

Dynamic arrays are arrays whose size can change during simulation.

int dyn_array[];
dyn_array = new[10];    // Allocate 10 elements
dyn_array[0] = 5;
  • Only allowed in simulation/testbenches
  • Useful when the size is not known at compile-time
  • Support methods like .size(), .delete()
$display("Size = %0d", dyn_array.size());
dyn_array.delete();  // Deallocate

🧠 4. Associative Arrays
#

Associative arrays use arbitrary data types as an index (e.g., string, int).

int aa[string];       // Associative array indexed by string
aa["apple"] = 3;
aa["banana"] = 5;
  • Index types can be: int, string, enum, etc.

  • Useful for sparse data or lookups

  • Provide advanced methods like:

    • .exists(key)
    • .first(index)
    • .next(index)
    • .num()
if (aa.exists("apple")) $display("Found apple");

🪜 5. Queues
#

Queues are like dynamic arrays with FIFO (First-In-First-Out) behavior.

int q[$];       // Queue of ints
q.push_back(1);
q.push_back(2);
q.push_front(0);
  • Use $ to declare an unbounded queue

  • Common queue methods:

    • .push_front(), .push_back()
    • .pop_front(), .pop_back()
    • .insert(index, value), .delete(index)
    • .size(), .empty()
int x = q.pop_front();

📏 Summary Table
#

Type Resizable? Indexed By Usage
Packed Array No Numeric (fixed) Bit-level logic, RTL signals
Unpacked Array No Numeric (fixed) Memories, LUTs
Dynamic Array Yes Numeric Runtime-sized storage
Associative Array Yes Any scalar Sparse maps, lookup tables
Queue Yes Integer (auto) FIFO behavior, buffers

🛠️ Tip: Combining Packed and Unpacked Arrays
#

You can combine both packed and unpacked arrays to describe memory blocks:

logic [31:0] mem_array [0:1023];  // 1024 words, each 32 bits

Here:

  • [31:0] is packed (bit vector)
  • [0:1023] is unpacked (array of entries)

✅ Best Practices
#

  • Use packed arrays in RTL for efficient synthesis
  • Use dynamic, associative, and queue arrays in testbenches
  • Prefer queues for scoreboards and transaction FIFOs
  • Use methods (.size(), .exists()) to write safe and scalable test code

This modular array system is one of the key strengths of SystemVerilog, enabling both hardware modeling and high-level software-like testbench structures.


SystemVerilog - This article is part of a series.
Part 3: This Article

Related

SystemVerilog `fork...join`
· loading · loading
HDL SystemVerilog HDL
SystemVerilog Loops & Control Flow Statements
· loading · loading
HDL SystemVerilog HDL
SystemVerilog Tasks and Functions
· loading · loading
HDL SystemVerilog HDL
SystemVerilog Typedef & Alias
· loading · loading
HDL SystemVerilog HDL
SystemVerilog `interface`
· loading · loading
HDL SystemVerilog HDL
Blocking vs Non-Blocking Assignments
· loading · loading
HDL SystemVerilog HDL