Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
begin : block_name
...
if (a == 0)
disable block_name;
b=1; $display(“value of b =%0d in block_name\n”,b);
end // end of named block
fork
$display( “ Thread 1 \n”);
$display( “ Thread 2 \n”);
$display( “ Thread 3 \n”);
join_any
disable fork;
$display(“End of simulation \n”);
end
Disable fork
task get_first( output int adr );
fork
wait_device( 1, adr );
wait_device( 7, adr );
wait_device( 13, adr );
join_any
forever begin
wait (count_enable) ;
#20 count = count + 1;
end
fork Output:
for (int i=1; i<4; i++) begin Producer: before put(1)
$display("Producer: before put(%0d)", i); Producer: after put(1)
mbx.put(i); Producer: before put(2)
$display("Producer: after put(%0d)", i); Consumer: after get(1)
end Producer: after put(2)
Producer: before put(3)
repeat(3) begin Consumer: after get(2)
mbx.get(j); Producer: after put(3)
$display("Consumer: after get(%0d)", j); Consumer: after get(3)
end
join
end
Driver thread
How to pass information between two threads
Mailbox : A mailbox is a communication mechanism that allows messages to be
exchanged between processes. Data can be sent to a mailbox by one process and
retrieved by another.
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 room in the queue.
class Generator;
Transaction tr;
task run;
repeat (10) begin
tr = new();
assert(tr.randomize());
mbx.put(tr);
end
endtask
endclass
Parameterized mailbox
Normal mailbox:
mailbox mbx;
mbx=new();
Paramerized Mailbox:
Difference:
All type mismatches are caught by the compiler and not at run time
Communication Between Threads
SystemVerilog also provides a powerful and easy-to-use set of synchronization
mechanism that can be created and reclaimed dynamically.
This set comprises of a semaphore built-in class, which can be used for synchronization
and mutual exclusion to shared resources.
semaphore sem;
sem=new(1); //creates semaphore with 1 key
sem.get(1);
sem.put(1);
get() : If the specified number of keys is available, the method returns and execution
continues. If the specified number of keys is not available, the process blocks until the
keys become available.
When the semaphore.put() function is called, the specified number of keys is returned
to the semaphore
The semaphore try_get() method is used to procure a specified number of keys from a
semaphore, but without blocking.
Allocates with one task device1_drive();
key sem.get(1);
semaphore sem; @(bus.cb);
initial begin bus.cb.addr <=tr.addr;
sem=new(1); bus.cb.data <= tr.data;
fork …….
device1_drive(); sem.put(1);
device2_drive();
join endtask
end
task device2_drive();
sem.get(1); Wait for bus to be
@(bus.cb); available
When completed , bus.cb.addr <=tr.addr;
return the keys back bus.cb.data <= tr.data;
…….
sem.put(1);
endtask
Semaphores
Conceptually, a semaphore is a bucket.
When a semaphore is allocated, a bucket that contains a fixed
number of keys is created.
Processes using semaphores must first procure a key from the
bucket before they can continue to execute.
If a specific process requires a key, only a fixed number of
occurrences of that process can be in progress simultaneously,
All others must wait until a sufficient number of keys is
returned to the bucket.
Semaphores are typically used for mutual exclusion, access
control to shared resources, and basic synchronization.
What is the output ?
program test;
initial begin
for (int j=0; j<3; j++) Output:
fork
3
$write(j);
3
join_none
3
#0
$display("\n");
end
endprogram
Make copy of index
program test;
initial begin
for (int j=0; j<3; j++) Output:
fork
0
automatic int k = j;
1
$write(k);
join_none 2
#0
$display("\n");
end
endprogram