title: initial block execution order in verilog
body: <p>I have made a counter module and I can't seem to grasp the execution order of initial block for both counter module and testbench module.</p>
  
  <p>As far as I know, I thought in verilog, <strong>initial block would always be executed first.</strong></p>
  
  <p>In this case, I'll be executing testbench module and I am assuming <code>initial</code> block
  for <code>testbench_counter</code> will be executed first. </p>
  
  <p>Next, I thought counter module <code>C</code> would be executed and value for <code>x_in</code> would be updated to <code>temp</code> with no need for <code>#1</code> delay in module <code>C</code>. </p>
  
  <p>But without <code>#1</code>, or changing <code>initial</code> to <code>always@(x_in)</code> I see <a href="https://i.stack.imgur.com/yx9LG.png" rel="nofollow noreferrer">redline</a>.</p>
  
  <p>So the question is </p>
  
  <ol>
  <li><p>would <code>initial</code> block for counter module <code>C</code>  be executed first even before the <code>initial</code> block for testbench?</p></li>
  <li><p>Why does it work if i use <code>always@(x_in)</code>? </p></li>
  </ol>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Code for counter</strong></p>
  
  <pre><code>`timescale 1ns/1ns
  module counter(x_in,clk,y_out);
      input [31:0] x_in;
      input clk;
      output [31:0] y_out;
      reg [31:0] y_out;
      reg [31:0] temp;
  
      initial begin
          #1 temp=x_in;
      end
  
      always@(posedge clk) begin
          temp&lt;=temp+1;   
          y_out&lt;=temp;
      end
  endmodule
  </code></pre>
  
  <p><strong>code for testbench</strong></p>
  
  <pre><code>`timescale 1ns/1ns
  
  module testbench_conter();
      wire [31:0]y_out;
      wire [31:0]temp;
      reg [31:0]x_in;
      reg clk;
  
      counter C(x_in,clk,y_out);
  
      always begin
          #5 clk=~clk;
      end
  
      initial begin
          clk=1'b0; 
          x_in=32'd0;
          #0
          #10000
          $stop;
      end
  endmodule
  </code></pre>