Lecture 6
Lecture 6
Week 5
▪ Registers
▪ Counters
▪ Basics of state machines
▪ How to design state machines
Flip-
Flops Outputs
Registers
▪ An n-bit register: a bank of n flip-flops that share a
common clock.
▪ Registers store a multi-bit value.
D0 Q D1 Q D2 Q D15 Q
…
Q Q Q Q
Clk
D Q D Q D Q D Q
EN EN EN EN
Q Q Q Q
Write
Clk
Q Q Q Q
Clk
one bit…
two bit…
three bits…
Counters
▪ Basic idea…
n
n
adder register
n
…00001
Clk
Ripple Counter (async!)
1
Q0 Q1 Q2 Q3
T Q T Q T Q T Q
Q Q Q Q
C
Q0
Q1
Q2
Q3
Synchronous Counter
Write
T Q T Q
T Q T Q Q2 Q3
Q0 Q1
Q Q
Q Q
Clk
1 1
D Q 0 D Q
0
Q Q
Clk
Clear
# flip-flops = 4
Question Zero
5¢
Five
10¢ 5¢ 10¢
5¢
▪ Dealing with too Ten Fifteen
5¢
much money means 10¢
5¢
10¢
Input 0 0 0 1 0 1 0 0 0 0
Output 0 0 0 1 0 0 0
Input 0 0 0 1 0 1 0 1 0 1 0 0 0 0
Input 1 0 1 1 0 1 0 1 0
Question: Barcode Reader
▪ Build a machine that
detects a 1010 pattern.
▪ We know how to build
a pattern recognizer:
One state for each possible sequence of 4 bits.
In other words, one FF per bit:
F0 last seen bit, F1 the bit before that, F2 for bit seen
before that, and F3.
▪ Can we do better than 4 FFs?
Question: Barcode Reader
▪ The previous pattern recognizer:
state for each possible sequence of 4 bits.
It essentially memorized
▪ Do you have another idea?
Question: Barcode Reader
▪ The previous pattern recognizer:
state for each possible sequence of 4 bits.
It essentially memorized
▪ Do you have another idea?
How about a state that shows us where we are in the
pattern?
1 0 1 0
A B C D E
Step #1: Draw state diagram
1 0 1 0
A
Step #1: Draw state diagram
0 1
1
A
B 0
1 1
C
0 0
D
1
1 0 1 0 0
E
A B C D E
Step #1: … and outputs
0 1
1
A/0
B/0 0
1 1
C/0
0 0
D/0
1
E/1
0
Step #2: State Table
Present Next
▪ Write state table with Z State
X Z
State
0 1
1
A/0
0
B/0
1 1
C/0
0 0
D/0
1
E/1
Step #2: State Table
Present Next
▪ Write state table with Z State
X Z
State
▪ Output Z is determined A 0 0 A
A 1 0 B
by the current state. B 0 0 C
B 1 0 B
Denotes Moore machine.
C 0 0 A
▪ Next step: allocate flip- C 1 0 D
D 0 0 E
flops values to each state. D 1 0 B
E 0 1 A
How many flip-flops will
E 1 1 D
we need for 5 states?
# flip-flops = log(# of states)
→ We need 3 flipflops
Discussion!
▪ Wait a minute… we built a recognizer before!
▪ We needed 8 states to recognize 3-bit pattern.
▪ Now we need only 5 states for a 4-bit pattern
▪ Why?
0 1
1
A/0
B/0
0
1 1
C/0
0 0
D/0
1
E/1
Discussion!
▪ The 3-bit pattern recognizer is doing more!
It is memorizing the last 3 bits.
To recognize another pattern, simply set the output
of the relevant state to one.
Even multiple patterns by the same machine.
▪ The 4-bit pattern recognizer can only recognize
one specific pattern.
Different pattern → different state machine.
To recognize n-bit pattern → need n+1 states.
Step #3: Flip-Flop Assignment
0 1
1 Unsafe!
A (might go through E)
▪ 3 flip-flops
B 0
needed here. 1
1
▪ Assign states C
0 0
carefully though! D
1
▪ Can’t simply do this: E
➢ A = 100 ➢B = 011 0
➢ C = 010 ➢D = 001
➢ E = 000
Why not?
Step #3: Flip-Flop Assignment
0 1
1
A
B 0
▪ Be careful of 1
1
race conditions. C
0 0
▪ Better solution: D
➢ A = 000 ➢B = 001 1
E
➢ C = 011 ➢D = 101 0
➢ E = 100
• Still has race conditions (C→D, C→A), but is safer.
• “Safer” is defined according to output behaviour.
• Sometimes, extra flip-flops are used for extra insurance.
Step #4: Redraw State Table
Present Next
▪ We can now F2 F1 F0
State
X Z F2 F1 F0
State
construct the K-maps 0 0
A 0 0 0 0 A0 0
0 0
A 0 1 0 0 B0 1
for the state logic 0 0
B 1 0 0 0 C1 1
combinational circuit. 0 0
B 1 1 0 0 B0 1
0 1
C 1 0 0 0 A0 0
Derive equations for each 0 1
C 1 1 0 1 D0 1
flip-flop value, given the 1 0
D 1 0 0 1 E0 0
1 0
D 1 1 0 0 B0 1
previous values and the
1 0
E 0 0 1 0 A0 0
input X. 1 0
E 0 1 1 1 D0 1
Three equations total,
plus one more for Z (trivial for Moore machines).
Step 5: Circuit design
▪ Karnaugh map for F2:
F2·F1 0 0 0 0
F2·F1 X X 1 0
F2·F1 X X X X
F2·F1 0 1 0 1
F2·F1 0 0 0 1
F2·F1 X X 0 0
F2·F1 X X X X
F2·F1 0 0 0 0
F1 = F2F1F0X
Step 5: Circuit design
▪ Karnaugh map for F0:
F2·F1 0 1 1 1
F2·F1 X X 1 0
F2·F1 X X X X
F2·F1 0 1 1 0
F0 = X + F2F1F0
Step 5: Circuit design
▪ Output value Z goes high for state E (100),
so the output expression is:
Z = F2F1F0
X
D Q
F1 Z
Q
D Q
F2
Q
Circuit
Clk
D Q
F0
Q
X
D Q
F1 Z
Q
D Q
F2
Q
Question: Mouse clicks
▪ Design a state machine
that takes in two signals:
P is high if the user
is pressing the mouse button.
M is high if the mouse is being moved.
▪ Based on the inputs, the state indicates
whether the user is clicking, double-clicking,
or dragging the mouse on the screen.
Hint: you will need more states than that!
36
Question: Mouse clicks
Clicked
Double
Clicked
37
Question: Mouse clicks
10
Clicked
PM = 11 00
01
0X 10 00
1X
Neutral 01
Dragging Released
11 0x 0x
1x
11
Double
Clicked
10
Assembly Language
Processors
Arithmetic Finite State
Logic Units Machines
Devices Flip-flops
Circuits
Gates
Transistors
We build these
The Final Destination
The Final Destination
PCWriteCond
Control PCSource
PCWrite Unit
ALUOp
IorD
ALUSrcB
MemRead
ALUSrcA
MemWrite
RegWrite
MemtoReg
RegDst
IRWrite
Opcode
0
1
Shift left 2 2
Instruction
[31-26] Registers
0 Instruction Read reg 1 0
A
PC
Controller
Thing
Storage Arithmetic
Thing Thing
The “Arithmetic Thing”
aka: the Arithmetic Logic Unit (ALU)
Arithmetic Logic Unit
▪ The first microprocessor
applications were calculators.
Remember adders
and subtractors?
These are part of a larger
structure called the arithmetic
logic unit (ALU).
You made a simple one for lab 2 and 3!
▪ This larger structure is responsible for the
processing of all data values in a basic CPU.
ALU inputs A B
X3 X2 X1 X0
Cout C3 C2 C1 Cin
FA FA FA FA
S3 S2 S1 S0
Arithmetic components
Cin
n
A X n
G
n n-bit G = X + Y + Cin
n parallel
B
Y
B input Y adder
S0 logic
S1
Cout
0 0 All 0s G =A Transfer
0 1 B G = A+B Addition
1 0 B G = A+B Subtraction - 1
1 1 All 1s G = A-1 Decrement
to perform? 3
Ai
Bi
Logic G
i
S0 circuit
S1
S2
ALU block diagram
▪ In addition to data inputs and outputs, this circuit
also has:
outputs indicating the different conditions,
inputs specifying the operation to perform (similar to Sub).
A0
A1
Data input A ... … G0
An-1 G1
… ... Data output G
B0 Gn-1
B1
Data input B ... … n-bit
Bn-1 ALU Cout Carry output
Carry input Cin V Overflow indicator
S0 N Negative indicator
Operation & S1 Z Zero indicator
Mode select S2
What about multiplication?
▪ Multiplication (and division) operations are
more complicated than other arithmetic
(plus, minus) or logical (AND, OR) operations.
▪ Three major ways that multiplication can be
implemented in circuitry:
Layered rows of adder units.
An adder/shifter circuit with accumulator.
Booth’s Algorithm
Multiplication
▪ Revisiting grade 3 math…
123 12 3
x 456 x 456 123
1368 x 456
1368
912
1 2 3 1 23 456
x 456 x 456
56088
1368 1368
912 912
456
Binary Multiplication
5*6 (unsigned)
▪ And now, in binary…
101 10 1 101
x 110 x 110 x 110
110 110
000
1 0 1 1 01 110
x 110 x 110 11110
110 110
000 000
110 Result: 30
Binary Multiplication
▪ Or seen another way….
101 101
x 110 x 11 0 101
000 x 110
000
101
101 101 101
x 1 1 0 x 1 10
11110
000 000
101 101
101
Binary Multiplication
Implementation
▪ Implementing this
in circuitry involves
the summation of
several AND terms.
AND gates combine
input signals.
Adders combine the
outputs of the AND
gates.
Multiplication
▪ This implementation
results in an array of
adder circuits to make
the multiplier circuit.
▪ This can get a little
expensive as the size
of the operands grows.
N-bit numbers → O(1) clock cycles, but O(N2) size.
▪ Is there an alternative to this circuit?
Accumulator circuits
▪ What if you could perform each stage of the
multiplication operation, one after the other?
This circuit would only
Register Y
need a single row of
adders and a couple Register X cout
Shift Left 1
of shift registers.
1 x n AND
How wide does
register R have
Shift Left 1 Adder
to be?
Is there a simpler
Register R
way to do this?