/////////////////////////////////////////////////////////////////////// /// /// Part 1 -- Processor.java /// /////////////////////////////////////////////////////////////////////// package duckMachine.architecture; /** * The processor of the machine. * @author Amr Sabry * @version jdk-1.1 */ public class Processor implements ProcessorI { private ALUI alu; private Word pc, acc; private Instruction ir; public Processor () { alu = new ALU(); pc = new Address(0); acc = new Data(0); ir = new HaltIns(); } public ALUI getALU () { return alu; } public Word fetchPC () { return pc; } public Word fetchACC () { return acc; } public Instruction fetchIR () { return ir; } public void storePC (Word newpc) { pc = newpc; } public void storeACC (Word newacc) { acc = newacc; } public void storeIR (Instruction newir) { ir = newir; } } /////////////////////////////////////////////////////////////////////// /// /// Execute methods for all instructions -- note this is not a program /// that will run /// /////////////////////////////////////////////////////////////////////// HALT throw new HaltE(); ADD Word w1 = proc.fetchACC(); Word w2 = mem.fetch(i.getAddress()); Word result = alu.add(w1,w2); proc.storeACC(result); CLEAR Word w = new Data(0); mem.store(i.getAddress(), w); COMPARE Word w1 = mem.fetch(i.getAddress()); Word w2 = proc.fetchACC(); alu.compare(w1,w2); DECREMENT Word w = mem.fetch(i.getAddress()); Word result = alu.dec(w); mem.store(i.getAddress(), result); IN Word w = io.readData(); mem.store(i.getAddress(), w); INCREMENT Word w = mem.fetch(i.getAddress()); Word result = alu.inc(w); mem.store(i.getAddress(), result); JUMPEQ if (alu.fetchEQ()) { proc.storePC(i.getAddress()); } JUMPGT if (alu.fetchGT()) { proc.storePC(i.getAddress()); } JUMPLT if (alu.fetchLT()) { proc.storePC(i.getAddress()); } JUMPNEQ if (! alu.fetchEQ()) { proc.storePC(i.getAddress()); } JUMP proc.storePC(i.getAddress()); LOAD Word w = mem.fetch(i.getAddress()); proc.storeACC(w); OUT Word w = mem.fetch(i.getAddress()); io.writeData(w); STORE Word w = proc.fetchACC(); mem.store(i.getAddress(), w); SUBTRACT Word w1 = proc.fetchACC(); Word w2 = mem.fetch(i.getAddress()); Word result = alu.sub(w1,w2); proc.storeACC(result);