csc321 emulate a 4-bit alu in java a0a0 a1a1 a2a2 a3a3 b0b0 b1b1 b2b2 b3b3 s0s0 s1s1 s2s2 s3s3 c in...
TRANSCRIPT
![Page 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/1.jpg)
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
![Page 2: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/2.jpg)
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}
![Page 3: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/3.jpg)
CSC321
Start by creating a full adder
![Page 4: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/4.jpg)
CSC321
Binary Full Adder
CinCout
Si
Ai
Bi
![Page 5: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/6.jpg)
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) {
}}
![Page 7: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/7.jpg)
CSC321
Then create the logic selector MUX
![Page 8: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/8.jpg)
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
![Page 9: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/9.jpg)
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);
}
![Page 10: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/10.jpg)
CSC321
Then create the logic unit
![Page 11: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/11.jpg)
CSC321
Logic Unit
0
1
2
3
S0
S1
Hi
Ai
Bi
4x1MUX
![Page 12: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/13.jpg)
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}
}
![Page 14: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/14.jpg)
CSC321
Create the shift unit
![Page 15: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/15.jpg)
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)
![Page 16: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/16.jpg)
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
![Page 17: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/17.jpg)
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
![Page 18: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/18.jpg)
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
![Page 19: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/19.jpg)
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
![Page 20: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/20.jpg)
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
![Page 21: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/21.jpg)
CSC321
Function Selector UnitSoftware Design
public void ALUUnit (String _select){
String select = _select.substring(0, 2);
carrybitwise = Cin;D = "";E = "";H = "";
![Page 22: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/22.jpg)
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;
}
![Page 23: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/23.jpg)
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;
}
![Page 24: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/24.jpg)
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
![Page 25: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/25.jpg)
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
![Page 26: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/26.jpg)
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
![Page 27: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/27.jpg)
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
![Page 28: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/28.jpg)
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
![Page 29: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/29.jpg)
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
![Page 30: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/30.jpg)
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";
}}
![Page 31: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/31.jpg)
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);
}
![Page 32: 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](https://reader036.vdocuments.net/reader036/viewer/2022062716/56649dd95503460f94ace884/html5/thumbnails/32.jpg)
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