Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 57 ยท Latches and Flip-Flops

D Latches and D Flip-Flops

Learn the D latch, why edge-triggered D flip-flops are safer, how master-slave design works, and what setup and hold times mean, with Python simulations.

The gated SR latch stores a bit, but it has two awkward properties. It has a forbidden input pair, and while its enable is high it follows its inputs, so the stored value can change many times during one enable pulse. Real systems want something calmer: capture one value, at one precise moment, and ignore everything else until the next moment. That is what the D flip-flop does, and it is the building block of registers, counters, state machines and memory.

This lesson builds the D flip-flop in two steps. First you remove the forbidden state to get the D latch. Then you combine two latches, back to back, to get an edge-triggered flip-flop. You will also meet setup and hold times, the two timing rules every flip-flop imposes on its input, and simulate everything with a small clocked class in Python.

The D latch: one data input

The D latch is a gated SR latch where S = D and R = NOT D. There is only one data input (D for data), and S and R can never be 1 at the same time, so the forbidden state is gone.

  • When the enable E is 1, the latch is transparent: Q follows D, immediately and continuously.
  • When E is 0, the latch is opaque: it holds the last value D had when E fell.

So the D latch is level-sensitive. Its behaviour is described by one line: if E: Q = D. It is a perfectly good memory element, but the transparent phase causes trouble. If the output of a latch feeds logic that feeds the same latch, the signal can race around the loop many times while E is high.

Latch or flip-flop?

Latch Open while the enable is high.
Flip-flop Takes one snapshot at the clock edge.

Same memory cell idea, very different timing.

1 / 2

From two latches to one flip-flop

Place two D latches in a row. The first (the master) is enabled by the inverted clock, and the second (the slave) is enabled by the clock itself:

flowchart LR
    D["D"] --> M["Master latch<br/>open when CLK = 0"]
    M -->|"m"| S["Slave latch<br/>open when CLK = 1"]
    S --> Q["Q"]
    C["CLK"] --> M
    C --> S

The two latches are never open at the same time, so data cannot race straight through.

Follow one clock cycle:

  1. Clock low. The master is open and follows D. The slave is closed, so Q does not change, whatever D does.
  2. Rising edge. The master closes and freezes the value D had at that instant. The slave opens and passes that frozen value to Q.
  3. Clock high. The slave is open, but the master is closed, so later changes of D cannot reach Q.
  4. Falling edge. The slave closes, the master opens again, and the cycle repeats.

The only time Q can change is just after the rising edge, and the value it takes is the value D had at that edge. That is edge-triggered behaviour. Designers describe it as Q(next) = D at the active clock edge, and Q holds otherwise. A flip-flop with a falling-edge trigger just swaps the roles of the two latches.

Simulating a clock

A simulation of sequential logic needs time. The following program steps through numbered ticks. Each tick has a clock value and a D value, and each memory element updates according to its rule. It compares the transparent latch with the master-slave flip-flop, using a D signal that changes in the middle of the clock-high phase to expose the difference.

Python Latch versus master-slave flip-flop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the rows top to bottom. The latch output copies D during every clock-high phase, wiggles whenever D wiggles, and then freezes when the clock falls. The flip-flop output only moves right after a rising edge, and the program confirms that the values after each edge equal the values of D just before it. If D changes after the edge, the flip-flop output doesn't care.

Drawing the waveforms

A timing diagram shows the same story visually. This version uses matplotlib to draw CLK, D, and the two outputs as stepped lines, with dotted lines at the rising edges.

Python Timing diagram of a D flip-flop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The dotted lines mark rising edges. Compare the bottom two traces: the latch trace follows D inside each clock-high block, and the flip-flop trace is a clean staircase that only steps at the dotted lines.

Setup and hold times

An edge-triggered flip-flop is not magic. Inside it, the master must have time to settle on the new D before it closes. Two numbers describe this requirement:

  • Setup time (t_su): D must be stable for at least this long before the active edge.
  • Hold time (t_h): D must stay stable for at least this long after the active edge.

Together they form a small window around the edge in which D must not change. If it does, the master latch may capture a half-formed value, and the output can end up wrong or even undefined. The program below adds a violation check to a behavioural flip-flop: when D changes inside the window, the output becomes X, meaning unknown.

Python Detecting setup and hold violations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With good timing, D changes well away from the edges and Q is always a clean 0 or 1. With bad timing, D changes one tick before the first and third edges, which breaks the two-tick setup rule, and the output becomes X after those edges (the second edge is fine, so Q recovers). In hardware that X is not a harmless label: it is the beginning of metastability, where the flip-flop sits between 0 and 1 for an unpredictable time.

Real flip-flops also have a clock-to-Q delay: Q does not change at the exact instant of the edge but a short time later. This delay, plus the logic delay between flip-flops, plus the setup time, decides how fast the clock may run. The timing lesson works through the calculation.

Control inputs

Practical flip-flops add extra inputs:

  • Asynchronous reset (clear) and preset: force Q to 0 or 1 immediately, without waiting for a clock edge. They are used for power-up.
  • Synchronous reset: treated like data, taking effect at the next clock edge.
  • Enable: the flip-flop loads D only when enable is 1; otherwise it keeps its value. Inside, this is a 2:1 multiplexer in front of D that chooses between D and the current Q.

Many flip-flops side by side with a shared clock make a register. The shift register lesson shows what happens when the Q of one flip-flop feeds the D of the next.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a D latch, what happens while the enable input is 1?

    A D latch is transparent while enabled, so Q copies D at every moment, and it freezes the last value when the enable falls.
  2. 2. Why can't a D latch be put in the forbidden state of an SR latch?

    With S = D and R = NOT D, S and R are always opposites, so they can never both be 1.
  3. 3. In a master-slave D flip-flop, when does the master latch follow D?

    The master is enabled by the inverted clock. It follows D while the clock is low and freezes the value when the clock rises.
  4. 4. What does the hold time of a flip-flop describe?

    Hold time is the minimum time that D must remain unchanged after the active edge. Setup time is the corresponding requirement before the edge.
  5. 5. A rising-edge D flip-flop sees D = 1 just before a rising edge, then D falls to 0 during the high phase. What is Q before the next edge?

    Q captured the 1 at the edge and ignores later changes of D until the next edge.

Summary

  • A D latch has one data input and is transparent while enabled (if E: Q = D), so the SR forbidden state cannot occur.
  • A master-slave D flip-flop chains an opposite-phase pair of latches, so Q changes only right after the active clock edge and takes the value D had at that edge.
  • Latches are level-sensitive; flip-flops are edge-triggered. Synchronous design relies on flip-flops.
  • Setup time is how long D must be stable before the edge, and hold time is how long it must stay stable after. Breaking either can leave the output undefined.
  • A clocked simulation steps through ticks, updating each element from the current clock and data values.

Next: JK and T Flip-Flops.