Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

D Flip Flop

module dff(d,clk,pr,cr,q,q_n);

input d,clk,pr,cr;

output reg q,q_n;

always@(posedge clk)

begin

if(pr==1&&cr==1)

begin

q=d;

q_n=~d;

end

end

always@(pr or cr or d or clk)

begin

if(pr==0&&cr==1)

begin

q=1'b1;

q_n=1'b0;

end

else if(pr==1&&cr==0)

begin

q=1'b0;

q_n=1'b1;

end

else if(pr==0&&cr==0)

begin

q=1'b1;

q_n=1'b1;

end
end

endmodule

Test Bench :-
module tbdff();

reg t_d,t_clk,t_pr,t_cr;

wire t_q,t_q_n;

dff dut(t_d,t_clk,t_pr,t_cr,t_q,t_q_n);

initial

t_clk=1'b0;

initial

begin

t_d=1'b1;t_pr=1'b0;t_cr=1'b1;

#5 t_d=1'b1;t_pr=1'b1;t_cr=1'b0;

#5 t_d=1'b1;t_pr=1'b1;t_cr=1'b1;

#5 t_d=1'b0;t_pr=1'b1;t_cr=1'b1;

#5 t_d=1'b1;t_pr=1'b0;t_cr=1'b0;

#5 t_d=1'b1;t_pr=1'b1;t_cr=1'b0;

end

always

#2 t_clk=~(t_clk);

initial

#40 $finish;

endmodule

You might also like