Memory and sequential logic: latch, flip-flop, register, FSM
Tracing the R-S latch and the gated D latch, registers, memory address calculation (address space, addressability), the clock, flip-flops and finite state machines. Short-answer and tracing questions.
Last week’s circuits remembered nothing: when you changed the input, the output changed immediately. A computer, on the other hand, needs to store numbers, the program and the information “where am I right now”. This page describes the storage elements: latch, register, memory. Then it moves on to the idea of the finite state machine that uses them. The LC-3’s control unit is also really a large FSM.
The question types from this topic on the midterm: latch tracing (a sequence of S, R is given, Q is asked), memory calculation (how many address bits, how many bytes in total), the number of flip-flops, FSM tracing.
What is a sequential circuit?
The output of a combinational circuit depends only on the current input. The output of a sequential circuit depends both on the input and on the information the circuit has stored up to that moment, that is, its state.
Example: a combination lock. If the correct code is 4-1-8, the “8” key alone does not open the lock. Whether it opens depends on which keys were pressed before. The lock stores a state.
The R-S latch
The simplest storage element is two cross-coupled NAND gates: the output of each NAND goes to the input of the other. The external inputs are S (set) and R (reset), the outputs are Q and Q’.
If one input of a NAND is 0, its output is definitely 1. That is why in this latch the inputs are active-low: to do something you make the input 0.
| S | R | What happens? | Q |
|---|---|---|---|
| 1 | 1 | hold (quiescent) | unchanged |
| 0 | 1 | set | 1 |
| 1 | 0 | reset | 0 |
| 0 | 0 | forbidden | Q = Q’ = 1, then undefined |
Why does it hold? With S = R = 1 both NANDs see only the other one’s output. If Q = 1, the lower NAND sees (1, 1) and gives Q’ = 0; the upper one sees (1, 0) and gives Q = 1. The system feeds itself, and the same holds for Q = 0. There are two stable states, and the latch stays in whichever one it is in.
Worked trace: initial Q = 1, inputs 10 → 11 → 10 → 01 → 11.
step S R what happens? Q
1 1 0 reset 0
2 1 1 hold 0
3 1 0 reset 0 (already 0)
4 0 1 set 1
5 1 1 hold 1
PITFALL · the active-low rule. In a latch built with NOR, S = 1 sets; in a NAND latch, S = 0 sets. This course (Patt & Patel) uses the NAND latch: 11 holds, the letter that is 0 does its job.
The gated D latch
The R-S latch has two problems: the forbidden input state and not being able to control “when to write”. The gated D latch solves both. The inputs: data D and WE (write enable).
Two NANDs are added in front of the R-S latch: , .
- WE = 0 → S = R = 1 → the latch holds.
- WE = 1, D = 1 → S = 0 → set → Q = 1.
- WE = 1, D = 0 → R = 0 → reset → Q = 0.
S and R can never be 0 at the same time, because one uses D and the other uses D’.
| WE | D | Q |
|---|---|---|
| 0 | x | holds |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
In short: when WE = 1, Q = D; when WE = 0, Q is frozen.
Register
A register is D latches (or flip-flops) sharing the same WE line. All of them are written at the same time. A 16-bit register = 16 storage elements.
Bit range notation: for a 16-bit A, A[15:12] is the leftmost 4 bits, A[3:0] is the rightmost 4 bits. Bits are numbered from the right, starting at 0.
A = x3A7C = 0011 1010 0111 1100
A[15:12] = 0011 A[11:8] = 1010 A[3:0] = 1100 A[6:4] = 111
You will use this notation constantly when decoding LC-3 instructions: opcode = IR[15:12], destination register = IR[11:9].
Memory
Memory is an orderly array of a large number of storage locations. It is described by two numbers:
- Address space: how many separate locations there are. address bits → locations.
- Addressability: how many bits each location has.
| Memory | Address bits | Total |
|---|---|---|
| 2 | 12 bits | |
| 10 | 8192 bits = 1 KB | |
| 14 | 64 KB | |
| LC-3: | 16 | 128 KB |
In byte-addressable memory each location is 1 byte (most modern computers). The LC-3 is word-addressable: there is a 16-bit word at each address.
How is a read done?
- A -bit address enters a -to- decoder.
- The decoder sets exactly one word line to 1: the location in that row is selected.
- The bits of the selected row pass to the output.
For a write, WE = 1 is added, and only the WE of the latches in the selected row is enabled.
TIP · Quick power table. = 1K, = 64K, = 1M, = 1G, = 4G. “How many address bits does 64 KB byte-addressable memory need?” → 64K = → 16 bits.
The clock and the flip-flop
In a computer, thousands of storage elements must be updated at the same time. The clock provides this: a signal that goes back and forth regularly between 0 and 1. One clock cycle is one full period. In a 3 GHz processor, one cycle is 1/3 of a nanosecond.
The problem with the gated D latch: it is transparent as long as WE = 1. The output changes, the changed output is fed back, and in the same cycle the latch can change again. To prevent this, the master-slave flip-flop is used: two gated D latches back to back. The master takes the new value while the clock is 1, and the slave passes it to the output while the clock is 0. The result: the output changes once per cycle, at the clock edge. Everything that stores state (registers, the FSM state) is built with flip-flops.
Finite state machine (FSM)
An FSM is defined by five things:
- A finite number of states,
- A finite number of external inputs,
- A finite number of external outputs,
- An explicit definition of the next state for each state and input (transitions),
- An explicit definition of what each output depends on.
Its drawing is the state diagram: circles are states, arrows are transitions, and the input value is written on the arrow.
States are stored in flip-flops. To distinguish states you need flip-flops: 4 states → 2, 5 states → 3, 9 states → 4.
Worked FSM trace
A machine that gives Z = 1 when it sees two 1s in a row. States: A (the last input was not 1), B (one 1 has been seen), C (at least two 1s in a row). Z = 1 only in C.
| State | input 0 | input 1 | Z |
|---|---|---|---|
| A | A | B | 0 |
| B | A | C | 0 |
| C | A | C | 1 |
Start at A, inputs 0, 1, 1, 1, 0, 1:
input: 0 1 1 1 0 1
new state: A B C C A B
Z: 0 0 1 1 0 0
At each step, look at a single row: current state + input → new state. Since there are 3 states, 2 flip-flops are enough.
A machine like this, whose output depends only on the state, is called a Moore machine. The LC-3’s control unit is also an FSM: each state is one step of the instruction cycle (week 5).
Common mistakes
| Wrong idea | The truth |
|---|---|
| In a NAND latch S = 1 sets | S = 0 sets; 11 holds |
| S = R = 0 → Q = 0 | Forbidden: Q = Q’ = 1, then undefined |
| A D latch has Q = D when WE = 0 | WE = 0 → holds |
| Address space = total bits | Address space = number of locations |
| locations × 16 bits = 4 KB | 65536 bits = 8192 bytes = 8 KB |
| 5 states → 2 flip-flops | , 3 are needed |
| A[11:8] is counted from the left | Bits are counted from the right, starting at 0 |
| Flip-flop = latch | A flip-flop changes once at the edge |
Test yourself
Exam-style questions. Solve them on paper first, then reveal the answer.
A NAND-based R-S latch starts with Q = 0. The inputs (S, R) are 11 → 01 → 11 → 10 → 11. Give Q after each step.
Show answer
In a NAND latch the inputs are active-low: 11 holds, S = 0 sets (Q = 1), R = 0 resets (Q = 0). The steps: 11 hold → 0. 01 set → 1. 11 hold → 1. 10 reset → 0. 11 hold → 0.
Why is S = R = 0 not allowed in a NAND R-S latch?
Show answer
If one input of a NAND is 0, its output is 1. With S = R = 0 the outputs of both NANDs are 1, so Q and Q' are no longer each other's complement. When the inputs return to 1 at the same time, which value the latch ends up in depends on the electrical speed of the gates, that is, it is unpredictable.
A gated D latch holds Q = 1. The inputs (WE, D) are applied in order: (1,0), (0,1), (0,0), (1,1), (0,0). Give Q after each step.
Show answer
When WE = 1, Q = D (write). When WE = 0, Q does not change (hold), whatever D is. (1,0) → 0. (0,1) → holds 0. (0,0) → holds 0. (1,1) → 1. (0,0) → holds 1.
A memory has 1024 locations and an addressability of 8 bits. How many address bits are needed? What is the total size in bytes?
Show answer
→ 10 address bits. Total = locations × addressability = bits = 1024 bytes (1 KB).
The LC-3 has a 16-bit address space and 16-bit addressability. What is its total memory size in bytes?
Show answer
locations, each 16 bits = 2 bytes. bytes = 128 KB.
How many address bits does a 4 GB byte-addressable memory need?
Show answer
4 GB = bytes. Byte-addressable: each location is 1 byte → locations → 32 address bits.
Let A = x3A7C be a 16-bit value. What is A[11:8]?
Show answer
x3A7C = 0011 1010 0111 1100. A[11:8] are the 4 bits from bit 11 down to bit 8, that is the second hex digit from the left: A = 1010. Bits are counted from the right, starting at 0.
How many D flip-flops are needed for a 16-bit register?
Show answer
A register is n single-bit storage elements sharing a common WE (or clock) line. 16 bits → 16 flip-flops (or latches). For the LC-3's 8 general-purpose registers, 8 × 16 = 128 bits are needed.
What is the minimum number of flip-flops needed to store the state of an FSM with 6 states? With 9 states?
Show answer
flip-flops can distinguish states. 6 states: → 3. 9 states: → 4. The formula: .
An FSM has states S0, S1, S2; output Z = 1 only in S2. Transitions: S0 -0→ S0, S0 -1→ S1, S1 -0→ S2, S1 -1→ S1, S2 -0→ S0, S2 -1→ S1. Starting in S0, the input is 1, 1, 0, 1, 0, 0, 1. Give Z after each input. What does the machine detect?
Show answer
States: 1 → S1, 1 → S1, 0 → S2 (Z=1), 1 → S1, 0 → S2 (Z=1), 0 → S0, 1 → S1.
Z is 1 when the last two inputs were "1 then 0": a detector for the sequence "10".
Why are master-slave (edge-triggered) flip-flops used instead of plain gated D latches in sequential circuits?
Show answer
A gated D latch is transparent as long as WE = 1: the output changes, goes back into the circuit, and the new input enters the latch again. The state can change more than once in a clock cycle. A master-slave flip-flop consists of two D latches; one is open while the clock is 1, the other while the clock is 0. This way the output changes only once, at the clock edge.
A memory is 2^3 × 4 (address space × addressability). How many word lines does the address decoder drive, and how many bits are read in one access?
Show answer
The 3 address bits enter a 3-to-8 decoder: 8 word lines, each one a location (a row). The 4 bits (addressability) in the selected row are read together. Total 32 bits.
Verification: The R-S and gated D latch traces were computed by a gate-level simulation of cross-coupled NAND gates, the memory sizes and flip-flop counts with node, and the FSM traces by simulating the transition table (verify/SEN2201/04.mjs).