emulate a 4-bit alu in java

32
CSC321 Emulate a 4-Bit ALU in Java A 0 A 1 A 2 A 3 B 0 B 1 B 2 B 3 S 0 S 1 S 2 S 3 C in I L I R C out OV 4-Bit ALU S 0 S 1 S 2 S 3 Sum bits Selector bits A register B register

Upload: holly

Post on 15-Jan-2016

120 views

Category:

Documents


2 download

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 Presentation

TRANSCRIPT

Page 1: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

CSC321

Start by creating a full adder

Page 4: Emulate a 4-Bit ALU in Java

CSC321

Binary Full Adder

CinCout

Si

Ai

Bi

Page 5: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

CSC321

Then create the logic selector MUX

Page 8: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

CSC321

Then create the logic unit

Page 11: Emulate a 4-Bit ALU in Java

CSC321

Logic Unit

0

1

2

3

S0

S1

Hi

Ai

Bi

4x1MUX

Page 12: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

CSC321

Create the shift unit

Page 15: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

CSC321

Function Selector UnitSoftware Design

public void ALUUnit (String _select){

String select = _select.substring(0, 2);

carrybitwise = Cin;D = "";E = "";H = "";

Page 22: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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: Emulate a 4-Bit ALU in Java

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