diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 4e2bfe9..b71b9f0 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -34,7 +34,7 @@ java { application { // Define the main class for the application. - mainClass.set("hatelace.App") + mainClass.set("hatelace.Main") } tasks.named("test") { diff --git a/app/src/main/java/hatelace/Address.java b/app/src/main/java/hatelace/Address.java new file mode 100644 index 0000000..913e50a --- /dev/null +++ b/app/src/main/java/hatelace/Address.java @@ -0,0 +1,13 @@ +package hatelace; + +public class Address { + int address; + + public Address(int address) { + this.address = address; + } + + public int getAddress() { + return address; + } +} diff --git a/app/src/main/java/hatelace/IntMemory.java b/app/src/main/java/hatelace/IntMemory.java new file mode 100644 index 0000000..99fb96c --- /dev/null +++ b/app/src/main/java/hatelace/IntMemory.java @@ -0,0 +1,46 @@ +package hatelace; + +public class IntMemory extends Memory { + protected int[] memory; + + public IntMemory() { + this.memory = new int[256]; + } + + public IntMemory(int size) { + this.memory = new int[size]; + } + + public int read(int address) { + return this.memory[address]; + } + + public int size() { + return this.memory.length; + } + + public void write(int address, Word data) { + if (address < 0 || address >= this.memory.length) { + throw new IllegalArgumentException("Invalid memory address"); + } + this.memory[address] = data.getValue(); + } + + /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */ + public void dump() { + 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/IntWord.java b/app/src/main/java/hatelace/IntWord.java new file mode 100644 index 0000000..286e337 --- /dev/null +++ b/app/src/main/java/hatelace/IntWord.java @@ -0,0 +1,29 @@ +package hatelace; + +public class IntWord extends Word { + private Integer value; + + public IntWord(Integer value) { + this.value = value; + } + + public T getValue() { + return (T) value; + } + + public T add(Word other) { + return (T) new IntWord(value + (Integer) other.getValue()); + } + + public T subtract(Word other) { + return (T) new IntWord(value - (Integer) other.getValue()); + } + + public T multiply(Word other) { + return (T) new IntWord(value * (Integer) other.getValue()); + } + + public T divide(Word other) { + return (T) new IntWord(value / (Integer) other.getValue()); + } +} diff --git a/app/src/main/java/hatelace/App.java b/app/src/main/java/hatelace/Main.java similarity index 74% rename from app/src/main/java/hatelace/App.java rename to app/src/main/java/hatelace/Main.java index 357054b..dfaa036 100644 --- a/app/src/main/java/hatelace/App.java +++ b/app/src/main/java/hatelace/Main.java @@ -2,15 +2,16 @@ package hatelace; import hatelace.instructions.*; -public class App { +public class Main { public static void main(String[] args) { System.out.println("Starting..."); - Memory memory = new Memory(64); // 64 words of memory + Memory memory = new IntMemory(64); // 64 words of memory Computer computer = new Computer(memory); 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 + new Mul(2, 2, 1), // Store 2 * 2 in address 1 + new Copy(new IntWord(3), new Address(2)) // Store 3 in address 2 }); computer.load(program); computer.run(); diff --git a/app/src/main/java/hatelace/Memory.java b/app/src/main/java/hatelace/Memory.java index d8d07ba..b4aaa41 100644 --- a/app/src/main/java/hatelace/Memory.java +++ b/app/src/main/java/hatelace/Memory.java @@ -1,46 +1,10 @@ package hatelace; -public class Memory { - protected int[] memory; - - public Memory() { - this.memory = new int[256]; - } - - public Memory(int size) { - this.memory = new int[size]; - } - - public int read(int address) { - return this.memory[address]; - } - - public int size() { - return this.memory.length; - } - - public void write(int address, int data) { - if (address < 0 || address >= this.memory.length) { - throw new IllegalArgumentException("Invalid memory address"); - } - this.memory[address] = data; - } +public abstract class Memory { + public abstract int read(int address); + public abstract int size(); + public abstract void write(int address, Word data); /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */ - public void dump() { - 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(); - } - } -} + public abstract void dump(); +} \ No newline at end of file diff --git a/app/src/main/java/hatelace/ProgramCounter.java b/app/src/main/java/hatelace/ProgramCounter.java index b8b3b04..38dba7b 100644 --- a/app/src/main/java/hatelace/ProgramCounter.java +++ b/app/src/main/java/hatelace/ProgramCounter.java @@ -1,5 +1,6 @@ package hatelace; +/** Simple class to represent the Program Counter */ public class ProgramCounter { private int PC; diff --git a/app/src/main/java/hatelace/Word.java b/app/src/main/java/hatelace/Word.java new file mode 100644 index 0000000..5f6d3bc --- /dev/null +++ b/app/src/main/java/hatelace/Word.java @@ -0,0 +1,9 @@ +package hatelace; + +public abstract class Word { + public abstract T getValue(); + public abstract T add(Word other); + public abstract T subtract(Word other); + public abstract T multiply(Word other); + public abstract T divide(Word other); +} diff --git a/app/src/main/java/hatelace/instructions/Add.java b/app/src/main/java/hatelace/instructions/Add.java index a5d2bad..9fa4959 100644 --- a/app/src/main/java/hatelace/instructions/Add.java +++ b/app/src/main/java/hatelace/instructions/Add.java @@ -3,6 +3,7 @@ package hatelace.instructions; import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; +import hatelace.IntWord; public class Add extends Instruction { private int op1; @@ -16,7 +17,7 @@ public class Add extends Instruction { } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, this.op1 + this.op2); + memory.write(this.dest, new IntWord(this.op1 + this.op2)); PC.incPC(); } diff --git a/app/src/main/java/hatelace/instructions/Copy.java b/app/src/main/java/hatelace/instructions/Copy.java new file mode 100644 index 0000000..a1ce0dc --- /dev/null +++ b/app/src/main/java/hatelace/instructions/Copy.java @@ -0,0 +1,30 @@ +package hatelace.instructions; + +import hatelace.Address; +import hatelace.Word; +import hatelace.Memory; +import hatelace.ProgramCounter; +import hatelace.Instruction; + +public class Copy extends Instruction { + private Word word; + private Address address; + + public Copy(Word word, Address address) { + this.word = word; + this.address = address; + } + + public void execute(Memory memory, ProgramCounter PC) { + memory.write(this.address.getAddress(), this.word); + PC.incPC(); + } + + protected String opcode() { + return "copy"; + } + + protected Object[] operands() { + return new Object[] {this.word, this.address}; + } +} diff --git a/app/src/main/java/hatelace/instructions/Mul.java b/app/src/main/java/hatelace/instructions/Mul.java index a749e6c..4a88590 100644 --- a/app/src/main/java/hatelace/instructions/Mul.java +++ b/app/src/main/java/hatelace/instructions/Mul.java @@ -3,6 +3,7 @@ package hatelace.instructions; import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; +import hatelace.IntWord; public class Mul extends Instruction { private int op1; @@ -16,7 +17,7 @@ public class Mul extends Instruction { } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, this.op1 * this.op2); + memory.write(this.dest, new IntWord(this.op1 * this.op2)); PC.incPC(); }