|
|
|
|
|
|
|
|
|
|
|
Mailboxes
|
|
|
A mailbox is a mechanism to exchange messages between processes. Data can be sent to a mailbox by one process and retrieved by another. Mailbox can be used a FIFO if required. Data can be any valid systemVerilog data types, including class data types. |
|
|
|
|
|
SystemVerilog provides following methods for working with mailbox. |
|
|
|
|
|
- Mailbox allocation : new()
- Put data : put()
- Try to place a message in a mailbox without blocking: try_put()
- Get data : get() or peek()
- Try to retrieve a message from a mailbox without blocking: try_get() or try_peek()
- Retrieve the number of messages in the mailbox: num()
|
|
|
|
|
|
Nonparameterized mailboxes are typeless, that is, a single mailbox can send and receive different types of data. Thus, in addition to the data being sent (i.e., the message queue), a mailbox implementation must maintain the message data type placed by put(). This is required in order to enable the run-time type checking. |
|
|
|
|
|
new()
|
|
|
Mailboxes are created with the new() method. |
|
|
|
|
|
function new(int bound = 0);
|
|
|
|
|
|
- The new() function returns the mailbox handle or, if the mailbox cannot be created, null.
- If the bound argument is 0, then the mailbox is unbounded (the default) and a put() operation shall never block.
|
|
|
|
|
|
num()
|
|
|
The number of messages in a mailbox can be obtained via the num() method. |
|
|
|
|
|
function int num();
|
|
|
|
|
|
- The num() method returns the number of messages currently in the mailbox.
- The returned value should be used with care because it is valid only until the next get() or put() is executed on the mailbox.
|
|
|
|
|
|
put()
|
|
|
The put() method places a message in a mailbox. |
|
|
|
|
|
task put( singular message);
|
|
|
|
|
|
- The message is any singular expression, including object handles.
- The put() method stores a message in the mailbox in strict FIFO order.
- If the mailbox was created with a bounded queue, the process shall be suspended until there is enough space in the queue.
|
|
|
|
|
|
try_put()
|
|
|
The try_put() method attempts to place a message in a mailbox. |
|
|
|
|
|
function int try_put( singular message);
|
|
|
|
|
|
- The try_put() method stores a message in the mailbox in strict FIFO order.
- If the mailbox is full, the method returns 0.
|
|
|
|
|
|
|
|
|
|
|
|
get()
|
|
|
The get() method retrieves a message from a mailbox. |
|
|
|
|
|
task get( ref singular message );
|
|
|
|
|
|
- The get() method removes one message from queue.
- If the mailbox is empty, then the current process blocks until a message is placed in the mailbox.
- If the type of the message variable is not equivalent to the type of the message in the mailbox, a run-time error is generated.
|
|
|
|
|
|
try_get()
|
|
|
The try_get() method attempts to retrieves a message from a mailbox without blocking. |
|
|
|
|
|
function int try_get( ref singular message );
|
|
|
|
|
|
- The try_get() method tries to retrieve one message from the mailbox.
- If the mailbox is empty, then the method returns 0.
|
|
|
|
|
|
peek()
|
|
|
The peek() method copies a message from a mailbox without removing the message from the queue. |
|
|
|
|
|
task peek( ref singular message );
|
|
|
|
|
|
- The peek() method copies one message from the mailbox without removing the message from the mailbox queue.
- If the mailbox is empty, then the current process blocks until a message is placed in the mailbox.
|
|
|
|
|
|
try_peek()
|
|
|
The try_peek() method attempts to copy a message from a mailbox without blocking. |
|
|
|
|
|
function int try_peek( ref singular message );
|
|
|
|
|
|
- The try_peek() method tries to copy one message from the mailbox without removing the message from the mailbox queue.
- If the mailbox is empty, then the method returns 0.
|
|
|
|
|
|
Example : Mailbox
|
|
|
|
|
|
1 program mailbox_ex;
2 mailbox checker_data = new();
3
4 initial begin
5 fork
6 input_monitor();
7 checker();
8 join_any
9 #1000 ;
10 end
11
12 task input_monitor();
13 begin
14 integer i = 0;
15 // This can be any valid data type
16 bit [7:0] data = 0;
17 for(i = 0; i < 4; i ++) begin
18 #(3);
19 data = $random();
20 $display("[%0d] Putting data : %x into mailbox", $time,data);
21 checker_data.put(data);
22 end
23 end
24 endtask
25
26 task checker();
27 begin
28 integer i = 0;
29 // This can be any valid data type
30 bit [7:0] data = 0;
31 while (1) begin
32 #(1);
33 if (checker_data.num() > 0) begin
34 checker_data.get(data);
35 $display("[%0d] Got data : %x from mailbox", $time,data);
36 end else begin
37 #(7);
38 end
39 end
40 end
41 endtask
42
43 endprogram
You could download file mailbox_ex.sv here
|
|
|
|
|
|
Simulation : Mailbox
|
|
|
|
|
|
[3] Putting data : 24 into mailbox
[6] Putting data : 81 into mailbox
[9] Putting data : 09 into mailbox
[9] Got data : 24 from mailbox
[10] Got data : 81 from mailbox
[11] Got data : 09 from mailbox
[12] Putting data : 63 into mailbox
[12] Got data : 63 from mailbox
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|