This is the third course in University Electrical Engineering: digital logic
design 数字逻辑设计. It shows how bits are represented, combined, stored and timed
inside every computer. It needs no electronics: everything is built from two voltage
levels (0 and 1) and a few kinds of gate.
Unit 3.1 covers number systems: binary, hexadecimal and two's complement.
Units 3.2–3.3 combine bits with logic: Boolean algebra, Karnaugh maps and standard
combinational blocks.
Units 3.4–3.5 add time: latches, flip-flops and clocked state machines.
Unit 3.6 builds storage from flip-flops: registers, counters and memory.
Unit 3.7 describes circuits in a hardware description language and checks them with
testbenches.
Each unit opens with a real situation and shows a picture before the formal idea. It
defines its terms, works two examples that get harder, lists the common mistakes and
ends with a short summary. This course has one main checking habit: check every Boolean
answer by exhaustive evaluation, which means trying every input combination. A
function of 3 inputs has only 8 rows, so check all of them, on a truth table by hand or
with a short program loop.
A thermostat shows 22.5 °C, but the microcontroller behind it holds only bits. How does
22.5 become a bit pattern that a machine can add, compare and store, and how does the
pattern become a number again? This unit gives that translation in both directions.
8-bit two's complement: the sign bit splits the range into negative ($-128$ to $-1$) and positive ($0$ to $127$)
Computers store numbers in binary 二进制 (base 2). Each bit position has a weight:
1, 2, 4, 8 and so on, doubling to the left. To convert a whole number, divide by 2
again and again and collect the remainders. To convert a fraction, multiply by 2 again
and again and collect the whole-number parts. Hexadecimal 十六进制 (base 16) is a
short way to write binary: each hex digit stands for exactly 4 bits, for example
$\text{0xB} = 1011$. The prefix "0x" marks a hex number.
Weights on the bits: read the pattern by adding the weights under each 1
Deriving two's complement. We want a way to store negative numbers so that the
ordinary adder can also subtract: $a - b = a + (\text{the negative of } b)$, working
modulo $2^N$ in an $N$-bit word. Modulo $2^N$, the negative of $b$ is
$2^N - b = (2^N - 1 - b) + 1$. The number $2^N - 1$ is all ones, so $2^N - 1 - b$ is
simply $b$ with every bit inverted. So to negate a number, invert every bit and add 1.
The same argument shows that the adder works unchanged.
Two's complement 二进制补码 stores signed whole numbers in $N$ bits this way. In
8 bits, $5 = 00000101$; inverting gives $11111010$, and adding 1 gives
$-5 = 11111011$. The range 范围 is $-2^{N-1}$ to $+2^{N-1}-1$, which is −128 to
+127 in 8 bits. Overflow 溢出 happens when the true result is outside the range.
Detect it by the signs: two positive numbers that give a negative result, or two
negative numbers that give a positive one. A carry out of the most significant bit (MSB)
is not overflow by itself. In circuit terms, overflow is when the carry into the MSB
differs from the carry out of it.
Worked example 1 (convert and check).
Convert 156 to binary by taking the largest weights that fit:
$156 = 128 + 16 + 8 + 4$, so $156 = 10011100_2$.
Check by adding the weights again: $128 + 16 + 8 + 4 = 156$.
Hex: group the bits in fours from the right: $1001\ 1100 = \text{0x9C}$.
Encode −100 in 8 bits: $100 = 01100100$; invert to get $10011011$; add 1 to get
$10011100$.
Check by decoding: read as unsigned, the pattern is 156, and $156 - 256 = -100$.
Overflow: $100 + 50 = 150$, which is more than 127, so 8-bit signed numbers cannot
hold it. The adder produces $10010110$, which decodes to $-106$.
The sign rule catches it: positive plus positive gave a negative. In the circuit, the
carry into the MSB was 1 and the carry out was 0. The machine did not fail; the word
was too narrow.
Common mistakes.
Reading a two's-complement pattern as unsigned. $10011100$ is 156 or −100; the
interpretation decides, not the bits.
Forgetting that the range is not symmetric: −128 fits in 8 bits, +128 does not.
Calling every carry-out an overflow. Only the sign rule, or the carry mismatch at the
MSB, decides.
Summary: convert between binary and decimal with the weights (and doubling for
fractions); hex groups bits in fours. Two's complement negates by inverting and adding
1, with range $-2^{N-1}$ to $2^{N-1}-1$. Overflow is detected by the signs, not by a
carry-out.
探索
Read a pattern both ways
10011100 is 156 unsigned and 156 − 256 = −100 in 8-bit two's complement. Change the value and watch the top bit's weight of −128 decide the sign.
A hallway light can be switched from either end: two switches, one bulb. The wiring the
electrician uses is the XOR function, which is 1 when the two inputs differ.
Boolean algebra 布尔代数 works with expressions in AND ($\cdot$), OR ($+$) and NOT
($'$). Its laws are like those of ordinary algebra, plus some new ones such as
absorption 吸收律: $A + A\cdot B = A$ and $A\cdot (A+B) = A$. De Morgan's
theorem 德摩根定律 is used constantly:
Loops of 1, 2, 4 or 8 ones; the term for a loop keeps only the variables that do not change inside it. Edges join, so a loop may wrap round, and the four corners count as adjacent
$$(A \cdot B)' = A' + B' \qquad (A + B)' = A' \cdot B'$$
In words: break the bar and change the sign.
A Karnaugh map 卡诺图 (K-map) lays out a truth table so that neighbouring cells
differ in exactly one variable. To do this, the rows and columns run in Gray-code order
(00, 01, 11, 10). Circle the 1s in rectangular groups of 1, 2, 4 or 8 cells. Each group
gives one product term, which keeps only the variables that stay the same across the
group.
F = Σm(1, 3, 5, 7): the group of four keeps only C, so F = C
Worked example 1 (the odd rows). Take the three-variable function
$F = \sum m(1, 3, 5, 7)$. The notation $\sum m(\ldots)$ lists the minterms, the
truth-table rows where $F = 1$.
These are the odd rows, which are exactly the rows where $C = 1$.
On the map they form one group of four. $A$ and $B$ both change inside the group, so
they drop out, and $F = C$.
Check: compare $C$ with $F$ on all 8 rows. They match.
Don't-cares 无关项 are input combinations that can never happen. Mark them X on the
map. An X may join a group to make it bigger, but never circle Xs on their own.
Worked example 2 (the four-input majority function).$M(A,B,C,D)$ is 1 when three
or more inputs are 1.
Its minterms are the codes with three or four 1s: $\sum m(7, 11, 13, 14, 15)$.
On the four-variable map, four overlapping pairs, each containing $m_{15}$, give
$M = ABC + ABD + ACD + BCD$.
This factors to $M = A\cdot B\cdot(C + D) + C\cdot D\cdot(A + B)$.
Check: on all 16 rows, compare the expression with the definition ("at least three
1s"). They agree everywhere.
Meaning: the map gives four 3-input products instead of five 4-input minterms, and
factoring saves more gates.
Common mistakes.
Grouping cells diagonally. Groups must be rectangles; diagonal neighbours differ in
two variables. (The map's edges wrap round, so cells on opposite edges are
neighbours.)
Keeping a variable that changes inside a group. Only the constant variables stay.
Trusting a simplification without checking it. A 16-row check takes a few minutes.
Summary: Boolean laws and De Morgan's theorem rewrite expressions. K-maps group
power-of-two rectangles in Gray-code order, and each group keeps only its constant
variables. Don't-cares can enlarge a group but never form one. Check every
simplification on the full table.
A vending machine picks one of four drinks from two button presses, shows a number on
its display and works out the change. Inside are a selector, a decoder and an adder.
These standard blocks sit between single gates and whole systems. They are
combinational logic 组合逻辑: their outputs depend only on the present inputs, with
no memory.
A logic IC: several gates in one package, wired to its pins exactly as the gate diagram showsXOR-and-AND make a half adder; two make a full adder; a mux picks a lane
Multiplexer 多路选择器 (MUX): a $2^n$-to-1 data selector. Its $n$ select bits,
read as a binary number, choose which data input $D_i$ reaches the output.
Decoder 译码器: $n$ inputs and $2^n$ outputs, with exactly one output high: the
one numbered by the input code. Each output is one minterm of the inputs.
Adder 加法器: a half adder 半加器 adds two bits, giving the sum
$S = A \oplus B$ ($\oplus$ is XOR) and the carry $C = A\cdot B$. A full adder全加器 adds three bits (two operand bits and a carry-in $C_{in}$):
$S = A \oplus B \oplus C_{in}$ and $C_{out} = A\cdot B + C_{in}\cdot (A \oplus B)$.
For every input, $2C_{out} + S = A + B + C_{in}$.
The full adder: five gates, one of the most-reused circuits in computing
Worked example 1 (the MUX and the full adder).
A 4-to-1 MUX with select $S_1S_0 = 10$: the select code is $10_2 = 2$, so the output
is $D_2$.
A full adder with $A = 1$, $B = 1$, $C_{in} = 1$:
$S = 1\oplus 1\oplus 1 = 1$ and $C_{out} = 1\cdot 1 + 1 \cdot (1 \oplus 1) = 1 + 0 = 1$.
Check: $2C_{out} + S = 2 + 1 = 3$, and three 1s went in.
Worked example 2 (a 2-bit ripple adder). Chain two full adders. The first adds the
low bits, and its carry-out feeds the carry-in of the second.
Add $11_2 + 01_2$, which is $3 + 1$.
Low bits: $1 + 1$ gives $S_0 = 0$ and carry 1.
High bits: $1 + 0 + 1$ (the carry) gives $S_1 = 0$ and carry 1.
Output, with the final carry: $100_2 = 4$. Check: $3 + 1 = 4$.
Meaning: an $n$-bit adder is just $n$ full adders in a chain. The carry must
ripple through every stage, and this limits its speed. Carry-lookahead adders,
met later, fix this.
Hazards 冒险 are short glitches at an output. When an input changes, the signal can
reach the output along paths with different delays, so the output may show the wrong
value for a moment. The final value is still correct, but a glitch matters if something
samples it, for example a clock input. An extra, redundant K-map group that covers the
change removes the glitch. If the specification forbids glitches, look for hazards by
checking every single-input change.
Common mistakes.
Reading a MUX backwards. The select bits choose which data input passes; they are
not the output value.
Running the carry the wrong way. Carries flow from the least significant stage
upwards.
Calling a hazard a logic error. The steady output is right; only the short transient
is wrong.
Summary: a MUX selects the input named by its select code; a decoder produces the
minterms; a full adder is five gates adding three bits. A ripple adder repeats the full
adder, and its carry chain sets its speed. Hazards are short glitches, and a redundant
group removes them.
探索
Follow the carry from the low bit upward
$2C_{out}+S=A+B+C_{in}$
The worked 11₂ + 01₂ addition produces low sum bits 00 and a final carry 1, giving the full 100₂ result. Inspect either stage in the full-adder truth table, compare three ones entering one stage, and extend the chain to four bits.
探索
Same code, a data value or one high line
$i=2S_1+S_0,\quad Y_{MUX}=D_i$
With S₁S₀ = 10, the MUX passes D₂ while the decoder raises Y₂. Set D₂ to 0 or 1 without changing the code, then change an unselected data bit and compare the outputs. The table shows every select code for the current data.
Press a doorbell with a memory light, and the light stays on after your finger leaves.
The simplest memory is feedback. Sequential logic 时序逻辑 has state: its output
depends on what happened before, not only on the present inputs.
A latch 锁存器 is level-sensitive. The SR latch (two NOR gates, each feeding the
other) remembers which input was last pulsed: S (set) makes $Q = 1$, R (reset) makes
$Q = 0$, and with both low it holds its value. The D latch follows its input D while
ENABLE is high and freezes when ENABLE goes low. Flip-flops 触发器 are
edge-triggered. A D flip-flop samples D only at the clock edge 时钟边沿 (usually
the rising edge) and holds that value until the next edge: Q after the edge equals D
just before it.
The SR latch: two NOR gates, each output fed back to the other's input — the feedback is the memory
$$Q_{next} = D \quad \text{(D flip-flop, at each rising edge)}$$
The J-K flip-flop turns the SR latch's forbidden input into a useful one: $J$ sets, $K$
resets, $J = K = 0$ holds, and $J = K = 1$toggles 翻转, so the next $Q$ is $Q'$.
The excitation table 激励表 works backwards: for each transition you want, from $Q$
to the next state $Q^+$, it lists the inputs needed. For a D flip-flop it is simply
$D = Q^+$. For a J-K flip-flop ($X$ means either value):
$Q \to Q^+$
$J$
$K$
$0 \to 0$
0
X
$0 \to 1$
1
X
$1 \to 0$
X
1
$1 \to 1$
X
0
Q copies D only at the rising clock edges
Worked example 1 (read the timing diagram). A D flip-flop sees $D = 1$ at the first
rising edge, $D = 0$ at the second and $D = 1$ at the third.
Q becomes 1, then 0, then 1, changing only at the edges.
Between edges D may change freely; nothing is sampled then.
A timing diagram makes this visible: every change of Q lines up with a clock edge.
Worked example 2 (a J-K sequence, then the table backwards). Start from $Q = 0$ and
apply $(J, K) = (1,0), (1,1), (0,1), (0,0)$ at four edges.
Now use the excitation table to get the same sequence. The transitions are
$0 \to 1$, $1 \to 0$, $0 \to 0$, $0 \to 0$. The table gives
$(J, K) = (1, X), (X, 1), (0, X), (0, X)$.
The inputs in step 1 fit these entries. The X entries are free, which is what keeps
J-K next-state logic small.
Common mistakes.
Using the wrong clock edge. Rising or falling is part of the specification; read it.
Mixing up a latch and a flip-flop. A latch is transparent for a whole level; a
flip-flop samples at one instant.
Expecting Q to change the moment D does. Q waits for the edge.
Summary: latches are level-sensitive and flip-flops edge-triggered; after the edge, Q
equals D from just before it. J-K replaces the forbidden input with a toggle. The
excitation table turns a wanted transition into the inputs needed.
探索
Same D and clock, different memory behaviour
$Q_{FF}^{+}=D\;\text{at }\uparrow CLK$
The worked sequence presents D = 1, 0 and 1 at three rising edges, with changes in between. Compare the transparent latch with the edge-triggered flip-flop, or change D and clock separately to see exactly when each output updates.
A door keypad opens for 1-0-1-1 but not for 1-1-0-1. The keys are the same; only the
order differs. Reacting to a sequence needs a circuit that remembers how far through the
sequence it is. A finite state machine 有限状态机 (FSM) has a fixed set of states,
rules for moving between them, and outputs. It is drawn as a state diagram 状态图
(bubbles for states, arrows labelled with inputs) and built as next-state logic, a state
register and output logic. Moore machines 穆尔机 compute their outputs from the
state alone, so outputs change only just after a clock edge. Mealy machines 米利机
use the state and the present inputs, so they respond sooner but an input glitch can
reach the output.
Two states, one output: the parity detector's whole design
Worked example 1 (the parity detector). Output 1 when the number of 1s seen so far
is odd.
Two states, EVEN and ODD. A 1 moves to the other state; a 0 stays. The output is 1 in
ODD.
Trace the input 1, 0, 1, 1: EVEN → ODD → ODD → EVEN → ODD. Three 1s have been seen,
an odd number, and the output is 1.
The design procedure: state diagram → state table → binary state encoding →
excitation table for each flip-flop → minimised next-state logic.
With one state bit $Q$ (0 = EVEN, 1 = ODD) and input $x$, the next-state logic is
one XOR: $Q^+ = Q \oplus x$. One gate and one flip-flop make the whole machine.
Worked example 2 (the 1011 detector). Detect the serial sequence 1011, with
overlapping matches allowed. The states record the longest part of 1011 matched so far:
S0 (nothing), S1 (seen 1), S10 (seen 10) and S101 (seen 101). On each input, go to the
longest ending of the input so far that is still a beginning of 1011:
State
input 0
input 1
S0
S0
S1
S1
S10
S1
S10
S0
S101
S101
S10
S1, detect
From S101, a 1 completes 1011. The output says detected, and the machine goes to
S1, because that final 1 can start the next match.
From S101, a 0 leaves "1010". Its ending "10" is a beginning of 1011, so the machine
goes to S10, not back to S0.
Four states need two flip-flops ($2^2 = 4$).
Trace the stream 1, 0, 1, 1, 0, 1, 1: S0 → S1 → S10 → S101 → detect (S1), then
0 → S10, 1 → S101, 1 → detect. The stream holds two copies, at bits 1–4 and bits
4–7, sharing bit 4, and the machine finds both. A detector that restarted at S0 after
a match would miss the second.
State minimisation 状态化简 comes before encoding: two states with the same outputs
and the same next states for every input are really one state. A machine with extra
states still works, but it wastes flip-flops and logic.
Common mistakes.
Restarting at S0 after a match. The end of one match can be the start of the next.
Encoding the states before checking the table for equivalent states.
Choosing the wrong machine type: Mealy when the response must come in the same cycle,
Moore when the outputs must not glitch.
Summary: an FSM is states, transitions and outputs. Moore outputs come from the state,
Mealy outputs from the state and inputs. Design in order: diagram, table, encoding,
excitation, minimise. Overlapping detection and state merging are where marks are won
and lost.
探索
Keep the suffix that starts the next match
$Q^{+}=f(Q,x)$
Clock the worked 1011011 stream and find both 1011 matches at bits 1–4 and 4–7. Compare the overlapping Mealy detector with the two-state parity machine, and distinguish a selected input from a clocked state transition.
A scoreboard changes from 7 to 8 while every other digit stays the same. That is a
register updating, and counting is a register that updates itself. A register寄存器 is a row of flip-flops sharing one clock, loading $n$ bits at each edge. In a
shift register 移位寄存器 each flip-flop feeds the next, so the bits move one place
per clock. Serial-in, serial-out delays a bit stream; serial-in, parallel-out turns a
stream into a word.
RAM modules: arrays of memory cells with address decoders, the same building blocks as the registers here at a much larger scale
A ripple counter 异步计数器 chains toggle (T) flip-flops: each output clocks the
next stage. It is simple, but each stage adds its delay, so the top bit settles late. A
synchronous counter 同步计数器 clocks every flip-flop from the same edge and uses
logic to decide which bits toggle, so the delays do not add up.
One clock, computed toggles: the synchronous counter's register and trace
Worked example 1 (the 3-bit up-counter). In a 3-bit synchronous up-counter with
bits $Q_2 Q_1 Q_0$:
Bit 0 toggles at every clock edge, bit 1 when $Q_0 = 1$, and bit 2 when
$Q_1 Q_0 = 11$.
After reset it counts 000, 001, 010, 011, …, 111 and then rolls over to 000.
Check: that is $2^3 = 8$ states.
Meaning: a bit toggles when all the bits below it are 1. That is exactly how adding 1
carries in binary.
Worked example 2 (a mod-6 counter). A mod-6 counter counts 000 to 101 and then
returns to 000: six states, not eight. The codes 110 and 111 are not used in normal
counting.
Rule for the synchronous design: if the present count is 101, 110 or 111, the next
state is 000; otherwise add 1.
Trace: 000, 001, 010, 011, 100, 101, 000. The jump from 101 to 000 happens at a
clock edge, like every other step.
Recovery: if noise or power-up leaves the counter in 110 or 111, the next edge also
gives 000.
The condition that selects these three codes is $Q_2\cdot(Q_1 + Q_0)$. The shorter
condition $Q_2\cdot Q_1$ detects only 110 and 111; it is 0 at 101, so the count would
step on to 110.
A different design uses an asynchronous clear, which resets the flip-flops at once
when 110 appears. Then the counter shows 110 for a moment before clearing, so do not
confuse that design with the synchronous one.
Memory 存储器 is an array of words. With $k$ address lines it selects one of $2^k$
words, each $m$ bits wide. An 8K × 8 memory holds $8\text{K} = 2^{13} = 8192$ words of
8 bits, so it has 13 address lines and 8 data lines.
Common mistakes.
Trusting a ripple counter at speed. It looks fine at a slow clock and fails at the
specified one, because each stage adds its delay.
Ignoring the unused states of a mod-N counter. It must recover from them to a legal
state.
Mixing up the shift direction: whether the serial stream sends the most significant
bit (MSB) first or the least significant bit (LSB) first.
Summary: registers load on a shared clock edge; shift registers convert between serial
and parallel. Synchronous counters use logic to decide which bits toggle. A mod-N
counter must handle its unused codes. Memory holds $2^k$ words of $m$ bits and needs
$k$ address lines.
探索
One clock edge moves every stored bit
$Q^{+}=\{sin,Q_3,Q_2,Q_1\}$
Starting from 0000, clock the lesson’s right-shift input stream 1, 1, 0, 1 to get 1000, 1100, 0110, 1011. Follow each old bit to its new position, and distinguish selecting the next input from clocking it into storage.
探索
Predict the toggles, then clock the new count
$Q^{+}=Q\oplus T$
Trace the 3-bit counter’s eight states and see why each bit toggles when all lower old bits are 1. Compare synchronous mod-6, where 101 goes directly to 000. Load either unused code 110 or 111 and check recovery at the next clock edge.
Wire 64 gates by hand and each connection is a chance for a mistake; modern chips have
billions of gates. Industry does not answer this with more careful drawing. It describes
the circuit in a language and checks the description by simulation. A hardware
description language 硬件描述语言 (HDL) such as Verilog or VHDL describes a circuit's
structure and behaviour. Only synthesizable 可综合 code can be turned into gates.
It is kept separate from the testbench 测试平台, code that drives the design in
simulation and never becomes hardware.
A small FPGA board: the chip's logic blocks are wired up by the HDL design you load into it
module full_adder(input a, b, cin, output sum, cout);
assign sum = a ^ b ^ cin;
assign cout = (a & b) | (cin & (a ^ b));
endmodule
In Verilog, ^ is XOR, & is AND and | is OR. Each assign is a wire that is
always active, so the statements work in parallel, as gates do.
One module, one exhaustive testbench: description and proof together
Worked example 1 (the exhaustive testbench).
The testbench drives all 8 combinations of a, b and cin.
For each one it checks the arithmetic rule $a + b + c_{in} = 2\cdot c_{out} + s$,
where $s$ is the sum output. All 8 pass.
Meaning: the reference is arithmetic, not a second gate description, so the check
is independent of the design. A copy of the design would repeat any mistake in it.
Worked example 2 (a shift register in HDL). Describe a 4-bit serial-in shift
register with input sin and output q:
always @(posedge clk) runs at each rising clock edge, so q is a register.
The concatenation {q[2:0], sin}is the shift: the top bit drops off the left,
and sin enters on the right.
Starting from 0000, the testbench drives sin = 1, 0, 1, 1 over four clocks. The
simulation shows q = 0001, 0010, 0101, 1011.
This is the shift register of unit 3.6, now written in four lines.
Common mistakes.
A testbench that restates the design. A copy proves nothing; use arithmetic or a hand
trace.
Putting testbench-only constructs, such as delays (#10), into a hardware module.
They do not synthesise.
Leaving out the reset in an always block. The register then starts in an unknown
state, the same trap as the unused states in unit 3.6.
Summary: keep synthesizable modules and testbenches separate. The reference in a
testbench must be independent, such as arithmetic or a traced sequence. Check small
input spaces exhaustively, and deliver each module with its testbench.
探索
The concatenation becomes a clocked bit transfer
$Q^{+}=\{Q_2,Q_1,Q_0,sin\}$
Follow q <= {q[2:0], sin} at each rising edge. From an explicitly initialized 0000, the lesson’s inputs 1, 0, 1, 1 produce 0001, 0010, 0101, 1011. Each new position takes an old bit; the previous Q₃ shifts out.