Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 67 ยท Digital Logic

Latches, Flip-Flops and Registers

See how feedback gives circuits memory: build an SR latch, a D flip-flop, registers, counters and shift registers, and simulate each one step by step in Python.

Every circuit you have met so far is combinational: its outputs depend only on its inputs right now. Change the inputs and the outputs follow, and nothing is remembered. A computer obviously needs more than that. It must remember the program counter, the registers, the result of the last addition. Memory in hardware comes from one trick: feed a gate's output back into its own input.

Circuits that remember are called sequential circuits. Their outputs depend on the inputs and on the state stored inside. In this lesson you will build the smallest memory element, the latch, tame it with a clock to get a flip-flop, and then stack flip-flops into registers, counters and shift registers. Everything is simulated in Python, so you can watch each step. If gates still feel new, revisit Logic Gates and Boolean Algebra and Combinational Circuits first.

Memory from feedback

Take two NOR gates and cross-couple them: the output of each gate becomes one input of the other. The result is the SR latch (S for set, R for reset).

flowchart LR
  S["S (set)"] --> N2["NOR 2"]
  R["R (reset)"] --> N1["NOR 1"]
  N1 --> Q["Q"]
  N2 --> QN["Q-bar"]
  N1 -. "feedback" .-> N2
  N2 -. "feedback" .-> N1

Each NOR gate feeds the other: Q = NOR(R, Q-bar) and Q-bar = NOR(S, Q).

Work through the cases by hand:

S R Effect Q after
0 0 Hold: the feedback keeps the old state unchanged
1 0 Set 1
0 1 Reset 0
1 1 Forbidden: both outputs forced to 0 undefined once released

The hold row is the whole point. With S = R = 0, each NOR gate acts as an inverter of the other's output, so the pair has two stable states (Q = 1 or Q = 0) and stays in whichever it was last pushed into. That is one bit of memory made from just two gates.

The forbidden input breaks the promise that Q and Q-bar are opposites, and when both inputs drop back to 0 together, the two gates race and the winner depends on tiny physical differences. A Python simulation can only pick one outcome, so treat the (1, 1) row as "never do this".

Python An SR latch built from two NOR gates

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.

Notice the for loop in apply. Real gates have delay, so after an input changes, the signals chase each other around the loop until they settle. The simulation repeats the gate evaluations until nothing changes. That settling loop is the software version of "wait for the circuit to stabilise".

Gated D latch: one data input

The SR latch has an awkward interface: two inputs, one forbidden combination. The D latch fixes both problems. It has a data input D and an enable input E (often wired to the clock). When E = 1 the latch is transparent: Q follows D. When E = 0 the latch is opaque: it holds its last value.

Inside, two AND gates turn D and E into S and R: S = D AND E, and R = (NOT D) AND E. S and R can never both be 1, so the forbidden case is impossible.

A transparent latch is still a problem inside a processor. While E is high, a change on D races straight through to Q, and if Q feeds back to D through some logic (as it does in a counter), the value can loop around several times in one clock phase. We want storage that grabs the input at a single instant.

The D flip-flop and clock edges

A flip-flop is a storage element that samples its input only at a clock edge: the moment the clock rises from 0 to 1 (a rising or positive edge) or falls from 1 to 0. Between edges, it ignores D entirely.

The classic construction is the master-slave pair of D latches:

  • The master is enabled when the clock is low, so it follows D.
  • The slave is enabled when the clock is high, so it copies the master.
flowchart LR
  D["D"] --> M["Master latch<br/>open when CLK = 0"]
  M --> S["Slave latch<br/>open when CLK = 1"]
  S --> Q["Q"]
  CLK["CLK"] --> M
  CLK --> S

Only one latch is open at a time, so there is never a transparent path from D to Q.

When the clock rises, the master closes at the very moment the slave opens. The slave therefore sees whatever the master captured just before the edge, and later changes on D are locked out until the clock falls again. The output changes at most once per clock cycle, right after the rising edge.

Python A latch versus a 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 trace. At t = 2 the clock is high and D drops to 0. The plain latch follows immediately (its Q becomes 0), but the flip-flop stays at 1. At t = 6 the clock rises while D is 0, and now the flip-flop output becomes 0. At t = 7 D goes back to 1 during the high phase, and again the flip-flop ignores it. It only picks up the 1 at the next rising edge, t = 9.

