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

Class Handle: Fixed Array

SystemVerilog Classes & OOP

Runnable example from the SystemVerilog Class Handle tutorial (Fixed Array)

Open in IDE
testbench.sv
// Define the Packet class
class Packet;
    int count;
    // Constructor to initialize count
    function new(int c = 0);
        count = c;
    endfunction
endclass

module tb;

    // Declare a fixed array of Packet objects
    Packet fixed_array[5];

    initial begin
        $display("--- Fixed Array Example ---");

        // Initialize each element of the fixed array
        for (int i = 0; i < 5; i++) begin
            fixed_array[i] = new(i * 10); // Create a new Packet object and assign it
        end

        // Access and display data from the fixed array
        for (int i = 0; i < 5; i++) begin
            $display("fixed_array[%0d].count = %0d", i, fixed_array[i].count);
        end

        // Example of accessing a null element if not initialized (would cause an error if dereferenced)
        // assert(fixed_array[0] != null); // Check if initialized
    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.