1 comp541 arithmetic circuits montek singh mar 20, 2007

52
1 COMP541 COMP541 Arithmetic Circuits Arithmetic Circuits Montek Singh Montek Singh Mar 20, 2007 Mar 20, 2007

Post on 21-Dec-2015

227 views

Category:

Documents


1 download

TRANSCRIPT

1

COMP541COMP541

Arithmetic CircuitsArithmetic Circuits

Montek SinghMontek Singh

Mar 20, 2007Mar 20, 2007

2

TopicsTopics Adder circuitsAdder circuits How to subtractHow to subtract

Why complemented representation works out so wellWhy complemented representation works out so well

OverflowOverflow

3

Iterative CircuitIterative Circuit Like a hierachy, except functional blocks per Like a hierachy, except functional blocks per

bitbit

4

AddersAdders Great example of this type of designGreat example of this type of design Design 1-bit circuit, then expandDesign 1-bit circuit, then expand Let’s look atLet’s look at

Half adder – 2-bit adder, no carry inHalf adder – 2-bit adder, no carry in Inputs are bits to be addedInputs are bits to be addedOutputs: result and possible carryOutputs: result and possible carry

Full adder – includes carry in, really a 3-bit adderFull adder – includes carry in, really a 3-bit adder

5

Half AdderHalf Adder S = X S = X Y Y C = XYC = XY

6

Full AdderFull Adder Three inputs. Third is CinThree inputs. Third is Cin Two outputs: sum and carryTwo outputs: sum and carry

7

Two Half Adders (and an OR)Two Half Adders (and an OR)

8

Ripple-Carry AdderRipple-Carry Adder

Straightforward – connect full addersStraightforward – connect full adders Carry-out to carry-in chainCarry-out to carry-in chain

CC00 in case this is part of larger chain, or just ‘0’ in case this is part of larger chain, or just ‘0’

9

Hierarchical 4-Bit AdderHierarchical 4-Bit Adder We can easily use hierarchy hereWe can easily use hierarchy here Design half adderDesign half adder Use in full adderUse in full adder Use full adder in 4-bit adderUse full adder in 4-bit adder

Verilog code in textbookVerilog code in textbook

10

Behavioral VerilogBehavioral Verilog// 4-bit Adder: Behavioral Verilog// 4-bit Adder: Behavioral Verilog

module adder_4_b_v(A, B, C0, S, C4);module adder_4_b_v(A, B, C0, S, C4);input[3:0] A, B;input[3:0] A, B;input C0;input C0;output[3:0] S;output[3:0] S;output C4;output C4;

assign {C4, S} = A + B + C0;assign {C4, S} = A + B + C0;endmoduleendmodule

Addition (unsigned)

Concatenation operation

11

What’s Problem with Design?What’s Problem with Design?

DelayDelay Approx how much?Approx how much?

Imagine a 64-bit adderImagine a 64-bit adder Look at carry chainLook at carry chain

12

Carry Lookahead AdderCarry Lookahead Adder Note that add itself just 2 levelNote that add itself just 2 level

Idea is to separate carry from adder functionIdea is to separate carry from adder function Then make carry approx 2-level all way across larger Then make carry approx 2-level all way across larger

adderadder

13

Four-bit Ripple CarryFour-bit Ripple Carry

Adder functionseparated fromcarry

Notice adder has A, B, C inand S out, as well as G,P out.

Reference

14

PropagatePropagate The P signal is called The P signal is called propagatepropagate P = A P = A B B Means to Means to propagate incoming carrypropagate incoming carry

15

What Does This Mean?What Does This Mean? No Carry HereNo Carry Here So the propagate signal indicates that So the propagate signal indicates that

condition of incoming should pass oncondition of incoming should pass on

16

GenerateGenerate The G is The G is generategenerate It’s G = AB, so It’s G = AB, so new carry creatednew carry created So it’s ORed with incoming carrySo it’s ORed with incoming carry

17

Said DifferentlySaid Differently If A If A B and there’s incoming carry, carry will B and there’s incoming carry, carry will

be propagatedbe propagated And S will be 0, of courseAnd S will be 0, of course

If AB, then will create carryIf AB, then will create carry Incoming will determine whether S is 0 or 1Incoming will determine whether S is 0 or 1

18

Ripple Carry Delay: 8 GatesRipple Carry Delay: 8 Gates

19

Turn Into Two Gate DelaysTurn Into Two Gate Delays

Design changed from deep (in delay) to wideDesign changed from deep (in delay) to wide

20

CC11 Just Like Ripple Carry Just Like Ripple Carry

21

CC22 Circuit Two Levels Circuit Two Levels

G from before and P to pass on This checks two propagates and a carry in

22