Real flip-flops also need D to be stable for a short time before the edge (setup time) and after it (hold time). Violating either can leave the flip-flop in an in-between state. Chip designers spend a great deal of effort making sure the slowest path between flip-flops still fits inside one clock period; that is what limits the clock rate you saw in Measuring Performance.

Other flip-flop types exist. A JK flip-flop adds a toggle mode, and a T flip-flop toggles every edge when T = 1. All of them can be built from a D flip-flop plus a few gates, and the D flip-flop is what most real designs use.

Registers, counters and shift registers

One flip-flop stores one bit. Put N of them side by side with a shared clock and you have an N-bit register. Real registers usually add a load enable: if it is 1 the register captures its new input at the next edge, otherwise it keeps its value (a multiplexer in front of each D input chooses between "new data" and "my own output").

Wiring flip-flops differently gives other useful parts:

  • A counter feeds its own output, plus one, back into its input. Each edge increments the stored number, and a 4-bit counter wraps from 15 back to 0. The program counter from The Von Neumann Model and the Fetch-Decode-Execute Cycle is exactly a counter that can also be loaded with a jump target.
  • A shift register connects each flip-flop's output to its neighbour's input. At every edge the bits move one place along. It converts between serial and parallel data and is how a serial link like UART or SPI sends a byte one bit at a time.

All of these rely on one rule: at an edge, every flip-flop samples its input at the same instant, using the old values. Software loops do not work like that, so in a simulation you must compute all the next values first and only then store them.

Python Register, counter and shift register

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 counter reaches 1111 (15) at edge 15 and wraps to 0000 at edge 16, as a 4-bit register must. The correct shift register moves each bit exactly one place per edge, so after the serial input 1, 0, 1, 1 you can see the pattern marching to the right. The "wrong" version copies a value that has just been overwritten, so the first bit floods every cell in a single step. Hardware avoids this automatically because all flip-flops look at the previous cycle's outputs; your simulator has to do it on purpose.

From bits to a CPU

The pieces now form a complete picture:

Building block Made of Used for
Gate Transistors Logic functions
Latch Cross-coupled gates One bit, level-sensitive
Flip-flop Two latches One bit, edge-triggered
Register N flip-flops Words: PC, IR, general registers
Counter, shift register Flip-flops plus gates Sequencing, serial data

A processor is, at heart, combinational logic (like the adder you built earlier) sitting between banks of registers. In each clock cycle the registers present their values, the logic computes, and at the next edge the results are captured. Next you will assemble the most important piece of that logic, the arithmetic and logic unit, in Building an ALU.

SRAM cells in caches and register files use the same cross-coupled idea with transistors instead of separate gates, while DRAM stores a bit as charge on a capacitor and needs refreshing. Both are covered in RAM, ROM, SRAM, DRAM and Flash.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What gives an SR latch its ability to remember a bit?

    With S = R = 0 the cross-coupled gates reinforce whichever of the two stable states they are in, so the bit is held without any input.
  2. 2. How does a D flip-flop differ from a D latch?

    A latch is level-sensitive and transparent while enabled. A flip-flop is edge-triggered, so its output changes at most once per clock cycle.
  3. 3. A master-slave flip-flop is built from:

    The master is open while the clock is low and the slave while it is high, so there is never a path straight from D to Q.
  4. 4. A 4-bit counter holds 1111. What does it hold after the next clock edge?

    Only four flip-flops exist, so 15 + 1 wraps around to 0 (the carry out of the top bit is lost).
  5. 5. Why must a simulation of a shift register compute all new values before storing any?

    Updating cells one at a time lets a cell read a neighbour that has already changed, so a bit would smear across the register instead of moving one place.

Summary

  • Combinational circuits have no memory. Sequential circuits add state by feeding outputs back to inputs.
  • An SR latch (two cross-coupled NOR gates) holds a bit when S = R = 0; S = R = 1 is forbidden.
  • A D latch is transparent while its enable is high. A D flip-flop, often two latches in master-slave form, samples D only on a clock edge.
  • Setup and hold times say how long D must be stable around the edge.
  • N flip-flops with a shared clock form a register; adding an increment gives a counter; chaining cells gives a shift register.
  • In simulation, compute every next state from the old state first, then update, just as the hardware does at an edge.

Continue with Building an ALU.