diff --git a/app/src/main/java/Programs/Factorial.java b/app/src/main/java/Programs/Factorial.java new file mode 100644 index 0000000..e69de29 diff --git a/app/src/main/java/hatelace/Addi.java b/app/src/main/java/hatelace/Addi.java deleted file mode 100644 index 69b4903..0000000 --- a/app/src/main/java/hatelace/Addi.java +++ /dev/null @@ -1,28 +0,0 @@ -package hatelace; - -/** Add immediate to address, not register */ -public class Addi extends Instruction { - private int dest_addr; - private int src1_addr; - private int imm; - - public Addi(int dest_addr, int src1_addr, int imm) { - this.dest_addr = dest_addr; - this.src1_addr = src1_addr; - this.imm = imm; - } - - public void execute(Memory memory, ProgramCounter PC) { - int src1 = memory.read(this.src1_addr); - memory.write(this.dest_addr, src1 + this.imm); - PC.incPC(); - } - - protected String opcode() { - return "addi"; - } - - protected Object[] operands() { - return new Object[] {this.dest_addr, this.src1_addr, this.imm}; - } -} diff --git a/app/src/main/java/hatelace/App.java b/app/src/main/java/hatelace/App.java index deb8916..357054b 100644 --- a/app/src/main/java/hatelace/App.java +++ b/app/src/main/java/hatelace/App.java @@ -1,16 +1,27 @@ package hatelace; +import hatelace.instructions.*; + public class App { public static void main(String[] args) { System.out.println("Starting..."); - Memory memory = new Memory(); + Memory memory = new Memory(64); // 64 words of memory Computer computer = new Computer(memory); - Program program = new Program(new int[] { 1, 2, 3, 4, 5 }); + Program program = new Program(new Instruction[] { + new Add(1, 1, 0), // Store 1 + 1 in address 0 + new Mul(2, 2, 1) // Store 2 * 2 in address 1 + }); computer.load(program); computer.run(); memory.dump(); + // Print the program + System.out.println("Program:"); + for (Instruction instruction : program.getInstructions()) { + System.out.println(instruction); + } + System.out.println("Completed"); } } diff --git a/app/src/main/java/hatelace/Computer.java b/app/src/main/java/hatelace/Computer.java index 6a7a07b..9898561 100644 --- a/app/src/main/java/hatelace/Computer.java +++ b/app/src/main/java/hatelace/Computer.java @@ -2,22 +2,20 @@ package hatelace; public class Computer { private Memory memory; + private Program program; public Computer(Memory Memory) { this.memory = Memory; } public void load(Program program) { - int[] instructions = program.getInstructions(); - for (int i = 0; i < instructions.length; i++) { - this.memory.write(i, instructions[i]); - } + this.program = program; } public void run() { - for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.memory.size(); PC.incPC()) { - int instruction = this.memory.read(PC.getPC()); - System.out.println(instruction); + // Note that the instructions themselves are responsible for incrementing the PC + for(ProgramCounter PC = new ProgramCounter(); PC.getPC() < this.program.size(); ) { + program.getInstructions()[PC.getPC()].execute(this.memory, PC); } } } diff --git a/app/src/main/java/hatelace/Memory.java b/app/src/main/java/hatelace/Memory.java index b79b3fc..d8d07ba 100644 --- a/app/src/main/java/hatelace/Memory.java +++ b/app/src/main/java/hatelace/Memory.java @@ -4,7 +4,11 @@ public class Memory { protected int[] memory; public Memory() { - this.memory = new int[1024]; + this.memory = new int[256]; + } + + public Memory(int size) { + this.memory = new int[size]; } public int read(int address) { @@ -27,7 +31,14 @@ public class Memory { for(int i = 0; i < this.memory.length; i+= 16) { System.out.printf(":%02X%04X00 ", 16, i); for(int j = 0; j < 16; j++) { + int content = this.memory[i + j]; + + // Print it as faded if it's zero + if (content == 0) + System.out.print("\u001B[2m"); + System.out.printf("%02X ", this.memory[i + j]); + System.out.print("\u001B[0m"); } System.out.println(); } diff --git a/app/src/main/java/hatelace/Program.java b/app/src/main/java/hatelace/Program.java index 32e3715..859b86b 100644 --- a/app/src/main/java/hatelace/Program.java +++ b/app/src/main/java/hatelace/Program.java @@ -1,13 +1,17 @@ package hatelace; public class Program { - private int[] instructions; + private Instruction[] instructions; - public Program(int[] instructions) { + public Program(Instruction[] instructions) { this.instructions = instructions; } - public int[] getInstructions() { + 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 new file mode 100644 index 0000000..a5d2bad --- /dev/null +++ b/app/src/main/java/hatelace/instructions/Add.java @@ -0,0 +1,30 @@ +package hatelace.instructions; + +import hatelace.Instruction; +import hatelace.Memory; +import hatelace.ProgramCounter; + +public class Add extends Instruction { + private int op1; + private int op2; + private int dest; + + public Add(int op1, int op2, int dest) { + this.op1 = op1; + this.op2 = op2; + this.dest = dest; + } + + public void execute(Memory memory, ProgramCounter PC) { + memory.write(this.dest, this.op1 + this.op2); + PC.incPC(); + } + + protected String opcode() { + return "add"; + } + + protected Object[] operands() { + return new Object[] {this.op1, this.op2, this.dest}; + } +} diff --git a/app/src/main/java/hatelace/instructions/Mul.java b/app/src/main/java/hatelace/instructions/Mul.java new file mode 100644 index 0000000..a749e6c --- /dev/null +++ b/app/src/main/java/hatelace/instructions/Mul.java @@ -0,0 +1,30 @@ +package hatelace.instructions; + +import hatelace.Instruction; +import hatelace.Memory; +import hatelace.ProgramCounter; + +public class Mul extends Instruction { + private int op1; + private int op2; + private int dest; + + public Mul(int op1, int op2, int dest) { + this.op1 = op1; + this.op2 = op2; + this.dest = dest; + } + + public void execute(Memory memory, ProgramCounter PC) { + memory.write(this.dest, this.op1 * this.op2); + PC.incPC(); + } + + protected String opcode() { + return "mul"; + } + + protected Object[] operands() { + return new Object[] {this.op1, this.op2, this.dest}; + } +}