diff --git a/app/src/main/java/hatelace/IntMemory.java b/app/src/main/java/hatelace/IntMemory.java index 3be2faa..99fb96c 100644 --- a/app/src/main/java/hatelace/IntMemory.java +++ b/app/src/main/java/hatelace/IntMemory.java @@ -19,8 +19,11 @@ public class IntMemory extends Memory { return this.memory.length; } - public void write(Address address, Word data) { - this.memory[address.getAddress()] = data.getValue(); + 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) */ diff --git a/app/src/main/java/hatelace/IntWord.java b/app/src/main/java/hatelace/IntWord.java index d77a068..286e337 100644 --- a/app/src/main/java/hatelace/IntWord.java +++ b/app/src/main/java/hatelace/IntWord.java @@ -7,23 +7,23 @@ public class IntWord extends Word { this.value = value; } - public Integer getValue() { - return value; + public T getValue() { + return (T) value; } - public Word add(Word other) { - return new IntWord(value + (Integer) other.getValue()); + public T add(Word other) { + return (T) new IntWord(value + (Integer) other.getValue()); } - public Word subtract(Word other) { - return new IntWord(value - (Integer) other.getValue()); + public T subtract(Word other) { + return (T) new IntWord(value - (Integer) other.getValue()); } - public Word multiply(Word value) { - return new IntWord(this.value * (Integer) value.getValue()); + public T multiply(Word other) { + return (T) new IntWord(value * (Integer) other.getValue()); } - public Word divide(Word other) { - return 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/Main.java b/app/src/main/java/hatelace/Main.java index 85b8503..dfaa036 100644 --- a/app/src/main/java/hatelace/Main.java +++ b/app/src/main/java/hatelace/Main.java @@ -9,8 +9,8 @@ public class Main { Memory memory = new IntMemory(64); // 64 words of memory Computer computer = new Computer(memory); Program program = new Program(new Instruction[] { - new Add(new IntWord(1), new IntWord(1), new Address(0)), // Store 1 + 1 in address 0 - new Mul(new IntWord(2), new IntWord(2), new Address(1)), // Store 2 * 2 in address 1 + new Add(1, 1, 0), // Store 1 + 1 in address 0 + 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); diff --git a/app/src/main/java/hatelace/Memory.java b/app/src/main/java/hatelace/Memory.java index 541a0d8..b4aaa41 100644 --- a/app/src/main/java/hatelace/Memory.java +++ b/app/src/main/java/hatelace/Memory.java @@ -3,7 +3,7 @@ package hatelace; public abstract class Memory { public abstract int read(int address); public abstract int size(); - public abstract void write(Address address, Word data); + public abstract void write(int address, Word data); /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */ public abstract void dump(); diff --git a/app/src/main/java/hatelace/Word.java b/app/src/main/java/hatelace/Word.java index c4e19c1..5f6d3bc 100644 --- a/app/src/main/java/hatelace/Word.java +++ b/app/src/main/java/hatelace/Word.java @@ -2,8 +2,8 @@ package hatelace; public abstract class Word { public abstract T getValue(); - public abstract Word add(Word other); - public abstract Word subtract(Word other); - public abstract Word multiply(Word other); - public abstract Word divide(Word other); + 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 e17c023..9fa4959 100644 --- a/app/src/main/java/hatelace/instructions/Add.java +++ b/app/src/main/java/hatelace/instructions/Add.java @@ -3,22 +3,21 @@ package hatelace.instructions; import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; -import hatelace.Word; -import hatelace.Address; +import hatelace.IntWord; public class Add extends Instruction { - private Word op1; - private Word op2; - private Address dest; + private int op1; + private int op2; + private int dest; - public Add(Word op1, Word op2, Address 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, op1.add(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 index 0da376c..a1ce0dc 100644 --- a/app/src/main/java/hatelace/instructions/Copy.java +++ b/app/src/main/java/hatelace/instructions/Copy.java @@ -16,7 +16,7 @@ public class Copy extends Instruction { } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.address, this.word); + memory.write(this.address.getAddress(), this.word); PC.incPC(); } diff --git a/app/src/main/java/hatelace/instructions/Mul.java b/app/src/main/java/hatelace/instructions/Mul.java index 6c587ed..4a88590 100644 --- a/app/src/main/java/hatelace/instructions/Mul.java +++ b/app/src/main/java/hatelace/instructions/Mul.java @@ -1,24 +1,23 @@ package hatelace.instructions; -import hatelace.Address; -import hatelace.Word; import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; +import hatelace.IntWord; public class Mul extends Instruction { - private Word op1; - private Word op2; - private Address dest; + private int op1; + private int op2; + private int dest; - public Mul(Word op1, Word op2, Address 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, op1.multiply(op2)); + memory.write(this.dest, new IntWord(this.op1 * this.op2)); PC.incPC(); }