CC33 Circuit Two Levels Circuit Two Levels

G from before and P to pass onThis checks three propagates and a carry in

Generate from level 0 and two propagates

23

What Happens as Scale Up?What Happens as Scale Up?

Can I realistically make 64-bit adder like Can I realistically make 64-bit adder like this?this?

Have to AND 63 propagates and CHave to AND 63 propagates and Cinin!! CompromiseCompromise

Hierarchical designHierarchical design More levels of gatesMore levels of gates

24

Making 4-Bit Adder ModuleMaking 4-Bit Adder Module

Create Create propagatepropagate and and generategenerate signals for signals for whole modulewhole module

25

Group PropagateGroup Propagate

Make Make propagatepropagate of whole 4-bit block of whole 4-bit block PP0-30-3 = P = P33PP22PP11PP00

26

Group GenerateGroup Generate

Does G created upstream pass on because of string of Does G created upstream pass on because of string of Ps (also GPs (also G33)?)?

Indicates carry generated in blockIndicates carry generated in block

27

Hierarchical CarryHierarchical Carry

4-bit adder

A B

S G P Cin

4-bit adder

A B

S G P Cin

C0Look Ahead

C8 C4

Left lookahead block is exercise for youLeft lookahead block is exercise for you

28

Practical MattersPractical Matters FPGAs like ours have limited inputs per blockFPGAs like ours have limited inputs per block Instead they have special circuits to make Instead they have special circuits to make

addersadders So don’t expect to see same results as theory So don’t expect to see same results as theory

would suggestwould suggest

29

On to SubtractionOn to Subtraction First, look at unsigned numbersFirst, look at unsigned numbers

Motivates why we typically use complemented Motivates why we typically use complemented representationrepresentation

Then look at 2s complementThen look at 2s complement

Imagine a subtractor circuit (next)Imagine a subtractor circuit (next)

30

One-bit SubtractorOne-bit Subtractor Inputs: Borrow in, Inputs: Borrow in, minuendminuend and and subtrahendsubtrahend

Review: Review: subtrahend issubtrahend is subtracted from subtracted from minuendminuend

Outputs: Difference, borrow outOutputs: Difference, borrow out Could use like adderCould use like adder One per bitOne per bit

1-bit subM S

BoutD

Bin

31

ExampleExample

BorrowBorrow 11 00 00

MinuendMinuend 00 11 11

Subtrahend Subtrahend 11 11 00

DifferenceDifference 11 00 11

Correct Diff -Correct Diff - 00 11 11

If no borrow, then result is non-negative (minuend >= subtrahend).

Since there is borrow, result must be negative.

The magnitude must be corrected.

Next slide.

32

Correcting ResultCorrecting Result

What, mathematically, does it mean to What, mathematically, does it mean to borrow?borrow?

If borrowing at digit If borrowing at digit i-1i-1 you are adding you are adding 22ii

Next SlideNext Slide

33

Correcting Result 2Correcting Result 2 If M is minuend and N subtrahend of numbers If M is minuend and N subtrahend of numbers

length length nn, difference was, difference was 22nn + M – N + M – N

What we want is What we want is magnitudemagnitude of of N-MN-M (with (with minus sign in front)minus sign in front)

Can get by subtracting previous result from 2Can get by subtracting previous result from 2nn

N - M = 2N - M = 2nn – – (M – N + 2(M – N + 2nn))

This is called2’s complement

34

Put Another WayPut Another Way This is equivalent to how we do subtraction in This is equivalent to how we do subtraction in

our headsour heads Decide which is greaterDecide which is greater Swap if necessarySwap if necessary SubtractSubtract

Could build a circuit this way…Could build a circuit this way… Or just look at borrow bitOr just look at borrow bit

35

AlgorithmAlgorithm1.1. Subtract N from MSubtract N from M

2.2. If no borrow, then M If no borrow, then M N and result is OK N and result is OK

3.3. Otherwise, N > M so result must be Otherwise, N > M so result must be subtracted from 2subtracted from 2nn (and minus sign (and minus sign prepended)prepended)

36

Pretty Expensive Hardware!Pretty Expensive Hardware!

37

That Complex Design not UsedThat Complex Design not Used That’s why people use That’s why people use complementedcomplemented

interpretation for numbersinterpretation for numbers 2’s complement2’s complement 1’s complement1’s complement

38

1’s Complement1’s Complement

Given: binary number N with n digitsGiven: binary number N with n digits 1’s complement 1’s complement defined asdefined as

(2(2nn – 1) - N – 1) - N

22nn - 1 - 1 11 11 11 11 11 11 11

- N- N 11 00 11 11 00 00 11

1’s Compl.1’s Compl. 00 11 00 00 11 11 00

