Chapter 23 of 32

Flip-Flops: SR, JK, D & T

In Latches we built memory from two cross-coupled gates and found its weak point: a latch is transparent for the whole time its enable is active. If the latch output feeds back to its own input through some logic, the value can race around the loop several times within one enable pulse. Digital systems avoid this with a simple agreement: state changes only at one instant per clock cycle, the clock edge. The memory element that follows this rule is the flip-flop.

A flip-flop is an edge-triggered one-bit memory element: it samples its inputs and can change its stored state only at an active clock edge (rising or falling). At all other times its output is held, no matter how the inputs change.

In this chapter we see how an edge-triggered flip-flop is built from two latches, then study the four standard types (SR, JK, D and T) with their characteristic tables, characteristic equations, state diagrams and excitation tables. We also cover the race-around condition, asynchronous preset and clear, and the timing rules (setup, hold, clock-to-Q) that set the maximum clock frequency of every synchronous circuit. All equations were derived and checked in Python, and every SystemVerilog example was simulated with Icarus Verilog; the outputs shown are real.


The Clock and Edge Triggering

A clock is a square wave that paces the whole system. Each cycle has a rising (positive) edge, where the clock goes 0 → 1, and a falling (negative) edge, where it goes 1 → 0.

          +-----+     +-----+     +-----+
CLK       |     |     |     |     |     |
     -----+     +-----+     +-----+     +-----
          ↑     ↓     ↑     ↓     ↑     ↓
       rising falling ...

period T = time from one rising edge to the next,  frequency f = 1/T
(a 100 MHz clock has T = 10 ns)

Ways a storage element can respond to the clock:

Triggering

Output can change

Device

Level (high)

Any time while CLK = 1

Latch

Positive edge

Only at the 0 → 1 transition

Positive-edge-triggered flip-flop

Negative edge

Only at the 1 → 0 transition

Negative-edge-triggered flip-flop

In circuit symbols, a small triangle (>) at the clock input means edge-triggered; a bubble before the triangle means the falling edge is active.


Building a Flip-Flop: Master-Slave D

Put two D latches in series and give them opposite enables:

             +-----------+   M    +-----------+
D ---------->| D latch   |------->| D latch   |-------> Q
             | (master)  |        | (slave)   |
        +--->| EN = CLK′ |   +--->| EN = CLK  |
        |    +-----------+   |    +-----------+
CLK ----+---[NOT]--- (to master)
        +--------------------+ (to slave)

CLK = 0: master open (M follows D),  slave closed (Q holds)
CLK = 1: master closed (M frozen),   slave open (Q = M)

At the rising edge the master shuts and keeps the value D had at that instant; at the same moment the slave opens and passes that frozen value to Q. While CLK stays 1, D can change freely because the master is closed. While CLK is 0, the master follows D but the slave is closed, so Q cannot change. Net effect: Q takes the value of D at the rising edge, and only then. That is a positive-edge-triggered D flip-flop.

The simulation below drives a plain D latch and the master-slave flip-flop with the same clock and the same D:

