Question regarding counter

Hi There,

My question is when req goes high my counter need to start counting soon after getting corresponding grant for my request I need to stop my counter. I written my code using combintaion of fork join with stimulus generation and check block. simulator is going to loop can any one help me what is the issue in my code.


module top;
  	logic req, gnt;
  	bit clk = 1;
  	int unsigned counter = 0;
  bit disable_fork_bit;
   	always #5 clk = ~clk;
  	
  	
  	initial
      begin
        fork
          
          
          begin : stimulus_generation_block
            @(posedge clk);
            req = 1;
            repeat(5)
              begin
                @(posedge clk);
              end
            gnt = 1;
          end : stimulus_generation_block
          
          begin : check
            fork
              @(posedge clk)
              if(req == 1)
              forever
              	fork
                  @(posedge clk)
                  counter ++;
                  if(gnt == 1);
                  begin
                    disable_fork_bit = 1;
                  end
                join
            join
          end : check
        join_any
        wait(disable_fork_bit == 1);
        disable fork;
        $display("counter = %d", counter);
      end
endmodule

In reply to marathuteja:

You are making things very difficult with multiple fork/join blocks.

All initial blocks are forked in parallel, so it saves you from having to write convoluted code:


module top;
  logic req, gnt;
  bit clk;
  int unsigned counter;
  
  initial begin
    clk = 1;
    forever #5 clk = ~clk;
  end
  
  initial begin : stimulus_generation_block
    req = 0;
    gnt = 0;
    @(posedge clk);
    req = 1;
    repeat(5) @(posedge clk);
    gnt = 1;
  end
  
  initial begin : check
    counter = 0;
    while (req == 0) @(posedge clk); // Wait for req
    while (gnt == 0) begin  // Wait for gnt
      counter++;
      @(posedge clk);
    end
    $display("counter = %d", counter);
    $finish();
  end
 
endmodule