Saturday 13 September 2014

Mailbox in System Verilog

Mailbox is the most common tool to for the communication between two class or processes while writing System Verilog test bench.
Conceptually maibox behaves like a real mailboxes that we use in our day to day life !! When latter is received in mailbox the person or owner of that mailbox can retrieve that mailbox if latter is not received person waits for latter. Same way mailbox works in System Verilog. System Verilog provides methods in mailbox to transfer controlled data between the process or different class.

Mailbox is nothing but a class. So there are some functions to manipulate queue of our object in mailbox like. put(), get(), new(), num(), try_put(), try_get() etc.
Mailboxes are created for either bounded or unbounded size of the queue. By default mailbox are created for unbounded size.  For bounded size when mailbox is full process can not put another data in mailbox. While for unbounded mailbox there is no such limits.

An example of creating a mailbox is as follows.
                  maibox mbx;
Mailbox is inbuilt class in system verilog that provides following methods for manipulation.

new()

Mailboxes are created using new() method.
                 prototype : function new(int bount = 0);
This new function returns handle of the mailbox. Input argument indicates the size of the mailbox user wants to keep in case if it is bound mailbox. Default value of this argument is 0. which indicated infinity size of the mailbox(Not practically !! Depends on RAM fo your machine). Bound value must be positive. Negative value of the bound will cause indefinite behavior of the mailbox.

num()

Number function of the mailbox gives total number of messages in mailbox.
  Prototype  : function int num();

put()

This method puts message in mailbox();
   Prototype : task put(singular message);
If mailbox is bounded size and if full than put() task is suspended till there is enough space in the mailbox. 

try_put();

This method puts message in mailbox(); but it is non-blocking method. 
  Prototype : function int try_put(singular message);
If mailbox is of bounded size and full than this function dose not put message mailbox and return without doing anything. If message has been put successfully in mailbox this method returns 0 otherwise it returns 1.

get()

This method fetches message from mailbox(). 
Prototype : task get(singular message);
If mailbox is empty this task suspends the process until some other process puts message in mailbox. This behavior is independent of size of the mailbox.

try_get()

This function fetches message from mailbox ; but it is not blocking process. 
Prototype : function int try_get(singular message);
If maibox is of bounded size and empty than this function does not retrieve any data from mailbox. If mailbox is empty this function returns 0 as it is not able to fetch any data. Other wise this function returns 1. 

peek()

This function does not remove data from mailbox queue it just copies the first data in mailbox queue.
 prototype : taskvtry_peek(ref singular message)
The peek() method copies one message from mailbox queue without removing message form mailbox. So it mailbox is empty than the current process blocks until the message gets into mailbox.

try_peek();

The try_peek() method attempts to copy message from mailbox without blocking.
Prototype :  function int try_peek(ref singular message);
Try peek method attempts to copy one message from the mailbox without removing the message from the mailbox queue. If mailbox is empty than it returns 0. 

Parametrize a Maibox

By default mailbox can be of any type. You can use mailbox to store int, string, user defined class etc. This means single mailbox can be used to send and receive any kind of data. This is really powerful mechanism but that also can lead us to run time error due to type mismatch between message in mailbox and type used to retrieve data as argument. For most of the cases mailbox is used to transfer some perticular type of message. In this case it is useful to detect type mismatch error during compile time. 

Prototype for parametrized mailbox is as follows :

     mailbox #(type = dynamic_type)

For example  we want to take mailbox of class ahb_data, this can be declare as follows.

    mailbox  #(ahb_data) mbx;

mbx is created mailbox of type ahb_data. So we can only add object of ahb_data only in this mbx mailbox non other than this type of data gives error.

The methods like get(), put(), try_get(), try_put(), num(), peek(), try_peek() same for normal mailbox as well as parametrized mailbox.



No comments:

Post a Comment