Volume 07 Intermediate 6 sub-modules ~20 min read

Latches and Flip-Flops

Every circuit so far has forgotten everything the moment its inputs changed. This volume gives circuits memory. It starts with two NOT gates in a ring, builds the SR latch and the D latch, then the edge-triggered D flip-flop that every digital system is built on - and measures its setup and hold times by simulating it one gate at a time.

You will learn
  • Why memory needs feedback, and how two NOT gates hold a bit
  • How the SR latch works, and why S = R = 1 is not allowed
  • The difference between a level-sensitive latch and an edge-triggered flip-flop
  • JK and T flip-flops, characteristic equations and excitation tables
  • What setup time, hold time and clock-to-Q mean, and where they come from
You need
  • Volumes 02 and 03 of this course: NAND and NOR gates, and reading timing diagrams.

7.1 Why circuits need memory

Every circuit so far forgets: its output depends only on its inputs right now. To remember, a circuit needs feedback - a loop that feeds its output back to its own input. The simplest memory is two NOT gates in a ring.

A calculator must remember the first number while you type the second. A counter must remember where it has got to. A lock must remember which digits you have already pressed. None of these can be built from the circuits of Volumes 02 to 06, whose outputs depend only on their inputs at that moment. They need sequential logic: circuits whose output also depends on what happened before.

Two NOT gates in a ring

Join two NOT gates in a loop, as in Figure 7.1. If the first output is 0, the second is 1, which keeps the first at 0. If the first is 1, the second is 0, which keeps the first at 1. Either way, the loop holds itself steady.

Two NOT gates joined in a ring, which hold either value for ever Q
Figure 7.1 - Each gate's output is the other gate's input. Whatever value the ring starts with, each gate keeps the other where it is. This is the simplest possible memory: one bit.

figure ring: holds 0 for ever if it starts at 0, and 1 if it starts at 1: True

A circuit with two steady states like this is bistable. The trouble is that nothing can change it: there is no input. The rest of this volume adds inputs.

Quick check

What does every circuit that can remember have?

Show the answer

Answer: C. Memory needs a loop, so that the circuit's own output helps hold it in place. A clock is common, but the ring of two NOT gates remembers without one.

7.2 The SR latch

Replace the NOT gates with NOR gates and the ring gets two inputs. The SR latch sets its output to 1 when S is 1, resets it to 0 when R is 1, and remembers when both are 0.

An SR latch built from two cross-coupled NOR gates R S Q Q'
Figure 7.2 - Each NOR gate's output feeds the other's input, as in the ring. R forces Q to 0, S forces Q' to 0 and so Q to 1, and with both inputs at 0 the loop holds its value. Wires that cross without a dot are not joined.

A NOR gate's output is 0 whenever any input is 1. So R = 1 forces Q to 0, and S = 1 forces Q' to 0, which then makes Q equal 1. With S and R both 0, each gate simply inverts the other's output, and the latch holds whatever it had.

The SR latch set, left alone, reset, left alone and set again, with every gate taking one step S R Q Q'
Figure 7.3 - Q is unknown until S first sets it. After that, each pulse on S or R changes Q, and Q holds its value in between. Q' is always the opposite of Q.

figure sr-nor: unknown until set, then set, holds, resets, holds and sets as its table says: True

Notice the start. Before S is first raised, nobody knows what Q holds - the simulator shows it as unknown. A real latch powers up in either state. That is why real designs reset their memories before use.

The SR latch's table

S R Q after
0 0 Q (no change)
0 1 0 (reset)
1 0 1 (set)
1 1 not allowed

Why is S = R = 1 not allowed? Both NOR gates are then forced to 0, so Q and Q' are both 0 - no longer opposites:


with S = R = 1, Q and Q' are both 0: True

Worse is what happens when both inputs drop to 0 at the same moment. Each gate sees two 0s and heads for 1, then each sees the other's 1 and heads back to 0. With two perfectly equal gates, the latch oscillates:

An SR latch with S and R both 1, then both released at once, in a model with two equal gates S R Q Q'
Figure 7.4 - While S and R are both 1, Q and Q' are both 0. When both are released together, the two gates chase each other. A real latch settles to 0 or 1 in the end, but nobody can say which, or how long it will take.

In a real circuit the two gates are never exactly equal, so the latch soon falls one way or the other. But which way, and when, cannot be predicted. So the rule is simple: never set S and R to 1 together.

Quick check

An SR latch has Q = 1. S and R are both 0. What happens to Q?

Show the answer

Answer: A. With both inputs at 0, the latch holds its value. That is its job: it remembers the last set or reset.

7.3 The D latch

A D latch has a data input D and an enable EN. While EN is 1 the latch is transparent: Q follows D. When EN goes to 0, the latch holds whatever D was at that moment.

The SR latch has two awkward points: two inputs to manage, and a combination that is not allowed. The D latch solves both. It makes S and R from a single input, D, so they can never both be 1, and it adds an enable that decides when D may get through.

