patternjavaMinor
Simulating ALU in java
Viewed 0 times
javaalusimulating
Problem
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) >1;
}
return result;
}
private boolean XOR(boolean src1,boolean src2){
boolean result = true;
if(src
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) >1;
}
return result;
}
private boolean XOR(boolean src1,boolean src2){
boolean result = true;
if(src
Solution
- Enum might be the real solution to define the different possible operations.
- The implementation of the XOR method could be more concise :
private boolean XOR(boolean src1,boolean src2){ return (src1 != src2 );} and you probably don't need it anyway because Java has a bitwise xor operator. Also, that would make your cases AND,OR and XOR more consistent.-
You should try to make your code a bit more consistent : sometimes you store
toSigned(srcValue1,0,31) in a variable before using it, sometimes you don't. My feeling is that even though it's not great from a performance point of view because it makes you compute stuff you may not need, it might make things easier to do :long usrc1 = toUnsigned(srcvalue1,0,31);
long usrc2 = toUnsigned(srcvalue2,0,31);
long ssrc1 = toSigned(srcvalue1,0,31);
long ssrc2 = toSigned(srcvalue2,0,31);to avoid the boring repetition in the rest of the method.
-
On the other hand, shift could probably be declared and defined only when you need it :
int shift = (int)toUnsigned(srcValue2,27,31);- It might be worth pointing out that according to the Java specs, integers are 32 bits so you probably don't need to do everything with arrays of 32 booleans. My feeling is that it would make everything much easier and you'd be able to do things in a straighforward way. For instance, case SEQ: would become : `case SEQ: return (src1==src2);'. If you do so, I think most of the loops you write might not be useful anymore.
I don't have much time to go through the end of the code but I hope this is a good beginning.
Code Snippets
long usrc1 = toUnsigned(srcvalue1,0,31);
long usrc2 = toUnsigned(srcvalue2,0,31);
long ssrc1 = toSigned(srcvalue1,0,31);
long ssrc2 = toSigned(srcvalue2,0,31);Context
StackExchange Code Review Q#23352, answer score: 4
Revisions (0)
No revisions yet.