Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC321 Emulate a 4-Bit ALU in Java A0A0 A1A1 A2A2 A3A3 B0B0 B1B1 B2B2 B3B3 S0S0 S1S1 S2S2 S3S3 C in ILIL IRIR C out OV 4-Bit ALU S0S0 S1S1 S2S2 S3S3 Sum.

Similar presentations


Presentation on theme: "CSC321 Emulate a 4-Bit ALU in Java A0A0 A1A1 A2A2 A3A3 B0B0 B1B1 B2B2 B3B3 S0S0 S1S1 S2S2 S3S3 C in ILIL IRIR C out OV 4-Bit ALU S0S0 S1S1 S2S2 S3S3 Sum."— Presentation transcript:

1 CSC321 Emulate a 4-Bit ALU in Java A0A0 A1A1 A2A2 A3A3 B0B0 B1B1 B2B2 B3B3 S0S0 S1S1 S2S2 S3S3 C in ILIL IRIR C out OV 4-Bit ALU S0S0 S1S1 S2S2 S3S3 Sum bits Selector bits A registerB register

2 CSC321 Class layout public class ALU { private static String S; // -- sum (multiple bits) private static String C; // -- carry out (multiple bits) private static String O; // -- overflow (1 bit) // -- test function, 4-bit adder public static void main(String[] args) { } public static String getS(); // -- getter for sum public static String getC(); // -- getter for carry out public static String getO (); // -- getter for overflow // -- add functions for arithmetic, logic, and shift units }

3 CSC321 Start by creating a full adder

4 CSC321 Binary Full Adder C in C out SiSi AiAi BiBi

5 CSC321 Creating Larger Adders Connect full adders together –n-bit binary adder is created from n full adders Full Adder B3B3 A3A3 B2B2 A2A2 B1B1 A1A1 B0B0 A0A0 C3C3 C2C2 C1C1 C0C0 C out S3S3 S2S2 S1S1 S0S0 4-bit binary adder

6 CSC321 Method layout // -- implements full-adder logic A+B+Cin -> S, C, O public static void 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) { }

7 CSC321 Then create the logic selector MUX

8 CSC321 4-Bit Arithmetic Unit

9 CSC321 Method layout public static void 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 } else if (selector.equals("01")) { // -- condition the B input based on the inputs to the MUX } else if (selector.equals("10")) { // -- condition the B input based on the inputs to the MUX } else if (selector.equals("11")) { // -- condition the B input based on the inputs to the MUX } else { // -- error in selector } Arithmetic.FULLADDER(A, Bconditioned, cin); }

10 CSC321 Then create the logic unit

11 CSC321 Logic Unit 0 1 2 3 S0S0 S1S1 HiHi AiAi BiBi 4x1 MUX

12 CSC321 Logic Unit Software 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

13 CSC321 Method layout public static void LogicMUX (String selector, String A, String B) { // -- result will go into S S = ""; if (selector.equals("00")) { } else if (selector.equals("01")) { } else if (selector.equals("10")) { } else if (selector.equals("11")) { } else { // -- error in selector }

14 CSC321 Create the shift unit

15 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)

16 CSC321 Function Selector Unit Software Design // -- 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 Function that contains the MUX and calls the full adder

17 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

18 CSC321 1-Bit Processing Units Arithmetic Unit Logic Unit Shift Unit MUX AiAi BiBi S1S1 S0S0 FiFi C in C out ILIL IRIR A i+1 A i-1 S2S2 S3S3 A0A0 A n-1 S2S2

19 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

20 CSC321 Function Selector Unit Software 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

21 CSC321 Function Selector Unit Software Design public void ALUUnit (String _select) { String select = _select.substring(0, 2); carrybitwise = Cin; D = ""; E = ""; H = "";

22 CSC321 Function Selector Unit Software 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; }

23 CSC321 Function Selector Unit Software 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; }

24 CSC321 Function Selector Unit Software 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

25 CSC321 Function Selector Unit Software 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

26 CSC321 Function Selector Unit Software Design 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 Function that contains the MUX and calls the Boolean logic operators

27 CSC321 Shift Unit (4 bit) 0 1 2 3 S0S0 S1S1 4x1 MUX IRIR 0 0 1 2 3 S0S0 S1S1 4x1 MUX ILIL 2x1 MUX H0H0 2x1 MUX H1H1 2x1 MUX H2H2 2x1 MUX H3H3 A3A3 A2A2 A1A1 A0A0 S2S2

28 CSC321 Shift Unit S 0, S 1 select shift type –00 – logical shift –01 – circular shift (rotate) –10 – arithmetic shift S 2 selects direction –0 – left –1 – right S 3 is not used

29 CSC321 Shift Unit Software Design Three parameters –Bit to the left of bit i –Bit to the right of bit i –Direction (left/right) selector Two stages –1 st stage determines the two bits to feed to the 2 nd 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 –2 nd stage is the 2x1 MUX that selects the left or right bit fed in

30 CSC321 Function Selector Unit Software 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"; }

31 CSC321 Function Selector Unit Software 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); }

32 CSC321 Function Selector Unit Software Design ShiftUnitBitwise(select.substring(1,2), bitA, bitB); H = shiftbitwise + H; F = H; } Private member variable that holds the results of the shift operation Function that contains the 2x1 bit shifter MUX


Download ppt "CSC321 Emulate a 4-Bit ALU in Java A0A0 A1A1 A2A2 A3A3 B0B0 B1B1 B2B2 B3B3 S0S0 S1S1 S2S2 S3S3 C in ILIL IRIR C out OV 4-Bit ALU S0S0 S1S1 S2S2 S3S3 Sum."

Similar presentations


Ads by Google