Tutorials

Learn More

Associative array in SystemVerilog

An associate array is used where the size of a collection is not known or data space is sparse.

Associative array declaration

data_type array_name [ index_type ];
bit[7:0] assoc_array [int];

Associative array methods

Methods

Description

function int size();

Returns the size of an associative array. Returns 0 if an array is empty

function int num();

Returns the number of entries of an associative array.

function int exists (input index)

Returns 1 if an element exists at a specified index else returns 0.

function int first (value)

Assign a value of the first index to the ‘value’ variable else returns 0 or an empty array.

function int last (value)

Assign a value of the last index to the ‘value’ variable else returns 0 or an empty array.

function int prev (value)

Assign a value of the previous index to the ‘value’ variable.

function int next (value)

Assign a value of the next index to the ‘value’ variable.

function void delete (input index)

Delete array entry for mentioned array index.

function void delete ()

Delete complete array means all entries will be removed.

Why do we need an associative array in SystemVerilog?

  1. In a dynamic array, we need to allocate memory before using it. But in an associative array, memory can be allocated when it is used.
  2. A dynamic array is specific for a particular data type. When it comes to an associative array, elements of an array can be of any type. We can store the concatenation of various data types or class structures as well.

Associative array Example

module associative_array_example;
  typedef enum {TRANS, RECEIVE, REPEATER} tr_type;
  bit [7:0] array_enum [tr_type];
  bit [7:0] array_int [int];
  
  initial begin
    
    array_enum[TRANS] = 10;
    array_enum[RECEIVE] = 20;
    array_enum[REPEATER] = 30;
    
    foreach (array_enum[i]) $display("array_enum[%s] = %0d", i.name(), array_enum[i]);
    
    array_int[5] = 2;
    array_int[10] = 4;
    array_int[7] = 6;
    
    foreach (array_int[i]) $display("array_int[%0d] = %0d", i, array_int[i]);
  end
endmodule

Output:

array_enum[TRANS] = 10
array_enum[RECEIVE] = 20
array_enum[REPEATER] = 30
array_int[5] = 2
array_int[7] = 6
array_int[10] = 4

Associative array methods Example

module associative_array_example;
   bit [7:0] array [int];
   int index;
  
  initial begin
    
    array[5] = 2;
    array[10] = 4;
    array[7] = 6;
    array[9] = 8;
    array[20] = 10;
    array[13] = 12;
    
    // Print array elements
    foreach (array[i]) $display("array[%0d] = %0d", i, array[i]);
    
    // Print array size and number of entries
    $display("size = %0d, Number of entries = %0d of array", array.size(), array.num());
    $display("--------------------------");
    
    // exists method
    if(array.exists(7)) $display("An element exists at index = 7");
    else $display("An element doesn't exists at index = 7");
    
    if(array.exists(8)) $display("An element exists at index = 8");
    else $display("An element doesn't exists at index = 8");
    $display("--------------------------");
    
    // first, last, prev, next method
    array.first(index);
    $display("First index of array = %0d", index);
    
    array.last(index);
    $display("Last index of array = %0d", index);
    
    index = 9;
    array.prev(index);  // Previous index of 9
    $display("Prev index of 9 is %0d", index);
    
    index = 10;
    array.next(index); // Next index of 10
    $display("Next index of 10 is %0d", index);
    
    $display("--------------------------");
    // Delete particular index
    array.delete(7);
    
    // Print array elements
    $display("After deleting element having index 7");
    foreach (array[i]) $display("array[%0d] = %0d", i, array[i]);
    $display("--------------------------");
    
    // Delete complete array
    array.delete();
    $display("size = %0d of array", array.size());
  end
endmodule

Output:

array[5] = 2
array[7] = 6
array[9] = 8
array[10] = 4
array[13] = 12
array[20] = 10
size = 6, Number of entries = 6 of array
--------------------------
An element exists at index = 7
An element doesn't exists at index = 8
--------------------------
First index of array = 5
Last index of array = 20
Prev index of 9 is 7
Next index of 10 is 13
--------------------------
After deleting element having index 7
array[5] = 2
array[9] = 8
array[10] = 4
array[13] = 12
array[20] = 10
--------------------------
size = 0 of array

System Verilog Tutorials