Every circuit we have built so far, from adders to multiplexers to parity checkers, was combinational: its output depends only on the inputs applied right now. Remove the input and the answer is gone. But a calculator remembers the number you typed, a lift remembers which floor was requested, and a CPU remembers the value in every register. For that we need circuits with memory.
The trick is surprisingly simple: take a gate's output and feed it back to an input. With the right gates, this loop can hold a 0 or a 1 indefinitely. The simplest such memory element is the latch, and it is the building block of every flip-flop, register and counter in the chapters that follow.
A latch is a bistable memory element that stores one bit. Its output can change whenever its inputs (and enable, if it has one) are active, i.e. it is level-sensitive, not edge-triggered.
In this chapter we analyse the SR latch built from NOR gates and from NAND gates, see exactly why the "forbidden" input is forbidden, add an enable input to get the gated SR latch and the D latch, and look at real uses of latches. The characteristic equations were derived and checked with a Python script, and the latch behaviour was simulated at gate level with Icarus Verilog; the outputs shown are real.
Combinational vs Sequential Circuits
Point | Combinational | Sequential |
|---|---|---|
Output depends on | Present inputs only | Present inputs and present state (past history) |
Memory | None | Yes (latches or flip-flops) |
Feedback paths | None | Yes |
Described by | Truth table | Characteristic/state table, state diagram |
Examples | Adder, MUX, decoder, comparator | Latch, flip-flop, register, counter, FSM |
We use Q for the present state (the value stored now) and Q+ (also written Q(t+1) or Qn+1) for the next state.
The Basic Bistable Element
Connect two inverters in a ring: the output of the first drives the second, and the output of the second drives the first.
+-----[ NOT ]-----+
| |
Q Q′
| |
+-----[ NOT ]-----+
If Q = 1 → Q′ = 0 → Q = 1 (stable)
If Q = 0 → Q′ = 1 → Q = 0 (stable)
The ring has two stable states, so it is bistable: it stores one bit for as long as power is on. (This is essentially the core of an SRAM cell.) The problem: there is no input, so we cannot choose which state it is in. Replacing the inverters with NOR or NAND gates gives us inputs to control it.
The SR Latch with NOR Gates
Recall that a NOR gate outputs 1 only when all its inputs are 0. In other words, a 1 on any input forces a NOR output to 0. Keep that in mind and the analysis is easy.
+-------+
R -------------->| |
| NOR |-----+------> Q
+---->| G1 | |
| +-------+ |
| |
+------\ /------+
\ /
\ / (cross-coupled feedback:
X Q goes to G2, Q′ goes to G1)
/ \
/ \
+------/ \------+
| |
| +-------+ |
+---->| | |
| NOR |-----+------> Q′
S -------------->| G2 |
+-------+
G1 inputs: R and Q′ G2 inputs: S and Q
Q = (R + Q′)′
Q′ = (S + Q)′
S stands for Set (make Q = 1) and R for Reset (make Q = 0).
Analysing Each Input Combination
S = 1, R = 0 (Set). S = 1 forces G2's output Q′ to 0. Now G1 has inputs R = 0 and Q′ = 0, so Q = 1. The latch is set.
S = 0, R = 1 (Reset). R = 1 forces Q to 0. G2 has inputs S = 0 and Q = 0, so Q′ = 1. The latch is reset.
S = 0, R = 0 (Hold). Each NOR gate now just inverts the other gate's output, exactly like the inverter ring. If Q was 1, Q′ = 0 keeps Q = 1. If Q was 0, Q′ = 1 keeps Q = 0. The latch remembers whatever was last set or reset.
S = 1, R = 1 (Forbidden). Both gates have a 1 input, so Q = 0 and Q′ = 0. The outputs are no longer complements of each other. Worse, see what happens when S and R return to 0.
S | R | Q+ | Q′+ | Action |
|---|---|---|---|---|
0 | 0 | Q | Q′ | Hold (no change) |
0 | 1 | 0 | 1 | Reset |
1 | 0 | 1 | 0 | Set |
1 | 1 | 0 | 0 | Forbidden (invalid) |
Why S = R = 1 Is Forbidden
While S = R = 1 the latch is in a defined (if useless) state: Q = Q′ = 0. The real trouble comes when both inputs return to 0 at the same time. Both gates now see 0, 0 and both try to switch to 1. Then both see a 1 and switch back to 0, and so on. With perfectly equal delays the latch oscillates; in real hardware tiny delay differences eventually make one gate win, but you cannot predict which one. Here is a gate-level simulation with a 1 ns delay per gate:
`timescale 1ns/1ns
// SR latch from two cross-coupled NOR gates, 1 ns per gate
module sr_nor(input logic S, R, output logic Q, Qn);
nor #1 g1(Q, R, Qn);
nor #1 g2(Qn, S, Q);
endmodule
module tb;
logic S, R, Q, Qn;
sr_nor u(S, R, Q, Qn);
initial begin
$monitor("t=%0d S=%b R=%b Q=%b Qn=%b", $time, S, R, Q, Qn);
S = 0; R = 1; // reset first so Q has a known value
#5 R = 0; // hold
#5 S = 1; // set
#5 S = 0; // hold (remembers 1)
#5 R = 1; // reset
#5 R = 0; // hold (remembers 0)
#5 S = 1; R = 1; // forbidden input
#5 S = 0; R = 0; // both released at the same instant
#8 $finish;
end
endmodule
Output (Icarus Verilog):
t=0 S=0 R=1 Q=x Qn=x
t=1 S=0 R=1 Q=0 Qn=x
t=2 S=0 R=1 Q=0 Qn=1
t=5 S=0 R=0 Q=0 Qn=1
t=10 S=1 R=0 Q=0 Qn=1
t=11 S=1 R=0 Q=0 Qn=0
t=12 S=1 R=0 Q=1 Qn=0
t=15 S=0 R=0 Q=1 Qn=0
t=20 S=0 R=1 Q=1 Qn=0
t=21 S=0 R=1 Q=0 Qn=0
t=22 S=0 R=1 Q=0 Qn=1
t=25 S=0 R=0 Q=0 Qn=1
t=30 S=1 R=1 Q=0 Qn=1
t=31 S=1 R=1 Q=0 Qn=0
t=35 S=0 R=0 Q=0 Qn=0
t=36 S=0 R=0 Q=1 Qn=1
t=37 S=0 R=0 Q=0 Qn=0
t=38 S=0 R=0 Q=1 Qn=1
t=39 S=0 R=0 Q=0 Qn=0
t=40 S=0 R=0 Q=1 Qn=1
t=41 S=0 R=0 Q=0 Qn=0
t=42 S=0 R=0 Q=1 Qn=1
t=43 S=0 R=0 Q=0 Qn=0
Read it row by row. From t = 5 to 30 the latch sets, holds 1, resets and holds 0, exactly as the table says. Notice the two-step response: when S rises at t = 10, Q′ falls first (t = 11) and only then Q rises (t = 12). The latch's delay is two gate delays. From t = 35, after S and R are released together from the forbidden state, the outputs oscillate between 00 and 11 every nanosecond until the simulation stops. That is the reason designers never apply S = R = 1.
Characteristic Table and Equation
To get an equation for the next state, write the table with the present state Q as an extra input and treat the forbidden rows as don't-cares (x):
S | R | Q | Q+ |
|---|---|---|---|
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 | x |
1 | 1 | 1 | x |
RQ
00 01 11 10
+----+----+----+----+
S = 0 | 0 | 1 | 0 | 0 |
+----+----+----+----+
S = 1 | 1 | 1 | x | x |
+----+----+----+----+
Group 1: S (the whole bottom row, using both x's)
Group 2: R′Q (cells S R Q = 001 and 101)
SR latch characteristic equation: Q+ = S + R′Q, with the condition SR = 0.
Read it in words: the next state is 1 if we set it, or if it was already 1 and we did not reset it.
The S′R′ Latch with NAND Gates
The same structure built with NAND gates behaves as the mirror image. A NAND output is forced to 1 by a 0 on any input, so the inputs are active-low: you pull an input to 0 to act. The inputs are therefore named S′ and R′ (or S̄ and R̄).
+--------+
S′ ----->| |
| NAND |--------+------> Q
+--->| | |
| +--------+ |
| +--------+ |
| | NAND |<-------+
+----| |
| |<------ R′
+--------+------------> Q′
Q = (S′ · Q′)′ Q′ = (R′ · Q)′
S′ | R′ | Q+ | Action |
|---|---|---|---|
0 | 0 | Q = Q′ = 1 | Forbidden |
0 | 1 | 1 | Set |
1 | 0 | 0 | Reset |
1 | 1 | Q | Hold |
Compare with the NOR table: the rows are in the opposite order, the hold condition is 1 1 instead of 0 0, and the forbidden state makes both outputs 1 instead of 0. In terms of its active-low inputs, the characteristic equation is Q+ = (S′)′ + R′·Q, with the condition that S′ and R′ are never 0 together.
An excerpt from a gate-level simulation of the NAND latch (1 ns per gate; comments on the right added by us):
t=5 Sn=1 Rn=1 Q=0 Qn=1 hold
t=10 Sn=0 Rn=1 Q=0 Qn=1 pull S′ low ...
t=11 Sn=0 Rn=1 Q=1 Qn=1 ... Q rises first
t=12 Sn=0 Rn=1 Q=1 Qn=0 ... then Q′ falls: set
t=15 Sn=1 Rn=1 Q=1 Qn=0 hold (remembers 1)
t=20 Sn=0 Rn=0 Q=1 Qn=0 forbidden input ...
t=21 Sn=0 Rn=0 Q=1 Qn=1 ... both outputs 1
Point | NOR SR latch | NAND S′R′ latch |
|---|---|---|
Input polarity | Active-high | Active-low |
Hold | S = R = 0 | S′ = R′ = 1 |
Forbidden | S = R = 1 (Q = Q′ = 0) | S′ = R′ = 0 (Q = Q′ = 1) |
Real IC | Usually built from NOR gates such as 74HC02 | 74LS279 / 74HC279 (quad S′R′ latch, now a legacy part), or built from 74HC00 NAND gates |
The Gated SR Latch
In a real system we want the latch to respond only at certain times, for example when a control signal says "now". We add an enable input E (also called C, G or clock) with two AND (or NAND) gates in front of the latch:
S ----+
[AND]---- S_int ---->+-----------+
E --+-+ | SR latch |----> Q
| | (NOR) |
+-+ | |----> Q′
[AND]---- R_int ---->+-----------+
R ----+
S_int = E·S, R_int = E·R
E | S | R | Q+ | Action |
|---|---|---|---|---|
0 | x | x | Q | Hold (inputs ignored) |
1 | 0 | 0 | Q | Hold |
1 | 0 | 1 | 0 | Reset |
1 | 1 | 0 | 1 | Set |
1 | 1 | 1 | ? | Forbidden |
A common textbook version uses four NAND gates: two NANDs produce (E·S)′ and (E·R)′, which feed a NAND S′R′ latch. Because the first NANDs invert, the gated version has active-high S and R again. The forbidden combination still exists, now as E = 1 with S = R = 1.
The D Latch (Transparent Latch)
The forbidden input is a nuisance. The fix is elegant: generate S and R from a single input D so that they can never be equal: S = D and R = D′. The result is the D latch (D for data).
D ---+-----------------[NAND]--- S′ -->+------------+
| | | S′R′ |----> Q
| E -----+ | latch |
| | | (2 NAND) |----> Q′
+--[NOT]-- D′ ----[NAND]--- R′ -->+------------+
S′ = (D·E)′, R′ = (D′·E)′ (4 NAND gates + 1 inverter)
E | D | Q+ | Action |
|---|---|---|---|
0 | x | Q | Hold (latched) |
1 | 0 | 0 | Q follows D |
1 | 1 | 1 | Q follows D |
D latch characteristic equation: Q+ = E·D + E′·Q. While E = 1 the output follows D (the latch is transparent); when E falls to 0 the latch holds the value D had just before.
We confirmed this equation by solving the 4-NAND circuit for all eight combinations of E, D and Q: in every case the circuit settles to Q+ = ED + E′Q with Q′+ being its complement, so there is no forbidden state.
Seeing Transparency in Simulation
In SystemVerilog, always_latch describes a level-sensitive latch directly:
`timescale 1ns/1ns
module d_latch(input logic EN, D, output logic Q);
always_latch
if (EN) Q <= D; // transparent while EN = 1, holds while EN = 0
endmodule
module tb;
logic EN, D, Q;
d_latch u(EN, D, Q);
initial begin
$monitor("t=%0d EN=%b D=%b Q=%b", $time, EN, D, Q);
EN = 1; D = 0;
#5 D = 1; // EN = 1: Q follows D
#5 D = 0;
#5 D = 1;
#5 EN = 0; // close the latch: Q keeps 1
#5 D = 0; // ignored
#5 D = 1; // ignored
#5 D = 0;
#5 EN = 1; // open again: Q picks up D = 0
#5 $finish;
end
endmodule
Output:
t=0 EN=1 D=0 Q=0
t=5 EN=1 D=1 Q=1
t=10 EN=1 D=0 Q=0
t=15 EN=1 D=1 Q=1
t=20 EN=0 D=1 Q=1
t=25 EN=0 D=0 Q=1
t=30 EN=0 D=1 Q=1
t=35 EN=0 D=0 Q=1
t=40 EN=1 D=0 Q=0
While EN = 1 (t = 0 to 20), Q copies every change of D, like a piece of wire. At t = 20 the latch closes and "freezes" the value 1. The changes of D at 25, 30 and 35 are ignored. When EN goes back to 1 at t = 40, Q immediately takes the current D = 0.
Timing of a D Latch
Because the latch captures D at the moment E goes inactive, D must be stable for a short setup time before that closing edge of E and a short hold time after it. If D changes inside that window, the latch can end up in a metastable state, an in-between voltage that takes an unpredictable time to resolve to 0 or 1. We study setup and hold in detail with flip-flops in the next chapter.
Where Latches Are Used
1. Switch Debouncing
When a mechanical switch closes, its metal contacts bounce for a few milliseconds, producing a burst of 0-1-0-1 pulses instead of one clean edge. A counter connected directly to the switch might count 5 presses instead of 1.
+5V +5V
| |
[R] [R] (pull-up resistors)
| |
S′ ------+ +------ R′
| | S′ and R′ go to a NAND S′R′ latch
o A B o
\
\ SPDT switch (common terminal to ground)
o
|
GND
Switch moves from B to A:
R′ goes to 1 (released) → latch holds
S′ touches ground → latch SETS, Q = 1
contact bounces: S′ = 1, 0, 1, 0 ...
S′ = 1 means "hold" and S′ = 0 means "set again" → Q stays 1
→ Q makes exactly one clean 0 → 1 transition
The bounces only alternate between "set" and "hold", and both leave Q = 1. This works because a bouncing contact never touches the opposite terminal.
2. Address Latching in Microprocessors
The Intel 8085, studied in many Indian university syllabi, multiplexes the lower address byte and the data byte on the same pins (AD0 to AD7). During the first part of a machine cycle the pins carry the address and the processor raises the ALE (Address Latch Enable) signal. An octal D latch such as the 74LS373 (or 74HC573) is transparent while ALE = 1 and latches the address when ALE falls, so the address stays available while the same pins switch to carrying data. This is a textbook use of transparency: we want the latch to pass the address through as soon as it appears and hold it afterwards.
3. Inside Flip-Flops and Memories
An edge-triggered D flip-flop is usually two D latches in a row (master and slave), and an SRAM cell is a cross-coupled pair of inverters, the bistable element from the start of this chapter. Some high-performance processors also use latch-based pipelines because latches are smaller and allow "time borrowing" between stages.
The Problem with Transparency
Suppose the output of a D latch goes through some logic and back to its own D input, for example to make a counter: D = Q + 1. While the enable is 1, the new Q immediately produces a new D, which immediately produces a new Q, and so on. The value races around the loop as many times as the enable pulse allows, and the final result depends on the pulse width. We need a device that takes one sample per clock cycle, at a single instant. That device is the edge-triggered flip-flop, the subject of Flip-Flops: SR, JK, D & T.
A related practical warning for anyone writing Verilog or VHDL: if you describe combinational logic but forget to assign the output in some branch (for example an if without an else in a combinational always block), the synthesis tool must remember the old value, so it infers a latch you never wanted. In SystemVerilog, writing combinational logic in always_comb lets the tools warn you when this happens, and always_latch documents that a latch is intended.
Common Mistakes
Mixing up NOR and NAND latch conventions. NOR: active-high, hold at 00, forbidden 11. NAND: active-low, hold at 11, forbidden 00.
Saying the forbidden state "gives Q = 1" or "gives Q = 0". While both inputs are active the outputs are equal (both 0 for NOR, both 1 for NAND); the real problem is the unpredictable state after both inputs are released together.
Calling a latch edge-triggered. A latch responds for the whole time its enable is active.
Forgetting Q in the characteristic equation. Q+ = S + R′Q, not S + R′. Without the Q term the latch would not remember anything.
Assuming a D latch removes all timing issues. It removes the forbidden input, but D must still meet setup and hold around the closing edge of the enable.
Interview and GATE-Style Questions
In a NOR-based SR latch, Q = 1. What are Q and Q′ after S = 0, R = 1 is applied, and then after both return to 0?
R = 1 resets the latch: Q = 0, Q′ = 1. When both inputs return to 0 the latch holds, so Q stays 0 and Q′ stays 1.
Why is S = R = 1 not allowed in a NOR SR latch?
It forces Q = Q′ = 0, which breaks the rule that the outputs are complements. When both inputs then return to 0 at the same time, both gates try to go to 1 together; the latch may oscillate or settle to a state that depends on tiny delay differences, so the next state is unpredictable.
Derive the characteristic equation of the SR latch.
Make a K-map of Q+ with inputs S, R, Q, putting don't-cares on the S = R = 1 rows. The 1s and don't-cares group into S (four cells) and R′Q (two cells), giving Q+ = S + R′Q with the condition SR = 0.
How many NAND gates are needed for a gated D latch?
Four NAND gates plus an inverter for D′ (two NANDs for the input gating, two for the cross-coupled latch). The inverter can also be made from a fifth NAND with its inputs tied, so five NAND gates in total. A common trick removes the inverter by feeding the output of the S-side NAND, (D·E)′, into the R-side NAND instead of D′, giving a 4-NAND D latch.
What is the difference between a latch and a flip-flop?
A latch is level-sensitive: it can change for the whole time its enable is active and is transparent during that time. A flip-flop is edge-triggered: it samples its input and changes state only at a clock edge. Latches are smaller; flip-flops make timing much easier to reason about.
A D latch has E = 1 from t = 0 to 10 ns and E = 0 after that. D is 1 from 0 to 4 ns, 0 from 4 to 8 ns and 1 after 8 ns. What is Q at t = 15 ns? (Ignore gate delays.)
Q follows D until E falls at 10 ns. At that moment D = 1, so the latch stores 1. Q = 1 at 15 ns.
Summary
Feedback creates memory: two cross-coupled inverting gates have two stable states and store one bit.
The NOR SR latch has active-high inputs: 00 hold, 01 reset, 10 set, 11 forbidden. Q+ = S + R′Q with SR = 0.
The NAND S′R′ latch has active-low inputs: 11 hold, 10 reset, 01 set, 00 forbidden.
An enable input gives the gated SR latch; driving S = D and R = D′ gives the D latch, Q+ = ED + E′Q, with no forbidden state.
Latches are transparent while enabled. That is useful for address latches and debouncing, but it causes races in feedback circuits.
Next, in Flip-Flops: SR, JK, D & T, we combine two latches into an edge-triggered flip-flop and meet the four standard flip-flop types.