ECE 274 - Digital Logic 4.8 Lecture 12

„ Can build subtractor as we built carry-ripple

„ Mimic subtraction by hand „ Lecture 12 – Components „ Compute borrows from columns on left

„ Use full-subtractor component: „ „ wi is borrow by column on right, wo borrow from column on left „ Two’s Complement 1stcolumn 2ndcolumn 3rd column 4th column 0 0 1 10 0 1 0 „ Overflow 1100 1 11010 11010 1100 - 0111 - 0111 - 0111 - 0111 „ ALUs 1 1 1 0 1 1 0 0 1 1 „ Register Files a3 b3 a2 b2 a1 b1 a0 b0 wi

abwi abwi abwi abwi a3 a2 a1 a0 b3 b2 b1 b0

FS FS FS FS 4- subtractor wi a wo s wo s wo s wo s wo s3s2s1s0

wo s3 s2 s1 s0 (b) (c)

1 2

Subtractor Example: DIP- Subtractor Example: Based Adding/Subtracting Calculator Color Space Converter – RGB to CMYK

„ Extend earlier DIP „ Color

calculator example 1 „ Often represented as weights 0 of three colors: red, green, „ Switch f indicates and blue (RGB) whether want to 8 8 8 8 „ Perhaps 8 each, so add (f=0) or 00 specific color is 24 bits AABBci wi subtract (f=1) „ White: R=11111111, 8-bit adder 8-bit subtractor G=11111111, B=11111111 „ Use subtractor and coSS wo „ Black: R=00000000, 2x1 mux G=00000000, B=00000000 1 f 8 8 012x1 „ Other colors: values in 0 8 between, e.g., e R=00111111, G=00000000, • Printers use opposite color scheme ld 8-bit register B=00001111 would be a – Because inks absorb light clk reddish purple CALC – Use complementary colors of RGB: 8 „ Good for computer monitors, Cyan (absorbs red), reflects green which mix red, green, and and blue, Magenta (absorbs green), LEDs blue lights to form all colors and Yellow (absorbs blue)

3 4

Subtractor Example: Subtractor Example: Color Space Converter – RGB to CMYK Color Space Converter – RGB to CMYK

RGB „ Printers must quickly 255 255 255 „ Try to save colored inks 8 8 8 convert RGB to CMY 8 8 8 „ Expensive

„ C=255-R, M=255-G, Y=255-B „ Imperfect – mixing C, M, Y doesn’t

o CMY

t „ Use subtractors as shown --- yield good-looking black

GB

R „ Solution: Factor out the black or 888 gray from the color, print that CMY part using black ink

„ e.g., CMY of (250,200,200)= (200,200,200) + (50,0,0).

„ (200,200,200) is a dark gray – use black ink

5 6 Subtractor Example: Representing Negative Numbers: Color Space Converter – RGB to CMYK Two’s Complement

„ Call black part K „ Negative numbers common RGBK 88 8 „ (200,200,200): K=200 Y „ How represent in binary? „ (Letter “B” already used for blue)

o CM RGB t „ Signed-magnitude „ Compute minimum of C, RGB to CMY GB CM YR M, Y values „ Use leftmost bit for sign bit

„ Use MIN component „ 8 8 8 8 So -5 would be: designed earlier, using C MY 1101 using four bits comparator and mux, to MIN 10000101 using eight bits compute K 8 „ Better way: Two’s complement „ Output resulting K value, and subtract K value MIN „ Big advantage: Allows us to perform subtraction using 8 from C, M, and Y values K addition „ Ex: Input of „ Thus, only need adder component, no need for separate (250,200,200) yields subtractor component! output of (50,0,0,200) --- 888 8 C2 M2 Y2 K

7 8

Ten’s Complement Ten’s Complement

1 9 „ Nice feature of ten’s complement

2 8 „ Instead of subtracting a number, adding its complement results in „ Before introducing two’s complement, let’s answer exactly 10 too much 3 7 consider ten’s complement „ So just drop the 1 – results in subtracting using addition only 4 6 complements „ But, be aware that computers DO NOT USE TEN’S 1 9 10 5 5 COMPLEMENT. Introduced for intuition only. 2 8 46 6 4 „ Complements for each base ten number shown to 3 7 7 7 3 right – Complement is the number that when 4 6 01020 added results in 10 8 2 5 5 Ð4 +6 3 - 13 9 1 6 4 13 7 3 3 8 2 7Ð4=3- 7+6=13 3 9 1 Adding the complement results in an answer exactly 10 too much – dropping the tens column gives 010theright answer.

