Skip to content
Digital Electronic Systems

Latches and flip-flops: memory, setup, hold and metastability

The NOR SR latch, the transparent D latch, the master-slave D flip-flop, setup and hold times, metastability with the MTBF calculation, and synchronisers.

A combinational network forgets everything as soon as its inputs change. To count, remember a state, build registers and processors, memory elements are needed: circuits that hold a bit until we decide to change it. In this lesson we see how feedback creates memory in the SR latch, the D latch, the edge-triggered D flip-flop, its timing constraints and the phenomenon of metastability.

The SR latch

Two NOR gates connected in a loop: the output of each is an input of the other.

SR latch made of NOR gates and timing diagram of a D flip-flop
On the left two NOR gates with their outputs cross-coupled to the inputs: R sets Q to 0, S sets it to 1, with both at 0 the latch holds its value. On the right a D flip-flop: the output Q copies the value of D only at the instant of the clock’s rising edge and holds it until the next edge; a short pulse on D between two edges is ignored.

Operation.

  • S = 1, R = 0: the lower gate has an input at 1, so \overline{Q} = 0; the upper gate has both inputs at 0, so Q = 1. The latch is set.
  • S = 0, R = 1: by symmetry Q = 0: the latch is reset.
  • S = R = 0: each gate acts as an inverter of the other. The combinations Q = 1, \overline{Q} = 0 and Q = 0, \overline{Q} = 1 are both stable, and the latch holds whichever it was in. This is memory.
  • S = R = 1: both outputs at 0, and when the two inputs return to 0 together the final state is unpredictable. It is the forbidden combination.

The principle is deep: two inverters in a loop have two stable equilibrium states, and the state they are in is the stored information. An SRAM memory cell is exactly this, plus two transistors to read and write it.

The D latch

To remove the forbidden combination and control when data is stored: a single data input D and an enable input E. With E = 1 the latch is transparent: Q follows D. With E = 0 the latch is closed and holds the last value. The problem with transparency is that, while E stays at 1, every change of D reaches the output, and in a circuit with feedback the signal could go round several times in the same period.

The D flip-flop

The D flip-flop changes state only at the instant of the clock’s rising edge: Q takes the value D has at that moment and holds it for the whole period, whatever D does meanwhile. It is built from two D latches in cascade (master-slave) enabled by opposite clock phases: with the clock low the first is transparent and follows D, the second is closed; on the rising edge the first closes, freezing the value, and the second opens and passes it to the output. There is never a transparent path from D to Q.

The D flip-flop is the memory element of almost all synchronous digital circuits. The other types (JK, T) are derived from it with a little logic: for example a T flip-flop, which toggles its state on every edge, is a D with D = \overline{Q}, that is a divide-by-two frequency divider.

Timing constraints

For the flip-flop to capture the data correctly, D must be stable:

  • for a time t_{su} before the edge (the setup time);
  • for a time t_h after the edge (the hold time).

After the edge, the output changes with a delay t_{cq} (clock to output). These three parameters, tens of picoseconds in modern integrated circuits, set the maximum frequency of a synchronous circuit, as we will see in the lesson on timing.

Metastability

What happens if D changes right inside the setup and hold window? The internal latch can stop at an unstable equilibrium point halfway between 0 and 1, like a ball on top of a hill, and stay there for an unpredictable time before falling one way or the other. This is metastability.

It cannot be avoided when sampling an asynchronous signal (a push-button, data arriving from another device with a different clock), because sooner or later it will change at the wrong moment. It can, however, be made extremely unlikely. The probability that the metastable state lasts longer than a time t_r decreases as e^{-t_r/\tau}, with \tau a flip-flop constant of tens of picoseconds, and the mean time between failures is

    \[ \mathrm{MTBF} = \frac{e^{t_r/\tau}}{T_0\,f_{clk}\,f_{data}} \]

Example. With \tau = 50 ps, T_0 = 20 ps, a 100 MHz clock and data changing at 1 MHz: allowing t_r = 1 ns to resolve, the MTBF is about 2.4\cdot 10^5 s, less than three days; with t_r = 2 ns it becomes 1.2\cdot 10^{14} s, almost four million years. The exponential makes every extra nanosecond enormously effective.

Hence the universal rule: an asynchronous signal goes through a synchroniser, two (or three) flip-flops in cascade on the same clock, before being used. The first may go metastable, but it has a whole period to resolve before the second reads it.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x