SystemVerilog Event Examples

events examples

trigger and wait for an event at the same time

In the example below,
event triggering and waiting for the event trigger will happen at the same time.
wait(); the operator is used to detects the event triggering.

module events_ex;
  event ev_1; //declaring event ev_1

  initial begin
    fork
      //process-1, triggers the event
      begin
         $display($time,"\tTriggering The Event");
        ->ev_1;
      end
   
      //process-2, wait for the event to trigger
      begin
        $display($time,"\tWaiting for the Event to trigger");
        wait(ev_1.triggered);
        $display($time,"\tEvent triggered");
      end
    join
  end
endmodule

Simulator Output

0 Triggering The Event
0 Waiting for the Event to trigger
0 Event triggered

Click to execute on   

trigger and wait for an event at the same time

In the example below,
event triggering and waiting for the event trigger will happen at the same time.@() operator is used to detecting the event trigger. but as triggering and waiting happens at the same time, @() operator will not detect the event. this can be solved by using wait() operator;

module events_ex;
  event ev_1; //declaring event ev_1

  initial begin
    fork
      //process-1, triggers the event
      begin
         $display($time,"\tTriggering The Event");
        ->ev_1;
      end
    
      //process-2, wait for the event to trigger
      begin
        $display($time,"\tWaiting for the Event to trigger");
        @(ev_1.triggered);
        $display($time,"\tEvent triggered");
      end
    join
  endendmodule

Simulator Output

0 Triggering The Event
0 Waiting for the Event to trigger

Click to execute on   

wait_order example

In the example below,
There are three events ev_1, ev_2 and ev_3. the wait_order(); is used to see to the order in which the events are triggered.

module events_ex;
  event ev_1; //declaring event ev_1
  event ev_2; //declaring event ev_2
  event ev_3; //declaring event ev_3
 
  initial begin
    fork
      //process-1, triggers the event ev_1
      begin
        #6;
        $display($time,"\tTriggering The Event ev_1");
        ->ev_1;
      end
      //process-2, triggers the event ev_2
      begin
        #2;
        $display($time,"\tTriggering The Event ev_2");
        ->ev_2;
      end
      //process-3, triggers the event ev_3
      begin
        #8;
        $display($time,"\tTriggering The Event ev_3");
        ->ev_3;
      end
      //process-4, wait for the events to trigger in order of ev_2,ev_1 and ev_3
      begin
        $display($time,"\tWaiting for the Event's to trigger");
        wait_order(ev_2,ev_1,ev_3)
          $display($time,"\tEvent's triggered Inorder");
        else
          $display($time,"\tEvent's triggered Out-Of-Order");
      end
    join
  end
endmodule

Simulator Output

0 Waiting for the Event's to trigger
2 Triggering The Event ev_2
6 Triggering The Event ev_1
8 Triggering The Event ev_3
8 Event's triggered Inorder

Click to execute on   

wait_order example

In the example below,
There are three events ev_1, ev_2 and ev_3. the wait_order(); is used to see to the order in which the events are triggered.

But events are triggered in out of order, leads to execution of else part of wait_order().

module events_ex;
  event ev_1; //declaring event ev_1
  event ev_2; //declaring event ev_2
  event ev_3; //declaring event ev_3

  initial begin
    fork
      //process-1, triggers the event ev_1
      begin
        #6;
        $display($time,"\tTriggering The Event ev_1");
        ->ev_1;
      end

      //process-2, triggers the event ev_2
      begin
        #2;
        $display($time,"\tTriggering The Event ev_2");
        ->ev_2;
      end

      //process-3, triggers the event ev_3
      begin
        #1;
        $display($time,"\tTriggering The Event ev_3");
        ->ev_3;
      end
      //process-4, wait for the events to trigger in order of ev_2,ev_1 and ev_3
      begin
        $display($time,"\tWaiting for the Event's to trigger");
        wait_order(ev_2,ev_1,ev_3)
          $display($time,"\tEvent's triggered Inorder");
        else
          $display($time,"\tEvent's triggered Out-Of-Order");
      end
    join
  end
endmodule

Simulator Output

0 Waiting for the Event's to trigger
1 Triggering The Event ev_3
1 Event's triggered Out-Of-Order
2 Triggering The Event ev_2
6 Triggering The Event ev_1

Click to execute on   

❮ Previous Next ❯