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

Virtual Methods: Without virtual

SystemVerilog Classes & OOP

Runnable example from the SystemVerilog Virtual Methods tutorial (Without virtual)

Open in IDE
testbench.sv
// Without declaring display() as virtual
class Packet;
   int addr;

   function new (int addr);
      this.addr = addr;
   endfunction

   // This is a normal function definition which
   // starts with the keyword "function"
   function void display ();
		$display ("[Base] addr=0x%0h", addr);
   endfunction
endclass

// ExtPacket from the Inheritance article, repeated here since this
// article's examples build on it directly
class ExtPacket extends Packet;
   int data;

   function new (int addr, data);
      super.new (addr);
      this.data = data;
   endfunction

   function void display ();
		$display ("[Child] addr=0x%0h data=0x%0h", addr, data);
   endfunction
endclass

module tb;
   Packet bc;
   ExtPacket sc;

	initial begin
        sc = new (32'hfeed_feed, 32'h1234_5678);

        bc = sc;
		bc.display ();
	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.