9 10

Two’s Complement is Easy to Compute: Two’s Complement Subtractor Built Just Invert Bits and Add 1 with an Adder

„ Hold on! „ Using two’s complement A B „ Sure, adding the ten’s complement achieves subtraction using addition A – B = A + (-B) only = A + (two’s complement of B) „ But don’t we have to perform subtraction to have determined the N-bit complement in the first place? e.g., we only know that the complement = A + invert_bits(B) + 1 of 4 is 6 by subtracting 10-4=6 in the first place. „ So build subtractor using A B 1 „ True – but in binary, it turns out that the two’s complement can be Adder cin computed easily adder by inverting B’s bits, „ Two’s complement of 011 is 101, because 011 + 101 is 1000 and setting carry in to 1

„ Could compute complement of 011 as 1000 – 011 = 101 S

„ 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=1011 a (check: 0101+1011=10000) Q: What is the two’s complement of 0011? A: 1100+1=1101

11 12 Adder/Subtractor Example: Adder/Subtractor Calculator

„ Previous calculator DIP switches „ Adder/subtractor: control used separate 1 input determines whether adder and 0 add or subtract subtractor 88 1 f AB „ Can use 2x1 mux – sub input sub 8-bit adder/subtractor „ Improve by using 0 passes either B or inverted B S adder/subtractor, 8 e „ Alternatively, can use XOR ld and two’s 8-bit register gates – if sub input is 0, B’s clk complement CALC bits pass through; if sub 8 DIP switches input is 1, XORs invert B’s numbers 1 0 LEDs bits 8 8 8 8 00 AABBci wi 8-bit adder 8-bit subtractor coSS wo

1 8 8 f 012x1 0 8 e ld 8-bit register clk CALC 8

LEDs 13 14

Design Challenge Not Really a Quiz Overflow

„ Determine the two’s complement representation for the „ Sometimes result can’t be represented with given number following decimal numbers (assume we are using 5-bit of bits

binary numbers): „ Either too large magnitude of positive or negative

„ -1 „ e.g., 4-bit two’s complement addition of 0111+0001 (7+1=8). But

„ -11 4-bit two’s complement can’t represent number >7 „ 0111+0001 = 1000 WRONG answer, 1000 in two’s complement is - „ -15 8, not +8

„ Adder/subtractor should indicate when overflow has occurred, so „ Determine the two’s complement representation for same result can be discarded decimal numbers assuming we are using 6-bit binary numbers.

15 16

Detecting Overflow: Method 1 Detecting Overflow: Method 2

„ Assuming 4-bit two’s complement numbers, can detect overflow by „ Even simpler method: Detect difference between carry-in to sign bit detecting when the two numbers’ sign bits are the same but are and carry-out from sign bit different from the result’s sign bit „ Yields simpler circuit: overflow = c3 xor c4 „ 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 1 11 0 00 0 00 0111 1111 1000 „ overflow = a3’b3’s3 + a3b3s3’

„ Include “overflow” output bit on adder/subtractor +0001+0100+1011 sign bits

0111 1111 1000 001 000 1 0111 1111 overflow overflow no overflow +0001+0100+1011 (a) (b) (c)

1000 0111 1111 If the carry into the sign bit column differs from the carry out of that column, overflow has occurred. overflow overflow no overflow (a) (b) (c) If the numbers’ sign bits have the same value, which differs from the result’s sign bit, overflow has occurred. 17 18 4.9 Multifunction Calculator without an Arithmetic-Logic Unit: ALU ALU

„ ALU: Component that „ Can build multifunction can perform any of calculator using separate various arithmetic components for each DIP switches (add, subtract, 1 operation, and muxes 0 increment, etc.) and 88 „ But too many wires, and logic (AND, OR, etc.) AB wasted power computing operations, based on Wasted all those operations when + Ð +1 AND OR XOR NOT power 8 control inputs 8 8 at any time you only use 8 8 8 8 „ Motivation: one of the results 8 A lot of wires 10 01234567 „ x Suppose want multi- s2 y 8-bit 8⋅ 1 function calculator that s1 z s0 not only adds and 8 e Id subtracts, but also 8-bit register clk increments, ANDs, ORs, CALC 8 XORs, etc. LEDs

