Search code examples
associative-arraysystem-verilog

System Verilog Nested Associative Array


How do you declare and initialize a nested associative array in SystemVerilog?

/*
  Creating an associative array(AA) called timings such that
  each key contains an AA with a list of relevant key value pairs
*/
typedef string timingObj [string];
timingObj timings [string] = {"A": {"B" : "C"}, "X": {"Y" : "Z"} };
//string timings [timingObj] = {"A": {"B" : "C"}, "X": {"Y" : "Z"} }; //Same error

timingObj t;
$cast(t, timings["A"]); // t = {"B" : "C"}
$display("%s", timings["A"]);
$display("%s", t["B"]);

The code above results in compiler errors:

"Syntax error. Unexpected token: }. Expected tokens: ':'." "testbench.sv" 2
"Syntax error. Unexpected token: $cast[_SYSTEM_CAST]. Expected tokens: ';' , 'checker' , 'function' , 'task' , 'timeprecision' ... ." "testbench.sv" 6  6

Solution

  • Assignment patterns for associative arrays need a '{} mark in front to distinguish it from a concatenation {}. There are some cases where it is ambiguous (but not here). So write

    timingObj timings [string] = '{"A": '{"B" : "C"}, "X": '{"Y" : "Z"} };