diff --git a/app/src/main/java/Programs/Factorial.java b/app/src/main/java/Programs/Factorial.java index bc5fb38..e69de29 100644 --- a/app/src/main/java/Programs/Factorial.java +++ b/app/src/main/java/Programs/Factorial.java @@ -1,20 +0,0 @@ -package Programs; - -import hatelace.*; -import hatelace.instructions.*; - -public class Factorial extends Program { - public Factorial() { - Address n = new Address(0), - fac = new Address(1); - - add(new Copy(new IntWord(5), n)); - add(new Copy(new IntWord(1), fac)); - add(new JumpEq(6, n, new IntWord(1))); - add(new Mul(fac, n, fac)); - add(new Add(n, new IntWord(-1), n)); - add(new Jump(2)); - add(new Print(fac)); - add(new Halt()); - } -} \ No newline at end of file diff --git a/app/src/main/java/hatelace/Computer.java b/app/src/main/java/hatelace/Computer.java index f8dcf99..50d762c 100644 --- a/app/src/main/java/hatelace/Computer.java +++ b/app/src/main/java/hatelace/Computer.java @@ -15,7 +15,7 @@ public class Computer { public void run() { // Note that the instructions themselves are responsible for incrementing the PC for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.program.size() && !PC.halted() && PC.getSysTicks() < 100; ) { - program.get(PC.getPC()).execute(this.memory, PC); + program.getInstructions()[PC.getPC()].execute(this.memory, PC); } } } diff --git a/app/src/main/java/hatelace/Main.java b/app/src/main/java/hatelace/Main.java index 9dcb7f1..f06c53e 100644 --- a/app/src/main/java/hatelace/Main.java +++ b/app/src/main/java/hatelace/Main.java @@ -1,6 +1,6 @@ package hatelace; -import Programs.Factorial; +import hatelace.instructions.*; public class Main { public static void main(String[] args) { @@ -8,15 +8,26 @@ public class Main { Memory memory = new IntMemory(64); // 64 words of memory Computer computer = new Computer(memory); - Program program = new Factorial(); + Address n = new Address(0), fac = new Address(1); + + Program program = new Program(new Instruction[] { + new Copy(new IntWord(5), n), + new Copy(new IntWord(1), fac), + new JumpEq(6, n, new IntWord(1)), + new Mul(fac, n, fac), + new Add(n, new IntWord(-1), n), + new Jump(2), + new Print(fac), + new Halt() + }); computer.load(program); computer.run(); memory.dump(); // Print the program System.out.println("Program:"); - for (Instruction instruction : program) { + for (Instruction instruction : program.getInstructions()) { System.out.println(instruction); } diff --git a/app/src/main/java/hatelace/Program.java b/app/src/main/java/hatelace/Program.java index 510478f..859b86b 100644 --- a/app/src/main/java/hatelace/Program.java +++ b/app/src/main/java/hatelace/Program.java @@ -1,6 +1,17 @@ package hatelace; -import java.util.ArrayList; +public class Program { + private Instruction[] instructions; -// This is simply an intermediate class to comply with the specified API requirements. -public abstract class Program extends ArrayList {} + public Program(Instruction[] instructions) { + this.instructions = instructions; + } + + public Instruction[] getInstructions() { + return this.instructions; + } + + public int size() { + return this.instructions.length; + } +} diff --git a/app/src/main/java/hatelace/instructions/Add.java b/app/src/main/java/hatelace/instructions/Add.java index 76b3115..a81f1d9 100644 --- a/app/src/main/java/hatelace/instructions/Add.java +++ b/app/src/main/java/hatelace/instructions/Add.java @@ -3,19 +3,18 @@ package hatelace.instructions; import hatelace.*; public class Add extends Instruction { - private Address src; - private Word imm; + private Address op1; + private Word op2; private Address dest; - /** Add immediate value to memory address. */ - public Add(Address src, Word imm, Address dest) { - this.src = src; - this.imm = imm; + public Add(Address op1, Word op2, Address dest) { + this.op1 = op1; + this.op2 = op2; this.dest = dest; } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, memory.read(this.src).add(this.imm)); + memory.write(this.dest, memory.read(this.op1).add(this.op2)); PC.incPC(); } @@ -24,6 +23,6 @@ public class Add extends Instruction { } protected Object[] operands() { - return new Object[] { this.src, this.imm, this.dest }; + return new Object[] { this.op1, this.op2, this.dest }; } } diff --git a/app/src/main/java/hatelace/instructions/Copy.java b/app/src/main/java/hatelace/instructions/Copy.java index 9521879..d9c602f 100644 --- a/app/src/main/java/hatelace/instructions/Copy.java +++ b/app/src/main/java/hatelace/instructions/Copy.java @@ -3,17 +3,16 @@ package hatelace.instructions; import hatelace.*; public class Copy extends Instruction { - private Word imm; - private Address dest; + private Word word; + private Address address; - /** Copy immediate value to memory address. */ - public Copy(Word imm, Address dest) { - this.imm = imm; - this.dest = dest; + public Copy(Word word, Address address) { + this.word = word; + this.address = address; } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, this.imm); + memory.write(this.address, this.word); PC.incPC(); } @@ -22,6 +21,6 @@ public class Copy extends Instruction { } protected Object[] operands() { - return new Object[] {this.imm, this.dest}; + return new Object[] {this.word, this.address}; } } diff --git a/app/src/main/java/hatelace/instructions/Halt.java b/app/src/main/java/hatelace/instructions/Halt.java index 569f868..20804d9 100644 --- a/app/src/main/java/hatelace/instructions/Halt.java +++ b/app/src/main/java/hatelace/instructions/Halt.java @@ -3,7 +3,6 @@ package hatelace.instructions; import hatelace.*; public class Halt extends Instruction { - /** Halts the CPU */ public Halt(){}; public void execute(Memory memory, ProgramCounter PC) { diff --git a/app/src/main/java/hatelace/instructions/Jump.java b/app/src/main/java/hatelace/instructions/Jump.java index 373dcb0..4cd3b92 100644 --- a/app/src/main/java/hatelace/instructions/Jump.java +++ b/app/src/main/java/hatelace/instructions/Jump.java @@ -5,7 +5,6 @@ import hatelace.*; public class Jump extends Instruction { private int index; - /** Unconditional jump, non-relative */ public Jump(int index) { this.index = index; } diff --git a/app/src/main/java/hatelace/instructions/JumpEq.java b/app/src/main/java/hatelace/instructions/JumpEq.java index ecfc138..2491248 100644 --- a/app/src/main/java/hatelace/instructions/JumpEq.java +++ b/app/src/main/java/hatelace/instructions/JumpEq.java @@ -4,18 +4,17 @@ import hatelace.*; public class JumpEq extends Instruction { private int index; - private Address src; - private Word imm; + private Address address; + private Word value; - /** Conditional jump */ - public JumpEq(int index, Address src, Word imm) { + public JumpEq(int index, Address address, Word value) { this.index = index; - this.src = src; - this.imm = imm; + this.address = address; + this.value = value; } public void execute(Memory memory, ProgramCounter PC) { - if (this.imm.equals(memory.read(this.src))) { + if (this.value.equals(memory.read(this.address))) { PC.setPC(this.index); } else { PC.incPC(); @@ -27,6 +26,6 @@ public class JumpEq extends Instruction { } protected Object[] operands() { - return new Object[] {this.index, this.src, this.imm}; + return new Object[] {this.index, this.address, this.value}; } } diff --git a/app/src/main/java/hatelace/instructions/Mul.java b/app/src/main/java/hatelace/instructions/Mul.java index d70cb0b..c71f703 100644 --- a/app/src/main/java/hatelace/instructions/Mul.java +++ b/app/src/main/java/hatelace/instructions/Mul.java @@ -3,19 +3,18 @@ package hatelace.instructions; import hatelace.*; public class Mul extends Instruction { - private Address src1; - private Address src2; + private Address op1; + private Address op2; private Address dest; - /** Multiply contents of two addresses and store the result in a third memory address. */ - public Mul(Address src1, Address src2, Address dest) { - this.src1 = src1; - this.src2 = src2; + public Mul(Address op1, Address op2, Address dest) { + this.op1 = op1; + this.op2 = op2; this.dest = dest; } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, memory.read(this.src1).multiply(memory.read(this.src2))); + memory.write(this.dest, memory.read(this.op1).multiply(memory.read(this.op2))); PC.incPC(); } @@ -24,6 +23,6 @@ public class Mul extends Instruction { } protected Object[] operands() { - return new Object[] { this.src1, this.src2, this.dest }; + return new Object[] {this.op1, this.op2, this.dest}; } } diff --git a/app/src/main/java/hatelace/instructions/Print.java b/app/src/main/java/hatelace/instructions/Print.java index 88f0f6c..6aa3cec 100644 --- a/app/src/main/java/hatelace/instructions/Print.java +++ b/app/src/main/java/hatelace/instructions/Print.java @@ -5,7 +5,6 @@ import hatelace.*; public class Print extends Instruction { private Address address; - /** Print content of memory address */ public Print(Address address) { this.address = address; }