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

$unit Compilation Scope: Declaring Something in $unit Scope

SystemVerilog Advanced Features

Runnable example from the SystemVerilog $unit Compilation Scope tutorial (Declaring Something in $unit Scope)

Open in IDE
testbench.sv
// Outside any module - this typedef and function live in $unit scope
typedef enum logic [1:0] {OK, WARN, ERROR} status_t;

function automatic string status_str(status_t s);
  return s.name();
endfunction

module tb;
  initial begin
    status_t a = OK;
    status_t b = ERROR;

    $display("a = %s", status_str(a));
    $display("b = %s", status_str(b));
  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.