Datapath Components
Datapath Components
Chapter 4:
Datapath Components
Slides to accompany the textbook Digital Design, First Edition,
by Frank Vahid, John Wiley and Sons Publishers, 2007.
http://www.ddvahid.com
Introduction
• Chapters 2 & 3: Introduced increasingly complex digital building
blocks
– Gates, multiplexors, decoders, basic registers, and controllers
• Controllers good for systems with control inputs/outputs
– Control input: Single bit (or just a few), representing environment event or
state
• e.g., 1 bit representing button pressed
– Data input: Multiple bits collectively representing single entity
• e.g., 7 bits representing temperature in binary
• Need building blocks for data si ansis
– Datapath components, aka register-transfer-level (RTL) components,
store/transform data
ez
• Put datapath components together to form a datapath
• This chapter introduces numerous datapath components, and simple
datapaths
– Next chapter will combine controllers and datapaths into “processors”
Digital Design
Copyright © 2006 2
Frank Vahid Note: Slides with animation are denoted with a small red "a" near the animated items
4.2
Registers
b x
• Can store data, very common in datapaths Combinational n1
logic
• Basic register of Ch 3: Loaded every cycle s1 s0
n0
– Useful for implementing FSM -- stores encoded state clk State register
– For other uses, may want to load only on certain
cycles
a
I3 I2 I1 I0
load 4-bit register
I3 I2 I1 I0
D D D D
reg(4)
Q Q Q Q
si ansis
clk Q3 Q2 Q1 Q0
Q3 Q2 Q1 Q0
ez
Basic register loads on every clock cycle
How extend to only load on certain cycles?
Digital Design
Copyright © 2006 3
Frank Vahid
Register with Parallel Load
• Add 2x1 mux to front of each flip-flop
• Register’s load input selects mux input to pass
– Either existing flip-flop value, or new value to load
Digital Design
Copyright © 2006 4
Frank Vahid
Basic Example Using Registers
a3 a2 a1 a0
1 ld I3 I2 I1 I0
• This example will show how clk R0
registers load simultaneously Q3 Q2 Q1 Q0
on clock cycles
– Notice that all load inputs set to
1 in this example -- just for
demonstration purposes
1 ld I3 I2 I1 I0 1 ld I3 I2 I1 I0
R1 R2
Q3 Q2 Q1 Q0 Q3 Q2 Q1 Q0
Digital Design
Copyright © 2006 5
Frank Vahid
Basic Example Using Registers
a3 a2 a1 a0
1 ld I3 I2 I1 I0
clk R0
Q3 Q2 Q1 Q0
1 ld I3 I2 I1 I0 1 ld I3 I2 I1 I0
R1 R2
Q3 Q2 Q1 Q0 Q3 Q2 Q1 Q0
Digital Design
Copyright © 2006 6
Frank Vahid
Register Example using the Load Input:
Weight Sampler
• Scale has two displays
– Present weight
– Saved weight Scale Weight Sampler
– Useful to compare 0 0 1 01
present item with previous
item Save
3 pounds
2
b 1 load
I3 I2 I1 I0 a
a4 a3 a2 a1 a0 b4 b3 b2 b1 b0 c4 c3 c2 c1 c0
a4 a3 a2 a1 a0 b4 b3 b2 b1 b0 c4 c3 c2 c1 c0
I4 Q4 I4 Q4 I4 Q4
t4 I4 Q4 I4 Q4 I4 Q4
x4 I3 Q3 I3 Q3 I3 Q3
t3 I3 Q3 I3 Q3 I3 Q3
x3 I2 Q2 I2 Q2 I2 Q2
t2 I2 Ra Q2 I2 Rb Q2 I2 RcQ2
x2 I1 Q1 I1 Q1 I1 Q1
t1 I1 Q1 I1 Q1 I1 Q1
x1 I0 Q0 I0 Q0 I0 Q0
t0 I0 Q0 I0 Q0 I0 Q0
x0
ld ld ld
Clk Ra Rb Rc
osc C
C TemperatureHistoryStorage
a timer new line TemperatureHistoryStorage
Digital Design
Copyright © 2006 8
Frank Vahid
Register Example: Above-Mirror Display
8
Shorthand notation
0001010
a
To t
C 8 Loaded on clock edge
et r d0 load reg0 T
mir
r
ompu
c 2 4
8
i0
Digital Design
Copyright © 2006 10
Frank Vahid
Register Example: Computerized Checkerboard
LED
lit LED
R7 R6 R5 R4 R3 R2 R1 R0
10100010 10100010 10100010 10100010
01000101 01000101 01000101 01000101
i2,i1,i0 000 (R0) 001 (R1) 010 (R2) 011 (R3) 100 (R4) 101 (R5) 110 (R6) 111 (R7)
e
clk
Digital Design
Copyright © 2006 11
Frank Vahid
Shift Register
Register contents
1 1 0 1
• Shift right 0
before shift right
a
Digital Design
Copyright © 2006 12
Frank Vahid
Shift Register
• To allow register to either shift or retain, use 2x1 muxes
– shr: 0 means retain, 1 shift
– shr_in: value to shift in
• May be 0, or 1
• Note: Can easily design shift register that shifts left instead
Digital Design
Copyright © 2006 13
Frank Vahid
Rotate Register
Register contents
1 1 0 1
before shift right
• Rotate right: Like shift right,
Register contents
but leftmost bit comes from 1 1 1 0
after shift right
rightmost bit
Digital Design
Copyright © 2006 14
Frank Vahid
Shift Register Example: Above-Mirror Display
central computer
C 8
mirror display
To the above
2× 4 i0
8
car’s computer to a1
i1
d2 load reg2 I
8
d
8
D
x y
Digital Design
Copyright © 2006 16
Frank Vahid
Multifunction Registers
s1 s0 Operation
0 0 Maintain present value
0 1 Parallel load
1 0 Shift right
1 1 Shift left
Digital Design
Copyright © 2006 17
Frank Vahid
Multifunction Registers with Separate Control
Inputs
ld shr shl Operation
0 0 0 Maintain present value
0 0 1 Shift left
0 1 0 Shift right
0 1 1 Shift right – shr has priority over shl
1 0 0 Parallel load
1 0 1 Parallel load – ld has priority I3 I2 I1 I0
1 1 0 Parallel load – ld has priority shr_in
1 1 1 Parallel load – ld has priority I3 I2 I1 I0
ld shr_in
combi- s1 shl_in
shl_in
?
shr national s0
Truth table for combinational circuit circuit Q3 Q2 Q1 Q0
shl
Inputs Outputs Note
ld shr shl s1 s0 Operation Q3 Q2 Q1 Q0
0 0 0 0 0 Maintain value
0 0 1 1 1 Shift left a
0 1 0 1 0 Shift right
0 1 1 1 0 Shift right
1 0 0 0 1 Parallel load s1 = ld’*shr’*shl + ld’*shr*shl’ + ld’*shr*shl
1 0 1 0 1 Parallel load
1 1 0 0 1 Parallel load s0 = ld’*shr’*shl + ld a
1 1 1 0 1 Parallel load
a
Digital Design
Copyright © 2006 18
Frank Vahid
Register Operation Table
• Register operations typically shown using compact version of table
– X means same operation whether value is 0 or 1
• One X expands to two rows
• Two Xs expand to four rows
– Put highest priority control input on left to make reduced table simple
Digital Design
Copyright © 2006 19
Frank Vahid
Register Design Process
• Can design register with desired operations using simple
four-step process
Digital Design
Copyright © 2006 20
Frank Vahid
Register Design Example
s2 s1 s0 Operation
• Desired register operations 0
0
0
0
0
1
Maintain present value
Parallel load
– Load, shift left, synchronous clear, 0 1 0 Shift left
synchronous set 0 1 1 Synchronous clear
1 0 0 Synchronous set
1 0 1 Maintain present value
Step 1: Determine mux size 1 1 0 Maintain present value
1 1 1 Maintain present value
5 operations: above, plus maintain In
1 0 from
a present value (don’t forget this one!) Qn-1
--> Use 8x1 mux s2
s1 7 6 5 4 3 2 1 0
s0
Step 2: Create mux operation table
D
Step 3: Connect mux inputs Q
Qn
Step 4: Map control lines Inputs Outputs
s2 = clr’*set clr set ld shl s2 s1 s0 Operation
0 0 0 0 0 0 0 Maintain present value
s1 = clr’*set’*ld’*shl + clr
0 0 0 1 0 1 0 Shift left
s0 = clr’*set’*ld + clr 0 0 1 X 0 0 1 Parallel load
0 1 X X 1 0 0 Set to all 1s
Digital Design 1 X X X 0 1 1 Clear to all 0s
Copyright © 2006 21
a
Frank Vahid
Register Design Example
I3 I2 I1 I0
shl I3 I2 I1 I0
s2
s1 shl_in
ld combi- s0 shl_in
national
set
circuit Q3 Q2 Q1 Q0
clr
Q3 Q2 Q1 Q0
Adders
• Adds two N-bit binary numbers Inputs Outputs
a1 a0 b1 b0 c s1 s0
– 2-bit adder: adds two 2-bit numbers, 0 0 0 0 0 0 0
0 0 0 1 0 0 1
outputs 3-bit result 0 0 1 0 0 1 0
– e.g., 01 + 11 = 100 (1 + 3 = 4) 0
0
0
1
1
0
1
0
0
0
1
0
1
1
• Can design using combinational 0
0
1
1
0
1
1
0
0
0
1
1
0
1
design process of Ch 2, but doesn’t 0
1
1
0
1
0
1
0
1
0
0
1
0
0
work well for reasonable-size N 1 0 0 1 0 1 1
1 0 1 0 1 0 0
– Why not? 1 0 1 1 1 0 1
1 1 0 0 0 1 1
1 1 0 1 1 0 0
1 1 1 0 1 0 1
1 1 1 1 1 1 0
Digital Design
Copyright © 2006 23
Frank Vahid
Why Adders Aren’t Built Using Standard 4.3
Transistors
A: 1000 transistors for N=5, doubles for each o
t rs
4000
increase of N. So transistors = 1000*2(N-5). Thus,
a
how people do A: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
a
B: + 0 1 1 0 + 0 1 1 0 + 0 1 1 0 + 0 1 1 0
addition by hand
• One column at a
time 1 0 1 1 0 1 1 0 1 0 1
– Compute sum,
add carry to next
column
Digital Design
Copyright © 2006 25
Frank Vahid
Alternative Method to Design an Adder: Imitate
Adding by Hand
• Create 1 1 0
component for A: 1 1 1 1
B: + 0 1 1 0
each column
– Adds that a
column’s bits, 1 0 1 0 1
generates sum
1 1 0
and carry bits
A: 1 1 1 1
+ B: 0 1 1 0
b a ci b a ci b a ci b a
co s co s co s co s
1
0 1 0 1 SUM
Digital Design
Full-adders Half-adder
Copyright © 2006 26
Frank Vahid
Half-Adder
1 1 0
• Half-adder: Adds 2 bits, generates
A: 1 1 1 1
sum and carry
• Design using combinational design + B: 0 1 1 0
process from Ch 2 b a ci b a ci b a ci b a
Inputs Outputs
co s co s co s co s
a b co s 1
0 0 0 0 0 1 0 1 SUM
Step 1: Capture the function 0 1 0 1
1 0 0 1
1 1 1 0
a b
process from Ch 2 b a ci b a ci b a ci b a
co s co s co s co s
1
0 1 0 1 SUM
Step 1: Capture the function
Step 2: Convert to equations Step 3: Create the circuit
Inputs Outputs a b ci
a b ci co s co = a’bc + ab’c + abc’ + abc
0 0 0 0 0 co = a’bc +abc +ab’c +abc +abc’ +abc
0 0 1 0 1 co = (a’+a)bc + (b’+b)ac + (c’+c)ab
0 1 0 0 1 co = bc + ac + ab
0 1 1 1 0
1 0 0 0 1 s = a’b’c + a’bc’ + ab’c’ + abc
1 0 1 1 0 s = a’(b’c + bc’) + a(b’c’ + bc)
1 1 0 1 0 s = a’(b xor c)’ + a(b xor c)
1 1 1 1 1
Full
s = a xor b xor c adder
Digital Design co s
Copyright © 2006 28
Frank Vahid
Carry-Ripple Adder
• Using half-adder and full-adders, we can build adder that adds like we
would by hand
• Called a carry-ripple adder
– 4-bit adder shown: Adds two 4-bit numbers, generates 5-bit output
• 5-bit output can be considered 4-bit “sum” plus 1-bit “carry out”
– Can easily build any size adder
a3 b3 a2 b2 a1 b1 a0 b0
a b ci a b ci a b ci a b
a3 a2 a1 a0 b3 b2 b1 b0
FA FA FA HA
4-bit adder
co s co s co s co s
co s3 s2 s1 s0
co s3 s2 s1 s0
(a)
(b)
Digital Design
Copyright © 2006 29
Frank Vahid
Carry-Ripple Adder
• Using full-adder instead of half-adder for first bit, we can
include a “carry in” bit in the addition
– Will be useful later when we connect smaller adders to form bigger
adders
a3 b3 a2 b2 a1 b1 a0 b0 ci
a b ci a b ci a b ci a b ci a3 a2 a1 a0 b3 b2 b1 b0
FA FA FA FA 4-bit adder ci
co s co s co s co s co s3 s2 s1 s0
co s3 s2 s1 s0
(a) (b)
Digital Design
Copyright © 2006 30
Frank Vahid
Carry-Ripple Adder’s Behavior
0 0 0 0 0 0 0 0 0 0 0
0
a b ci a b ci a b ci a b ci
FA FA FA FA Assume all inputs initially 0
co s co s co s co s
0 0 0
0 0 0 0 0
0 0 0 1 0 0 1 0 0 1 1 0111+0001
0
(answer should be 01000)
a b ci a b ci a b ci a b ci
a
FA FA FA FA
co s co s co s co s
0 0 10
co2 co1 co0
0 0 1 1 0 Output a fter 2 ns (1 FA del ay)
Digital Design Wrong answer -- something wrong? No -- just need more time
Copyright © 2006 for carry to ripple through the chain of full adders. 31
Frank Vahid
Carry-Ripple Adder’s Behavior
0 0 0 1 0 0 1 0 1 1 1
0 0111+0001
(answer should be 01000)
a b ci a b ci a b ci a b ci
FA FA FA FA
co s co s co s co s
0 1 1
co1 Outputs after 4ns (2 FA delays)
0 0 1 0 0
(b)
0 0 0 1 01
0 1 0 1 1 1
0
a b ci a b ci a b ci a b ci
FA FA FA FA
co s co s co s co s
1 1 1
a
co2 Outputs after 6ns (3 FA delays)
0 0 0 0 0
(c)
0 0 01 1 0 1 1 0 1 1 1
0
a b ci a b ci a b ci a b ci
FA FA FA FA
co s co s co s co s
1 1 1
0 1 0 0 0 Output after 8ns (4 FA delays)
Digital Design (d)
Copyright © 2006 Correct answer appears after 4 FA delays 32
Frank Vahid
Cascading Adders
a7 a6 a5 a4 b7b6b5b4 a3 a2 a1 a0 b3b2b1b0
co s3 s2 s1 s0 co s3 s2 s1 s0 co s7.. s0
co s7 s6 s5 s4 s3 s2 s1 s0
(a) (b)
Digital Design
Copyright © 2006 33
Frank Vahid
4.4
Shifters
• Shifting (e.g., left shifting 0011 yields 0110) useful for:
– Manipulating bits
– Converting serial data to parallel (remember earlier above-mirror display
example with shift registers)
– Shift left once is same as multiplying by 2 (0011 (3) becomes 0110 (6))
• Why? Essentially appending a 0 -- Note that multiplying decimal number by 10
accomplished just be appending 0, i.e., by shifting left (55 becomes 550)
i3 i2 i1 i0
– Shift right once same as dividing by 2
i3 i2 i1 i0 inR
i3 i2 i1 i0 inL
<<1 q3 q2 q1 q0
q3 q2 q1 q0
C
00001100 (12) 8 *2
<<1 0 (shift in 0)
a
8 00100000 (32)
00011000 (24) 8
8-bit adder
8
00111000 (56)
F
Digital Design
Copyright © 2006 35
Frank Vahid
Barrel Shifter i3 i2 i1 i0
Comparators
• N-bit equality comparator: Outputs 1 if two N-bit numbers are equal
– 4-bit equality comparator with inputs A and B
• a3 must equal b3, a2 = b2, a1 = b1, a0 = b0
– Two bits are equal if both 1, or both 0
– eq = (a3b3 + a3’b3’) * (a2b2 + a2’b2’) * (a1b1 + a1’b1’) * (a0b0 + a0’b0’)
• Recall that XNOR outputs 1 if its two input bits are the same
– eq = (a3 xnor b3) * (a2 xnor b2) * (a1 xnor b1) * (a0 xnor b0)
a3 b3 a2 b2 a1 b1 a0 b0
0110 = 0111 ? 0 0 1 1 1 1 0 1
a3 a2 a1 a0 b3 b2 b1 b0 a
1 1 1 0
4-bit equality comparator
eq
(b)
0 eq
(a)
Digital Design
Copyright © 2006 37
Frank Vahid
Magnitude Comparator
• N-bit magnitude comparator: A=1011 B=1001
Indicates whether A>B, A=B, or
A<B, for its two N-bit inputs A and B 1011 1001 Equal
– How design? Consider how compare 1011 1001 Equal
by hand. First compare a3 and b3. If
equal, compare a2 and b2. And so on. 1011 1001 Unequal
Stop if comparison not equal -- So A > B
whichever’s bit is 1 is greater. If never
see unequal bit pair, A=B.
a
Digital Design
Copyright © 2006 38
Frank Vahid
Magnitude Comparator
• By-hand example leads to idea for design
– Start at left, compare each bit pair, pass results to the right
– Each bit pair called a stage
– Each stage has 3 inputs indicating results of higher stage, passes results to
lower stage
a3 b3 a2 b2 a1 b1 a0 b0
a b a b a b a b
Igt in_gt out_gt in_gt out_gt in_gt out_gt in_gt out_gt AgtB
Ieq in_eq out_eq in_eq out_eq in_eq out_eq in_eq out_eq AeqB
Ilt in_lt out_lt in_lt out_lt in_lt out_lt in_lt out_lt AltB
0 Igt a3 a2 a1 a0 b3b2b1b0
AgtB
1 Ieq 4-bit magnitude comparator AeqB
Digital Design
0 Ilt AltB
Copyright © 2006 39
Frank Vahid
(b)
Magnitude Comparator
a3 b3 a2 b2 a1 b1 a0 b0
a b a b a b a b
Igt in_gt out_gt in_gt out_gt in_gt out_gt in_gt out_gt AgtB
Ieq in_eq out_eq in_eq out_eq in_eq out_eq in_eq out_eq AeqB
Ilt in_lt out_lt in_lt out_lt in_lt out_lt in_lt out_lt AltB
• Each stage:
– out_gt = in_gt + (in_eq * a * b’)
• A>B (so far) if already determined in higher stage, or if higher stages equal but in
this stage a=1 and b=0
– out_lt = in_lt + (in_eq * a’ * b)
• A<B (so far) if already determined in higher stage, or if higher stages equal but in
this stage a=0 and b=1
– out_eq = in_eq * (a XNOR b)
• A=B (so far) if already determined in higher stage and in this stage a=b too
– Simple circuit inside each stage, just a few gates (not shown)
Digital Design
Copyright © 2006 40
Frank Vahid
Magnitude Comparator
• How does it 1011 = 1001 ?
1 = 1 0 0 1 0 1 1
work? a3 b3 a2 b2 a1 b1 a0 b0
a b a b a b a b
0 0
Igt in_gt out_gt in_gt out_gt in_gt out_gt in_gt out_gt AgtB
Ieq=1 causes this 1 1
Ieq in_eq out_eq in_eq out_eq in_eq out_eq in_eq out_eq AeqB
0 0
stage to compare Ilt in_lt out_lt in_lt out_lt in_lt out_lt in_lt out_lt AltB
1 1 0 = 0 1 0 1 1
a3 b3 a2 b2 a1 b1 a0 b0
a b a b a b a b
0 0
Igt in_gt out_gt in_gt out_gt in_gt out_gt in_gt out_gt AgtB
1 1
Ieq in_eq out_eq in_eq out_eq in_eq out_eq in_eq out_eq AeqB
0 0
Ilt in_lt out_lt in_lt out_lt in_lt out_lt in_lt out_lt AltB
Counters
• N-bit up-counter: N-bit register
01
that can increment (add 1) to its cnt 4-bit up-counter
a
tc C
own value on each clock cycle
4
– 0000, 0001, 0010, 0011, ...., 1110,
01
0 0001
0000
0101
0100
0011
0010
1110
1111
0001
0000
...
1111, 0000
– Note how count “rolls over” from 1111
to 0000
4-bit up-counter
• Terminal (last) count, tc, equals1
during value just before rollover cnt
ld
4-bit register
• Internal design
a
– Register, incrementer, and N-input 4 4
AND gate to detect terminal count 4 +1
4
tc C
Digital Design
Copyright © 2006 43
Frank Vahid
Incrementer
• Counter design used incrementer
• Incrementer design
– Could use carry-ripple adder with B input set to 00...001
• But when adding 00...001 to another number, the leading 0’s
obviously don’t need to be considered -- so just two bits being
added per column
– Use half-adders (adds two bits) rather than full-adders (adds
three bits)
a3 a2 a1 a0 1
carries: 011
a b a b a b a b a3 a2 a1 a0
0011
unused HA HA HA HA +1
+ 1
co s co s co s co s
tn co s3 s2 s1 s0
0 0 10 0
co s3 s2 s1 s0
r
I (b)
Digital Design (a)
Copyright © 2006 44
Frank Vahid
Incrementer
• Can build faster incrementer a3
Inputs
a2 a1 a0 c0 s3
Outputs
s2 s1 s0
using combinational logic 0 0 0 0 0 0 0 0 1
0 0 0 1 0 0 0 1 0
design process 0 0 1 0 0 0 0 1 1
– Capture truth table 0 0 1 1 0 0 1 0 0
0 1 0 0 0 0 1 0 1
– Derive equation for each output 0 1 0 1 0 0 1 1 0
• c0 = a3a2a1a0 0 1 1 0 0 0 1 1 1
0 1 1 1 0 1 0 0 0
• ... 1 0 0 0 0 1 0 0 1
• s0 = a0’ 1 0 0 1 0 1 0 1 0
Digital Design
Copyright © 2006 45
Frank Vahid
Counter Example: Mode in Above-Mirror Display
• Recall above-mirror display example from Chapter 2
– Assumed component that incremented xy input each time button
pressed: 00, 01, 10, 11, 00, 01, 10, 11, 00, ...
– Can use 2-bit up-counter
• Assumes mode=1 for just one clock cycle during each button press
– Recall “Button press synchronizer” example from Chapter 3
mode
cnt 2-bit up counter
tc c1c0
clk
x y
Digital Design
Copyright © 2006 46
Frank Vahid
Counter Example: 1 Hz Pulse Generator Using 256 Hz
Oscillator
• Suppose have 256 Hz
oscillator, but want 1 Hz
pulse
– 1 Hz is 1 pulse per second
-- useful for keeping time
1
– Design using 8-bit up- cnt 8-bit up-counter
counter, use tc output as tc C
osc
pulse (256 Hz) 8
• Counts from 0 to 255 (256 p (unused)
counts), so pulses tc every (1 Hz)
256 cycles
Digital Design
Copyright © 2006 47
Frank Vahid
Down-Counter
• 4-bit down-counter
4-bit down-counter
– 1111, 1110, 1101, 1100, …,
0011, 0010, 0001, 0000, cnt
1111, … ld
4-bit register
– Terminal count is 0000
• Use NOR gate to detect
– Need decrementer (-1) – 4 4
design like designed 4 –1
incrementer
tc C 4
Digital Design
Copyright © 2006 48
Frank Vahid
Up/Down-Counter
• Can count either up 4-bit up/down counter
or down
– Includes both dir
1 4-bit 2 x 1 0
incrementer and 4
decrementer
clr
– Use dir input to cnt
clr
ld 4-bit register
select, using 2x1:
dir=0 means up
– Likewise, dir selects 4 4 4 4
appropriate terminal –1 +1
4
count value
4 4
1 2x 1 0
tc C
Digital Design
Copyright © 2006 49
Frank Vahid
Counter with Parallel Load
• Up-counter that can be
L 4
loaded with external
ld
value 1 4-bit 2 x 1 0
– Designed using 2x1 mux 4
– ld input selects cnt ld
incremented value or 4-bit register
external value
– Load the internal register 4 4
when loading external +1
4
value or when counting
tc C
Digital Design
Copyright © 2006 50
Frank Vahid
Counter with Parallel Load
• Useful to create pulses at 1000
specific multiples of clock 4
Digital Design
Copyright © 2006 52
Frank Vahid
4.8
Subtractor
• Can build subtractor as we built carry-ripple adder
– Mimic subtraction by hand
– Compute borrows from columns on left
• Use full-subtractor component:
– wi is borrow by column on right, wo borrow from column on left
1st column 2nd column 3rd column 4th column
0 0 1 10 0 1 0
1 0 1 10 1 10 1 0 1 10 1 0 1 0 1 0
- 0 1 1 1 - 0 1 1 1 - 0 1 1 1 - 0 1 1 1
1 1 1 0 1 1 0 0 1 1
a3 b3 a2 b2 a1 b1 a0 b0 wi
a b wi a b wi a b wi a b wi a3 a2 a1 a0 b3 b2 b1 b0
FS FS FS FS 4-bit subtractor wi a
wo s wo s wo s wo s wo s3 s2 s1 s0
wo s3 s2 s1 s0
(b) (c)
Digital Design
Copyright © 2006 53
Frank Vahid
Subtractor Example: DIP-Switch Based
Adding/Subtracting Calculator
• Extend earlier DIP switches
calculator example 1
0
– Switch f indicates
whether want to
8 8 8 8
add (f=0) or 0 0
subtract (f=1) A B ci A B wi
8-bit adder 8-bit subtractor
– Use subtractor and co S wo S
2x1 mux
1 f 8 8
0 2x1 1
0
8
e
ld
8-bit register
clk
CALC
8
LEDs
Digital Design
Copyright © 2006 54
Frank Vahid
Subtractor Example:
Color Space Converter – RGB to CMYK
• Color
– Often represented as weights
of three colors: red, green, and
blue (RGB)
• Perhaps 8 bits each, so
specific color is 24 bits
– White: R=11111111,
G=11111111, B=11111111
– Black: R=00000000,
G=00000000, B=00000000
– Other colors: values in
between, e.g., R=00111111,
G=00000000, B=00001111 • Printers use opposite color scheme
would be a reddish purple – Because inks absorb light
– Good for computer monitors, – Use complementary colors of RGB:
which mix red, green, and blue Cyan (absorbs red), reflects green
lights to form all colors and blue, Magenta (absorbs green),
and Yellow (absorbs blue)
Digital Design
Copyright © 2006 55
Frank Vahid
Subtractor Example:
Color Space Converter – RGB to CMYK
R G B
• Printers must quickly convert 255
8
255
8
255
8
RGB to CMY 8 8 8
GB
R 8 8 8
C M Y
Digital Design
Copyright © 2006 56
Frank Vahid
Subtractor Example:
Color Space Converter – RGB to CMYK
Digital Design
Copyright © 2006 57
Frank Vahid
Subtractor Example:
Color Space Converter – RGB to CMYK
• Call black part K
R G B
8 8 8
– (200,200,200): K=200 K
– (Letter “B” already used for blue)
R G B
Y
• Compute minimum of C, R GB t o CMY o
t CM
C M Y
M, Y values GB
R
– Use MIN component 8 8 8 8
designed earlier, using C M Y
comparator and mux, to MIN
compute K 8
– Output resulting K value,
and subtract K value from MIN
C, M, and Y values 8
K
– Ex: Input of (250,200,200)
yields output of
- - -
(50,0,0,200)
8 8 8 8
C2 M2 Y2 K
Digital Design
Copyright © 2006 58
Frank Vahid
Representing Negative Numbers: Two’s
Complement
• Negative numbers common
– How represent in binary?
• Signed-magnitude
– Use leftmost bit for sign bit
• So -5 would be:
1101 using four bits
10000101 using eight bits
Digital Design
Copyright © 2006 59
Frank Vahid
Ten’s Complement
1 9
2 8
• Before introducing two’s complement, let’s 3 7
consider ten’s complement
4 6
– But, be aware that computers DO NOT USE TEN’S
5 5
COMPLEMENT. Introduced for intuition only.
6 4
– Complements for each base ten number shown to
right – Complement is the number that when added 7 3
results in 10 8 2
9 1
Digital Design
Copyright © 2006 60
Frank Vahid
Ten’s Complement
• Nice feature of ten’s complement
– Instead of subtracting a number, adding its complement results in answer
exactly 10 too much
– So just drop the 1 – results in subtracting using addition only
Digital Design
Copyright © 2006 61
Frank Vahid
Two’s Complement is Easy to Compute:
Just Invert Bits and Add 1
• Hold on!
– Sure, adding the ten’s complement achieves subtraction using addition
only
– But don’t we have to perform subtraction to have determined the
complement in the first place? e.g., we only know that the complement of 4
is 6 by subtracting 10-4=6 in the first place.
• True – but in binary, it turns out that the two’s complement can be
computed easily
– Two’s complement of 011 is 101, because 011 + 101 is 1000
– Could compute complement of 011 as 1000 – 011 = 101
– Easier method: Just invert all the bits, and add 1
– The complement of 011 is 100+1 = 101 -- it works!
(check: 0101+1011=10000)
Q: What is the two’s complement of 0011? A: 1100+1=1101
Digital Design
Copyright © 2006 62
Frank Vahid
Two’s Complement Subtractor Built with an Adder
• Using two’s complement
A B
A – B = A + (-B)
= A + (two’s complement of B) N-bit
= A + invert_bits(B) + 1
• So build subtractor using A B 1
Adder cin
adder by inverting B’s bits,
and setting carry in to 1
S
Digital Design
Copyright © 2006 63
Frank Vahid
Adder/Subtractor
• Adder/subtractor: control
input determines whether
add or subtract
– Can use 2x1 mux – sub input
passes either B or inverted B
– Alternatively, can use XOR
gates – if sub input is 0, B’s
bits pass through; if sub input
is 1, XORs invert B’s bits
Digital Design
Copyright © 2006 64
Frank Vahid
Adder/Subtractor Example: Calculator
• Previous calculator DIP switches
used separate 1
0
adder and
8 8
subtractor
1 f A B
• Improve by using 0
sub 8-bit adder/subtractor
S
adder/subtractor, 8
e
and two’s ld
8-bit register
clk
CALC
complement 8
numbers 1
DIP switches
0
LEDs
8 8 8 8
A B ci 0 A B wi 0
8-bit adder 8-bit subtractor
co S wo S
1 f 8 8
0 2x1 1
0
8
e ld 8-bit register
clk CALC
8
Digital Design
Copyright © 2006 LEDs 65
Frank Vahid
Overflow
• Sometimes result can’t be represented with given number
of bits
– Either too large magnitude of positive or negative
– e.g., 4-bit two’s complement addition of 0111+0001 (7+1=8). But 4-
bit two’s complement can’t represent number >7
• 0111+0001 = 1000 WRONG answer, 1000 in two’s complement is -8,
not +8
– Adder/subtractor should indicate when overflow has occurred, so
result can be discarded
Digital Design
Copyright © 2006 66
Frank Vahid
Detecting Overflow: Method 1
• Assuming 4-bit two’s complement numbers, can detect overflow by
detecting when the two numbers’ sign bits are the same but are
different from the result’s sign bit
– If the two numbers’ sign bits are different, overflow is impossible
• Adding a positive and negative can’t exceed largest magnitude positive or
negative
• Simple circuit
– overflow = a3’b3’s3 + a3b3s3’
– Include “overflow” output bit on adder/subtractor
sign bits
0 1 1 1 1 1 1 1 1 0 0 0
+ 0 0 0 1 + 1 0 0 0 + 0 1 1 1
1 0 0 0 0 1 1 1 1 1 1 1
overflow overflow no overflow
(a) (b) (c)
If the numbers’ sign bits have the same value, which
Digital Design
differs from the result’s sign bit, overflow has occurred.
Copyright © 2006 67
Frank Vahid
Detecting Overflow: Method 2
• Even simpler method: Detect difference between carry-in to sign bit and
carry-out from sign bit
• Yields simpler circuit: overflow = c3 xor c4
1 1 1 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0 0 0
+ 0 0 0 1 + 1 0 0 0 + 0 1 1 1
0 1 0 0 0 1 0 1 1 1 0 1 1 1 1
overflow overflow no overflow
(a) (b) (c)
If the carry into the sign bit column differs from the
carry out of that column, overflow has occurred.
Digital Design
Copyright © 2006 68
Frank Vahid
4.9
Digital Design
Copyright © 2006 69
Frank Vahid
Multifunction Calculator without an ALU
• Can build multifunction
calculator using separate
DIP switches
components for each 1
operation, and muxes 0
8 8
– But too many wires, and
A B
wasted power computing
all those operations when + Ð +1 AND OR XOR NOT
Wasted
power
8
at any time you only use 8 8
8
8 8 8
one of the results 8
1 0 A lot of wires
0 1 2 3 4 5 6 7
x
s2
y 8-bit 8 1
s1
z s0
8
e
Id
8-bit reg ister
clk
CALC
8
Digital Design
Copyright © 2006 71
Frank Vahid
Arithmetic-Logic Extender in Front of ALU
• xyz=000: Want S=A+B – just pass a to ia, b to ib, and set cin=0
• xyz=001: Want S=A-B – pass a to ia, b’ to ib, and set cin=1
• xyz=010: Want S=A+1 – pass a to ia, set ib=0, and set cin=1
• xyz=011: Want S=A – pass a to ia, set ib=0, and set cin=0
• xyz=1000: Want S=A AND B – set ia=a*b, b=0, and cin=0
• others: likewise
• Based on above, create logic for ia(x,y,z,a,b) and ib(x,y,z,a,b) for each abext, and
create logic for cin(x,y,z), to complete design of the AL-extender component
Digital Design
Copyright © 2006 72
Frank Vahid
ALU Example: Multifunction Calculator
DIP swi tches
1
0
8 8
A B
+ Ð +1 W as ted
AND OR X OR NOT p o w er
8
8 8
8
8 8 8
DIP switches
8
1 0 0 1 2 3 4 5 6 7 A lot of wi re s.
x s2
y
z
s1
s0
8-bit 8 1
1 1
8
clk
e
Id
8-bit re g is ter 0 0
C A LC
8
LEDs
8 8
A B
A B
• Design using ALU is x
y
x
ALU
y
elegant and efficient z z S
8
– No mass of wires e
ld
– No big waste of power clk
8-bit register
8 CALC
LEDs
Digital Design
Copyright © 2006 73
Frank Vahid
4.10
Register Files
• MxN register file
component provides 32
er C C
efficient access to M N- er 8 a
Digital Design
Copyright © 2006 74
Frank Vahid
Register File
• Instead, want component that has one data input and one data output,
and allows us to specify which internal register to write and which to read
32 32
W_data R_data a
4 4
W_addr R_addr
W_en R_en
16×32
register file
Digital Design
Copyright © 2006 75
Frank Vahid
Register File Timing Diagram
• Can write one
register and read
one register each
clock cycle
– May be same
register
32 32
W_data R_data
2 2
W_addr R_addr
W_en R_en
4x32
register file
Digital Design
Copyright © 2006 76
Frank Vahid
Register-File Example: Above-Mirror Display
• 16 32-bit registers that
can be written by car’s 32
OLD design
C
computer, and d0 load reg0
a
huge mux
displayed 32
i0
4 16
– Use 16x32 register file
mirror display
To the above-
32
central computer
C 32too much D
Digital Design
Copyright © 2006 78
Frank Vahid