`timescale 1ns/1ns
module d_latch(input logic EN, D, output logic Q);
  always_latch if (EN) Q <= D;
endmodule

// Positive-edge D flip-flop built as master-slave: master open while CLK = 0,
// slave open while CLK = 1
module dff_ms(input logic CLK, D, output logic Q);
  logic M;
  d_latch master(~CLK, D, M);
  d_latch slave ( CLK, M, Q);
endmodule

module tb;
  logic CLK = 0, D = 0;
  logic Q_latch, Q_ff;
  d_latch L (CLK, D, Q_latch);   // the same CLK used as the latch enable
  dff_ms  F (CLK, D, Q_ff);
  always #10 CLK = ~CLK;         // period 20 ns, rising edges at 10, 30, 50, ...
  initial begin
    $monitor("t=%2d  CLK=%b D=%b  Q_latch=%b  Q_ff=%b", $time, CLK, D, Q_latch, Q_ff);
    #2  D = 1;     // D changes while CLK = 0
    #13 D = 0;     // t=15: D changes while CLK = 1 (latch is transparent)
    #4  D = 1;     // t=19
    #6  D = 0;     // t=25
    #20 $finish;   // stop at t=45
  end
endmodule

Output:

t= 0  CLK=0 D=0  Q_latch=x  Q_ff=x
t= 2  CLK=0 D=1  Q_latch=x  Q_ff=x
t=10  CLK=1 D=1  Q_latch=1  Q_ff=1
t=15  CLK=1 D=0  Q_latch=0  Q_ff=1
t=19  CLK=1 D=1  Q_latch=1  Q_ff=1
t=20  CLK=0 D=1  Q_latch=1  Q_ff=1
t=25  CLK=0 D=0  Q_latch=1  Q_ff=1
t=30  CLK=1 D=0  Q_latch=0  Q_ff=0
t=40  CLK=0 D=0  Q_latch=0  Q_ff=0

Between t = 10 and t = 20 the clock is high: the latch output copies D's pulses at 15 and 19, but the flip-flop output stays at the value sampled at the edge (t = 10). The flip-flop changes again only at the next rising edge (t = 30), where it samples D = 0. The two outputs start as x because neither element has been given a value yet; real flip-flops power up in an unknown state too, which is why systems have a reset.


The Four Flip-Flop Types

Each type is described in four ways, and you should be able to move between them:

  • Characteristic table: inputs (and Q) → next state Q+. Used for analysis.

  • Characteristic equation: Q+ as a Boolean expression.

  • State diagram: two states (0 and 1) with arrows labelled by the inputs.

  • Excitation table: desired transition Q → Q+ → required inputs. Used for design (counters, FSMs, conversions).

SR Flip-Flop

The clocked version of the SR latch: S sets, R resets, at the clock edge.

S

R

Q+

Action

0

0

Q

Hold

0

1

0

Reset

1

0

1

Set

1

1

?

Invalid

Characteristic equation (from the K-map with the invalid rows as don't-cares): Q+ = S + R′Q, with SR = 0.

JK Flip-Flop

The JK flip-flop fixes the SR's invalid input by giving it a job: J = K = 1 toggles the output. J plays the role of S and K the role of R.

J

K

Q+

Action

0

0

Q

Hold

0

1

0

Reset

1

0

1

Set

1

1

Q′

Toggle

Deriving the characteristic equation. Expand the table with Q as an input and plot Q+:

               KQ
             00   01   11   10
           +----+----+----+----+
   J = 0   |  0 |  1 |  0 |  0 |
           +----+----+----+----+
   J = 1   |  1 |  1 |  0 |  1 |
           +----+----+----+----+

Group JQ′ : cells J K Q = 100, 110
Group K′Q : cells J K Q = 001, 101

Q+ = JQ′ + K′Q

JK characteristic equation: Q+ = JQ′ + K′Q. If Q = 0, the next state is J; if Q = 1, the next state is K′.

That last sentence is a fast way to evaluate a JK flip-flop in exam questions. A simulation stepping through all four input combinations:

`timescale 1ns/1ns
module jk_ff(input logic CLK, J, K, output logic Q);
  always_ff @(posedge CLK)
    case ({J, K})
      2'b00: Q <= Q;      // hold
      2'b01: Q <= 1'b0;   // reset
      2'b10: Q <= 1'b1;   // set
      2'b11: Q <= ~Q;     // toggle
    endcase
endmodule

module tb;
  logic CLK = 0, J, K, Q;
  jk_ff u(CLK, J, K, Q);
  always #5 CLK = ~CLK;                 // rising edges at 5, 15, 25, ...
  initial begin
    u.Q = 0;                            // start from Q = 0 for the demo
    {J,K} = 2'b10;                      // set
    #10 {J,K} = 2'b00;                  // hold
    #10 {J,K} = 2'b01;                  // reset
    #10 {J,K} = 2'b11;                  // toggle, toggle, toggle, toggle
    #40 $finish;
  end
  always @(posedge CLK) #1 $display("edge at t=%0d  J=%b K=%b  ->  Q=%b", $time-1, J, K, Q);
endmodule

Output:

edge at t=5  J=1 K=0  ->  Q=1
edge at t=15  J=0 K=0  ->  Q=1
edge at t=25  J=0 K=1  ->  Q=0
edge at t=35  J=1 K=1  ->  Q=1
edge at t=45  J=1 K=1  ->  Q=0
edge at t=55  J=1 K=1  ->  Q=1
edge at t=65  J=1 K=1  ->  Q=0

With J = K = 1, Q changes on every rising edge, so it completes one full cycle every two clock cycles: the output is a square wave at half the clock frequency. This divide-by-2 behaviour is the basis of the counters in Asynchronous Counters.

D (Data / Delay) Flip-Flop

D

Q+

0

0

1

1

