|
|
|
|
|
|
|
|
|
|
|
Introduction
|
|
|
High-level and easy-to-use synchronization and communication mechanism are essential to control the kinds of interactions that occur between dynamic processes used to model a complex system or a highly reactive testbench. Verilog provides basic synchronization mechanisms (i.e., -> and @), but they are all limited to static objects and are adequate for synchronization at the hardware level, but fall short of the needs of a highly dynamic, reactive testbench. At the system level, an essential limitation of Verilog is its inability to create dynamic events and communication channels, which match the capability to create dynamic processes. |
|
|
|
|
|
SystemVerilog provides following to help interprocess communication |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Semaphore
|
|
|
Semaphore is used for lock/unlock of a common resource. If resource is in unlock state the resource can be used, if it is in lock state, then it can not be used. |
|
|
|
|
|
Lets assume that in a Verification env, we have single ethernet port/driver and there are 2 packet generators using this one ethernet port, one generating normal frames and other generating flow control (pause) frames. |
|
|
|
|
|
So each of this two packet generators can use semaphone to if ethenet driver is free (unlock) state, before it can transmit the frame. So if the driver is free, then it locks the drivers and transmits the frame, once tramitted, it unlocks the drivers. |
|
|
|
|
|
When driver is in lock state, it will wait till driver is in unlock state. |
|
|
|
|
|
Semaphore in SystemVerilog supports following methods for above operations. |
|
|
|
|
|
- Semaphore allocation : new()
- Using semaphore keys : get()
- Returing semaphore keys : put()
- Try to obtain one or more keys without blocking: try_get()
|
|
|
|
|
|
|
|
|
new()
|
|
|
Semaphores are created with the new() method. |
|
|
|
|
|
function new(int keyCount = 0 );
|
|
|
|
|
|
Where |
|
|
|
|
|
- keycount : The keyCount specifies the number of keys initially allocated to the semaphore bucket.
- The new() function returns the semaphore handle or, if the semaphore cannot be created, null.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
put()
|
|
|
The semaphore put() method is used to return keys to a semaphore. |
|
|
|
|
|
task put(int keyCount = 1);
|
|
|
|
|
|
Where |
|
|
|
|
|
- The keyCount specifies the number of keys being returned to the semaphore. The default is 1.
|
|
|
|
|
|
get()
|
|
|
The semaphore get() method is used to procure a specified number of keys from a semaphore. |
|
|
|
|
|
task get(int keyCount = 1);
|
|
|
|
|
|
Where |
|
|
|
|
|
- The keyCount specifies the required number of keys to obtain from the semaphore. The default is 1.
- If the specified number of keys is not available, the process blocks until the keys become available.
- The semaphore waiting queue is first-in first-out (FIFO).
|
|
|
|
|
|
try_get()
|
|
|
The semaphore try_get() method is used to procure a specified number of keys from a semaphore, but without blocking. |
|
|
|
|
|
function int try_get(int keyCount = 1);
|
|
|
|
|
|
Where |
|
|
|
|
|
- The keyCount specifies the required number of keys to obtain from the semaphore. The default is 1.
- If the specified number of keys is available, the method returns a positive integer and execution continues.
- If the specified number of keys is not available, the method returns 0.
|
|
|
|
|
|
Example : Semaphore
|
|
|
|
|
|
1 program semaphore_ex;
2
3 semaphore semBus = new(1);
4
5 initial begin
6 fork
7 agent("AGENT 0",5);
8 agent("AGENT 1",20);
9 join
10 end
11
12 task automatic agent(string name, integer nwait);
13 integer i = 0;
14 for (i = 0 ; i < 4; i ++ ) begin
15 semBus.get(1);
16 $display("[%0d] Lock semBus for %s", $time,name);
17 #(nwait);
18 $display("[%0d] Release semBus for %s", $time,name);
19 semBus.put(1);
20 #(nwait);
21 end
22 endtask
23
24 endprogram
You could download file semaphore_ex.sv here
|
|
|
|
|
|
Simulation : Semaphore
|
|
|
|
|
|
[0] Lock semBus for AGENT 0
[5] Release semBus for AGENT 0
[5] Lock semBus for AGENT 1
[25] Release semBus for AGENT 1
[25] Lock semBus for AGENT 0
[30] Release semBus for AGENT 0
[35] Lock semBus for AGENT 0
[40] Release semBus for AGENT 0
[45] Lock semBus for AGENT 1
[65] Release semBus for AGENT 1
[65] Lock semBus for AGENT 0
[70] Release semBus for AGENT 0
[85] Lock semBus for AGENT 1
[105] Release semBus for AGENT 1
[125] Lock semBus for AGENT 1
[145] Release semBus for AGENT 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|