A D latch built from four NAND gates and a NOT gate D EN Q Q'
Figure 7.5 - The left two NAND gates pass D and its inverse only while EN is 1. The right two NAND gates form a latch - an SR latch made of NAND gates, whose inputs are active when 0.
A D latch: Q follows D while EN is 1, and holds its value while EN is 0 EN D Q
Figure 7.6 - While EN is 1, every change of D reaches Q a few steps later. While EN is 0, D can do what it likes and Q stays put. When EN returns to 1, Q starts following D again.

Q rises 2 steps after D rises, and falls 4 steps after D falls (the falling path has the NOT gate)
figure d-latch: settles to D within 4 steps while EN = 1, and holds while EN = 0: True

The first line is a reminder that real paths have different lengths. Q rises in two steps but falls in four, because the falling change has to pass through the extra NOT gate first.

Common mistake

Expecting a latch to change only at one instant. A D latch is level sensitive: all the time EN is 1, any change of D goes straight through to Q, glitches included. For a circuit that changes at one exact moment, you need the flip-flop of the next sub-module.

Quick check

A D latch has EN = 0. D changes from 0 to 1 and back to 0. What does Q do?

Show the answer

Answer: B. With EN at 0 the latch is closed, so it ignores D completely and Q holds whatever it had.

7.4 The D flip-flop

A D flip-flop copies D to Q only at the rising edge of its clock, and holds it until the next rising edge. It is built from two D latches, a master and a slave, that are never open at the same time.

Master and slave

The master latch is open while the clock is 0, and the slave while the clock is 1. So while the clock is low, the master follows D and the slave holds. The moment the clock rises, the master closes on the value D had, and the slave opens to pass that value to Q. Nothing D does after that can get past the closed master until the clock falls and rises again.

A master-slave D flip-flop: two D latches, one enabled by the clock and one by its inverse CLK D Q master latch D EN Q slave latch D EN Q
Figure 7.7 - The NOT gate opens the master while CLK is 0. The slave is opened by CLK itself. At the rising edge the master shuts and the slave passes its value to Q, so Q changes only at that edge.

Figure 7.8 shows a gate-level flip-flop built this way, from two NAND latches like the one in sub-module 7.3 and a NOT gate on the clock. Every gate takes one step:

A gate-level D flip-flop: Q takes the value D had at each rising clock edge CLK D Q
Figure 7.8 - Q changes only just after a rising edge of CLK, to the value D had just before it. The short pulse on D while CLK is high never reaches Q, because the master latch is shut at that time.

rising edges at 4, 12, 20, 28, 36, 44; Q changes at 14, 23, 38
every change of Q comes just after a rising edge: True
D pulses high from 21 to 23, while CLK is high and the master is shut, and Q never sees it: True

A circuit that acts only at the edge of a clock is edge-triggered. This is what makes large digital systems work. Every flip-flop in a chip changes at the same instant, the rising edge, and between edges the logic has a whole clock period to settle.

Quick check

When does a D flip-flop copy D to Q?

Show the answer

Answer: D. At the rising edge the master latch shuts on the value of D, and the slave passes it to Q. At every other moment, one of the two latches is closed, so D cannot get through.

7.5 JK and T flip-flops

Other flip-flops decide their next state differently. A JK flip-flop can hold, reset, set or toggle. A T flip-flop toggles when T is 1. Each is summed up by a characteristic equation, and each has an excitation table for designing with it.

The JK flip-flop

The JK flip-flop fixes the SR latch's forbidden input. J acts like set and K like reset, but J = K = 1 is allowed: it toggles Q to the opposite value.

J K Q Q next
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 0

Its characteristic equation - the characteristic table written as a formula - gives the next state in one line:


Q next = J·Q' + K'·Q gives hold, reset, set, toggle for JK = 00, 01, 10, 11: True

The T flip-flop

A T flip-flop has one input. When T is 0 it holds, and when T is 1 it toggles. Its equation is Q next = T ⊕ Q:

T Q Q next
0 0 0
0 1 1
1 0 1
1 1 0

With T held at 1, Q toggles on every rising edge. Its output is a clock at half the speed - the start of every counter in Volume 08:

A T flip-flop with T held at 1: Q toggles at every clock edge clk T Q
Figure 7.9 - Q changes at every rising edge, so it completes one cycle for every two cycles of the clock. The output is the clock frequency divided by two.

Excitation tables

A characteristic table answers "given the inputs, what comes next?". When designing, the question is the other way round: "to get from this Q to that Q, what inputs do I need?". That is an excitation table. X means the input may be 0 or 1:

Q Q next D J K T
0 0 0 0 X 0
0 1 1 1 X 1
1 0 0 X 1 1
1 1 1 X 0 0

Read the JK row for 0 to 1: J must be 1, but K does not matter. J = 1 with K = 0 sets Q, and J = 1 with K = 1 toggles it - and both take a 0 to a 1. Those X entries are don't cares, and Volume 08 uses them to make counters smaller.

