From b301ba7669ec34e8456cf4e68b4622d5e501ed16 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 16 Apr 2024 12:54:33 +0200 Subject: [PATCH 1/3] Instructions --- app/src/main/java/hatelace/instructions/Add.java | 12 +++++++----- app/src/main/java/hatelace/instructions/Copy.java | 2 +- app/src/main/java/hatelace/instructions/Mul.java | 12 +++++++----- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/hatelace/instructions/Add.java b/app/src/main/java/hatelace/instructions/Add.java index 9fa4959..979b0d7 100644 --- a/app/src/main/java/hatelace/instructions/Add.java +++ b/app/src/main/java/hatelace/instructions/Add.java @@ -4,20 +4,22 @@ import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; import hatelace.IntWord; +import hatelace.Word; +import hatelace.Address; public class Add extends Instruction { - private int op1; - private int op2; - private int dest; + private Word op1; + private Word op2; + private Address dest; - public Add(int op1, int op2, int dest) { + public Add(Word 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, new IntWord(this.op1 + this.op2)); + memory.write(this.dest, new IntWord(op1.add(op2))); PC.incPC(); } diff --git a/app/src/main/java/hatelace/instructions/Copy.java b/app/src/main/java/hatelace/instructions/Copy.java index a1ce0dc..0da376c 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.getAddress(), this.word); + memory.write(this.address, 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 4a88590..23c69b8 100644 --- a/app/src/main/java/hatelace/instructions/Mul.java +++ b/app/src/main/java/hatelace/instructions/Mul.java @@ -1,23 +1,25 @@ 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 int op1; - private int op2; - private int dest; + private Word op1; + private Word op2; + private Address dest; - public Mul(int op1, int op2, int dest) { + public Mul(Word 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, new IntWord(this.op1 * this.op2)); + memory.write(this.dest, new IntWord(op1.multiply(op2))); PC.incPC(); } From 7d7509e8528c5f3998a5127c4622058655c7ed78 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 16 Apr 2024 12:56:14 +0200 Subject: [PATCH 2/3] Checkpoint --- app/src/main/java/hatelace/IntMemory.java | 7 ++----- app/src/main/java/hatelace/IntWord.java | 18 +++++++++++++----- app/src/main/java/hatelace/Main.java | 4 ++-- app/src/main/java/hatelace/Memory.java | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/hatelace/IntMemory.java b/app/src/main/java/hatelace/IntMemory.java index 99fb96c..3be2faa 100644 --- a/app/src/main/java/hatelace/IntMemory.java +++ b/app/src/main/java/hatelace/IntMemory.java @@ -19,11 +19,8 @@ public class IntMemory extends Memory { 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(); + public void write(Address address, Word data) { + this.memory[address.getAddress()] = 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 286e337..7473907 100644 --- a/app/src/main/java/hatelace/IntWord.java +++ b/app/src/main/java/hatelace/IntWord.java @@ -7,8 +7,16 @@ public class IntWord extends Word { this.value = value; } - public T getValue() { - return (T) value; + // public T getValue() { + // return value; + // } + + public Integer getValue() { + return value; + } + + public IntWord multiply(Word value) { + return new IntWord(this.value * (Integer) value.getValue()); } public T add(Word other) { @@ -19,9 +27,9 @@ public class IntWord extends Word { return (T) new IntWord(value - (Integer) other.getValue()); } - public T multiply(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/Main.java b/app/src/main/java/hatelace/Main.java index dfaa036..85b8503 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(1, 1, 0), // Store 1 + 1 in address 0 - new Mul(2, 2, 1), // Store 2 * 2 in address 1 + 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 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 b4aaa41..541a0d8 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(int address, Word data); + public abstract void write(Address address, Word data); /** Dump as IHEX-like format (see https://en.wikipedia.org/wiki/Intel_HEX) */ public abstract void dump(); From e0845968a17e2dc3ff7e7634d9fc2c4a21ca15d4 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 16 Apr 2024 13:02:44 +0200 Subject: [PATCH 3/3] Checkpoint 2 --- app/src/main/java/hatelace/IntWord.java | 30 +++++++------------ app/src/main/java/hatelace/Word.java | 8 ++--- .../main/java/hatelace/instructions/Add.java | 3 +- .../main/java/hatelace/instructions/Mul.java | 3 +- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/hatelace/IntWord.java b/app/src/main/java/hatelace/IntWord.java index 7473907..d77a068 100644 --- a/app/src/main/java/hatelace/IntWord.java +++ b/app/src/main/java/hatelace/IntWord.java @@ -7,31 +7,23 @@ public class IntWord extends Word { this.value = value; } - // public T getValue() { - // return value; - // } - public Integer getValue() { return value; } - public IntWord multiply(Word value) { + public Word add(Word other) { + return new IntWord(value + (Integer) other.getValue()); + } + + public Word subtract(Word other) { + return new IntWord(value - (Integer) other.getValue()); + } + + public Word multiply(Word value) { return new IntWord(this.value * (Integer) value.getValue()); } - 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()); + public Word divide(Word other) { + return new IntWord(value / (Integer) other.getValue()); } } diff --git a/app/src/main/java/hatelace/Word.java b/app/src/main/java/hatelace/Word.java index 5f6d3bc..c4e19c1 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 T add(Word other); - public abstract T subtract(Word other); - public abstract T multiply(Word other); - public abstract T divide(Word other); + public abstract Word add(Word other); + public abstract Word subtract(Word other); + public abstract Word multiply(Word other); + public abstract Word divide(Word other); } diff --git a/app/src/main/java/hatelace/instructions/Add.java b/app/src/main/java/hatelace/instructions/Add.java index 979b0d7..e17c023 100644 --- a/app/src/main/java/hatelace/instructions/Add.java +++ b/app/src/main/java/hatelace/instructions/Add.java @@ -3,7 +3,6 @@ package hatelace.instructions; import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; -import hatelace.IntWord; import hatelace.Word; import hatelace.Address; @@ -19,7 +18,7 @@ public class Add extends Instruction { } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, new IntWord(op1.add(op2))); + memory.write(this.dest, op1.add(op2)); PC.incPC(); } diff --git a/app/src/main/java/hatelace/instructions/Mul.java b/app/src/main/java/hatelace/instructions/Mul.java index 23c69b8..6c587ed 100644 --- a/app/src/main/java/hatelace/instructions/Mul.java +++ b/app/src/main/java/hatelace/instructions/Mul.java @@ -5,7 +5,6 @@ import hatelace.Word; import hatelace.Instruction; import hatelace.Memory; import hatelace.ProgramCounter; -import hatelace.IntWord; public class Mul extends Instruction { private Word op1; @@ -19,7 +18,7 @@ public class Mul extends Instruction { } public void execute(Memory memory, ProgramCounter PC) { - memory.write(this.dest, new IntWord(op1.multiply(op2))); + memory.write(this.dest, op1.multiply(op2)); PC.incPC(); }