Download Digital Design: Magnitude Comparators, Counters, Incrementers, Timers, and Subtractors - P and more Exams Electrical and Electronics Engineering in PDF only on Docsity!
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
1 of 58
Digital Design
Chapter 4.4 – 4.10:
Datapath Components
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 2 of 58
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)
Shift right once same as dividing by 2
i
q3 q2 q1 q
in
i3 i1 i
Left shifter
in
sh
i
q3 q2 q1 q
i2 i1 i
Shifter with left shift
or no shift
inL
i
q3 q2 q1 q
i2 i1 i
inR
2 0 s s
shL shR
Shifter with left
shift, right shift,
and no shift
Symbol
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 3 of 58
Shifter Example: Approximate Celsius to Fahrenheit Converter
Convert 8-bit Celsius input to 8-bit Fahrenheit output
F = C * 9/5 + 32
Approximate: F = C*2 + 32
Use left shift: F = left_shift(C) + 32
C
<<1 0 (shift in 0)
F
8-bit adder
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
4 of 58
Shifter Example: Temperature Averager
Four registers storing a history of
temperatures
Want to output the average of those
temperatures
Add, then divide by four
Same as shift right by 2
Use three adders, and right shift by two
Tavg
ldRavg
ld
T
clk
ld
Ra Rb Rc Rd
shift in 0 divide by 4
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 5 of 58
Barrel Shifter
A shifter that can shift by any amount
4-bit barrel left shift can shift left by 0,
1, 2, or 3 positions
8-bit barrel left shifter can shift left by
0, 1, 2, 3, 4, 5, 6, or 7 positions
(Shifting an 8-bit number by 8 positions is pointless -- you just lose all the bits)
Could design using 8x1 muxes and lots
of wires
Too many wires
More elegant design
Chain three shifters: 4, 2, and 1
Can achieve any shift of 0..7 by
enabling the correct combination of
those three shifters, i.e., shifts should
sum to desired amount
in sh
i
q3 q2 q1 q
i2 i1 i
Shift by 1 shifter uses 2x1 muxes. 8x
mux solution for 8-bit barrel shifter: too
many wires.
sh <<1 in
x
y
z
Q
sh <<2 in
sh <<4 in
Q: xyz to shift by 5? I
1 a
01100000 (by 4)
11000000 (by 1)
Net result: shift by 5
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 6 of 58
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 = b 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 b
eq (a)
(b)
a3 a2a1 a0 b
eq
b2b1 b 4-bit equality comparator
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
10 of 58
Magnitude Comparator
How does it work?
Final answer appears on the right
Takes time for answer to “ripple” from left to right
Called “carry-ripple style” after the carry-ripple adder, even though there’s no “carry”
involved
0 1 0
0 1 0
0 1 0
Ieq=1 causes this stage3 to compare
a
1
0
0 0
1
0
in_gt in_eq in_lt
out_gt out_eq out_lt
Igt Ieq llt
Stage 3
a3 b
a b in_gt in_eq in_lt
out_gt out_eq out_lt
Stage
a2 b
a b in_gt in_eq in_lt
out_gt out_eq out_lt
Stage
a1 b
a b in_gt in_eq in_lt
out_gt out_eq out_lt
AgtB AeqB AltB
Stage
a0 b
a b
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 11 of 58
Magnitude Comparator Example: Minimum of Two Numbers
Design a combinational component that computes the minimum of two 8-bit numbers
Solution: Use 8-bit magnitude comparator and 8-bit 2x1 mux
If A<B, pass A through mux. Else, pass B.
MIN
Igt Ieq Ilt
AgtB AeqB AltB
A
A B
B
8-bit magnitude comparator
s I1^ I 2x1 mux
8-bit
C
C
A B
Min
(a)
(b)
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 12 of 58
Counters
N-bit up-counter : N-bit register that
can increment (add 1) to its own value
on each clock cycle
Note how count “rolls over” from 1111
to 0000
Terminal (last) count, tc, equals1 during value just before rollover
Internal design
Register, incrementer, and N-input
AND gate to detect terminal count
ld 4-bit register
tc C
cnt
4-bit up-counter
a
a
cnt tc C
4-bit up-counter
4
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
13 of 58
Incrementer
Counter design used incrementer
Incrementer design
Could use carry-ripple adder with B input set to 00...
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)
carries:
unused
(a)
(b)
a3 a2 a1 a0 1
cos3 s2 s1 s
a b
co s
HA
a b
co s
HA
a b
co s
HA
a b
co s
HA
I
r
nt
a
co s3s
s1 s
a2 a1 a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 14 of 58
Incrementer
Can build faster incrementer using
combinational logic design process
Capture truth table
Derive equation for each output
c0 = a3a2a1a ... s0 = a0’
Results in small and fast circuit
Note: works for small N -- larger N leads to
exponential growth, like for N-bit adder
s 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0
s 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0
s 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
s 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0
c 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
a 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
a 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1
a 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
Inputs Outputs a 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 15 of 58
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,
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
cnt tc c1c
x y
2-bit upcounter mode
clk
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
19 of 58
Counter Example: Light Sequencer
Illuminate 8 lights from right to
left, one at a time, one per second
Use 3-bit up-counter to counter
from 0 to 7
Use 3x8 decoder to illuminate
appropriate light
Note: Used 3-bit counter with 3x
decoder
NOT an 8-bit counter – why not?
lights
cnt^ 3-bit up-counter
tc c2 c1 c
3 x8 dcd i2 i1 i
unused
clk
(1 Hz)
d7 d6 d5 d4 d3 d2 d1 d
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 20 of 58
Counter with Parallel Load
Up-counter that can be
loaded with external value
Designed using 2x1 mux –
ld input selects incremented
value or external value
Load the internal register
when loading external value
or when counting
ld
4-bit register
tc C
cnt
ld
1 4-bit 2x 10
L 4
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 21 of 58
Counter with Parallel Load
Useful to create pulses at specific
multiples of clock
Not just at N-bit counter’s natural
wrap-around of 2 N
Example: Pulse every 9 clock
cycles
Use 4-bit down-counter with
parallel load
Set parallel load input to 8 (1000)
Use terminal count to reload
When count reaches 0, next cycle loads 8.
Why load 8 and not 9? Because 0
is included in count sequence:
8, 7, 6, 5, 4, 3, 2, 1, 0 Æ 9 counts
cnt
ld
tc C
L
clk
4-bit down-counter
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
22 of 58
Counter Example: New Year’s Eve Countdown Display
Chapter 2 example previously used microprocessor to counter from 59 down to 0
in binary
Can use 8-bit (or 7- or 6-bit) down-counter instead, initially loaded with 59
i0^ d i i i i i
c c c c c c c c
tc
d d d
d d d d d d 6x dcd
8-bit down- counter
59 8 L
ld
cnt
clk (1 Hz)
reset
fireworks
Happy New Year
countdown
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 23 of 58
Counter Example: 1 Hz Pulse Generator from 60 Hz Clock
U.S. electricity standard uses 60 Hz
signal
Device may convert that to 1 Hz
signal to count seconds
Use 6-bit up-counter
Can count from 0 to 63
Create simple logic to detect 59
(for 60 counts)
Use to clear the counter back to 0 (or to load 0)
tc C
p
osc
(60 Hz)
(1 Hz)
clr
cnt 6-bit up counter
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 24 of 58
Timer
A type of counter used to measure time
If we know the counter’s clock frequency and the count, we know the time that’s been
counted
Example: Compute car’s speed using two sensors
First sensor (a) clears and starts timer
Second sensor (b) stops timer
Assuming clock of 1kHz, timer output represents time to travel between sensors.
Knowing the distance, we can compute speed
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
28 of 58
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
1stcolumn 1 1 0
3rd column 1 1 0
4th column
wo
a
a b FS
wi
wo s
b
s
a
a b FS
wi
wo s
b
s
a
a b FS
wi
wo s
b
s
a
a3 a2 a1 a0 b
wo s3s2s1s
wi
a b b2 b1 b FS
wi
wi
wo s
b
s (b) (c)
4-bit subtractor a
2ndcolumn 10
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 29 of 58
Subtractor Example: DIP-Switch Based Adding/Subtracting Calculator
Extend earlier calculator
example
Switch f indicates whether
want to add (f=0) or
subtract (f=1)
Use subtractor and 2x
mux
DIP switches 1 0
8-bit register CALC
LEDs
e
f
clk
ld
0 2 x 1 1
A B ci A Bwi
co S woS
8-bit adder 8-bit subtractor
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 30 of 58
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= Black: R=00000000, G=00000000, B= Other colors: values in between, e.g., R=00111111, G=00000000, B= would be a reddish purple
Good for computer monitors,
which mix red, green, and blue
lights to form all colors
- Printers use opposite color scheme
- Because inks absorb light
- Use complementary colors of RGB:
Cyan (absorbs red), reflects green and
blue, Magenta (absorbs green), and
Yellow (absorbs blue)
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
31 of 58
Subtractor Example: Color Space Converter – RGB to CMYK
Printers must quickly convert RGB
to CMY
C=255-R, M=255-G, Y=255-B
Use subtractors as shown
- - -
R G B
C M Y
R
GB
toCMY
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 32 of 58
Subtractor Example: Color Space Converter – RGB to CMYK
Try to save colored inks
Expensive
Imperfect – mixing C, M, Y doesn’t yield
good-looking black
Solution: Factor out the black or gray from
the color, print that part using black ink
e.g., CMY of (250,200,200)= (200,200,200) +
(200,200,200) is a dark gray – use black ink
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 33 of 58
Subtractor Example: Color Space Converter – RGB to CMYK
Call black part K
(200,200,200): K=
(Letter “B” already used for
blue)
Compute minimum of C, M, Y
values
Use MIN component designed
earlier, using comparator and
mux, to compute K
Output resulting K value, and
subtract K value from C, M, and
Y values
Ex: Input of (250,200,200)
yields output of (50,0,0,200)
RGB
toCM
K
C2 M2 Y2 K
MIN
MIN
C
C M Y
R G
RGB t o CMY
Y B
M Y
K
R 8 G 8 B 8
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
37 of 58
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!
Q: What is the two’s complement of 0101? A: 1010+1=
(check: 0101+1011=10000)
a
Q: What is the two’s complement of 0011? A: 1100+1=
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 38 of 58
Two’s Complement Subtractor Built with an Adder
Using two’s complement
A – B = A + (-B)
= A + (two’s complement of B)
= A + invert_bits(B) + 1
So build subtractor using adder by inverting
B’s bits, and setting carry in to 1 cin^1
A B
Adder
S
A B
N-bit
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 39 of 58
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 Frank Vahid
ECE 474a/575a Susan Lysecky
40 of 58
Adder/Subtractor Example: Calculator
Previous calculator used separate adder and subtractor
Improve by using adder/subtractor, and two’s complement numbers
8-bit register
sub8-bit adder/subtractor
CALC
LEDs
S
A B
ld
8 8
8-bitregister CALC
ld
8 8 0 0
8
8
8
8 8 2x1 mux 0 1
A B ci A B wi co S woS
8-bit adder 8-bit subtractor
DIP switches 1 0
DIP switches 1 0
e
f
clk
e
f
clk
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 41 of 58
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 >
0111+0001 = 1000 WRONG answer, 1000 in two’s complement is -8, not +
Adder/subtractor should indicate when overflow has occurred, so result can be discarded
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 42 of 58
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
overflow
(a)
overflow
(b)
no overflow
(c)
If the numbers’ sign bits have the same value, which
differs from the result’s sign bit, overflow has occurred.
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
46 of 58
ALU
More efficient design uses ALU
ALU design not just separate components multiplexed (same problem as previous slide!),
Instead, ALU design uses single adder, plus logic in front of adder’s A and B inputs
Logic in front is called an arithmetic-logic extender
Extender modifies the A and B inputs such that desired operation will appear at output of
the adder
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 47 of 58
Arithmetic-Logic Extender in Front of ALU
xyz=000: Want S=A+B – just pass a to ia, b to ib, and set cin= xyz=001: Want S=A-B – pass a to ia, b’ to ib, and set cin= xyz=010: Want S=A+1 – pass a to ia, set ib=0, and set cin= xyz=011: Want S=A – pass a to ia, set ib=0, and set cin= xyz=1000: Want S=A AND B – set ia=a*b, b=0, and cin= 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 Frank Vahid
ECE 474a/575a Susan Lysecky 48 of 58
ALU Example: Multifunction Calculator
Design using ALU is elegant and efficient
No mass of wires
No big waste of power
8-bit register
ALU S
CALC
e
z
y x
clk
ld
z
y x
8
8
8
8 A A
B B
DIP switches 1 0
1 0
LEDs
1 0
8-bit register
8-bit 8x1 mux
CALC
e
z
y x
clk
Id
s s s 0 1 2 3 4 5 6 7
8 8
8 (^8 )
8
8
(^8 8 )
8 8
A B
A lot of wi res.
Wasted power
DIP switches 1 0
1 0
LEDs
1 0
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
49 of 58
omthecar's
ompu
er
otheab ordispl
C
d
d
d
d e
i
i
i
i
i
a
a
load
i
2 ⋅^4
Frcen tr al
c
t
d D
x y
s1 s
8-bit 4 × 1
T
ov e
mi rr
ay
load
load
load
load
reg
reg
reg
reg
T
A
I
M
Register Files
MxN register file component
provides efficient access to M
N-bit-wide registers
If we have many registers but
only need access one or two at
a time, a register file is more
efficient
Ex: Above-mirror display
(earlier example), but this time
having 16 32-bit registers
Too many wires, and big mux is too slow
Fr
omthecar
?^ s
ce
nt ral
c
tompu
er
T
otheab
ov e-
mi rr
ordispl
ay
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 50 of 58
omthecar's
ompu
er
otheab ordispl
Register Files
MxN register file component
provides efficient access to M
N-bit-wide registers
If we have many registers but
only need access one or two at
a time, a register file is more
efficient
Ex: Above-mirror display
(earlier example), but this time
having 16 32-bit registers
Too many wires, and big mux is too slow
Fr
omthecar
?^ s
ce
nt ral
c
tompu
er
T
otheab
o ve
mi rr
ordispl
ay
C
d
d e
i
i load
i3-i
d D
s3-s
32-bit 16 x 1
load
load
reg
reg
congestion
too much fanout
huge mux
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky 51 of 58
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
W_data W_addr
W_en
R_data R_addr
R_en 16 × 32 register file
a
a
Digital Design Copyright © 2006 Frank Vahid
ECE 474a/575a Susan Lysecky
55 of 58
Chapter Summary
Need datapath components to store and operate on multibit data
Also known as register-transfer-level (RTL) components
Components introduced
Registers
Shifters
Adders
Comparators
Counters
Multipliers
Subtractors
Arithmetic-Logic Units
Register Files
Next, we’ll combine knowledge of combinational logic design, sequential logic
design, and datapath components, to build digital circuits that can perform general
and powerful computations