SEN2201 · Week 4 · Digital Logic Structures (concept of memory, sequential logic circuits)

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.

sequential logicstateR-S latchgated D latchwrite enable (WE)registermemoryaddress spaceaddressabilityword lineclockflip-flopmaster-slave flip-flopfinite state machine (FSM)state diagram

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.

SRWhat happens?Q
11hold (quiescent)unchanged
01set1
10reset0
00forbiddenQ = 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: S=(D⋅WE)′S = (D \cdot WE)', R=(D′⋅WE)′R = (D' \cdot WE)'.

  • 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’.

WEDQ
0xholds
100
111

In short: when WE = 1, Q = D; when WE = 0, Q is frozen.

Register

A register is nn 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. kk address bits → 2k2^k locations.
  • Addressability: how many bits each location has.

total size=2k×m bits\text{total size} = 2^k \times m \text{ bits}

MemoryAddress bitsTotal
22×32^2 \times 3212 bits
210×82^{10} \times 8108192 bits = 1 KB
214×322^{14} \times 321464 KB
LC-3: 216×162^{16} \times 1616128 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?

  1. A kk-bit address enters a kk-to-2k2^k decoder.
  2. The decoder sets exactly one word line to 1: the location in that row is selected.
  3. The mm 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. 2102^{10} = 1K, 2162^{16} = 64K, 2202^{20} = 1M, 2302^{30} = 1G, 2322^{32} = 4G. “How many address bits does 64 KB byte-addressable memory need?” → 64K = 2162^{16} → 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:

  1. A finite number of states,
  2. A finite number of external inputs,
  3. A finite number of external outputs,
  4. An explicit definition of the next state for each state and input (transitions),
  5. 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 ss states you need ⌈log⁡2s⌉\lceil \log_2 s \rceil 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.

Stateinput 0input 1Z
AAB0
BAC0
CAC1

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 ideaThe truth
In a NAND latch S = 1 setsS = 0 sets; 11 holds
S = R = 0 → Q = 0Forbidden: Q = Q’ = 1, then undefined
A D latch has Q = D when WE = 0WE = 0 → holds
Address space = total bitsAddress space = number of locations
2122^{12} locations × 16 bits = 4 KB65536 bits = 8192 bytes = 8 KB
5 states → 2 flip-flops22=4<52^2 = 4 < 5, 3 are needed
A[11:8] is counted from the leftBits are counted from the right, starting at 0
Flip-flop = latchA flip-flop changes once at the edge

Test yourself

Exam-style questions. Solve them on paper first, then reveal the answer.

Question 1easy

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
0, 1, 1, 0, 0

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.

Question 2medium

Why is S = R = 0 not allowed in a NAND R-S latch?

Show answer
Both Q and Q' become 1; the final state is unpredictable

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.

Question 3easy

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
0, 0, 0, 1, 1

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.

Question 4easy

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 bits, 1024 bytes

1024=2101024 = 2^{10} → 10 address bits. Total = locations × addressability = 1024×8=81921024 \times 8 = 8192 bits = 1024 bytes (1 KB).

Question 5medium

The LC-3 has a 16-bit address space and 16-bit addressability. What is its total memory size in bytes?

Show answer
131072 bytes (128 KB)

216=655362^{16} = 65536 locations, each 16 bits = 2 bytes. 65536×2=13107265536 \times 2 = 131072 bytes = 128 KB.

Question 6medium

How many address bits does a 4 GB byte-addressable memory need?

Show answer
32

4 GB = 4×230=2324 \times 2^{30} = 2^{32} bytes. Byte-addressable: each location is 1 byte → 2322^{32} locations → 32 address bits.

Question 7easy

Let A = x3A7C be a 16-bit value. What is A[11:8]?

Show answer
1010

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.

Question 8easy

How many D flip-flops are needed for a 16-bit register?

Show answer
16

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.

Question 9medium

What is the minimum number of flip-flops needed to store the state of an FSM with 6 states? With 9 states?

Show answer
3 and 4

kk flip-flops can distinguish 2k2^k states. 6 states: 22=4<6≤8=232^2 = 4 < 6 \le 8 = 2^3 → 3. 9 states: 23=8<9≤162^3 = 8 < 9 \le 16 → 4. The formula: ⌈log⁡2s⌉\lceil \log_2 s \rceil.

Question 10medium

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
Z = 0, 0, 1, 0, 1, 0, 0; detects the pattern 10

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".

Question 11hard

Why are master-slave (edge-triggered) flip-flops used instead of plain gated D latches in sequential circuits?

Show answer
So the state changes only once per clock cycle, at the clock edge

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.

Question 12hard

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
8 word lines, 4 bits

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).

Review card

Sequential = holds state, the output depends on the past too R-S latch (NAND, active-low): 11 hold | 01 set Q=1 | 10 reset Q=0 | 00 FORBIDDEN (Q=Q'=1) Gated D latch: WE=1 -> Q=D | WE=0 -> hold (S=(D·WE)', R=(D'·WE)') Register: n bits = n latches/flip-flops, common WE Memory: address space = 2^k locations (k address bits) | addressability = bits per location total = 2^k × m bits | LC-3: 2^16 × 16 bits = 128 KB read: address -> decoder -> word line -> mux/output A[11:8] = bits 11..8 (count from the right, starting at 0) Clock: a periodic 0/1 signal, synchronizes the transitions Master-slave FF = 2 gated D latches, changes once at the clock edge FSM 5 parts: states, inputs, outputs, transitions (next state), output function s states -> ceil(log2 s) flip-flops (5 -> 3, 9 -> 4)