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

'extern': Where extern Actually Earns Its Keep

SystemVerilog Advanced Features

Runnable example from the SystemVerilog 'extern' tutorial (Where extern Actually Earns Its Keep)

Open in IDE
testbench.sv
class Counter;
  local int value;

  extern function new (int start_value = 0);
  extern function void increment ();
  extern function void decrement ();
  extern function int get_value ();
endclass

function Counter::new (int start_value = 0);
  value = start_value;
endfunction

function void Counter::increment ();
  value++;
endfunction

function void Counter::decrement ();
  value--;
endfunction

function int Counter::get_value ();
  return value;
endfunction

module tb;
  initial begin
    Counter c = new (5);
    c.increment ();
    c.increment ();
    c.decrement ();
    $display ("value = %0d", c.get_value ());
  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.