Q+ = D. The output is the input delayed by one clock cycle, hence "delay" flip-flop. It has no hold input of its own: to keep a value you must feed Q back to D (usually through a multiplexer, as we will do in Registers & Shift Registers). The D flip-flop is by far the most used type: FPGAs and standard-cell ASIC libraries are built around it, and synthesis tools turn every always_ff block into D flip-flops.

Connecting Q′ back to D gives Q+ = Q′: a toggle on every edge, i.e. a divide-by-2 circuit using only a D flip-flop.

T (Toggle) Flip-Flop

T

Q+

Action

0

Q

Hold

1

Q′

Toggle

Q+ = T ⊕ Q = TQ′ + T′Q. The T flip-flop is a JK flip-flop with J and K tied together. It is not usually sold as a separate IC, but it is the natural building block for binary counters, because bit i of a counter toggles exactly when all lower bits are 1.

State Diagrams

SR:   (0) --S=1,R=0--> (1)      (0) loops on S=0 (R=x)
      (1) --S=0,R=1--> (0)      (1) loops on R=0 (S=x)

JK:   (0) --J=1 (K=x)--> (1)    (0) loops on J=0 (K=x)
      (1) --K=1 (J=x)--> (0)    (1) loops on K=0 (J=x)

D:    (0) --D=1--> (1)          (0) loops on D=0
      (1) --D=0--> (0)          (1) loops on D=1

T:    (0) --T=1--> (1)          (0) loops on T=0
      (1) --T=1--> (0)          (1) loops on T=0

Excitation Tables

For design we ask the reverse question: "I want Q to go from this value to that value; what inputs must I apply?" x means either value works.

Q

Q+

S

R

J

K

D

T

0

0

0

x

0

x

0

0

0

1

1

0

1

x

1

1

1

0

0

1

x

1

0

1

1

1

x

0

x

0

1

0

How to read one row: for 0 → 1 on a JK, both "set" (J = 1, K = 0) and "toggle" (J = 1, K = 1) work, so K = x. For 0 → 0 on an SR, both "hold" (S = 0, R = 0) and "reset" (S = 0, R = 1) work, so R = x. The JK has the most don't-cares (two per row), which is why JK-based counter designs often need the least logic. The D column is simply Q+, and the T column is Q ⊕ Q+.


The Race-Around Condition

Suppose a JK flip-flop is built as a level-triggered (gated latch) circuit, with the outputs fed back to the input gates as in the JK equation, and J = K = 1. When the clock goes high, Q toggles after one propagation delay tpd. The new Q feeds back to the input gates, and since the clock is still high, it toggles again, and again, every tpd, until the clock goes low.

CLK    ____|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾|________
Q      ____|‾‾‾|___|‾‾‾|___|‾‾‾|___________    (J = K = 1, pulse width ≫ t_pd)
           ←tpd→
final Q depends on how many toggles fit in the pulse → unpredictable

Race-around condition: in a level-triggered JK flip-flop with J = K = 1, the output toggles repeatedly during a single clock pulse when the pulse width tw is greater than the flip-flop's propagation delay tpd, so the final state is unpredictable.

Ways to avoid it:

  1. Make the clock pulse width shorter than the propagation delay (tw < tpd). Hard to guarantee in practice.

  2. Use a master-slave JK flip-flop: the master responds while CLK = 1, but the outputs that feed back come from the slave, which does not change until CLK falls. One toggle per clock cycle.

  3. Use an edge-triggered JK flip-flop (the modern solution).

The classic pulse-triggered master-slave JK has its own quirk, ones catching: while CLK = 1, a brief glitch to 1 on J or K can set or reset the master, and that wrong value is passed on at the falling edge even if the glitch has gone. Edge-triggered designs do not have this problem because they look at the inputs only around the edge. (This is one more reason the glitches from Hazards in Combinational Circuits matter in older designs.)


Asynchronous Preset and Clear

Clocked inputs (D, J, K, S, R, T) are synchronous: they act only at a clock edge. Most flip-flop ICs also have asynchronous inputs that act immediately, overriding the clock:

  • Preset (PRE): forces Q = 1.

  • Clear (CLR), also called reset: forces Q = 0.

They are usually active-low (written PRE′ and CLR′, or with a bar) and are used to put a system into a known state at power-on. Both must not be active together. On the 74HC74 (dual positive-edge D flip-flop with preset and clear) asserting both drives Q and Q′ high at the same time, which is not a valid stored state.

