System Verilog: Associative Arrays

Associative array is one of aggregate data types available in system verilog.

We have already discussed about dynamic array, which is useful for dealing with contiguous collection of variables whose number changes dynamically.

When the size of the collection is unknown or the data space is sparse, an associative array is used, which does not have any storage allocated unitil it is used. That means, it is dynamically allocated, but has non-contiguous elements. Associative array’s index expression is not restricted to integral expressions, but can be of any type.

Associative array uses key value pairs and it implements a look up table. That is, associative array maintains the entries that have been assigned values and their relative order according to the index data type. The associative array methods available ensure fastest access to all array elements.

Associative arrays can be assigned only to another Associative array of a compatible type and with the same index type.In the same way, associative arrays can be passed as arguments only to associative arrays of a compatible type and with the same index type.

Associative array literals use the ‘{index:value} syntax with an optional default index. Like all other arrays, an associative array can be written one entry at a time, or the whole array contents can be replaced using an array literal.

If a default value is specified, then reading a nonexistent element shall yield the specified default value, and no warning shall be issued.

Below example shows associative array declarations and adding elements to the array. The entire array can be displayed using do while. Some of the properties of associative arrays which are specified using wildcard index type are also shown below.

  • The ordering is numerical (smallest to largest).
  • Nonintegral index values are illegal and result in an error.
  • A 4-state index value containing X or Z is invalid.
  • Indexing expressions are self-determined and treated as unsigned.
  • A string literal index is automatically cast to a bit vector of equivalent size.
  • It shall not be used in a foreach loop or with an array manipulation method

In the above code, you can see that entry is done in different order, but final array elements have been printed from smallest to largest.

If a read operation uses an index that is a 4-state expression with one or more x or z bits, or an attempt is made to read a nonexistent entry, then a warning shall be issued and the nonexistent entry value for the array type shall be returned. A user-specified default shall not issue a warning.If an invalid index is used during a write operation, the write shall be ignored, and a warning shall be issued.

Associative Array Methods
num() size():
The num() or size() method returns the number of entries in the associative array. If the array is empty, it returns 0.

exists()
The exists() function checks whether an element exists at the specified index within the given array. It returns 1 if the element exists; otherwise, it returns 0.

delete() :
The delete() method removes the entry at the specified index. If the index is specified, then the delete() method removes the entry at the specified index. The method does not issue any warning, if the entry to be deleted does not exist. If the index is not specified, then the delete() method removes all the elements in the array.

first() :
The first() method assigns to the given index variable the value of the first (smallest) index in the associative array. It returns 0 if the array is empty; otherwise, it returns 1.

next() :
The next() method finds the smallest index whose value is greater than the given index argument.If there is a next entry, the index variable is assigned the index of the next entry, and the function returns 1. Otherwise, the index is unchanged, and the function returns 0.

last() :
The last() method assigns to the given index variable the value of the last (largest) index in the associative array. It returns 0 if the array is empty; otherwise, it returns 1.

prev():
The prev() function finds the largest index whose value is smaller than the given index argument. If there is a previous entry, the index variable is assigned the index of the previous entry, and the function returns 1. Otherwise, the index is unchanged, and the function returns 0.

The argument that is passed to any of the four associative array traversal methods first(), last(), next(), and prev() shall be assignment compatible with the index type of the array.

If the argument has an integral type that is smaller than the size of the corresponding array index type, then the function returns –1 and shall truncate in order to fit into the argument.

5 Comments

  1. sujay

    Hi
    I want to read a associative array element and pass it to a function that has an argument of the type string.
    I get this error:
    The type of the target is ‘string’, while the type of the source is ‘string$[$]’

    Thanks

    1. Hi Sujay

      Please check a simple example below.

      module assoc_array_str;
      int AarrStr [ string ];
      string str;

      function string func_aa(string str1);
      $display( “String Inside function func_aa : %s”,str1);
      return str1;
      endfunction

      initial begin
      AarrStr[“pencil”] = “blue”;
      $display( “String – Directly from Associative Array : %s”, AarrStr[“pencil”] );
      str = func_aa(AarrStr[“pencil”]);
      $display( “String from function call : %s”, str );

      end
      endmodule

      —Result ——-
      String – Directly from Associative Array : blue
      String Inside function func_aa : blue
      String from function call : blue

Leave a Reply

Your email address will not be published. Required fields are marked *