BitFlow
← Academy·Latches and Flip-Flops
↻Core~40 min

Latches and Flip-Flops

How a circuit remembers: a bit stored in a loop, the difference between level and edge, and the four flip-flop types.

Interactive·Flip-Flop Simulator

Set inputs, then hit Clock ↑ to see the flip-flop capture on the rising edge — just like posedge clk in Verilog.

Inputs

State Register

Q
0
Q̄
1

Memory from feedback

Every circuit so far forgets instantly: change the inputs and the old output is gone. To remember, a circuit needs an output that feeds back into its own input. Two NOT gates in a loop can hold a bit forever, as long as there's power. The only trouble is that you can't change it. Replace the NOTs with NORs, and you get a way in.

The SR latch

The SR latch has two inputs: S (set) makes the stored bit 1, and R (reset) makes it 0. With both at 0, the loop holds whatever it had.

Loading the animation…

SRNext QWhat happens
00QHold
101Set
010Reset
11—Forbidden: Q and Q' stop being opposites
Watch outTry S = R = 1, then press Release both. Both gates change together, and the result depends on which gate is faster, so the outcome is unpredictable. Designs must never do this.

A NAND version works the same way with active-low inputs, so its forbidden input is S' = R' = 0.

Level and edge

A D latch removes the forbidden input by adding a data line D and an enable. While the enable is high, Q copies D. When it goes low, Q holds its last value. It's called transparent, because the input passes straight through while enabled.

That transparency is a problem. If a latch's output feeds back through logic to its own input, the signal can run round the loop many times while the clock is high. What we want is a device that changes only at one instant: a flip-flop, which responds to a clock edge.

Loading the animation…

A flip-flop can be built from two latches back to back, called master and slave. While the clock is low, the master follows D and the slave holds. When the clock rises, the master freezes and the slave copies it. So the output only changes at the rising edge, and there's never a straight path from input to output.

D flip-flop01234567clkdq
A rising-edge D flip-flop. Q takes the value D had just before each rising edge. Glitches on D between edges don't matter.

The four flip-flop types

Each type differs only in how its inputs decide the next state. Q⁺ means the value of Q after the next clock edge.

TypeNext stateUsed for
DQ⁺ = DStoring data. Nearly everything in modern design
TQ⁺ = T ⊕ QToggling, so counters
JKQ⁺ = JQ' + K'QGeneral purpose; J = K = 1 toggles
SRQ⁺ = S + R'Q, with SR = 0Mostly historic

Loading the circuit…

Three flip-flops on one clock. Set the inputs, then press Clock edge.
  • Turn on t, and press Clock edge a few times. q_t toggles each time.
  • Set j and k both on: q_jk toggles too. Now try only j, then only k.
  • Change d without clocking. q_d doesn't move until the edge.

To design with flip-flops you need the reverse question: to go from this Q to that Q⁺, what must the inputs be? That's the excitation table, and an X means either value works.

QQ⁺DTJKSR
00000X0X
01111X10
1001X101
1110X0X0
TipAll those X's are why JK flip-flops give small logic, and they feed K-maps as don't-cares. D is simplest to think about, since D is just the next state.
Practice

A JK flip-flop has Q = 1, J = 1 and K = 1. What's Q after three clock edges?

Converting between types

To turn a flip-flop of type X into type Y, work out what X's inputs must be, given what Y's inputs ask for. The method is the same each time.

  1. Write Y's characteristic table: Y's inputs and Q, giving Q⁺.
  2. Using X's excitation table, add the X input that produces each Q to Q⁺ change.
  3. Minimise X's input as a function of Y's inputs and Q.
To makeFromConnect
DJKJ = D, K = D'
TJKJ = T, K = T
TDD = T ⊕ Q
JKDD = JQ' + K'Q
DTT = D ⊕ Q

Loading the circuit…

A D flip-flop made from a JK and an inverter: J and K can never be equal, so it always loads D.
Challenge: JK flip-flop from D →Build the conversion in the table in the circuit editor, and earn XP.Challenge: T flip-flop from D →A shorter one, and a good place to start.

Reset, enable and Verilog

  • Asynchronous reset forces Q to 0 immediately, whatever the clock is doing.
  • Synchronous reset does it at the next clock edge, so it's just more logic in front of D.
  • Clock enable holds the value unless enabled, which is a MUX in front of D. The clock itself is left alone, because gating a clock with ordinary logic can create glitches that clock flip-flops by accident.
// A D flip-flop with asynchronous reset
always @(posedge clk or posedge rst)
  if (rst) q <= 1'b0;
  else     q <= d;
Use <= (nonblocking) for flip-flops, so that every flip-flop in the design sees the old values at the edge. Chapter 20 explains why.

Flip-flops have timing rules too. D must be stable for a short setup time before the edge and a hold time after it, and Q changes a little after the edge. Chapter 17 is about what happens when those rules are broken.

Exam pointsGATE: fine to skip at first

Race-around: in a level-triggered JK latch with J = K = 1, the output toggles repeatedly for as long as the clock stays high. Master-slave and edge-triggered flip-flops solve this, because the output changes only once per cycle.

A T flip-flop with T = 1 divides its clock by 2. Chain n of them and you divide by 2ⁿ.

Practice · GATE-style

What's the next-state equation of a flip-flop made by connecting J = K = T?

Practice · GATE-style

To change Q from 1 to 0 in a JK flip-flop, what must J and K be?

Practice · GATE-style

A 1 MHz clock drives a chain of 4 toggling flip-flops. What frequency comes out of the last?

Practice · GATE-style

To make a T flip-flop from a D flip-flop, D should be…

Was this chapter helpful?