---
title: Latches and flip-flops
heading: Feedback that remembers
eyebrow: C7 · Lesson 26 · Latches and flip-flops
xp: 50
---

::: hook
Connect two NOR gates so that each one's output feeds the other's input, and the pair can sit stably in either of two states. Pulse one input and it flips to 1; pulse the other and it flips to 0; release both and it remembers. That's a latch: one bit of memory, made from logic and feedback. Add a clock so that it only listens at one instant, and you have a flip-flop, the element from which every register, counter and processor state is built.

::: play
- An SR latch from two NOR gates. The timing diagram shows S and R being pulsed in turn. Before S is pulsed, what's Q? (Red means unknown.)
- After S goes back to 0, does Q forget?
- Below it, a D flip-flop: Q copies D only when the clock rises. Watch D change between edges.
:::
:::

```sim logic
input S, R
output Q, QN
assign Q = ~(R | QN)
assign QN = ~(S | Q)
stim S 0@0 1@6 0@10 1@26 0@28
stim R 0@0 1@16 0@20
wave: S, R, Q, QN
time: 36
```

::: predict
## A D flip-flop's clock rises at t = 10 and t = 20. D goes to 1 at t = 12 and back to 0 at t = 17. What does Q do?

- [ ] Follows D: 1 from 12 to 17
- [x] Nothing: D was 0 at both rising edges
- [ ] Goes to 1 at t = 20
- [ ] Goes unknown

::: reveal
Nothing. An edge-triggered flip-flop samples D only at the clock's rising edge and ignores it the rest of the time. D was 0 at t = 10 and 0 again at t = 20, so Q stays 0 even though D was 1 in between. A **latch** would have let the pulse through while it was transparent; the flip-flop's single sampling instant is what makes clocked circuits predictable.
:::
:::

::: nameit
## Memory elements

**SR latch** (two cross-coupled NORs):

| S | R | Q |
|---|---|---|
| 0 | 0 | unchanged (memory) |
| 1 | 0 | 1 (set) |
| 0 | 1 | 0 (reset) |
| 1 | 1 | both outputs 0; avoid |

At power-up, with neither input asserted, its state is unknown.

**D latch:** a latch with an enable. While enabled it's transparent (Q follows D); when disabled it holds the last value.

**D flip-flop** (edge-triggered): two latches back to back, enabled on opposite clock phases, so Q takes D's value at the rising edge only and holds it for a full cycle. It's the basic storage element of synchronous design.

**Timing rules.** D must be stable for a **setup time** before the edge and a **hold time** after it. Q changes a **clock-to-Q** delay after the edge. Violate setup or hold and the flip-flop can go **metastable**: hover between 0 and 1 for an unpredictable time before falling one way. That's why signals arriving from outside a clock domain pass through two flip-flops in a row (a synchronizer).

**Reset.** Flip-flops usually have a reset that forces Q to 0: synchronous (at the next edge) or asynchronous (at once).
:::

```sim logic
input D
output Q
clock CLK 10
always @(posedge CLK) Q <= D;
stim D 0@0 1@12 0@17 1@28 0@44
wave: CLK, D, Q
time: 50
```


::: example
## A flip-flop has $t_\text{setup}$ = 0.2 ns and $t_\text{hold}$ = 0.1 ns, and its clock rises at 10 ns. When must D be stable?

::: solution
1. **Setup, before the edge.**
   $$10 - 0.2 = 9.8\ \text{ns}$$
2. **Hold, after the edge.**
   $$10 + 0.1 = 10.1\ \text{ns}$$
3. **Outside the window.** D can change freely.

**Answer:** D must be stable from 9.8 ns to 10.1 ns.
:::
:::

::: example
## An SR latch (cross-coupled NORs) gets S = 1, R = 0, then S returns to 0. What is Q?

::: solution
1. **Set.** S = 1 sets Q to 1.
2. **Hold.** With S = R = 0 the latch holds, so Q stays 1.
3. **Why it holds.** The feedback loop remembers the last set or reset.

**Answer:** Q = 1.
:::
:::

::: example
## A D latch's clock is high from 0 to 5 ns, and D pulses high from 2 to 3 ns. What does Q do? What would an edge-triggered flip-flop clocked at 0 ns do?

::: solution
1. **The latch while the clock is high.** The latch is transparent: Q follows D, going high at 2 ns and low at 3 ns (each a gate delay later).
2. **The latch when the clock falls.** At 5 ns it holds the 0 it has then.
3. **The flip-flop.** It samples D only at the rising edge, when D was 0, so Q stays 0 and the pulse is never seen.
4. **Why designs use flip-flops.** That difference is why synchronous designs use flip-flops: their outputs change only at edges, whatever the inputs do in between.

**Answer:** the latch's Q copies the pulse, high from about 2 to 3 ns, then holds 0; the flip-flop's Q stays 0.
:::
:::

