RTL Studio Open IDE

2.1 · SYSTEMVERILOG CLASS

Dynamic Arrays

Allocate dynamic arrays at runtime with new[] in testbench code.

Watch on YouTube

What you practice

Dynamic Arrays (2.1). Allocate dynamic arrays at runtime with new[] in testbench code. It is in Dynamic Data Types — Part 2 · Verification & OOP of the SystemVerilog Masterclass. Software-side verification: dynamic data structures, classes, and the first UVM master keys.

The same IDE is the SystemVerilog simulator. Slang lints the file, Verilator runs it, and the waveform viewer sits next to the source.

What you will learn

Today you will learn how to declare and allocate dynamic arrays at runtime in SystemVerilog using the new[] constructor, enabling flexible memory management during simulation.

Learning objectives

  • Declare a dynamic array using int dyn_arr[].
  • Allocate memory for the array at runtime using dyn_arr = new[size].
  • Iterate over the array and access its elements.
  • Reallocate the array with a different size during simulation.

Theory

Dynamic arrays in SystemVerilog have no fixed size at compile time. Their size is determined during simulation using the new[] constructor.

SyntaxDescription
int dyn_arr[];Declares a dynamic array of integers
dyn_arr = new[N];Allocates space for N elements
dyn_arr.size()Returns the current number of elements
dyn_arr[i]Accesses the i-th element

You can reallocate the array with a new size at any time — the old contents are preserved only if you use dyn_arr = new[N](dyn_arr) (copy constructor), but here we use plain new[N] to reset contents.

Practice

Open this chapter in RtlStudio. Files appear automatically in Explore.

  1. Open student/tb/tb.sv in the editor.
  2. Locate the two dyn_arr = new[__TODO__] lines.
  3. Replace each __TODO__ with the correct integer size: first 5, then 3.
  4. Run Simulation from the left sidebar and read the Log panel.
  5. Optionally, open the Waveform tab to inspect signal timing.

Summary

You practiced declaring and allocating dynamic arrays at runtime using the new[] constructor in a testbench. You iterated through the array and printed its contents to verify correct allocation and access. The key takeaway is that dynamic arrays allow flexible memory management during simulation, enabling size changes without recompilation.

Run it in the Verilog IDE

  1. Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
  2. Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
  3. Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
  4. Run Yosys when the lesson is synthesizable RTL, and keep testbench-only constructs out of that module.

The lesson is done when the simulation matches Dynamic Arrays. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.