title: Ripple Counter Using Dflip flop
body: <p>I wrote this code for simulating an asynchronous counter using D flip flop. The program gives correct output for the first to iterations but then the output doesn't change at all. What am I doing wrong?</p>
  
  <p>Here is the code:</p>
  
  <p><em><strong>1st module:</em></strong></p>
  
  <pre><code>module DFF(d,q,reset,clk);
    input      d,reset,clk;
    output reg q;
  
    always @(posedge reset, posedge clk) begin
      if(reset) begin
        q=0;
      end
      if(d)
        q=d;
      else
        q=q;
     end
  endmodule
  </code></pre>
  
  <p><em><strong>second module:</em></strong></p>
  
  <pre><code>module RippleCounter(d,clk,reset,out);
    input  [3:0] d;
    input        clk, reset;
    output [3:0] out; // 4bit
  
    DFF a(d[0],out[0],reset,clk);
    DFF b(d[1],out[1],reset,out[0]);
    DFF c(d[2],out[2],reset,out[1]);
    DFF dx(d[3],out[3],reset,out[2]);
  endmodule
  </code></pre>
  
  <p><em><strong>3rd module:</em></strong></p>
  
  <pre><code>module RippleCounterTOP;
    reg  [3:0] d;
    reg        clk, reset;
    wire [3:0] out;
  
    RippleCounter r(d,clk,reset,out);
  
    initial begin
      d=0;clk=0;reset=1;
    end
  
    always begin 
      #5 d=d+1; clk=~clk;
    end
  endmodule
  </code></pre>