ss10 Adders
ss10 Adders
[ Background image: Logic gate diagram for a portion of the PDP 11 data path; The PDP 11 was a famous “minicomputer” from the 1970s.
Copyright © DEC. Source: http://www.psych.usyd.edu.au/pdp-11/Images/11_20_dp.gif ]
Learning Objectives
By the end of this slide set you should be able to:
1. Explain how addition is performed using
combinational logic
2. Represent negative numbers in 2’s complement
3. Build an efficient adder/subtractor unit
110_
10_
0_ 111_
11_
1_
6 110 5 101
+ 3 011 +7 111
9 1001
001
01
1 12 1100
100
00
0
carry-in carry-out
a
c
s // half adder
b module HalfAdder(a,b,c,s);
input a,b ;
output c,s ; // carry and sum
wire s = a ^ b ;
wire c = a & b ;
endmodule
Slide Set #10 6
Text: Dally §10.2
Full Adder
• Counts the number of “1” bits on its input
• For a full-adder, 3 inputs, output can be 0, 1, 2, or 3
c[i] b[i] a[i] count c[i+1] s[i]
0 0 0 0 0 0
0 0 1 1 0 1
0 1 0 1 0 1
0 1 1 2 1 0
1 0 0 1 0 1
1 0 1 2 1 0
1 1 0 2 1 0
1 1 1 3 1 1
a
cout
cin
FA
s
a (1) (2)
(2) (2) (4)
a c a c
(2)
HA HA
b (1) (1)
(1) (2) (2)
b s a c b s
HA
cin (1) (1)
(1)
b s
(1) (1)
a[1] a
cout
s[1]
FA s
b[1] b
cin
c[1]
a[0] a
cout
s[0]
FA s
b[0] b
cin
cin
c[i] p
a[i] g
p[i] s[i]
b[i]
c[i+1]
g[i]
c[i+1]
input cin ;
output [n-1:0] s ;
output cout ;
g[i]
c[i+1]
wire [n-1:0] p = a ^ b ;
wire [n-1:0] g = a & b ;
wire [n:0] c = {g | (p & c[n-1:0]), cin} ;
wire [n-1:0] s = p ^ c[n-1:0] ;
wire cout = c[n] ;
endmodule
7 = 0111
-7 = 1000+1 = 1001
Interesting case: -8
5-bits:
8 = 01000
-8 = 10111 + 1 = 11000
Example, n=4:
-810 = 10002 = (-23 • 1) + (0 • 20) + (0 • 21) + (0 • 20)
Slide Set #10 21
Text: Dally §10.3
Impact of <signed> (s) in
Verilog Bit Literals…
wire [7:0] a;
assign a = <val>;
<val>
2’b10 // a == 8’b0000_0010 (zero extended)
2’b01 // a == 8’b0000_0001
Note: “Does not work” with “x” – use {n{x}} to get n x’s.
Slide Set #10 22
2’s Complement Adder/Subtractor
a a
cout
b n out
Adder s
n b n
n cin
sub
This works fine, but it turns out there is a more clever implementation
that uses fewer gates...
Slide Set #10 25
Text: Dally §10.3
Alternative implementation
Let’s consider inputs and outputs of full-adder used to compute sign bit:
as bs cis ss cos ovf comment
0 0 0 0 0 0 Both inputs positive, no overflow, carry in = carry out
0 0 1 1 0 1 Both inputs positive, overflow, carry in != carry out
0 1 0 1 0 0 Inputs signs different, no overflow, carry in = carry out
0 1 1 0 1 0 Inputs signs different, no overflow, carry in = carry out
1 1 0 0 1 1 Both inputs negative, overflow, carry in != carry out
1 1 1 1 1 0 Both inputs negative, no overflow, carry in = carry out
a[n-1] a
cout
b[n-1] out[n-1]
Adder s
b
cin
a[n-2:0] a
cout
sub
a[n-2:0] a
cout
sub