Quick check

A JK flip-flop has Q = 1, J = 1 and K = 1. What is Q after the next clock edge?

Show the answer

Answer: C. J = K = 1 toggles the flip-flop, so Q goes from 1 to 0. Unlike the SR latch, the JK flip-flop has no forbidden input.

7.6 Setup, hold and clock-to-Q

A flip-flop needs D to be steady for a short setup time before the clock edge and a short hold time after it. Its output then changes one clock-to-Q delay after the edge. All three come from the delays of the gates inside.

A flip-flop cannot capture D in zero time. Inside, the master latch has to settle on the new value before it shuts. If D changes too close to the edge, the flip-flop may catch the old value, the new one, or something in between.

Measuring them on the simulated flip-flop

The simulator can find these numbers for the gate-level flip-flop of sub-module 7.4. It changed D at different times around a clock edge, once rising and once falling, and watched Q. A capture is clean when Q changes exactly as it does for a D that changed long before: once, to the right value, at the usual time. After the edge, clean means Q does not move at all.

D changes D rising D falling
7 steps before clean clean
6 steps before clean clean
5 steps before clean clean
4 steps before clean clean
3 steps before clean clean
2 steps before clean late or glitch
1 step before late or glitch late or glitch
0 steps after changed clean
1 step after clean clean
2 steps after clean clean
3 steps after clean clean
4 steps after clean clean
5 steps after clean clean

setup: D must be steady at least 3 steps before the edge
hold: D may change from 1 step after the edge
clock-to-Q: Q rises 2 steps after the rising edge, and falls 3 steps after it

So this flip-flop's setup time is 3 gate delays and its hold time is 1. Notice that a falling D needs more time than a rising one, because it passes through the extra NOT gate. Datasheets quote the worst case, and so should you.

What goes wrong in a real chip

In the simulator, a late change gives a late or glitching output. In a real flip-flop it can be worse. If D changes right at the edge, the latch inside can be caught exactly between 0 and 1, like a coin balanced on its edge. It stays there for an unpredictable time before falling one way or the other. This is metastability, and a simulation that only knows 0 and 1 cannot show it.

The cure is to keep the rules: D steady for the setup time before every edge, and for the hold time after it. The course on static timing analysis is about checking exactly that, for every flip-flop in a chip.

Quick check

A flip-flop's setup time is 3 gate delays. D changes 2 gate delays before the clock edge. What might happen?

Show the answer

Answer: A. D did not settle for the full setup time, so the master latch may not have settled when it shut. In the course's simulation that gave a late or glitching output; in a real chip it can even cause metastability.

What you learned

Key words from this volume

Every word below has a plain-English entry in the glossary.

Practice

Practice 1

Follow an SR latch

An SR latch starts with Q = 0. S goes to 1 then back to 0; then R goes to 1 then back to 0; then nothing happens for a while. What is Q after each step?

Show the solution
  1. S = 1: set, so Q = 1.
  2. S back to 0: both inputs 0, so it holds: Q = 1.
  3. R = 1: reset, so Q = 0.
  4. R back to 0: holds: Q = 0, for as long as nothing changes.
Practice 2

Latch or flip-flop?

A signal D changes several times while a clock is high. Which of these ends up with the value D had at the start of the high phase: a D latch enabled by the clock, or a rising-edge D flip-flop?

Show the solution

The flip-flop. It captures D at the rising edge and ignores everything until the next one. The latch is transparent the whole time the clock is high, so its Q follows D through every change and ends with D's last value.

Practice 3

JK inputs for a change

A JK flip-flop holds Q = 1. What must J and K be so that after the next edge Q = 0?

Show the solution

From the excitation table, the change from 1 to 0 needs J = X and K = 1. K = 1 with J = 0 resets, and K = 1 with J = 1 toggles - and both take a 1 to a 0, so J does not matter.

Interview corner

Interview question 1

Latch versus flip-flop

"What is the difference between a latch and a flip-flop?"

Show the solution

"A latch is level sensitive: while its enable is active it is transparent, and any change of D passes to Q. A flip-flop is edge-triggered: it samples D only at a clock edge. A D flip-flop is usually two latches in a row, master and slave, opened by opposite levels of the clock, so there is never a path straight through. Designs use flip-flops so that everything changes at one instant, and timing is easy to check."

Interview question 2

Setup, hold and clock-to-Q

"Define setup time, hold time and clock-to-Q delay."

Show the solution

"Setup time is how long D must be steady before the active clock edge; hold time is how long it must stay steady after it. Clock-to-Q is the delay from the clock edge to the new value at Q. They come from the gates inside the flip-flop. In a gate-level simulation of a master-slave flip-flop, a D that changed less than 3 gate delays before the edge gave a late or glitching output. In silicon, violating setup or hold can cause metastability."

Volume 08 puts flip-flops to work: registers that store words, shift registers and counters.