Interprocess Communication - mbits-mirafra/verilator GitHub Wiki

Events:

                                          
module wait_example;
  event e;
  initial begin 
    $display($time,"\t triggering the event");
    ->e;
  end 
  initial begin
    $display($time,"\t waiting for the event using wait");
    wait(e.triggered);
    $display($time,"\t event is triggered using wait");
  end
  initial begin
    $display($time,"\t waiting for the event using @");
    @(e.triggered);
    $display($time,"\t event is triggered using @");
  end
endmodule


Output:

WhatsApp Image 2024-01-11 at 8 07 02 PM

Semaphore:

                                          
  module semaphore_example_1;
 
     semaphore sem=new(6);
 
     initial begin :BEGIN_I
        $display("In first initial block At time=[%0t] ",$time);
        sem.get(4);
        $display("Thread 1:Accessing 4 keys from semaphore At time=[%0t] ",$time);
        #5;
        sem.put(4);
        $display("Thread 1:Done using 4 keys At time=[%0t] ",$time);
     end :BEGIN_I
 
     initial begin :BEGIN_II
        $display("In second initial block At time=[%0t] ",$time);
        sem.get(2);
        $display("Thread 2:Accessing 2 keys from semaphore At time=[%0t] ",$time);
        #10;
        sem.put(2);
        $display("Thread 2:Done using 2 keys At time=[%0t] ",$time);
     end :BEGIN_II
 
  endmodule :semaphore_example_1

Output:

WhatsApp Image 2024-01-11 at 8 07 02 PM

Mailbox:

                                          
class A;

   int a;
   int i;
   mailbox m;

   function new(mailbox m1);
      this.m = m1;
   endfunction

   task tra_data();

      for(i =0;i<3;i++)begin:BEGIN_MAIN

         if(m.num()==2)begin:BEGIN_1
         $display("mailbox is full");

      end:BEGIN_1

      else
         begin:BEGIN_2

            a++;
            m.put(a);
            $display("[%0t] 1. Transmitter: value of a = %0d",$time,a);

         end:BEGIN_2

      end:BEGIN_MAIN

   endtask

endclass:A

class B;

   int a;
   int i;
   mailbox m;

   function new(mailbox m2);
      this.m = m2;
   endfunction

      task rec_data();

         begin:BEGIN_MAIN

            m.get(a);
            $display("[%0t] 2. Receiver: value of a = %0d",$time,a);
         end:BEGIN_MAIN
 
   endtask

endclass:B

module tb();

   A a1;
   B b1;
   mailbox mb = new(2);

   initial begin:BEGIN_MAIN

      a1 = new(mb);
      b1 = new(mb);
      repeat(3)

      begin:BEGIN_1
         
         a1.tra_data();
         $display("...............................................");
         b1.rec_data();

      end:BEGIN_1

   end:BEGIN_MAIN

endmodule:tb

ERROR:

WhatsApp Image 2024-01-11 at 7 42 18 PM