yeolyi.comSeongyeol's CS

Flip-Flop

In the flipping circuit at the end of the clock post, Q flipped several times while CLK was 1. To take a value only once per clock, we need a circuit that reads D exactly once when CLK changes from 0 to 1, and ignores D after that even if it changes.

CLKLatchGoal

The filled parts show when D is accepted. A latch accepts it the whole time CLK is 1, but the goal is to accept it only at the moment CLK changes from 0 to 1.

D flip-flop

The circuit that meets this goal is the D flip-flop. The moment the clock changes from 0 to 1 is called the rising edge, so a D flip-flop is a circuit that accepts D only on the rising edge.

This circuit can be built from two D latches. The first latch’s E gets CLK inverted by a NOT, and the second latch’s E gets CLK as is. That way, whether CLK is 0 or 1, one of the two latches is always closed. It is like an airlock whose two doors never open at the same time.

  1. Right now CLK=0, so the first latch is open. Change D to 1, and the first latch takes the 1, but the second latch is closed, so Q stays the same.
  2. Change CLK to 1, and the first latch closes and holds the D of that moment, while the second latch opens and passes that value out as Q.
  3. Now change D to 0. The first latch is closed, so Q doesn’t change.

For D to reach Q, the first latch has to open (CLK=0) and then the second latch has to open (CLK=1). So Q changes only when CLK changes from 0 to 1, and the value Q takes is D just before the first latch closed.

The same holds no matter how wildly D changes. The lightly shaded parts are when the first latch is open (CLK=0), and the hatched parts are when the second latch is open (CLK=1).

CLKDFirst latch outputQ

Let’s replace the latch in the clock post’s flipping circuit with a D flip-flop. Below, the D flip-flop is drawn as a single box.

This time, Q flips exactly once every time CLK changes from 0 to 1. By the time the flipped value comes back to D, that moment has already passed.

Wrapping up

Now circuits can reliably “remember” values in step with the clock. But a single D flip-flop stores only a single bit. In the next post, we’ll build a register that changes its value only when we want and stores several bits together.

Challenge

In the post's D flip-flop, the E inputs of the two latches are swapped. The first latch's E gets CLK as is, and the second latch's E gets CLK inverted by a NOT. When does Q change now?