System Verilog : Disable Fork & Wait Fork

Last modified 2017-01-23

Sini

To model concurrent and serial behavior, disable fork and wait fork will be used along with fork join constructs. These constructs allow one process to terminate or wait for the completion of other processes.

If you want to model your verification environment in such a way that, it has to spawn concurrent processes which will be waiting for events. And as soon as one event is received, terminate the other outstanding processes to go ahead with your next process. In this case disable fork would be the best option along with joinany.

Disable Fork :

class forkjoinclass;
 task run(input event evblk1,evblk4);
 fork
   BLK1: begin
     # 2ns
     $display( "BLOCK 1\\n" );
     # 10ns;
     ->evblk1;
   end
   BLK2: begin
     $display( "BLOCK 2\\n" );
   end
   BLK3: begin
     @evblk4;
     $display( "BLOCK 3\\n" );
   end
  BLK4: begin
     @evblk1
     $display( "BLOCK 4\\n" );
     ->evblk4;
  end
  joinany
  disable fork;
 endtask
 endclass

program myprog;
  event evblk1,evblk4;
  forkjoinclass c1 = new;
  
  initial begin
   c1.run(evblk1,evblk4);
   P1: begin
   $display("Parent Process Started\\n");
   end
   #100ns;
  end
endprogram

BLOCK 2 Parent Process Started */

The disable fork terminates all active descendants of the current process. In this case BLK1,BLK3 and BLK4 are terminated.

Wait Fork :

The wait fork statement blocks process execution flow until all immediate child subprocesses (processes created by the current process, excluding their descendants) have completed their execution.

class forkjoinclass;
 
 task run(input event evblk1,evblk4);
 FJ1 : fork
   BLK1: begin
     # 2ns
     $display( "FJ1 BLOCK 1\\n" );
     # 10ns;
     ->evblk1;
   end
 
   BLK2: begin
     $display( "FJ1 BLOCK 2\\n" );
   end
 
   BLK3: begin
     @evblk4;
     $display( "FJ1 BLOCK 3\\n" );
   end

   BLK4: begin
     @evblk1
     $display( "FJ1 BLOCK 4\\n" );
     ->evblk4;
  end
  joinany

 FJ2: fork
   BLK1: begin
     # 2ns
     $display( "FJ2 BLOCK 1\\n" );
     # 10ns;
     ->evblk1;
   end
 
   BLK2: begin
     $display( "FJ2 BLOCK 2\\n" );
   end
 
   BLK3: begin
     @evblk4;
     $display( "FJ2 BLOCK 3\\n" );
   end

   BLK4: begin
     @evblk1
     $display( "FJ2 BLOCK 4\\n" );
     ->evblk4;
  end
  joinnone

//wait fork;

 endtask
 
 endclass

program myprog;
  event evblk1,evblk4;
  forkjoinclass c1 = new;
  
  initial begin
   c1.run(evblk1,evblk4);
   P1: begin
   $display("Parent Process Started\\n");
   end
   
   #10ns;
  end
  
endprogram

/* -- Without wait-fork FJ1 BLOCK 2

Parent Process Started

FJ2 BLOCK 2

FJ1 BLOCK 1

FJ2 BLOCK 1

-- With wait fork FJ1 BLOCK 2

FJ2 BLOCK 2

FJ1 BLOCK 1

FJ2 BLOCK 1

FJ2 BLOCK 4

FJ1 BLOCK 4

FJ2 BLOCK 3

FJ1 BLOCK 3

Parent Process Started

*/

In the above code, FJ1 uses joinany and FJ2 uses joinnone. Without wait fork construct (Line 52), BLK2 will be executed first and then P1 . Execution of other processes depend on when simulation ends. With wait fork construct(Line 52), all spawned child processes in FJ1 and FJ2 will be executed before going to P1 process.