Associative Arrays

Arrays indexed by any key — numbers, strings, enums — that only store what was written: sparse memories and lookup tables.

An associative array stores elements under keys of any type, and only the keys you actually write use memory. A model of a 4 GB memory that the test touches in only a few places needs a few entries, not 4 billion.

Methods

if (mem[addr] == 0)... // treats unwritten as 0 if (!mem.exists(addr))... // unwritten else if (mem[addr] == 0)...

Reading a missing key returns the default value, which cannot be told apart from a real 0. Check exists() when the difference matters.

Declare an associative array of 8-bit values indexed by a 64-bit address.

bit [7:0] mem [bit [63:0]];

Why is an associative array a good memory model for a testbench?

It only stores the addresses that were written, so huge address spaces cost almost nothing, and exists() shows which addresses were never written.