Title: Arithmetic Circuits
1Arithmetic Circuits
2Administrative
- May have messed up scoring of HW1
- Because I went for 100 total and didnt use the
25 point scale on web page - Look at yours and send it back if I made mistake.
3Iterative Circuit
- Like a hierachy, except functional blocks per bit
4Adders
- Great example of this type of design
- Design 1-bit circuit, then expand
- Look at
- Half adder 2-bit adder, no carry in
- Inputs are bits to be added
- Outputs result and possible carry
- Full adder includes carry in, really a 3-bit
adder
5Half Adder
6Full Adder
- Three inputs. Third is Cin
- Two outputs sum and carry
7K Map for S
8K Map for C
9Two Half Adders (and an OR)
10Ripple-Carry Adder
- Straightforward connect full adders
- Carry-out to carry-in chain
- C0 in case this is part of larger chain, maybe
just set to zero
11Hierarchical 4-Bit Adder
- We can easily use hierarchy here
- Design half adder
- Use in full adder
- Use full adder in 4-bit adder
- Verilog code from textbook
- First part of lab
12Verilog Half Adder
- module half_adder_v(x, y, s, c)
- input x, y
- output s, c
- assign s x y
- assign c x y
- endmodule
13Verilog Full Adder
- module full_adder_v(x, y, z, s, c)
- input x, y, z
- output s, c
- half_adder_v HA1(x, y, hs, hc),
- HA2(hs, z, s, tc)
- assign c tc hc
- endmodule
14Four-Bit Adder
- module adder_4_v(B, A, C0, S, C4)
- input30 B, A
- input C0
- output30 S
- output C4
-
- wire31 C
- full_adder_v Bit0(B0, A0, C0, S0, C1),
- Bit1(B1, A1, C1, S1,
C2), - Bit2(B2, A2, C2, S2,
C3), - Bit3(B3, A3, C3, S3, C4)
- endmodule
Look at connections between adders
15Behavioral Verilog
- // 4-bit Adder Behavioral Verilog
- module adder_4_b_v(A, B, C0, S, C4)
- input30 A, B
- input C0
- output30 S
- output C4
- assign C4, S A B C0
- endmodule
Addition (unsigned)
Concatenation operation
16Whats Problem with Design?
- Delay
- Approx how much?
- Imagine a 64-bit adder
- Look at carry chain
17Post Map Delay
18Post Place and Route
- Odd delays caused by placement
19Carry Lookahead Adder
- Note that add itself just 2 level
- Idea is to separate carry from adder function
- Then make carry approx 2-level all way across
larger adder
20Four-bit Ripple Carry
Reference
Adder function separated from carry
Notice adder has A, B, C in and S out, as well as
G,P out.
21Propagate
- The P signal is called propagate
- P A ? B
- Means to propagate incoming carry
22What Does This Mean?
- No Carry Here
- So the propagate signal indicates that condition
of incoming should pass on
23Generate
- The G is generate
- Its G AB, so new carry created
- So its ORed with incoming carry
24Said Differently
- If A ? B and theres incoming carry, carry will
be propagated - And S will be 0, of course
- If AB, then will create carry
- Incoming will determine whether S is 0 or 1
25Ripple Carry Delay 8 Gates
26Turn Into Two Gate Delays
- Design changed from deep (in delay) to wide
27C1 Just Like Ripple Carry
28C2 Circuit Two Levels
G from before and P to pass on
This checks two propagates and a carry in
29C3 Circuit Two Levels
Generate from level 0 and two propagates
G from before and P to pass on
This checks three propagates and a carry in
30What Happens as Scale Up?
- Can I realistically make 64-bit adder like this?
- Have to AND 63 propagates and Cin!
- Compromise
- Hierarchical design
- More levels of gates
31Making 4-Bit Adder Module
- Create propagate and generate signals for whole
module
32Group Propagate
- Make propagate of whole 4-bit block
- P0-3 P3P2P1P0
33Group Generate
- Does G created upstream pass on because of string
of Ps (also G3)? - Indicates carry generated in block
34Hierarchical Carry
A
B
4-bit adder
S
G
P
Cin
C4
C8
Look Ahead
C0
- Left lookahead block is exercise for you
35Practical Matters
- FPGAs like ours have limited fan-in
- Instead they have special circuits to make adders
- So dont expect to see same results as theory
would suggest
36Binary Subtraction
- Use unsigned subtraction to motivate use of
complemented representation - Imagine a subtractor circuit (next)
37One-bit Subtractor
- Inputs Borrow in, minuend and subtrahend
- Review subtrahend is subtracted from minuend
- Outputs Difference, borrow out
- Lets make truth table
- See how it expands
- Next slide
38Example
If no borrow, then result is non-negative
(minuend gt subtrahend).
Since there is borrow, result must be
negative. The result must be corrected to a
negative number. Next slide.
39Correcting Result
- What, mathematically, does it mean to borrow?
- If borrowing at digit i-1 you are adding 2i
- Next Slide
40Correcting Result 2
- If M is minuend and N subtrahend of numbers
length n, difference was - 2n M N
- What we want is magnitude of N-M with minus sign
in front - Can get by subtracting previous result from 2n
- N - M 2n (M N 2n)
This is called 2s complement
41Put Another Way
- This is numerically equivalent to how we do
subtraction in our heads - Decide which is greater
- Swap if necessary
- Subtract
- Could build a circuit this way, but expensive
42Algorithm
- Subtract N from M
- If no borrow, then M ? N and result is OK
- Otherwise, N gt M so result must be subtracted
from 2n and minus sign prepended
43Pretty Expensive Hardware!
44Designs Arent Like This
- Thats why people use complemented interpretation
for signed numbers - 2s complement
- 1s complement
451s Complement
- Given binary number N with n digits
- 1s complement defined as
- (2n 1) - N
- Note that (2n 1) is number with n digits, all
of them 1 - For n 4, (2n 1) 1111
46Example
- Notice that 1s complement is complement of each
bit
472s Complement
- Given binary number N with n digits
- 2s complement defined as
- 2n N for N ? 0
- 0 for N 0
- Exception is so result will always have n bits
- 2s complement is just a 1 added to 1s complement
48Important Property
- Complement of a complement generates original
number - NOTE We havent talked about negative numbers
yet. Still looking at unsigned - Lets look at new design for subtractor
49New Algorithm for M-N
- Add 2s complement of N to M
- This is M (2n N) M N 2n
- If M ? N, will generate carry (why?)
- Discard carry
- Result is positive M - N
- If M lt N, no end carry (why?)
- Take 2s complement of result
- Place minus sign in front
50Example
- X 101_0100 minus Y 100_0011
51Example 2
- Y 100_0011 minus X 101_0100
- No end carry
- Answer - (2s complement of Sum)
- - 0010001
We said numbers are unsigned. What does this
mean?
52Adder-Subtractor
- Need only adder and complementer for input to
subtract - Need selective complementer to make negative
output back from 2s complement - Or go through adder again. See next slide
53Design
S low for add, high for subtract
Inverts each bit of B if S is 1
Adds 1 to make 2s complement
- Output is 2s complement if B gt A
54Signed Binary
- First review signed representations
- Signed magnitude
- Left bit is sign, 0 positive, 1 negative
- Other bits are number
- 2s complement
- 1s complement
55Example in 8-bit byte
- Represent -9 in different ways
- Signed magnitude 10001001
- 1s Complement 11110110
- 2s Complement 11110111
56Observations
- 1s C and Signed Mag have two zeros
- 2s C has more negative than positive
- All negative numbers have 1 in high-order
57Advantages/Disadvantages
- Signed magnitude has problem that we need to
correct after subtraction - Ones complement has a positive and negative zero
- Twos complement is most popular
- Arithmetic operations easy
58Twos Complement
- Addition easy on any combination of positive and
negative numbers - To subtract
- Take 2s complement of subtrahend
- Add
- This performs A ( -B), same as A B
59Examples from Book
- Addition
- 6 13
- -6 13
- 6 (- 13)
- (-6) (-13)
- Subtraction
- -6 - (-13)
- 6 - (- 13)
- Try some of these
60Overflow
- Two cases of overflow for addition of signed
numbers - Two large positive numbers overflow into sign bit
- Not enough room for result
- Two large negative numbers added
- Same not enough bits
- Carry out can be OK
61Examples
- 4-bit signed numbers
- 7 7
- 7 7
- Generates carry but result OK
- -7 -7
- 4 4
- Generates no Cout, but overflowed
62Overflow Detection
- Condition is that either Cn-1 or Cn is high, but
not both
63Multiplier
- Multiply by doing single-bit multiplies and
shifts - Look at combinational circuit to do this
64Combinational Multiplier
AND computes A0 B0
Half adder computes sum. Will need FA for larger
multiplier.
65Larger Multiplier
66Sequential Multiply
- Imagine doing over time rather than in parallel
- Bitwise multiply
- Shift
- Add
- If we have time later in semester well look at
fancier multipliers
67Simple Testbench (Friday lab)
- initial
- begin
- A 4'd0 B 4'd0 C0 1'b0
- 50 A 4'd3 B 4'd4
- 50 A 4'd2 B 4'd5
- 50 A 4'd9 B 4'd9
- 50 A 4'd10 B 4'd15
- 50 A 4'd10 B 4'd5 C0 1'b1
- 50 A 4'd0 B 4'd0 C0 1'b0
- 50 A 4'b1111 B 4'b1111 C0 1'b1
- end
This is impractical, except for initial test.
Use do loop.
68Lab
- Write your lookahead adder in Verilog before lab
- Give you more time to debug any errors
- Suggest you spend some time experimenting with
testbenches - Write some programs to test your designs
different ways
69Homework
- Homework 2 is posted
- Due next Thursday
70Today
- Adders
- Ripple carry
- Carry lookahead
- Subtracting unsigned numbers
- New design for adder-subtractor
- Signed numbers
- Signed addition/subtraction
- Multiplication
71Next
- Sequential Circuits
- Latches
- Flip-Flops
- Start reading Chapter 6