350 likes | 572 Views
Emulate a 4-Bit ALU in Java. A register. B register. A 3. A 2. A 1. A 0. B 3. B 2. B 1. B 0. C in. I L. I R. 4-Bit ALU. C out. OV. S 3. S 2. S 1. S 0. Selector bits. Sum bits. S 3. S 2. S 1. S 0. Class layout. public class ALU {
E N D
Emulate a 4-Bit ALU in Java A register B register A3 A2 A1 A0 B3 B2 B1 B0 Cin IL IR 4-Bit ALU Cout OV S3 S2 S1 S0 Selector bits Sum bits S3 S2 S1 S0 CSC321
Class layout publicclass ALU { privatestatic String S; // -- sum (multiple bits) privatestatic String C; // -- carry out (multiple bits) privatestatic String O; // -- overflow (1 bit) // -- test function, 4-bit adder publicstaticvoid main(String[] args) { } publicstatic String getS(); // -- getter for sum publicstatic String getC(); // -- getter for carry out publicstatic String getO (); // -- getter for overflow // -- add functions for arithmetic, logic, and shift units } CSC321
Ai Bi Si Cin Cout Binary Full Adder CSC321
B3 A3 B2 A2 B1 A1 B0 A0 Full Adder Full Adder Full Adder Full Adder C3 C2 C1 C0 Cout S3 S2 S1 S0 Creating Larger Adders • Connect full adders together • n-bit binary adder is created from n full adders 4-bit binary adder CSC321
Method layout // -- implements full-adder logic A+B+Cin -> S, C, O publicstaticvoid FULLADDER (String A, String B, String cin) { // -- strip off bits from A and B and pass through 1-bit // adder circuitry passing Cout(i) to Cin(i+1) for (int i = A.length() - 1; i >= 0; --i) { } } CSC321
0 Cin A0 S1 S0 B0 A1 B1 A2 B2 A3 B3 0 0 0 S0 S1 1 S0 S1 1 S0 S1 1 2 2 2 0 3 3 3 S0 S1 1 2 3 4x1 MUX 4x1 MUX 4x1 MUX 4x1 MUX Full Adder Full Adder Full Adder Full Adder D0 D1 D2 D3 Cout 4-Bit Arithmetic Unit CSC321
Method layout publicstaticvoid ArithmeticMUX (String selector, String A, String B, String cin) { String Bconditioned = “”; if (selector.equals("00")) { // -- condition the B input based on the inputs to the MUX } elseif (selector.equals("01")) { // -- condition the B input based on the inputs to the MUX } elseif (selector.equals("10")) { // -- condition the B input based on the inputs to the MUX } elseif (selector.equals("11")) { // -- condition the B input based on the inputs to the MUX } else { // -- error in selector } Arithmetic.FULLADDER(A, Bconditioned, cin); } CSC321
Then create the logic unit CSC321
S0 S1 4x1 MUX Ai 0 Bi Hi 1 2 3 Logic Unit CSC321
Logic UnitSoftware Design • Receives 3 parameters • The 2-bit operation select input • The two operands • Has a MUX function to determine which logic operation is to be performed • if-then-else-if construct • Produces an output in the class sum bits CSC321
Method layout publicstaticvoid LogicMUX (String selector, String A, String B) { // -- result will go into S S = ""; if (selector.equals("00")) { } elseif (selector.equals("01")) { } elseif (selector.equals("10")) { } elseif (selector.equals("11")) { } else { // -- error in selector } } CSC321
Create the shift unit CSC321
Arithmetic Unit Software Design • Since the Arithmetic Unit also has a MUX at the input to each bit you’ll need a method that preprocesses the input bits prior to sending them to the FullAdder • The input to this method will be • The 2-bit selector (which is stripped off of the input to the Function Selector Unit parameter) CSC321
Function Selector UnitSoftware Design Function that contains the MUX and calls the full adder // -- simulate a 4x1 MUX if (select.equals("00")) { // -- arithmetic operation ArithmeticUnitBitwise(_select.substring(2, 4), bitA, bitB); D = sumbitwise + D; Cout = carrybitwise; F = D; } Private member variables that hold the results of the full adder CSC321
4-Bit ALU Software Design • We need variables • A, B, F registers • Carry-in, Carry-out, Overflow bits • Shift-in-left, Shift-in-right bits • These will all be private String types • We need mutators and accessors • Set values for A and B registers • Set values for Carry-in, Shift-in-left, and Shift-in-right bits • Get value of F register • Get value of Carry-out bit CSC321
1-Bit Processing Units Cin S1 Arithmetic Unit Cout S0 S3 S2 Ai Bi Logic Unit MUX Fi Shift Unit S2 Ai+1 Ai-1 A0 An-1 IL IR CSC321
1-Bit Processing Units Software Design • We need methods (member functions) • Bitwise arithmetic unit • Bitwise logic unit • Bitwise shift left unit • Bitwise shift right unit • Function selector unit CSC321
Function Selector UnitSoftware Design • Once you’ve set values into you’re A and B registers and Carry-in and Shift-in-left/right bits you will call the Function Selector Unit method to perform the desired operation • The only input to this method is a 4-bit selector string • Primary construct is a for loop that pulls bits out of the A and B registers one at a time • Secondary construct is an if-then-else-if that implements the MUX functionality for selecting the appropriate operation CSC321
Function Selector UnitSoftware Design public void ALUUnit (String _select) { String select = _select.substring(0, 2); carrybitwise = Cin; D = ""; E = ""; H = ""; CSC321
Function Selector UnitSoftware Design for (int i = 3; i >= 0; --i) { String bitA = A.substring(i, i+1); String bitB = B.substring(i, i+1); // -- simulate a 4x1 MUX if (select.equals("00")) { // -- arithmetic operation ArithmeticUnitBitwise(_select.substring(2, 4), bitA, bitB); D = sumbitwise + D; Cout = carrybitwise; F = D; } else if (select.equals("01")) { // -- logic operation LogicUnitBitwise(_select.substring(2, 4), bitA, bitB); E = logicbitwise + E; F = E; } CSC321
Function Selector UnitSoftware Design else if (select.equals("10")) { // -- shift left operation if (i == 3) { ShiftUnitBitwise("0", A.substring(2, 3), Ir); } else if (i == 0) { ShiftUnitBitwise("0", Il, A.substring(1, 2)); } else { ShiftUnitBitwise("0", A.substring(i-1, i), A.substring(i+1, i+2)); } H = shiftbitwise + H; F = H; } CSC321
Function Selector UnitSoftware Design else if (select.equals("11")) { // -- shift right operation if (i == 3) { ShiftUnitBitwise("1", A.substring(2, 3), Ir); } else if (i == 0) { ShiftUnitBitwise("1", Il, A.substring(1, 2)); } else { ShiftUnitBitwise("1", A.substring(i-1, i), A.substring(i+1, i+2)); } H = shiftbitwise + H; F = H; } } // -- end of for loop CSC321
Function Selector UnitSoftware Design • Note that I have not implemented “arithmetic shift left” and “arithmetic shift right” yet • You may take this code and study it, use it as is, modify it, whatever CSC321
Function Selector UnitSoftware Design Function that contains the MUX and calls the Boolean logic operators else if (select.equals("01")) { // -- logic operation LogicUnitBitwise(_select.substring(2, 4), bitA, bitB); E = logicbitwise + E; F = E; } Private member variable that holds the results of the logic operation CSC321
Shift Unit (4 bit) S2 S1 S0 2x1 MUX 4x1 MUX H0 IR 0 1 A0 0 2 2x1 MUX 3 H1 A1 A2 2x1 MUX S1 H2 S0 4x1 MUX A3 IL 2x1 MUX 0 1 H3 2 3 CSC321
Shift Unit • S0, S1 select shift type • 00 – logical shift • 01 – circular shift (rotate) • 10 – arithmetic shift • S2 selects direction • 0 – left • 1 – right • S3 is not used CSC321
Shift UnitSoftware Design • Three parameters • Bit to the left of bit i • Bit to the right of bit i • Direction (left/right) selector • Two stages • 1st stage determines the two bits to feed to the 2nd stage • Note that the output LSB and MSB are fed from MUXs that determine the type of shift to perform • These MUXs are implemented in the Function Selector Unit • 2nd stage is the 2x1 MUX that selects the left or right bit fed in CSC321
Function Selector UnitSoftware Design else if (select.equals("10") || select.equals("11")) { // -- shift operations // -- LSB is handled as a special case via a 4x1 MUX String s01 = _select.substring(2, 4); if (i == 3) { if (s01.equals("00")) { bitA = A.substring(2, 3); bitB = Ir; } else if (s01.equals("01")) { bitA = A.substring(2, 3); bitB = A.substring(0, 1); } else if (s01.equals("10")) { bitA = A.substring(2, 3); bitB = "0"; } } CSC321
Function Selector UnitSoftware Design // -- MSB is handled as a special case via a 4x1 MUX else if (i == 0) { if (s01.equals("00")) { bitA = Il; bitB = A.substring(1, 2); } else if (s01.equals("01")) { bitA = A.substring(3, 4); bitB = A.substring(1, 2); } else if (s01.equals("10")) { bitA = A.substring(0, 1); bitB = A.substring(1, 2); } } // -- "center" bits are wired directly else { bitA = A.substring(i-1, i); bitB = A.substring(i+1, i+2); } CSC321
Function Selector UnitSoftware Design Function that contains the 2x1 bit shifter MUX ShiftUnitBitwise(select.substring(1,2), bitA, bitB); H = shiftbitwise + H; F = H; } Private member variable that holds the results of the shift operation CSC321