`timescale 1ns/1ns
// D flip-flop with asynchronous active-low preset and clear (like one half of a 74HC74)
module dff_pc(input logic CLK, D, PRE_n, CLR_n, output logic Q);
  always_ff @(posedge CLK, negedge PRE_n, negedge CLR_n)
    if (!CLR_n)      Q <= 1'b0;
    else if (!PRE_n) Q <= 1'b1;
    else             Q <= D;
endmodule

module tb;
  logic CLK = 0, D = 0, PRE_n = 1, CLR_n = 1, Q;
  dff_pc u(CLK, D, PRE_n, CLR_n, Q);
  always #10 CLK = ~CLK;            // rising edges at 10, 30, 50, 70
  initial begin
    $monitor("t=%2d  CLK=%b D=%b PRE_n=%b CLR_n=%b  Q=%b", $time, CLK, D, PRE_n, CLR_n, Q);
    #3  CLR_n = 0;                  // t=3: clear at once, no clock edge needed
    #4  CLR_n = 1;                  // t=7
    #10 D = 1;                      // t=17: stored at the edge at t=30
    #20 D = 0;                      // t=37: stored at the edge at t=50
    #18 PRE_n = 0;                  // t=55: preset at once
    #3  PRE_n = 1;                  // t=58
    #20 $finish;                    // t=78
  end
endmodule

Output:

t= 0  CLK=0 D=0 PRE_n=1 CLR_n=1  Q=x
t= 3  CLK=0 D=0 PRE_n=1 CLR_n=0  Q=0
t= 7  CLK=0 D=0 PRE_n=1 CLR_n=1  Q=0
t=10  CLK=1 D=0 PRE_n=1 CLR_n=1  Q=0
t=17  CLK=1 D=1 PRE_n=1 CLR_n=1  Q=0
t=20  CLK=0 D=1 PRE_n=1 CLR_n=1  Q=0
t=30  CLK=1 D=1 PRE_n=1 CLR_n=1  Q=1
t=37  CLK=1 D=0 PRE_n=1 CLR_n=1  Q=1
t=40  CLK=0 D=0 PRE_n=1 CLR_n=1  Q=1
t=50  CLK=1 D=0 PRE_n=1 CLR_n=1  Q=0
t=55  CLK=1 D=0 PRE_n=0 CLR_n=1  Q=1
t=58  CLK=1 D=0 PRE_n=1 CLR_n=1  Q=1
t=60  CLK=0 D=0 PRE_n=1 CLR_n=1  Q=1
t=70  CLK=1 D=0 PRE_n=1 CLR_n=1  Q=0

The clear at t = 3 and the preset at t = 55 take effect immediately, in the middle of a clock cycle. The change of D at t = 17 has no effect until the edge at t = 30. Note that after the preset, the next rising edge (t = 70) loads D = 0 again: asynchronous inputs only override the flip-flop while they are active.


Timing Parameters

A real flip-flop needs its input to be steady for a short window around the active edge, and it takes time to respond:

               setup   hold
              |<---->|<->|
D    =========X======STABLE===X==========
CLK  ________________|‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
                     |<-t_cq->|
Q    =========================X new value
  • Setup time tsu: how long D must be stable before the edge.

  • Hold time th: how long D must stay stable after the edge.

  • Clock-to-Q delay tcq (propagation delay tpd): time from the edge until Q shows the new value.

Violating setup or hold can leave the flip-flop metastable: its output hovers between logic levels for an unpredictable time before falling to 0 or 1. This is a real risk whenever a signal comes from outside the clock domain (a push button, another chip with its own clock). The standard defence is a two-flip-flop synchronizer, which gives the first flip-flop a whole clock cycle to settle.

Maximum Clock Frequency

Consider a flip-flop whose output passes through combinational logic into another flip-flop (or back into itself). After an edge, the signal needs tcq to leave the first flip-flop, tcomb to pass through the logic, and must then arrive tsu before the next edge:

Tclk ≥ tcq + tcomb(max) + tsu, so fmax = 1 / (tcq + tcomb(max) + tsu). Hold condition: tcq(min) + tcomb(min) ≥ th.

Worked example. tcq = 2 ns, longest logic path = 5 ns, tsu = 1 ns, th = 0.5 ns, shortest logic path = 0.3 ns, minimum tcq = 1.5 ns.

Setup:  T_min = 2 + 5 + 1 = 8 ns   →  f_max = 1 / 8 ns = 125 MHz
Hold:   1.5 + 0.3 = 1.8 ns ≥ 0.5 ns  →  no hold violation

Notice the clock period does not appear in the hold condition. A setup problem can be fixed by slowing the clock; a hold problem cannot, it must be fixed by adding delay to the short path. (These numbers are illustrative; real values come from the datasheet of the specific device or cell library.)


Comparison of Flip-Flop Types

Type

Inputs

Characteristic equation

Special behaviour

Typical use

SR

S, R

Q+ = S + R′Q (SR = 0)

S = R = 1 invalid

Mainly teaching; simple set/reset control

JK

J, K

Q+ = JQ′ + K′Q

J = K = 1 toggles

Counters in discrete logic; most flexible

D

D

Q+ = D

No hold mode by itself

Registers, pipelines, FSMs, FPGAs/ASICs

T

T

Q+ = T ⊕ Q

T = 1 toggles

Binary counters, frequency division

Point

Latch

Flip-flop

Triggering

Level-sensitive

Edge-triggered

Transparent

Yes, while enabled

No

Size

Smaller

Roughly two latches

Timing analysis

Harder (time borrowing)

Simple: one sample per cycle


Common Mistakes

  • Writing Q+ = J + K′Q for the JK. The J term needs Q′: Q+ = JQ′ + K′Q. Check it: with J = K = 1 and Q = 1, the wrong form gives 1 instead of the toggle result 0.

  • Confusing characteristic and excitation tables. Characteristic: inputs → next state (analysis). Excitation: present → next state → inputs (design).

  • Thinking a master-slave flip-flop changes at the rising edge. A master-slave pair whose slave opens when CLK goes high (as drawn above) updates Q at the rising edge; the classic pulse-triggered master-slave JK with the opposite arrangement updates Q at the falling edge. Always check which latch drives the output.

  • Saying race-around happens in edge-triggered JK flip-flops. It is a problem of level-triggered JK circuits when the pulse width exceeds the propagation delay.

  • Treating preset/clear as synchronous. They act immediately, without a clock edge.

  • Trying to fix a hold violation by lowering the clock frequency. Hold time does not depend on the period.


Interview and GATE-Style Questions

A JK flip-flop has J = K = 1 and is clocked at 10 MHz. What is the frequency of Q?

Q toggles on every active edge, so one output cycle takes two clock cycles: 5 MHz (a square wave with 50% duty cycle).

In a JK flip-flop, K is connected to J′ (K = J′). What does it behave like?

Q+ = JQ′ + K′Q = JQ′ + JQ = J. It behaves like a D flip-flop with D = J.

A JK flip-flop has J = Q′ and K = Q (its own outputs). Starting from Q = 0, what is the output sequence?

Q+ = JQ′ + K′Q = Q′·Q′ + Q′·Q = Q′. It toggles every clock: 0, 1, 0, 1, ...

What is the race-around condition and how is it eliminated?

In a level-triggered JK flip-flop with J = K = 1, the output keeps toggling during the clock pulse whenever the pulse width exceeds the propagation delay, giving an unpredictable final state. It is eliminated by keeping the pulse width below the propagation delay, or (in practice) by using a master-slave or edge-triggered flip-flop.

Fill in the excitation table entries for a JK flip-flop for the transitions 1 → 0 and 1 → 1.

1 → 0: J = x, K = 1 (reset or toggle). 1 → 1: J = x, K = 0 (hold or set).

A flip-flop has tcq = 1 ns and tsu = 0.5 ns. The logic between two flip-flops has a worst-case delay of 3.5 ns. What is the maximum clock frequency?

Tmin = 1 + 3.5 + 0.5 = 5 ns, so fmax = 200 MHz.

How can a D flip-flop be used as a divide-by-2 circuit?

Connect Q′ to D. Then Q+ = Q′, so Q toggles on every active edge and its frequency is half the clock frequency.


Summary

  • A flip-flop changes state only at a clock edge; a master-slave pair of latches with opposite enables is one way to build it.

  • SR: Q+ = S + R′Q with SR = 0. JK: Q+ = JQ′ + K′Q, J = K = 1 toggles. D: Q+ = D. T: Q+ = T ⊕ Q.

  • Excitation tables give the inputs needed for each transition and are the main tool for designing counters and state machines.

  • Race-around affects level-triggered JK flip-flops with J = K = 1; master-slave or edge-triggered designs remove it.

  • Asynchronous preset/clear override the clock and initialise the system.

  • Timing: Tclk ≥ tcq + tcomb(max) + tsu; hold requires tcq(min) + tcomb(min) ≥ th. Violations can cause metastability.

Next, in Flip-Flop Conversions, we use the characteristic and excitation tables from this chapter to make any flip-flop behave like any other.