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: try_put(): Checking for Room Without Blocking

SystemVerilog Interprocess Communication

Runnable example from the SystemVerilog Mailbox tutorial (try_put(): Checking for Room Without Blocking)

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 (1);   // bound = 1, room for only one item

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

    // The mailbox #(int) is already full - try_put() fails instead of blocking
    if (mbx.try_put (20))
      $display ("[%0t] try_put(20) succeeded", $time);
    else
      $display ("[%0t] try_put(20) failed - mailbox #(int) is full, moving on instead of blocking", $time);

    #5 begin
      int x;
      mbx.get (x);
      $display ("[%0t] Got item %0d, size=%0d", $time, x, mbx.num());
    end

    // Now that item 10 has been removed, there's room again
    if (mbx.try_put (20))
      $display ("[%0t] try_put(20) succeeded now that there's room", $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.