D Flip-Flop

In the previous lesson you saw that a latch is level-sensitive: its output can change at any moment while it is enabled. That makes timing hard to control. The D flip-flop fixes the problem by sampling its input only at one instant, the moment the clock edge arrives. It is the most widely used memory element in digital design, and it is the building block of registers, counters and state machines.

In this lesson you will learn how a D flip-flop behaves, read its timing, and use it in three small circuits: a delay element, a divide-by-2 stage and a register with an enable. You should know the basic gates and the idea of a latch.

Learning objectives

After this lesson you will be able to:

  • Explain the difference between a latch and an edge-triggered flip-flop.
  • Write the characteristic table and equation of a D flip-flop.
  • Trace the output of a D flip-flop over several clock edges.
  • Use feedback to build a divide-by-2 circuit.
  • Calculate the shortest clock period from setup and delay times.

How a D flip-flop works

A D flip-flop has a data input D, a clock input and outputs Q and Q'. On each active clock edge it copies D to Q and then holds that value until the next edge. A positive-edge-triggered flip-flop reacts to the rising edge, the change from 0 to 1. A negative-edge-triggered one reacts to the falling edge. Between edges, changes on D are ignored.

D Q (next)
0 0
1 1

The characteristic equation is simply Q+ = D, where Q+ means "the value of Q after the clock edge". Because the next state does not depend on the present state, the D flip-flop is easy to use: whatever you want stored, put on D.

Setup and hold times

Real flip-flops need D to be stable for a short time before the clock edge, called the setup time (tsu), and for a short time after it, called the hold time (th). If D changes inside this window, the flip-flop may output a wrong or unstable value. After the edge, Q takes a short time to settle, the clock-to-Q delay (tcq).

For a path from one flip-flop through some logic to another, the clock period must satisfy: period is at least tcq + logic delay + tsu. For example, with tcq = 5 ns, logic delay = 8 ns and tsu = 3 ns, the period must be at least 16 ns, so the maximum clock frequency is 1000 / 16 = 62.5 MHz.

Example 1: tracing a D flip-flop

A positive-edge D flip-flop starts with Q = 0. The value of D just before each of six clock edges is shown below. Find Q after every edge.

Clock edge D before the edge Q after the edge
1 1 1
2 1 1
3 0 0
4 1 1
5 0 0
6 0 0

Result: Q after each edge equals the D value present just before that edge, so Q follows D with a delay of one clock period. This is why a D flip-flop can be used as a one-clock delay element.

Example 2: a divide-by-2 circuit

Connect Q' back to D. At every clock edge the flip-flop loads the opposite of what it holds, so it toggles. Starting from Q = 0:

Clock edge D = Q' (before edge) Q after the edge
1 1 1
2 0 0
3 1 1
4 0 0
5 1 1
6 0 0

Result: Q changes once per clock edge, and a full cycle of Q (0, 1, 0) takes two clock periods. The output frequency is half the clock frequency. Chaining n of these stages divides by 2^n, which is the basis of the counters in later lessons.

Example 3: a register with an enable

Sometimes the flip-flop should load new data only when an enable signal E is 1, and keep its old value otherwise. Feed D from a 2:1 multiplexer: D = E.Din + E'.Q. When E = 1 the new data goes in, and when E = 0 the flip-flop feeds its own output back.

Edge E Din Q before D = E.Din + E'.Q Q after
1 1 1 0 1 1
2 0 0 1 1 1
3 1 0 1 0 0
4 0 1 0 0 0
5 1 1 0 1 1

Result: the output loads Din on edges 1, 3 and 5, where E = 1, and holds its value on edges 2 and 4, where E = 0. Many chips build this feature into the flip-flop and call it a clock enable.

Common mistakes

  • Treating a flip-flop like a latch. A flip-flop ignores D between clock edges, but a latch follows D while enabled.
  • Using the D value after the edge. The value that is stored is the D present just before the edge.
  • Forgetting setup and hold times. A data change close to the clock edge can cause an unreliable output.
  • Mixing up the edge. A rising-edge device does nothing on the falling edge, and the other way round.
  • Leaving out the feedback path in an enable circuit. Without Q fed back to D, the register loses its data when E = 0.

Practice problems

Check your logic with the truth table generator.

  1. A positive-edge D flip-flop has Q = 1 and D = 0 when the clock rises. What is Q after the edge?

    Answer: Q = 0, because Q+ = D.

  2. A D flip-flop has D connected to Q'. If Q = 1, what is Q after three clock edges?

    Answer: Q = 0. The sequence is 1, 0, 1, 0, because it toggles on each edge.

  3. A path has tcq = 4 ns, logic delay = 10 ns and tsu = 2 ns. What is the minimum clock period and the maximum frequency?

    Answer: 4 + 10 + 2 = 16 ns, so 62.5 MHz.

  4. In the enable circuit of Example 3, E = 0, Q = 1 and Din = 0. What is Q after the edge?

    Answer: Q = 1, because with E = 0 the register keeps its value.

Frequently asked questions

What is the difference between a latch and a flip-flop? A latch is level-sensitive and changes whenever its enable is active. A flip-flop is edge-triggered and changes only at the clock edge, which makes the timing of a whole system predictable.

Why is the D flip-flop so popular? Its next state equals D, so there is nothing to work out: you put the value you want on D. This makes registers and state machines simple to design, and it is the type most FPGAs are built from.

What happens if the setup time is violated? The flip-flop may capture the wrong value, or it may go into an unstable state called metastability for a short time. Designers avoid this by keeping the clock period long enough, as in the timing calculation above.

You can also experiment with the next-state logic of your own circuits in the Boolean calculator.

Next lesson: JK and T Flip-Flops