39

2’s Complement2’s Complement

Given: binary number N with n digitsGiven: binary number N with n digits 2’s complement 2’s complement defined asdefined as

22nn – N for N – N for N 0 0

0 for N = 00 for N = 0 Exception is so result will always have n bitsException is so result will always have n bits

2’s complement is just a 1 added to 1’s 2’s complement is just a 1 added to 1’s complementcomplement

40

Important PropertyImportant Property Complement of a complement generates Complement of a complement generates

original numberoriginal number

NOTE: We haven’t talked about negative NOTE: We haven’t talked about negative numbers yet. Still looking at unsignednumbers yet. Still looking at unsigned

Let’s look at new design for subtractorLet’s look at new design for subtractor

41

New Algorithm for M-NNew Algorithm for M-N

1.1. Add 2’s complement of N to MAdd 2’s complement of N to M This is M + (2This is M + (2nn – N) = M – N + 2 – N) = M – N + 2nn

2.2. If M If M N, will generate carry (why?) N, will generate carry (why?)• Discard carryDiscard carry• Result is positive M - NResult is positive M - N

3.3. If M < N, no end carry (why?)If M < N, no end carry (why?)• Take 2’s complement of resultTake 2’s complement of result• Place minus sign in frontPlace minus sign in front

42

ExampleExample X = 101_0100 minus Y = 100_0011X = 101_0100 minus Y = 100_0011

XX 11 00 11 00 11 00 00

+ 2’s comp Y+ 2’s comp Y 00 11 11 11 11 00 11

SumSum 11 00 00 11 00 00 00 11

43

Example 2Example 2 Y = 100_0011 minus X = 101_0100Y = 100_0011 minus X = 101_0100

No end carryNo end carry Answer: - (2’s complement of Sum)Answer: - (2’s complement of Sum) - 0010001- 0010001

YY 11 00 00 00 00 11 11

+ 2’s comp X+ 2’s comp X 00 11 00 11 11 00 00

SumSum 11 11 00 11 11 11 11

44

Adder-SubtractorAdder-Subtractor Need only adder and complementer for input Need only adder and complementer for input

to subtractto subtract

Need selective complementer to make Need selective complementer to make negative output back from 2’s complementnegative output back from 2’s complement Or go through adder again. See next slideOr go through adder again. See next slide

45

Design of Adder/SubtractorDesign of Adder/Subtractor

Output is 2’s complement if B > AOutput is 2’s complement if B > A

Inverts each bit of B if S is 1

Adds 1 to make 2’s complement

S low for add,high for subtract

46

Signed ArithmeticSigned Arithmetic Review characteristics of signed Review characteristics of signed

representationsrepresentations

Signed magnitudeSigned magnitude Left bit is sign, 0 positive, 1 negativeLeft bit is sign, 0 positive, 1 negative Other bits are numberOther bits are number

2’s complement2’s complement 1’s complement1’s complement

47

ObservationsObservations

1’s C and Signed 1’s C and Signed Mag have two Mag have two zeroszeros

2’s C has more 2’s C has more negative than negative than positivepositive

All negative All negative numbers have 1 in numbers have 1 in high-orderhigh-order

48

Advantages/DisadvantagesAdvantages/Disadvantages Signed magnitude has problem that we need Signed magnitude has problem that we need

to to correctcorrect after subtraction after subtraction One’s complement has a positive and negative One’s complement has a positive and negative

zerozero Two’s complement is most popularTwo’s complement is most popular

Arithmetic operations easyArithmetic operations easy

49

Conclusion: 2’s ComplementConclusion: 2’s Complement Addition easy on any combination of positive Addition easy on any combination of positive

and negative numbersand negative numbers To subtractTo subtract

Take 2’s complement of subtrahendTake 2’s complement of subtrahend AddAdd This performs A + ( -B), same as A – BThis performs A + ( -B), same as A – B

50

OverflowOverflow Two cases of overflow for addition of signed Two cases of overflow for addition of signed

numbersnumbers Two large positive numbers overflow into sign bitTwo large positive numbers overflow into sign bit

Not enough room for resultNot enough room for result Two large negative numbers addedTwo large negative numbers added

Same – not enough bitsSame – not enough bits

Carry out can be OKCarry out can be OK

51

ExamplesExamples 4-bit signed numbers4-bit signed numbers 7 + 77 + 7 7 – 77 – 7

Generates carry but result OKGenerates carry but result OK

-7 -7-7 -7 4 + 44 + 4

Generates no CGenerates no Coutout, but overflowed, but overflowed

52

Overflow DetectionOverflow Detection Condition is that either CCondition is that either Cn-1n-1 or C or Cnn is high, but is high, but

not bothnot both