package duckMachine.architecture;
/**
* The arithmetic and logic unit of the machine. The ALU uses Java's int
* datatype and its operations to implement its own operations. This means
* that all the operations are modulo 32-bits.
* @author Amr Sabry
* @version jdk-1.1
*/
public class ALU implements ALUI {
private boolean GTflag, EQflag, LTflag;
public ALU () {
GTflag = false;
EQflag = false;
LTflag = false;
}
/**
* Returns the current contents of the GT flag.
*/
public boolean fetchGT () { return GTflag; }
/**
* Returns the current contents of the EQ flag.
*/
public boolean fetchEQ () { return EQflag; }
/**
* Returns the current contents of the EQ flag.
*/
public boolean fetchLT () { return LTflag; }
/**
* The ALU addition is implemented using Java's ints. It works modulo
* 32 bits.
*/
public Word add (Word w1, Word w2) {
int i1 = w1.toInt();
int i2 = w2.toInt();
return new Data(i1+i2);
}
/**
* The ALU subtraction is implemented using Java's ints. It works modulo
* 32 bits.
*/
public Word sub (Word w1, Word w2) {
int i1 = w1.toInt();
int i2 = w2.toInt();
return new Data(i1-i2);
}
/**
* The ALU increment is implemented using Java's ints. It works modulo
* 32 bits.
*/
public Word inc (Word w) {
int i = w.toInt();
return new Data(i+1);
}
/**
* The ALU decrement is implemented using Java's ints. It works modulo
* 32 bits.
*/
public Word dec (Word w) {
int i = w.toInt();
return new Data(i-1);
}
/**
* Sets the condition flags according to the result of comparing
* w1 and w2.
* If w1>w2 then GT should be set and the other flags made FALSE.
* If w1=w2 then EQ should be set and the other flags made FALSE.
* If w1>w2 then LT should be set and the other flags made FALSE.
*/
public void compare (Word w1, Word w2) {
int i1 = w1.toInt();
int i2 = w2.toInt();
if (i1 > i2) { GTflag=true; EQflag=false; LTflag=false; }
else if (i1 == i2) { GTflag=false; EQflag=true; LTflag=false; }
else { GTflag=false; EQflag=false; LTflag=true; }
}
}