I want to create a program(in Java) which simulates the functionality of Arithmetic Logistic Unit(ALU).
Here is my code. Please check is this correct. Can I implement it better?
public class ALU {
public static final int NOP = 0;
public static final int SEQ = 1;
public static final int SNE = 2;
public static final int SGT = 3;
public static final int SLE = 4;
public static final int SLT = 5;
public static final int ADD = 6;
public static final int ADDU = 7;
public static final int SUB = 8;
public static final int SUBU = 9;
public static final int MULT = 10;
public static final int MULTU = 11;
public static final int DIV = 12;
public static final int DIVU = 13;
public static final int AND = 14;
public static final int OR = 15;
public static final int XOR = 16;
public static final int SLL = 17;
public static final int SRL = 18;
public static final int SRA = 19;
public boolean[] getResult(boolean[] srcValue1, boolean[] srcValue2, int ope){
boolean [] result = new boolean[32];
int shift = 0; //ÒÆÎ»Êý
long src1 = 0;
long src2 = 0;
switch(ope){
case NOP: break;
case SEQ:
if(toSigned(srcValue1,0,31) == toSigned(srcValue2,0,31))
result[31] = true;
break;
case SNE:
if(toSigned(srcValue1,0,31) != toSigned(srcValue2,0,31))
result[31] = true;
break;
case SGT:
if(toSigned(srcValue1,0,31) > toSigned(srcValue2,0,31))
result[31] = true;
break;
case SLE:
if(toSigned(srcValue1,0,31) <= toSigned(srcValue2,0,31))
result[31] = true;
break;
case SLT:
if(toSigned(srcValue1,0,31) < toSigned(srcValue2,0,31))
result[31] = true;
break;
case ADD:
result = ADD(srcValue1,srcValue2);
break;
case ADDU:
result = ADD(srcValue1,srcValue2);
break;
case SUB:
result = SUB(srcValue1,srcValue2);
break;
case SUBU:
result = SUB(srcValue1,srcValue2);
break;
case MULT:
src1 = toSigned(srcValue1,0,31);
src2 = toSigned(srcValue2,0,31);
result = long2Boolean(src1*src2);
break;
case MULTU:
src1 = toUnsigned(srcValue1,0,31);
src2 = toUnsigned(srcValue2,0,31);
result = long2Boolean(src1*src2);
break;
case DIV:
src1 = toSigned(srcValue1,0,31);
src2 = toSigned(srcValue2,0,31);
result = long2Boolean(src1/src2);
break;
case DIVU:
src1 = toUnsigned(srcValue1,0,31);
src2 = toUnsigned(srcValue2,0,31);
result = long2Boolean(src1/src2);
break;
case AND:
for(int i=0;i<32;i++)
result[i] = srcValue1[i]&srcValue2[i];
break;
case OR:
for(int i=0;i<32;i++)
result[i] = srcValue1[i]|srcValue2[i];
break;
case XOR:
for(int i=0;i<32;i++){
result[i] = XOR(srcValue1[i],srcValue2[i]);
}
break;
case SLL:
shift = (int)toUnsigned(srcValue2,27,31);
for(int i=0;i<32-shift;i++)
result[i] = srcValue1[i+shift];
for(int i=32-shift;i<32;i++)
result[i] = false;
break;
case SRL:
shift = (int)toUnsigned(srcValue2,27,31);
for(int i=0;i<shift;i++)
result[i] = false;
for(int i=shift;i<32;i++)
result[i] = srcValue1[i-shift];
break;
case SRA:
shift = (int)toUnsigned(srcValue2,27,31);
boolean sign = srcValue1[0];
for(int i=0;i<shift;i++)
result[i] = sign;
for(int i=shift;i<32;i++)
result[i] = srcValue1[i-shift];
break;
}
return result;
}
private long toUnsigned(boolean[] data, int pos1, int pos2) {
long number = 0;
for (int i = pos1; i <= pos2; i++) {
if (data[i] == true)
number += (int) Math.pow(2, pos2 - i);
}
return number;
}
private long toSigned(boolean[] data, int pos1, int pos2) {
long number = 0;
for (int i = (pos1 + 1); i <= pos2; i++) {
if (data[i] == true)
number += (long) Math.pow(2, pos2 - i);
}
if (data[pos1] == true)
number = number - (long) Math.pow(2, pos2 - pos1);
return number;
}
private boolean[] long2Boolean(long number){
boolean[] result = new boolean[32];
for(int i=0;i<32;i++){
int bit = (int) (number&1);
if(bit == 1)
result[31-i] = true;
number = number>>1;
}
return result;
}
private boolean XOR(boolean src1,boolean src2){
boolean result = true;
if(src1 == src2 )
result = false;
return result;
}
private boolean[] ADD(boolean[] srcValue1,boolean[] srcValue2){
boolean Ci = false;
boolean[] result = new boolean[32];
for(int i=31;i>=0;i--){
result[i] = XOR(XOR(srcValue1[i],srcValue2[i]),Ci);
Ci = (srcValue1[i]&srcValue2[i])|(XOR(srcValue1[i],srcValue2[i])&Ci);
}
return result;
}
private boolean[] SUB(boolean[] srcValue1,boolean[] srcValue2){
boolean[] result = new boolean[32];
//¼ÆËãsrcValue2µÄ²¹Âë
for(int i=0;i<32;i++)
srcValue2[i] = !srcValue2[i];
boolean[] one = new boolean[32];
one[31] = true;
srcValue2 = ADD(srcValue1,srcValue2);
result = ADD(srcValue1,srcValue2);
return result;
}
public static void main(String[] args) {
long a = (long)Math.pow(2, 33);
boolean [] b = new ALU().long2Boolean(a);
System.out.print(b.length+"\n");
for(int i=0;i<32;i++)
System.out.println(b[i]);
}
}
Does this code satisfy all the functionality of Arithmetic Logistic Unit? . I just want to simulate the ALU functionality in my program. How to insert control bits in my program?