emulate a 4-bit alu in java
DESCRIPTION
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 { - PowerPoint PPT PresentationTRANSCRIPT
CSC321
Emulate a 4-Bit ALU in Java
A0A1A2A3 B0B1B2B3
S0S1S2S3
Cin IL IR
Cout OV4-Bit ALUS0S1S2S3
Sum bitsSelector bits
A register B register
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 adderpublic static void main(String[] args) {
}
public static String getS(); // -- getter for sumpublic static String getC(); // -- getter for carry outpublic static String getO (); // -- getter for overflow
// -- add functions for arithmetic, logic, and shift units}
CSC321
Start by creating a full adder
CSC321
Binary Full Adder
CinCout
Si
Ai
Bi
CSC321
Creating Larger Adders
• Connect full adders together– n-bit binary adder is created from n full adders
Full Adder Full Adder Full Adder Full Adder
B3 A3 B2 A2 B1 A1 B0 A0
C3 C2 C1 C0
Cout S3 S2 S1 S0
4-bit binary adder
CSC321
Method layout
// -- implements full-adder logic A+B+Cin -> S, C, Opublic 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) {
}}
CSC321
Then create the logic selector MUX
CSC321
4-Bit Arithmetic Unit
4x1 MUX 4x1 MUX 4x1 MUX 4x1 MUX
Full Adder Full Adder Full Adder Full Adder
0B3B2B1B0A0 A1 A2 A3Cin
Cout
S0S1
D3D2D1D0
0 1 2 3S1S00 1 2 3S1S00 1 2 3S1S00 1 2 3S1S0
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);
}
CSC321
Then create the logic unit
CSC321
Logic Unit
0
1
2
3
S0
S1
Hi
Ai
Bi
4x1MUX
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
public static void LogicMUX (String selector, String A, String B) {
// -- result will go into SS = "";
if (selector.equals("00")) {}else if (selector.equals("01")) {}else if (selector.equals("10")) {}else if (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
// -- simulate a 4x1 MUXif (select.equals("00")) {
// -- arithmetic operationArithmeticUnitBitwise(_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
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
ArithmeticUnit
LogicUnit
ShiftUnit
MUX
Ai
Bi
S1
S0
Fi
Cin
Cout
IL
IR
Ai+1
Ai-1
S2S3
A0
An-1
S2
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 MUXif (select.equals("00")) {
// -- arithmetic operationArithmeticUnitBitwise(_select.substring(2, 4), bitA, bitB);D = sumbitwise + D;Cout = carrybitwise;F = D;
}else if (select.equals("01")) {
// -- logic operationLogicUnitBitwise(_select.substring(2, 4), bitA, bitB);E = logicbitwise + E;F = E;
}
CSC321
Function Selector UnitSoftware Design
else if (select.equals("10")) {// -- shift left operationif (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 operationif (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
else if (select.equals("01")) {// -- logic operationLogicUnitBitwise(_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
CSC321
Shift Unit (4 bit)
0123
S0
S1
4x1MUX
IR
0
0123
S0
S1
4x1MUX
IL
2x1MUX H0
2x1MUX H1
2x1MUX H2
2x1MUX H3
A3
A2
A1
A0
S2
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 MUXString 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 MUXelse 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 directlyelse {
bitA = A.substring(i-1, i);bitB = A.substring(i+1, i+2);
}
CSC321
Function Selector UnitSoftware 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