Skip navigation.
Home

SystemVerilog Mailbox

Mailboxes are a way in SystemVerilog to provide inter-process communication. They essentially act like a FIFO where one process can write to the mailbox and the other process can read from the mailbox. This snippet demonstrates how to create a transmitter and receiver that will read and write to mailboxes. Writing to the mailbox is accomplished with the put() function and reading is accomplished with the get() function.

program mailbox_test();
 
class Transmitter;   // Transmitter sends values 0-19
mailbox mb;
 
  function new(mailbox mb);
    this.mb = mb;
  endfunction
 
  task run;
    for(int ii=0;ii<20;ii++) mb.put(ii);
  endtask
endclass
 
class Receiver;  // Receiver gets values from transmitter and displays them.
mailbox mb;
int       rx_val;
 
  function new(mailbox mb);
    this.mb = mb;
  endfunction
 
  task run;
    for(int ii=0;ii<20;ii++) begin
      mb.get(rx_val);
      $display("rx val = %d",rx_val);
    end
  endtask
endclass
 
mailbox mb;        // maibox used for communcation.
Transmitter t1;    // Transmitter.
Receiver r1;       // Receiver.
 
initial begin
  mb = new;        // Create new objects.
  t1 = new(mb);
  r1 = new(mb);
  fork             // Fork off the processes.
    t1.run();
    r1.run();
  join
end
endprogram
 
module tb_mailbox;  // kick off the test.
mailbox_test test();
endmodule