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

Const: Global Constant Class Properties

SystemVerilog Classes & OOP

Runnable example from the SystemVerilog Const tutorial (Global Constant Class Properties)

Open in IDE
testbench.sv
class Jumbo_Packet;
  // max_size is a global constant, same for all Jumbo_Packet instances
  const int max_size = 5 * 10;

  byte payload [];

  function new(int size);
    // payload size is limited by the global constant max_size
    payload = new[ size > max_size ? max_size : size ];
  endfunction
endclass

module tb;
  Jumbo_Packet 	m_pkt;
  
  initial begin
    m_pkt = new(100);
  
    $display("size=%d", m_pkt.payload.size());
  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.