Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

Associative Array Iterators

SystemVerilog Data Types

Simple examples of associative array methods

Open in IDE
testbench.v
module tb;
  int aa_array [int];

  initial begin
    int idx;

    // Initialized indices in a mixed order, not 1,2,5,8
    aa_array[5] = 20;
    aa_array[8] = 10;
    aa_array[1] = 50;
    aa_array[2] = 100;

    // Finds the smallest index whose value is greater than the given index
    // Current index is 0, the next smallest value out of 1,2,5,8 is 1
    aa_array.next(idx);
    $display("aa_array[%0d]=%0d", idx, aa_array[idx]);

    // Current index is 1, the next smallest value out of 2,5,8 is 2
    aa_array.next(idx);
    $display("aa_array[%0d]=%0d", idx, aa_array[idx]);

    // Finds the largest index whose value is smaller than the given index
    // Current index is 5, the next largest value out of 1,2 is 2
    idx = 5;
    aa_array.prev(idx);
    $display("aa_array[%0d]=%0d", idx, aa_array[idx]);
  end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.