program example();
// semaphores are a unique datatype in SV semaphore want_to_print;
// this is the "shared resource", would be a bus,
// but in this case its a $display
task write_bus(int addr, int data);
$display("here is a write to the bus at addr %d", addr);
#100;
endtask
int done = 0, i = 0;
initial
begin
// here we initialize the semaphore with one token availabe to
//share. We could initialize
// with more, if there were an interleaved bus, for example
want_to_print = new(1);
fork
begin
/// here is one parallel thread competing
while(!done)
begin
// wait for some random time
#($random() % 100);
// attempt to get the one semaphore token
want_to_print.get(1);
// got it!, lets use the bus
write_bus(200, $random());
i++;
// we must return the semaphore token want_to_print.put(1);
end
end |