Upload
others
View
21
Download
2
Embed Size (px)
Citation preview
Building an ALU (Part 2):
Lab 2 Part 1 due SundayToday’s handout will have most of your
circuit specifications!!!!
Our efforts to improve the course, have been helping studentsMore As and Bs (up 5% points)Fewer Ds and Fs (down 2% points)Reported time spent on the course has decreased
State – the central concept of computing
StateStorage
Computer can do 2 things1) Store state2) Manipulate state (Combine arithmetic and logical operations into one unit)
State Manipulations (ALU)
Today’s lectureWe’ll finish the 32‐bit ALU today! 32‐bit ALU specification
Complete 1‐bit ALU Assembling them to make 32‐bit ALUHandling flags: zero, negative, overflow
You will be building this for Lab 3
module alu32(out, overflow, zero, negative, A, B, control);output[31:0] out;output overflow, zero, negative;input [31:0] A, B;input [2:0] control;
Did overflow occur?Is the output equal to zero?Is the output negative?
control out=
0 A + B (unsigned)
1 undefined
2 A + B (signed)
3 A – B (signed)
4 A AND B
5 A OR B
6 A NOR B
7 A XOR B
A
B
out
32
32
32
3
OverflowZeroNegative
A specification for a 32-bit ALU
𝑜𝑣𝑒𝑟𝑓𝑙𝑜𝑤,𝑛𝑒𝑔𝑎𝑡𝑖𝑣𝑒 =a 0,0b 0, 1c 1, 0d 1, 1
control out=
0 A + B (unsigned)
1 undefined
2 A + B (signed)
3 A – B (signed)
4 A AND B
5 A OR B
6 A NOR B
7 A XOR B
For the following values of A, B, and controldetermine the values of the overflow and negative flags. control = 010A = 0xE106 5830 B = 0x4052 9453
A
B
out
32
32
32
3
OverflowZeroNegative
Use a modular 1-bit ALU to build 32-bit ALU
Previously we showed 1‐bit adder/subtractor, 1‐bit logic unit Time to put them together.
module alu1(out, carryout, A, B, carryin, control);output out, carryout;input A, B, carryin;input [2:0] control;
A B control3
carryincarryout
out
control outi=
0 Ai + Bi1 undefined
2 Ai + Bi3 Ai – Bi4 Ai AND Bi5 Ai OR Bi6 Ai NOR Bi7 Ai XOR Bi
Addition + Subtraction in one circuit (1-bit Arithmetic Unit)When Sub = 0, Y = B and Cin = 0. Result = A + B + 0 = A + B.When Sub = 1, Y = ~B and Cin = 1. Result = A + ~B + 1 = A – B.
Which parts belong in inside the 1‐bit ALU?
Addition + Subtraction in one circuit (1-bit Arithmetic Unit)When Sub = 0, Y = B and Cin = 0. Result = A + B + 0 = A + B.When Sub = 1, Y = ~B and Cin = 1. Result = A + ~B + 1 = A – B.
What should we do with the full adder’s Cin input?
A B control3
carryincarryout
out
Addition + Subtraction in one circuit (1-bit Arithmetic Unit)When Sub = 0, Y = B and Cin = 0. Result = A + B + 0 = A + B.When Sub = 1, Y = ~B and Cin = 1. Result = A + ~B + 1 = A – B.
Where will the “Sub” signal come from?
control out=
0 A + B (unsigned)
1 undefined
2 A + B (signed)
3 A – B (signed)
Complete 1-bit Logic Unit
NOR
AND
OR
XOR
X
Y
I0
S1
G
S0
I1
I2
I3
Y
R1R0
What should the control inputs (R0, R1) connect to?
How do we select between the adder and the logic unit? How do we control the selection?
R1 R0 Output0 0 Gi = XiYi0 1 Gi = Xi+Yi1 0 Gi = (Xi+Yi)’1 1 Gi = Xi⨁Yi
Complete 1-bit ALU
Complete 1-bit ALU
A
B
control[0]
Full Adder
0
1
LogicUnit
carryin
out
carryout
A
B
Cin
Sum
Cout
A
B
R[0]
out
1-bit ALU slice
control[1]control[2]
R[1]
XOR
If the control input for our 1-bit ALU indicates that the ALU should perform a bitwise logical function, what happens to the result of the full adder inside our 1-bit ALU?“It dies at the mux RIP”“The result for the full adder is still computed and passed into the multiplexer, but it never leaves the multiplexer; only the result of the logical operation does.”“The sum is left unused, but the carry is left over and transmitted to the next ALU
Connecting 1-bit ALUs
A B ctrl
3
cincout
out
A B ctrl
3
cincout
out
A B ctrl
3
cincout
out
A B ctrl
3
cincout
out
A B ctrl
3
cincout
out
Use a code generator to help you build your ALUinput [7:0] in;wire [7:1] chain;or o1(chain[1], in[1], in[0]);or o2(chain[2], in[2], chain[1]); // Note how lines from here toor o3(chain[3], in[3], chain[2]);or o4(chain[4], in[4], chain[3]);or o5(chain[5], in[5], chain[4]);or o6(chain[6], in[6], chain[5]);or o7(chain[7], in[7], chain[6]); // here are basically the samenot n0(zero, chain[7]);
Use a code generator to help you build your ALU
// This function generates the repeated part of the circuit
int main() {
for (int i = 2 ; i < width ; i ++) {
printf("or o%d(chain[%d], in[%d], chain[%d]);\n”, i, i, i, i-1);
}
return 0;
}
Flags (overflow, zero, negative) Let’s do negative first; negative evaluates to: 1 when the output is negative, and 0 when the output is positive or zero
Negative = a)carryout[30]b)output[30] c)carryout[31]d)output[31] e)control[0]
Flags (overflow, zero, negative) zero evaluates to: 1 when the output is equal to zero, else 0
Zero =
Flags (overflow, zero, negative)Overflow (for 2’s complement) evaluates to: 1 when the overflow occurred, else 0
adding two positive numbers yields a negative number adding two negative numbers yields a positive number
Consider the adder for the MSB:
Overflow =
a)cin[31] NOR cout[31]b)cin[31] AND cout[31] c)cin[31] OR cout[31]d)cin[31] XOR cout[31]e)cin[31] NAND cout[31]
X Y Cin Cout S
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
A specification for a 32-bit ALU
𝑜𝑣𝑒𝑟𝑓𝑙𝑜𝑤,𝑛𝑒𝑔𝑎𝑡𝑖𝑣𝑒 =a 0,0b 0, 1c 1, 0d 1, 1
control out=
0 A + B (unsigned)
1 undefined
2 A + B (signed)
3 A – B (signed)
4 A AND B
5 A OR B
6 A NOR B
7 A XOR B
For the following values of A, B, and controldetermine the values of the overflow and negative flags. control = 100A = 0x6692 0602 B = 0x5045 1053
A
B
out
32
32
32
3
OverflowZeroNegative
A specification for a 32-bit ALU
𝑜𝑣𝑒𝑟𝑓𝑙𝑜𝑤,𝑛𝑒𝑔𝑎𝑡𝑖𝑣𝑒 =a 0,0b 0, 1c 1, 0d 1, 1
control out=
0 A + B (unsigned)
1 undefined
2 A + B (signed)
3 A – B (signed)
4 A AND B
5 A OR B
6 A NOR B
7 A XOR B
For the following values of A, B, and controldetermine the values of the overflow and negative flags. control = 011A = 0x6692 0602 B = 0x5045 1053
A
B
out
32
32
32
3
OverflowZeroNegative
A specification for a 32-bit ALU
𝑜𝑣𝑒𝑟𝑓𝑙𝑜𝑤,𝑛𝑒𝑔𝑎𝑡𝑖𝑣𝑒 =a 0,0b 0, 1c 1, 0d 1, 1
control out=
0 A + B (unsigned)
1 undefined
2 A + B (signed)
3 A – B (signed)
4 A AND B
5 A OR B
6 A NOR B
7 A XOR B
For the following values of A, B, and controldetermine the values of the overflow and negative flags. control = 101A = 0x9105 6831 B = 0x5916 9631
A
B
out
32
32
32
3
OverflowZeroNegative