Verilog-Based Case Study of D-FlipFlop
Verilog-Based Case Study of D-FlipFlop
From the above circuit, we can see that we need four NAND gates and one NOT gate to construct a D-flip flop
in gate-level modeling.
The module is declared listing the terminal ports in the logic circuit.
module d_ff_gate(q,qbar,d,clk);
...
endmodule
Note that outputs are declared first followed by inputs since built-in gates also follow the same pattern.
Thereafter, let’s declare the input and output ports.
input d,clk;
output q, qbar;
module dff_behavioral(d,clk,clear,q,qbar);
input d, clk, clear;
output reg q, qbar;
always@(posedge clk)
begin
if(clear== 1)
q <= 0;
qbar <= 1;
else
q <= d;
qbar = !d;
end
endmodule
module dff_behavioral(d,clk,clear,q,qbar);
input d, clk, clear;
output reg q, qbar;
always@(posedge clk or posedge clear)
begin
if(clear== 1)
q <= 0;
qbar <= 1;
else
q <= d;
qbar = !d;
end
endmodule
Structural Modeling
In the case of D-flip flop, we have a NOT and four NAND gates that build the circuit.
Hence, we have to structurize each gate with their respective module.
Then, we use assign statement to write the logical expression for NAND.
assign c= ~(a & b);
The endmodule keyword is used for representing the end of the module.
Similarly, we do for NOT gate
module not_gate(f,e);
input e;
output f;
assign f = ~e;
endmodule
Note: All the variables for assigning inputs and outputs in one module are kept different from others. This
ensures mixing up of signals does not happen during a simulation.
Now, there is need to integrate these lower modules to form our D-flip flop. In order to do that, module
instantiation is used. First, start with the name of the lower hierarchy module (defined and declared above)
and write the name of the instance of your choice. The port-list will contain the output signals, followed by the
input ones.
For example,
nand_gate nand1(x,clk,d);
Here,
(1.) module-name :- nand_gate
(2.) instance name:- nand1
(3.) output port:- x (intermediate signal)
(4.) input ports:- d and clk
module not_gate(f,e);
input e;
output f;
assign f= ~e;
endmodule
module d_ff_struct(q,qbar,d,clk);
input d,clk;
output q, qbar;
not_gate not1(dbar,d);
nand_gate nand1(x,clk,d);
nand_gate nand2(y,clk,dbar);
nand_gate nand3(q,qbar,y);
nand_gate nand4(qbar,q,x);
endmodule
Testbench
//test bench for d flip flop
//1. Declare module and ports
module dff_test;
reg D, CLK,reset;
wire Q, QBAR;
Simulated Waveform
The functional correctness of described D flip-flop can be verified by simulation. The simulated waveform of D flip flop is given below:
In this waveform, you will see that the Q and Q’ will be reset state at the positive cycle after clear is activated.
In this waveform, we can see that the Q and Q’ will be in the reset state as soon as clear is activated.