SV Part-6 communication

Inter process communication
Inter-Process Communication (IPC) means:
Two or more processes (different blocks of code running in parallel) communicating or synchronizing with each other.
| SV Feature | Why |
| Semaphore | Created using new(), used for synchronization or resource control |
| Mailbox | Created using new(), used for data passing |
| Event | Event is a built-in synchronization object |
All three are IPC mechanisms in SystemVerilog testbenches.
They differ in what problem they solve:
Events - sync,
Semaphores - resource control,
Mailboxes - data transfer.
you choose the right IPC primitive depending on whether you need synchronization, resource sharing, or message passing
Events
In Verilog , event is just a flag.
It does not carry data.
It is not persistent : if you miss the trigger at that moment, you never catch it later.
System Verilog event is more powerful.
Supports persistent triggering : if the event was triggered before you wait, you still catch it.
Persistence means:
Even if the event is triggered before someone waits for it, the waiter will still see it.
Semaphores
What is a semaphore : A semaphore is a System Verilog built-in synchronization object that controls access to a shared resource using keys.
why we use semaphore : To prevent race conditions when multiple processes/threads want to use the same resource.
How it works :
semaphore is creatced with some number of keys
A thread gets() keys - enters critical section.
A thread puts() - leaves critical section.
if keys are not available - thread waits.
Methods in Semaphore
| Method | Meaning (Very Simple) |
| new(n) | Creates semaphore with n keys |
| get(k) | Take k keys (waits if not available) |
| try_get(k) | Try to take k keys (does NOT wait), returns 1/0 |
| put(k) | Return k keys back |
These 4 are the main methods. (Semaphore is simple only these are used in real testbenches.)
mailbox
mailbox is used to connect the classes
a mailbox does not explicitly use indices like an array or queue to access its contents. Instead, a mailbox operates as a FIFO (First-In-First-Out) queue, where messages are added to the end (via put()) and retrieved from the front (via get() or peek()).
mailbox does not provide a mechanism to access messages by an index (e.g., mbox[0], mbox[1]). Unlike arrays or queues, mailboxes are designed for inter-process communication with a strict FIFO order, and you can only interact with the head of the queue (for retrieval) or the tail (for insertion).
The mailbox methods (put(), get(), peek(), etc.) do not expose or allow direct access to specific positions in the mailbox’s internal storage.
Types: Based on the size declaration
1.Bounded mailbox syntax: mailbox <mailbox_name> = new(size);
2.Un-Bounded mailbox syntax: mailbox <mailbox_name> = new();
Based on the type
1.generic mailbx --> it allow different datatype syntax: mailbox <mailbox_name> = new();
2.parameterized mailbox syntax: mailbox #(parameter type)<mailbox_name> = new();
Table: SystemVerilog Mailbox Methods and Their Details
| Method | Description | Parameters | Return Value |
| new(size) | Creates a new mailbox, optionally specifying a size for bounded mailboxes. | size: Integer (default: 0 for unbounded). | Mailbox handle (reference to the created mailbox). |
| put(message) | Places a message into the mailbox. | message: Data to send (any type for generic mailbox, specific type for parameterized mailbox). | None (void). |
| try_put(value) | Non-blocking attempt to place a message into the mailbox. | message: Data to send. | 1 (success), 0 (failure, e.g., mailbox full). |
| get(value) | Retrieves a message from the mailbox. | message: Variable to store the retrieved data. | None (void). |
| try_get(value) | Non-blocking attempt to retrieve a message. | message: Variable to store the retrieved data. | 1 (success), 0 (failure, e.g., mailbox empty). |
| peek(message) | Retrieves a message without removing it from the mailbox. | message: Variable to store the retrieved data. | None (void). |
| try_peek(message) | Non-blocking attempt to peek at a message. | message: Variable to store the retrieved data. | 1 (success), 0 (failure, e.g., mailbox empty). |
| num() | Returns the number of messages in the mailbox. | None. | Integer (number of messages). |



