public class ExecIns implements InsVisitor {
...
}
Clearly the visitor needs access to the machine registers, memory,
ALU, etc to perform its job. In other words, you will need instance
variables for the machine components. The constructor takes the entire
machine as an argument and initializes the instance variables:
public ExecIns (MachineI m) {
...
}
Our job now is to write each method in the InsVisitor interface so
that it updates the machine's state to reflect the effect of executing
the instruction. Let's look at some examples.
First, a halt instruction is straightforward:
public void visitHaltIns (HaltIns i) throws HaltE {
throw new HaltE();
}
A slightly more interesting example is the clear instruction. An
instruction ``clear X'' stores a 0 in the location X. This can be
easily implemented as follows:
public void visitClearIns (ClearIns i) throws MachineE {
Word w = new Data(0);
mem.store(i.getAddress(), w);
}
To implement the remaining operations, we will need to describe the
meaning of the instructions in some detail:
public class ExecTraceIns extends ExecIns {
public ExecTraceIns (MachineI m) {
super(m);
}
public void visitHaltIns (HaltIns i) throws HaltE {
outWriter.println(i.toString());
super.visitHaltIns(i);
}
public void visitAddIns (AddIns i) throws MachineE {
outWriter.println(i.toString());
super.visitAddIns(i);
}
public void visitClearIns (ClearIns i) throws MachineE {
outWriter.println(i.toString());
super.visitClearIns(i);
}
...
}
public class DebugIns extends ExecTraceIns {
public DebugIns (MachineI m) {
super(m);
}
public void visitHaltIns (HaltIns i) throws HaltE {
super.visitHaltIns(i);
}
public void visitAddIns (AddIns i) throws MachineE {
super.visitAddIns(i);
printStatus();
}
public void visitClearIns (ClearIns i) throws MachineE {
super.visitClearIns(i);
printStatus();
}
...
// assuming the visitor has instance variables proc and alu
private void printStatus () {
outWriter.print("\tPC=" + proc.fetchPC());
outWriter.print("\tACC=" + proc.fetchACC());
outWriter.print("\tGT/EQ/LT=" + alu.fetchGT() + "/" +
alu.fetchEQ() + "/" + alu.fetchLT());
outWriter.print("\n\n");
outWriter.flush();
}
sabry@cs.uoregon.edu