# Question about testbench can not catch posedge clk

**URL:** <https://verificationacademy.com/forums/t/question-about-testbench-can-not-catch-posedge-clk/50398>\
**Category:** SystemVerilog\
**Tags:** Verilog\
**Created:** [June 1, 2025, 6:22pm UTC](https://verificationacademy.com/forums/t/question-about-testbench-can-not-catch-posedge-clk/50398 "2025-06-01T18:22:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidColin](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@DavidColin](https://verificationacademy.com/forums/u/DavidColin)\
**Post date:** [June 1, 2025, 6:22pm UTC](https://verificationacademy.com/forums/t/question-about-testbench-can-not-catch-posedge-clk/50398/1 "2025-06-01T18:22:35Z")

</div>

Hi, I am testing apb signals on eda-playground.

```verilog
module fsm(clk, PSEL, PENABLE, reset, out1, out2);
  input clk;
  input PSEL;
  input PENABLE;
  input reset;
  output out1;
  output out2;
  
  reg [2:0] state;
  reg [2:0] next_state;

  // State encodings
  parameter [2:0]
    IDLE = 3'b001,
    STATE_1 = 3'b010,
    FINAL = 3'b100;
    
  // State machine output
  assign out1 = (state == STATE_1);
  assign out2 = (state == FINAL);
  
  // State transitions
  always @(*) begin // (*) because this state being affect by all others signals 
		case (state)
			IDLE: begin 
				if (PSEL & ~PENABLE) begin
					next_state = STATE_1;
				end else begin
					next_state = IDLE;
				end
			end
			STATE_1: begin
				if (PSEL & PENABLE) begin
					next_state = FINAL;
				end else begin
					next_state = STATE_1;
				end
			end
			FINAL: begin
				next_state = IDLE;
			end
			default: begin
				next_state = IDLE;
			end
		endcase
	end

  always @(posedge clk or negedge reset) begin
    if(~reset) begin
    	state <= IDLE;
    end else begin
    	state <= next_state;
    end
  end
endmodule

```

```verilog
module test;

  reg clk, reset, PSEL, PENABLE;
  wire out1, out2;
  
  // Instantiate device under test
  fsm DUT(.clk(clk),
          .PSEL(PSEL),
          .PENABLE(PENABLE),
          .reset(reset),
          .out1(out1),
          .out2(out2));
  
  task toggle_clk;
    begin
      #10 clk = ~clk;
      #10 clk = ~clk;
    end
  endtask
  
  initial begin
    reset = 0;
    #10;
    @(posedge clk);
    reset = 1;
  end
  
  initial begin
    	PSEL = 0;
    	PENABLE = 0;
    	//
// #10;
// PSEL = 1;
// #20;
// PENABLE = 1;
// #20;
// PSEL = 0;
// PENABLE = 0;
    
      @(posedge clk);
          PSEL = 1;

      @(posedge clk);
          PENABLE = 1;

      @(posedge clk);
          PSEL = 0;
          PENABLE = 0;
  end
  
  initial begin
    clk = 0;
    
    $display("Initial out1: %0h, out2: %0h",
      out1, out2);

    toggle_clk;
    $display("IDLE out1: %0h, out2: %0h",
      out1, out2);
    
    toggle_clk;
    $display("STATE_1 out1: %0h, out2: %0h",
      out1, out2);

    toggle_clk;
    $display("FINAL out1: %0h, out2: %0h",
      out1, out2);

    toggle_clk;
    $display("FINAL out1: %0h, out2: %0h",
      out1, out2);
    
    toggle_clk;
    $display("IDLE out1: %0h, out2: %0h",
      out1, out2);
    
    #1000;
    $stop;
  end
  
  initial begin
    // Dump waves
    $dumpfile("dump.vcd");
    $dumpvars(1, test);
    $dumpvars(1, DUT);
  end
  
endmodule

```

My question is when I try to use @(posedge clk); then state and next\_state is not running correctly.  
I thought it is a proper way to test APB signal. I do not know why it is wrong.

```verilog
  initial begin
    	PSEL = 0;
    	PENABLE = 0;
    
// #10;
// PSEL = 1;
// #20;
// PENABLE = 1;
// #20;
// PSEL = 0;
// PENABLE = 0;
    
      @(posedge clk);
          PSEL = 1;

      @(posedge clk);
          PENABLE = 1;

      @(posedge clk);
          PSEL = 0;
          PENABLE = 0;

```

 ![image](https://verificationacademy.com/forums/uploads/default/original/2X/a/a93ca1d731dd3f6cf000811c1ad0fe9666ce28f9.png)  
But if I use timing to configure PSEL and PENABLE

```verilog
  initial begin
    	PSEL = 0;
    	PENABLE = 0;
    
    	#10;
    	PSEL = 1;
    	#20;
    	PENABLE = 1;
    	#20;
    	PSEL = 0;
    	PENABLE = 0;
    
// @(posedge clk);
// PSEL = 1;

// @(posedge clk);
// PENABLE = 1;

// @(posedge clk);
// PSEL = 0;
// PENABLE = 0;
  end
  

```

then I can get state signal running after next\_state one cycle. That is correct, right?

 ![image](https://verificationacademy.com/forums/uploads/default/original/2X/a/ae370a8c14107d4b64b6f24ab1c841e920e64481.png)  
In this example, I use cadence xcelium 23.09.  
Can anybody explain why using @(posedge clk); is not correct? I hope it is not a tool related question. Thank you.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 2, 2025, 3:46pm UTC](https://verificationacademy.com/forums/t/question-about-testbench-can-not-catch-posedge-clk/50398/2 "2025-06-02T15:46:21Z")

</div>

Use nonblocking assignments in your testbench when using `@(posedge clk)`.

---

<div class="post-metadata">

**Author:** ![DavidColin](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/e9bcb4/32.png) [@DavidColin](https://verificationacademy.com/forums/u/DavidColin)\
**Post date:** [June 2, 2025, 7:20pm UTC](https://verificationacademy.com/forums/t/question-about-testbench-can-not-catch-posedge-clk/50398/3 "2025-06-02T19:20:55Z")

</div>

Thank you for your answer @dave_59.