19 20

Arithmetic-Logic Extender in Front ALU of 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

„ 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

21 22

ALU Example: Multifunction 4.10 Calculator Register Files

DIP swi tches 1 „ MxN 0

88 er 32 AB component provides Wasted C + Ð +1 AND OR XOR NOT er C a power t 8 8 8 8

s 8 ompu t

DIP switches efficient access to M N-? 8 8 8 8 d0 load o theab A lot of wi res. loadreg0 huge mux 10 01234567 d0 reg0 T x s2

ompu

y 8-bit 8 ⋅ 1 mi

or displ

s1 T

c 1 1 bit-wide registers omthe car 32 z s0 o theab

mi

T 8 r

al i0 e c i0 r Id r 4⋅ 16 8-bit reg ist er 0 0 ⋅ or displ t 2 4

omthe car's

clk r

r CALC „ al n If we have many 8 r r too much 8 F

e

t

c 32-bit r 8-bit 8 8 n fanout LEDs F d1 load o registers but only need e reg1 A x4×1 c 16 1 4 a0 v

a

e

o A B i0 y access one or two at a v

a i1 e

y

A B - x 8 „ x time, a register file is i3-i0i1 Design using ALU is y ALU a1 d d DD y more efficient d2 load reg2 I 8 elegant and efficient z z S 32 i2 8 „ Ex: Above-mirror display 8 „ No mass of wires congestion e (earlier example), but ld d3 load reg3 8-bit register d15e load reg15 M „ No big waste of power clk this time having 16 32- e load i15i3 s1 s0 8 CALC bit registers load 8 32 s3-s0 „ Too many wires, and xy LEDs big mux is too slow

23 24 Register File Register File Timing Diagram

„ „ cycle 1 cycle 2 cycle 3 cycle 4 cycle 5 cycle 6 Instead, want component that has one data input and one data output, Can write one clk and allows us to specify which internal register to write and which to register and 1234 5 6 read read one W_dat a 9 22 X X 177 555 32 32 register each W_data R_data a W_addr 3 1XX23 4 4 clock cycle W_addr R_addr W_en „ May be same R_ d at a Z ZZ9 Z 22 9 555 W_en R_en register 16×32 register file R_addr X XX331 R_en

0: ? 0: ? 0: ? 0: ? 0: ? 0: ? 0: ? 32 32 1: ? 1: ? 1: 22 1: 22 1: 22 1: 22 1: 22 W_data R_data 2: ? 2: ? 2: ? 2: ? 2: ? 2: 177 2: 177 2 2 W_addr R_addr 3: ? 3: 9 3: 9 3: 9 3: 9 3: 9 3: 555

W_en R_en 4x32 register file a

25 26

Register-File Example: Above-Mirror Display Datapath Component Summary

„ 16 32-bit registers that „ Need datapath components to store and operate on multibit data „ Also known as register-transfer-level (RTL) components can be written by car’s 32 C OLD design „ Components introduced computer, and a d0 load reg0 huge mux „ Registers 32 displayed „ Shifters

i0 mirror display 4⋅ 16 - e v o ab e h t To 32 „ Use 16x32 register file s 32too much „ Adders CD fanoutW_dat a R_ d at a 32-bit 4 16x1 „ Comparators „ Simple, elegant design WA 4 4 W_addr R_addr „ load Counters „ Register file hides i3-i0 Fr o m t h e car W_en R_ e n d D „ Multipliers central computer complexity internally 16⋅ 32 1 32 register file RA „ Subtractors „ And because only one congestion „ Arithmetic-Logic Units register needs to be d15 load reg15 „ Register Files e written and/or read at a i15 „ load Next, we’ll combine knowledge of design, time, internal design is 32 s3-s0 design, and datapath components, to build digital simple circuits that can perform general and powerful computations

27 28

Design Challenge

„ Design Challenge

„ Design a more accurate version of the Celsius to Fahrenheit converter. The new conversion circuit receives a digitized temperature in Celsius as a 16-bit C and outputs the temperature in Fahrenheit as a 16-bit output F. Our more accurate equation for calculating an approximate conversion from Celsius to Fahrenheit is: F = C*30/16 + 32.

„ Due:

„ Next Lecture (Wednesday, October 19)

„ Extra Credit (Homework)

„ 3 points

29