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

Mailbox: peek() and try_peek(): Looking Without Removing

SystemVerilog Interprocess Communication

Runnable example from the SystemVerilog Mailbox tutorial (peek() and try_peek(): Looking Without Removing)

Open in IDE
testbench.sv
// Note: Verilator needs a typed mailbox, so the mailboxes below are declared as mailbox #(int).
module tb;
  mailbox #(int) mbx = new ();

  initial begin
    int peeked, gotten;

    mbx.put (42);
    $display ("[%0t] Put item 42, size=%0d", $time, mbx.num());

    // Look at the item without removing it
    mbx.peek (peeked);
    $display ("[%0t] Peeked item %0d, size=%0d", $time, peeked, mbx.num());

    // Now actually remove it
    mbx.get (gotten);
    $display ("[%0t] Got item %0d, size=%0d", $time, gotten, mbx.num());

    // Mailbox is empty now - try_peek() fails instead of blocking
    if (mbx.try_peek (peeked))
      $display ("[%0t] try_peek succeeded", $time);
    else
      $display ("[%0t] try_peek found the mailbox #(int) empty", $time);
  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.