CodeRigor

LESSON

Latches, flip-flops and the clock

Build an SR latch, a D latch, a master-slave flip-flop, a register with load and a counter from NAND gates, and tell edge triggering from transparency.

Every circuit so far forgets its inputs the moment they change. Memory needs a loop: a gate whose output feeds back into its own input, so that it can hold a bit after the cause is gone. This lesson pairs with How Do Computers Remember? by Sebastian Lague and D latch by Ben Eater, linked under Watch & read. The first video goes from a set-reset latch through a data latch and a clock to an edge-triggered flip-flop and registers, and the second builds a data latch. You build all of that out of NAND gates in a small settling simulator: a latch, a rising-edge flip-flop, a register with a load input, and a counter.

Start with two NANDs, each feeding the other. Call the inputs set_n and reset_n; both are active low, because a NAND with a 0 on one input must output 1. With both inputs at 1 each gate only passes on the inverse of the other, so whatever state the pair is in is stable: that is the memory. Pull set_n to 0 and q is forced to 1; pull reset_n to 0 and qn is forced to 1, which makes q fall to 0.

Python
def nand(a, b):
    return 0 if a and b else 1

q, qn = 0, 1
for sn, rn in [(0, 1), (1, 1), (1, 0), (1, 1)]:   # set, hold, reset, hold
    for _ in range(3):
        q = nand(sn, qn)
        qn = nand(rn, q)
    print(sn, rn, "->", q, qn)

Pulling both inputs low at once is the forbidden input: q and qn are both 1, and when you release them together, which way the pair falls depends on which gate is faster. The code you write raises ValueError for it. Active-high S and R, as in the video, are the same latch with a NAND wired as an inverter in front of each input.

A set-reset latch is awkward to drive, so add a data input d and an enable en. Set becomes d AND en and reset becomes (NOT d) AND en, written as NANDs so they come out active low. While en is 0 both inputs sit at 1 and the latch holds. While en is 1 it is transparent: q follows d. That is a D latch, five NAND gates in this build.

Transparency is a problem as soon as a latch feeds itself through other logic, as a counter does. With the enable high, the new value races around the loop and arrives back before the enable has dropped. The cure is the clock and two latches in series. The first, the master, is open while the clock is low; the second, the slave, is open while it is high. They are never open at the same time, so a value can only move one latch per half cycle. At the rising edge the master closes holding the d it had, and the slave opens and copies it. After that, changes of d meet a closed master. The pair is a D flip-flop: 10 gates plus one shared inverter that makes nclk from clk, 11 in all.

Master-slave flip-flop
start / 3
Two gated latches in series. The master is enabled by the inverted clock, the slave by the clock itself.
Master-slave flip-flop
Two gated latches in series. The master is enabled by the inverted clock, the slave by the clock itself.

Here is the difference on real numbers. A D latch enabled by the clock and the flip-flop above see the same eight half-steps of clock and data.

A latch and a flip-flop on the same inputs
Eight half-steps. The latch (enable = clk) follows d while the clock is high; the flip-flop changes only at a rising edge, to the d it saw before it. The highlighted cells are where they differ.
A latch and a flip-flop on the same inputs
Eight half-steps. The latch (enable = clk) follows d while the clock is high; the flip-flop changes only at a rising edge, to the d it saw before it. The highlighted cells are where they differ.

Read the flip-flop row. It rises at step 2 because d was 1 while the clock was low. At step 3 d falls but q stays 1, because the master is shut. It falls at step 6, the next rising edge, because d had been 0 since step 3. The latch tracks d wherever the clock is high, which is why it cannot be used as a flip-flop.

Wider memory is the same cell repeated. A register is one flip-flop per bit, with a multiplexer in front of each: d is the stored q when load is 0 and the incoming bit when load is 1. A counter feeds each flip-flop the old value toggled when every lower bit is 1:

Python
bits = [0, 0, 0]
seen = []
for edge in range(8):
    carry = 1
    for i in range(3):
        bits[i], carry = bits[i] ^ carry, bits[i] & carry
    seen.append(sum(b << i for i, b in enumerate(bits)))
print(seen)

The mistakes people make come in a few kinds. The first is a flip-flop made from one latch, or from two latches enabled by the same phase: it passes the build of a single clean cycle and then lets data through whenever the clock is high. The second is a register whose load input is the wrong way round, so that it reloads on every edge where load is 0 and holds when it is 1. The third is to drive the forbidden set-reset input and trust the result. The fourth is to expect that a change of data in the same instant as the edge is stored; here the clock settles first and the data arrives after it.

The tests compare each part with a few lines of plain Python over hundreds of random clock and data sequences, and settle raises RuntimeError for a ring that never reaches a fixed point. That evidence has a limit. The simulator has no gate delays: it cannot show setup and hold times, clock skew or a latch caught half way between 0 and 1. It tells you the logic is right, not that a real chip would be fast enough to run it.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Covers the set-reset latch, the data latch, the clock, the edge-triggered flip-flop and registers built from them. Watch it first, then build the same parts here as NAND gates and test them against a plain reference.

A short build of a latch with a single data input and an enable, on a breadboard. A second view of the same gated latch this lesson wires from NANDs.