::: check
kind: choice

## An SR latch has Q = 1. S and R are both 0. Then R is pulsed to 1 and back. What's Q now?

- [ ] 1
- [x] 0
- [ ] Unknown
- [ ] It oscillates

::: right
Right. R resets the latch, and with both inputs back at 0 it holds the 0.
:::

::: misses
- [1] R is the reset input.
- [3] It was reset to a definite 0.
- [4] A latch settles; it doesn't oscillate.
:::
:::

::: check
given: d1 = 0 | 1; d2 = 0 | 1
answer: d2
tolerance: 0.01

## A D flip-flop's D is {d1} at one rising edge and {d2} at the next. What is Q just after the second edge?

::: right
Right: {answer}. Q takes D's value at the most recent edge.
:::

::: misses
- [any] Not yet. Q copies D at each rising edge.
:::
:::

::: check
given: ts = 50 | 100; th = 20 | 30; tcq = 80 | 120
answer: ts + th
unit: ps
tolerance: 0.5%

## A flip-flop has a setup time of {ts} ps, a hold time of {th} ps and a clock-to-Q delay of {tcq} ps. For how long around the edge must D stay still?

::: right
Right. From setup before to hold after: {answer}.
:::

::: misses
- [ts + th + tcq] Clock-to-Q is about the output, not about how long D must hold.
- [ts] The hold time after the edge counts too.
- [any] Not yet. Setup plus hold.
:::
:::

::: check
kind: match

## Match each element to how it behaves.

- SR latch :: set and reset inputs, holds when both are 0
- D latch :: follows D while enabled, holds when disabled
- D flip-flop :: copies D only at the clock's edge
- a synchronizer :: two flip-flops in a row on an outside signal

::: right
Right. Latches are level-sensitive; flip-flops are edge-triggered.
:::
:::

::: check
given: n = 1 | 2 | 4 | 8
answer: 8*n
tolerance: 0

## How many flip-flops does a register holding an {n}-byte value need?

::: right
Right: {answer}, one per bit.
:::

::: misses
- [n] One flip-flop stores one bit, not one byte.
- [any] Not yet. One flip-flop per bit, 8 per byte.
:::
:::

::: check
kind: parts
given: su = 0.05 | 0.2; ho = 0.02 | 0.1; t = 5 | 10 | 20

## A flip-flop with setup {su} ns and hold {ho} ns has its clock rise at {t} ns.

::: part
answer: t - su
unit: ns
tolerance: 0.1%

### From when must D be stable?

::: misses
- [any] Setup before the edge.
:::
:::

::: part
answer: t + ho
unit: ns
tolerance: 0.1%

### Until when?

::: misses
- [any] Hold after the edge.
:::
:::
:::

::: check
kind: derivation
vars: f = 1000..5000000000, n = 1..32

## A chain of n toggle flip-flops, each clocked by the previous one's output, starts from a clock of frequency f.

::: step
answer: f/2

### What frequency comes out of the first?
:::

::: step
answer: f/2^n

### What comes out of the last?
:::

::: right
Right. Each toggle halves the frequency: a ripple counter.
:::
:::

::: explain
## Why do two cross-coupled NOR gates remember a bit?

::: model
Each gate's output feeds the other's input. If Q is 1, it forces the other NOR's output to 0, and that 0, with R at 0, lets the first NOR keep outputting 1: the state holds itself up. The opposite state, Q = 0 with the other output at 1, is just as self-consistent. With S and R both 0, nothing pushes it out of whichever state it's in, so it remembers. Pulsing S or R overrides one gate and forces the pair into the other state.
:::
:::

::: practice

::: check
given: c = 1..6 int; let q = 1 - c + 2*floor(c/2); let S = step(c - 3) - step(c - 5); let R = step(c - 5)
answer: S + (1 - S)*(1 - R)*q
tolerance: 0.01

## An SR latch holds Q = {q}. Its inputs then go to S = {S} and R = {R}. What is Q afterward?

::: misses
- [any] S = 1 sets Q to 1, R = 1 resets it to 0, and with both at 0 the latch keeps the Q it had.
:::
:::

::: check
given: ts = 40 | 60; tcq = 70 | 90
answer: ts + tcq
unit: ps
tolerance: 0.5%

## Flip-flop overhead in a clock period: setup {ts} ps plus clock-to-Q {tcq} ps?

::: misses
- [any] Add them.
:::
:::

::: check
kind: choice

## A latch is called "transparent" because...

- [x] while enabled, its output follows its input
- [ ] it uses no power
- [ ] it has no delay
- [ ] it can be seen through

::: right
Right. A flip-flop never is: it only samples at the edge.
:::

::: misses
- [2] Latches use power like any gate.
- [3] It has a delay.
- [4] It's a figure of speech for following the input.
:::
:::
:::
