What you practice
Associative Arrays (2.3). Declare associative arrays with correct index types for sparse memory modeling. 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 use associative arrays in SystemVerilog to model sparse memories efficiently, avoiding wasted memory for unused addresses.
Learning objectives
- Understand the syntax and purpose of associative arrays.
- Identify the correct index type for associative array keys.
- Use
exists()to safely check for key presence before reading. - Simulate scattered memory access patterns using associative arrays.
Theory
Associative arrays (hash maps) map arbitrary keys to values without requiring contiguous storage. They are ideal for sparse memories where only a few addresses are used.
| Feature | Description |
|---|---|
| Key type | Any type (e.g., int, logic [7:0], string) — must match the declared index |
| Declaration | type array[key_type] |
| Key check | array.exists(key) returns 1 if key exists |
| Access | array[key] — no bounds checking, so exists() is recommended first |
In this lesson, the associative array mem uses int as the key type, matching the 8-bit address written as an int literal (e.g., mem[10]).
Practice
Open RtlStudio and select this chapter. Files appear automatically in Explore. Open student/tb/tb.sv and fix the associative array declaration to use the correct index key type.
- In
student/tb/tb.sv, locate the line withlogic [31:0] __TODO__. - Replace
__TODO__with the correct associative array declaration usingintas the key type. - Run Simulation and verify the Log panel shows expected
INFO:messages.
The associative array must match the key type used in the writes (mem[10], mem[100], etc.), which are int literals.
Summary
You practiced using associative arrays to model sparse memories efficiently by declaring an array with integer keys and storing values at scattered addresses. This approach avoids allocating memory for unused addresses, making it ideal for modeling large, sparse data structures. Associative arrays use hash-based indexing, allowing flexible key types like integers while maintaining fast access. The key takeaway is that associative arrays provide an efficient, scalable alternative to traditional packed or unpacked arrays when memory usage must be minimized.
Run it in the Verilog IDE
- Open the lesson in the IDE. The SystemVerilog for this topic is already in the editor.
- Lint with Slang before you simulate. It covers both Verilog and SystemVerilog in this IDE.
- Run Verilator and inspect the VCD. Interfaces, classes, and assertions count only if the simulator agrees.
- 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 Associative Arrays. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.