From 1bdb35a8995ae4b6f2517efaed7e2f7d742e6a33 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 21 Apr 2024 22:17:49 +0200 Subject: [PATCH] Instruction abstract -> interface --- app/src/main/java/hatelace/Instruction.java | 13 +++++-------- app/src/main/java/hatelace/instructions/Add.java | 6 +++--- app/src/main/java/hatelace/instructions/Copy.java | 6 +++--- app/src/main/java/hatelace/instructions/Halt.java | 6 +++--- app/src/main/java/hatelace/instructions/Jump.java | 6 +++--- app/src/main/java/hatelace/instructions/JumpEq.java | 6 +++--- app/src/main/java/hatelace/instructions/Mul.java | 6 +++--- app/src/main/java/hatelace/instructions/Print.java | 6 +++--- 8 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/hatelace/Instruction.java b/app/src/main/java/hatelace/Instruction.java index f7d04be..43d5bfb 100644 --- a/app/src/main/java/hatelace/Instruction.java +++ b/app/src/main/java/hatelace/Instruction.java @@ -1,11 +1,8 @@ package hatelace; -public abstract class Instruction { - public abstract void execute(Memory memory, ProgramCounter pc); - - protected abstract String opcode(); - - protected abstract Object[] operands(); - - public abstract String toString(); +public interface Instruction { + void execute(Memory memory, ProgramCounter pc); + String opcode(); + Object[] operands(); + String toString(); } diff --git a/app/src/main/java/hatelace/instructions/Add.java b/app/src/main/java/hatelace/instructions/Add.java index fc5c2ab..e45609c 100644 --- a/app/src/main/java/hatelace/instructions/Add.java +++ b/app/src/main/java/hatelace/instructions/Add.java @@ -2,7 +2,7 @@ package hatelace.instructions; import hatelace.*; -public class Add extends Instruction { +public class Add implements Instruction { private Address src; private Word imm; private Address dest; @@ -19,7 +19,7 @@ public class Add extends Instruction { PC.incPC(); } - protected String opcode() { + public String opcode() { return "ADD"; } @@ -27,7 +27,7 @@ public class Add extends Instruction { return String.format("%s [%s] %s [%s]", this.opcode(), this.src, this.imm, this.dest); } - protected Object[] operands() { + public Object[] operands() { return new Object[] { this.src, this.imm, this.dest }; } } diff --git a/app/src/main/java/hatelace/instructions/Copy.java b/app/src/main/java/hatelace/instructions/Copy.java index 985ce1a..215b500 100644 --- a/app/src/main/java/hatelace/instructions/Copy.java +++ b/app/src/main/java/hatelace/instructions/Copy.java @@ -2,7 +2,7 @@ package hatelace.instructions; import hatelace.*; -public class Copy extends Instruction { +public class Copy implements Instruction { private Word imm; private Address dest; @@ -17,11 +17,11 @@ public class Copy extends Instruction { PC.incPC(); } - protected String opcode() { + public String opcode() { return "CPY"; } - protected Object[] operands() { + public Object[] operands() { return new Object[] {this.imm, this.dest}; } diff --git a/app/src/main/java/hatelace/instructions/Halt.java b/app/src/main/java/hatelace/instructions/Halt.java index 7823e35..3fc8485 100644 --- a/app/src/main/java/hatelace/instructions/Halt.java +++ b/app/src/main/java/hatelace/instructions/Halt.java @@ -2,7 +2,7 @@ package hatelace.instructions; import hatelace.*; -public class Halt extends Instruction { +public class Halt implements Instruction { /** Halts the CPU */ public Halt(){}; @@ -10,11 +10,11 @@ public class Halt extends Instruction { PC.halt(); } - protected String opcode() { + public String opcode() { return "HLT"; } - protected Object[] operands() { + public Object[] operands() { return new Object[] {}; } diff --git a/app/src/main/java/hatelace/instructions/Jump.java b/app/src/main/java/hatelace/instructions/Jump.java index 972945f..5761c4c 100644 --- a/app/src/main/java/hatelace/instructions/Jump.java +++ b/app/src/main/java/hatelace/instructions/Jump.java @@ -2,7 +2,7 @@ package hatelace.instructions; import hatelace.*; -public class Jump extends Instruction { +public class Jump implements Instruction { private int index; /** Unconditional jump, non-relative */ @@ -14,11 +14,11 @@ public class Jump extends Instruction { PC.setPC(this.index); } - protected String opcode() { + public String opcode() { return "JMP"; } - protected Object[] operands() { + public Object[] operands() { return new Object[] {this.index}; } diff --git a/app/src/main/java/hatelace/instructions/JumpEq.java b/app/src/main/java/hatelace/instructions/JumpEq.java index f2538be..9152637 100644 --- a/app/src/main/java/hatelace/instructions/JumpEq.java +++ b/app/src/main/java/hatelace/instructions/JumpEq.java @@ -2,7 +2,7 @@ package hatelace.instructions; import hatelace.*; -public class JumpEq extends Instruction { +public class JumpEq implements Instruction { private int index; private Address src; private Word imm; @@ -22,11 +22,11 @@ public class JumpEq extends Instruction { } } - protected String opcode() { + public String opcode() { return "JEQ"; } - protected Object[] operands() { + public Object[] operands() { return new Object[] {this.index, this.src, this.imm}; } diff --git a/app/src/main/java/hatelace/instructions/Mul.java b/app/src/main/java/hatelace/instructions/Mul.java index cd61d81..0cf60da 100644 --- a/app/src/main/java/hatelace/instructions/Mul.java +++ b/app/src/main/java/hatelace/instructions/Mul.java @@ -2,7 +2,7 @@ package hatelace.instructions; import hatelace.*; -public class Mul extends Instruction { +public class Mul implements Instruction { private Address src1; private Address src2; private Address dest; @@ -19,11 +19,11 @@ public class Mul extends Instruction { PC.incPC(); } - protected String opcode() { + public String opcode() { return "MUL"; } - protected Object[] operands() { + public Object[] operands() { return new Object[] { this.src1, this.src2, this.dest }; } diff --git a/app/src/main/java/hatelace/instructions/Print.java b/app/src/main/java/hatelace/instructions/Print.java index 17e7ce6..f73ce0b 100644 --- a/app/src/main/java/hatelace/instructions/Print.java +++ b/app/src/main/java/hatelace/instructions/Print.java @@ -2,7 +2,7 @@ package hatelace.instructions; import hatelace.*; -public class Print extends Instruction { +public class Print implements Instruction { private Address address; /** Print content of memory address */ @@ -15,11 +15,11 @@ public class Print extends Instruction { PC.incPC(); } - protected String opcode() { + public String opcode() { return "PRT"; } - protected Object[] operands() { + public Object[] operands() { return new Object[] { this.address }; }