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

foreach loop: Using foreach in Verification

SystemVerilog Control Flow & Loops

Runnable example from the SystemVerilog foreach loop tutorial (Using foreach in Verification)

Open in IDE
testbench.sv
module tb;
  int pkt_count [string];        // associative array indexed by a string

  initial begin
    pkt_count["write"] = 7;
    pkt_count["read"]  = 12;
    pkt_count["idle"]  = 30;

    // Visits existing keys in ascending order: "idle", "read", "write"
    foreach (pkt_count[key])
      $display ("%s : %0d", key, pkt_count[key]);
  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.