Finishing touches
This commit is contained in:
parent
4227d3998b
commit
a323782664
4 changed files with 27 additions and 29 deletions
|
@ -0,0 +1,20 @@
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,7 @@ public class Computer {
|
||||||
public void run() {
|
public void run() {
|
||||||
// Note that the instructions themselves are responsible for incrementing the PC
|
// 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; ) {
|
for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.program.size() && !PC.halted() && PC.getSysTicks() < 100; ) {
|
||||||
program.getInstructions()[PC.getPC()].execute(this.memory, PC);
|
program.get(PC.getPC()).execute(this.memory, PC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package hatelace;
|
package hatelace;
|
||||||
|
|
||||||
import hatelace.instructions.*;
|
import Programs.Factorial;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -8,26 +8,15 @@ public class Main {
|
||||||
|
|
||||||
Memory memory = new IntMemory(64); // 64 words of memory
|
Memory memory = new IntMemory(64); // 64 words of memory
|
||||||
Computer computer = new Computer(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.load(program);
|
||||||
computer.run();
|
computer.run();
|
||||||
memory.dump();
|
memory.dump();
|
||||||
|
|
||||||
// Print the program
|
// Print the program
|
||||||
System.out.println("Program:");
|
System.out.println("Program:");
|
||||||
for (Instruction instruction : program.getInstructions()) {
|
for (Instruction instruction : program) {
|
||||||
System.out.println(instruction);
|
System.out.println(instruction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,6 @@
|
||||||
package hatelace;
|
package hatelace;
|
||||||
|
|
||||||
public class Program {
|
import java.util.ArrayList;
|
||||||
private Instruction[] instructions;
|
|
||||||
|
|
||||||
public Program(Instruction[] instructions) {
|
// This is simply an intermediate class to comply with the specified API requirements.
|
||||||
this.instructions = instructions;
|
public abstract class Program extends ArrayList<Instruction> {}
|
||||||
}
|
|
||||||
|
|
||||||
public Instruction[] getInstructions() {
|
|
||||||
return this.instructions;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int size() {
|
|
||||||
return this.instructions.length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue