A D flip-flop is a sequential element that follows the input pin d at the given edge of a clock.

Design #1: With async active-low reset

 
module dff   ( input d,
              input rstn,
              input clk,
              output reg q);
 
  always @ (posedge clk or negedge rstn) 
       if (!rstn)
          q <= 0;
       else
          q <= d;
endmodule
 

Hardware Schematic

Testbench

 
module tb_dff;
  reg clk;
  reg d;
  reg rstn;
  reg [2:0] delay;
 
    dff  dff0 ( .d(d),
                .rsnt (rstn),
                .clk (clk),
                .q (q));
 
    // Generate clock
    always #10 clk = ~clk;
 
    // Testcase
    initial begin
      clk <= 0;
      d <= 0;
      rstn <= 0;
 
      #15 d <= 1;
      #10 rstn <= 1;
      for (int i = 0; i < 5; i=i+1) begin
        delay = $random;
        #(delay) d <= i;
      end
    end
endmodule
 

Design #1: With sync active-low reset

 
module dff   ( input d,
              input rstn,
              input clk,
              output reg q);
 
  always @ (posedge clk) 
       if (!rstn)
          q <= 0;
       else
          q <= d;
endmodule
 

Hardware Schematic

Testbench

 
module tb_dff;
  reg clk;
  reg d;
  reg rstn;
  reg [2:0] delay;
 
    dff  dff0 ( .d(d),
                .rsnt (rstn),
                .clk (clk),
                .q (q));
 
    // Generate clock
    always #10 clk = ~clk;
 
    // Testcase
    initial begin
      clk <= 0;
      d <= 0;
      rstn <= 0;
 
      #15 d <= 1;
      #10 rstn <= 1;
      for (int i = 0; i < 5; i=i+1) begin
        delay = $random;
        #(delay) d <= i;
      end
    end
endmodule
 

You may also like:

You consent to our cookies if you continue to use our website. To know more about cookies, see our privacy policy. I accept cookies from this site.

Agree