SourceSV Verification Directory

Associative Arrays

Associative arrays in SystemVerilog allow you to index arrays with arbitrary data types. An associative array can be stored by the simulator as a tree or a hash table. An associative array is declared with a data type in square brackets. Only the elements that are explicitly assigned are stored, making it memory-efficient for sparse arrays.

Example of associative arrays in SV is shown below:

module associative_arrays ();

  byte assoc[byte];

  byte indx = 1;

  int pow_2[int] = '{0:1, 1:2, 2:4, 3:8, 4:16};

  initial begin

    do begin
      assoc[indx] = indx;
      indx = indx << 1;
    end while (indx != 0);

    $display("--------------  Associative Array   --------------");
    foreach(assoc[i]) $display("\t assoc[%0d] = %0d", i, assoc[i]);


    $display("--------------  Associative Array   --------------");
    for(int i=5; i<10; i++)
      pow_2[i] = 1 << i;
    $display("%p", pow_2);

  end
endmodule

Execute the example in EDA Playground