From bed4736ddd3053ba2f8c312eb9ab730f64c85b4d Mon Sep 17 00:00:00 2001 From: clerie Date: Mon, 21 Jan 2019 21:37:46 +0100 Subject: [PATCH] Initial commit --- .idea/encodings.xml | 4 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/uiDesigner.xml | 124 +++++++++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 ++ registermaschine.iml | 11 ++++ src/Configuration.java | 48 ++++++++++++++ src/Instruction.java | 26 ++++++++ src/InstructionAdd.java | 17 +++++ src/InstructionCAdd.java | 17 +++++ src/InstructionCDiv.java | 17 +++++ src/InstructionCMult.java | 17 +++++ src/InstructionCSub.java | 17 +++++ src/InstructionDiv.java | 17 +++++ src/InstructionEnd.java | 14 +++++ src/InstructionGoto.java | 11 ++++ src/InstructionIfGoto.java | 16 +++++ src/InstructionLoad.java | 17 +++++ src/InstructionMult.java | 17 +++++ src/InstructionStore.java | 17 +++++ src/InstructionSub.java | 17 +++++ src/Machine.java | 99 +++++++++++++++++++++++++++++ 22 files changed, 543 insertions(+) create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 registermaschine.iml create mode 100644 src/Configuration.java create mode 100644 src/Instruction.java create mode 100644 src/InstructionAdd.java create mode 100644 src/InstructionCAdd.java create mode 100644 src/InstructionCDiv.java create mode 100644 src/InstructionCMult.java create mode 100644 src/InstructionCSub.java create mode 100644 src/InstructionDiv.java create mode 100644 src/InstructionEnd.java create mode 100644 src/InstructionGoto.java create mode 100644 src/InstructionIfGoto.java create mode 100644 src/InstructionLoad.java create mode 100644 src/InstructionMult.java create mode 100644 src/InstructionStore.java create mode 100644 src/InstructionSub.java create mode 100644 src/Machine.java diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..bfbb0ff --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5a6af28 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/registermaschine.iml b/registermaschine.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/registermaschine.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/Configuration.java b/src/Configuration.java new file mode 100644 index 0000000..974505a --- /dev/null +++ b/src/Configuration.java @@ -0,0 +1,48 @@ +public class Configuration { + public final static int NUM_REGISTERS = 10; + int ic; // Instruction Counter + int[] registers = new int[NUM_REGISTERS]; + + public Configuration() { + init(); + } + + public void init() { + ic = 0; + + for(int i = 0; i < registers.length; i++) { + registers[i] = 0; + } + } + + public int getIC() { + return ic; + } + + public void setIC(int nic) { + ic = nic; + } + + public void incIC() { + ic++; + } + + public void setRegister(int i, int val) { + registers[i] = val; + } + + public int getRegister(int i) { + return registers[i]; + } + + @Override + public String toString() { + String out = ""; + + for(int r : registers) { + out += r + ","; + } + + return "[" + out + "..."; + } +} diff --git a/src/Instruction.java b/src/Instruction.java new file mode 100644 index 0000000..44c3221 --- /dev/null +++ b/src/Instruction.java @@ -0,0 +1,26 @@ +public interface Instruction { + void eval(Configuration config); + String toString(); +} + +/* + * + * ADD + * CADD + * + * SUB + * CSUb + * + * MULT + * CMULT + * + * DIV + * CDIV + * + * GOTO + * IFGOTO + * + * LOAD + * STORE + * + */ \ No newline at end of file diff --git a/src/InstructionAdd.java b/src/InstructionAdd.java new file mode 100644 index 0000000..1622d15 --- /dev/null +++ b/src/InstructionAdd.java @@ -0,0 +1,17 @@ +public class InstructionAdd implements Instruction { + private int reg; + + public InstructionAdd(int i) { + reg = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(0) + config.getRegister(reg)); + config.incIC(); + } + + @Override + public String toString() { + return "ADD " + reg; + } +} diff --git a/src/InstructionCAdd.java b/src/InstructionCAdd.java new file mode 100644 index 0000000..2104f05 --- /dev/null +++ b/src/InstructionCAdd.java @@ -0,0 +1,17 @@ +public class InstructionCAdd implements Instruction { + private int con; + + public InstructionCAdd(int i) { + con = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(0) + con); + config.incIC(); + } + + @Override + public String toString() { + return "CADD " + con; + } +} diff --git a/src/InstructionCDiv.java b/src/InstructionCDiv.java new file mode 100644 index 0000000..12a3ba1 --- /dev/null +++ b/src/InstructionCDiv.java @@ -0,0 +1,17 @@ +public class InstructionCDiv implements Instruction { + private int con; + + public InstructionCDiv(int i) { + con = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(0) / con); + config.incIC(); + } + + @Override + public String toString() { + return "CDIV " + con; + } +} diff --git a/src/InstructionCMult.java b/src/InstructionCMult.java new file mode 100644 index 0000000..6b376ca --- /dev/null +++ b/src/InstructionCMult.java @@ -0,0 +1,17 @@ +public class InstructionCMult implements Instruction { + private int con; + + public InstructionCMult(int i) { + con = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(0) * con); + config.incIC(); + } + + @Override + public String toString() { + return "CMULT " + con; + } +} diff --git a/src/InstructionCSub.java b/src/InstructionCSub.java new file mode 100644 index 0000000..da46a99 --- /dev/null +++ b/src/InstructionCSub.java @@ -0,0 +1,17 @@ +public class InstructionCSub implements Instruction { + private int con; + + public InstructionCSub(int i) { + con = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(0) - con); + config.incIC(); + } + + @Override + public String toString() { + return "CSUB " + con; + } +} diff --git a/src/InstructionDiv.java b/src/InstructionDiv.java new file mode 100644 index 0000000..1067482 --- /dev/null +++ b/src/InstructionDiv.java @@ -0,0 +1,17 @@ +public class InstructionDiv implements Instruction { + private int reg; + + public InstructionDiv(int i) { + reg = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(0) / config.getRegister(reg)); + config.incIC(); + } + + @Override + public String toString() { + return "DIV " + reg; + } +} diff --git a/src/InstructionEnd.java b/src/InstructionEnd.java new file mode 100644 index 0000000..1561454 --- /dev/null +++ b/src/InstructionEnd.java @@ -0,0 +1,14 @@ +public class InstructionEnd implements Instruction { + public InstructionEnd() { + + } + + public void eval(Configuration config) { + + } + + @Override + public String toString() { + return "END"; + } +} diff --git a/src/InstructionGoto.java b/src/InstructionGoto.java new file mode 100644 index 0000000..370141c --- /dev/null +++ b/src/InstructionGoto.java @@ -0,0 +1,11 @@ +public class InstructionGoto implements Instruction { + private int reg; + + public InstructionGoto(int i) { + reg = i; + } + + public void eval(Configuration config) { + config.setIC(reg); + } +} diff --git a/src/InstructionIfGoto.java b/src/InstructionIfGoto.java new file mode 100644 index 0000000..ccbe51d --- /dev/null +++ b/src/InstructionIfGoto.java @@ -0,0 +1,16 @@ +public class InstructionIfGoto implements Instruction { + private int reg; + + public InstructionIfGoto(int i) { + reg = i; + } + + public void eval(Configuration config) { + if(config.getRegister(0) == 0) { + config.setIC(reg); + } + else { + config.incIC(); + } + } +} diff --git a/src/InstructionLoad.java b/src/InstructionLoad.java new file mode 100644 index 0000000..88824cf --- /dev/null +++ b/src/InstructionLoad.java @@ -0,0 +1,17 @@ +public class InstructionLoad implements Instruction { + private int reg; + + public InstructionLoad(int i) { + reg = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(reg)); + config.incIC(); + } + + @Override + public String toString() { + return "LOAD " + reg; + } +} diff --git a/src/InstructionMult.java b/src/InstructionMult.java new file mode 100644 index 0000000..d82ae61 --- /dev/null +++ b/src/InstructionMult.java @@ -0,0 +1,17 @@ +public class InstructionMult implements Instruction { + private int reg; + + public InstructionMult(int i) { + reg = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(0) * config.getRegister(reg)); + config.incIC(); + } + + @Override + public String toString() { + return "MULT " + reg; + } +} diff --git a/src/InstructionStore.java b/src/InstructionStore.java new file mode 100644 index 0000000..61a0867 --- /dev/null +++ b/src/InstructionStore.java @@ -0,0 +1,17 @@ +public class InstructionStore implements Instruction { + private int reg; + + public InstructionStore(int i) { + reg = i; + } + + public void eval(Configuration config) { + config.setRegister(reg, config.getRegister(0)); + config.incIC(); + } + + @Override + public String toString() { + return "STORE " + reg; + } +} diff --git a/src/InstructionSub.java b/src/InstructionSub.java new file mode 100644 index 0000000..e2501c2 --- /dev/null +++ b/src/InstructionSub.java @@ -0,0 +1,17 @@ +public class InstructionSub implements Instruction { + private int reg; + + public InstructionSub(int i) { + reg = i; + } + + public void eval(Configuration config) { + config.setRegister(0, config.getRegister(0) - config.getRegister(reg)); + config.incIC(); + } + + @Override + public String toString() { + return "SUB " + reg; + } +} diff --git a/src/Machine.java b/src/Machine.java new file mode 100644 index 0000000..7de5cf4 --- /dev/null +++ b/src/Machine.java @@ -0,0 +1,99 @@ +public class Machine { + private Configuration config = null; + private Instruction[] program = null; + + public Machine() { + config = new Configuration(); + } + + public void setProgram(Instruction[] prog) { + program = prog; + } + + public void setProgram(String prog) { + String[] lines = prog.split("\\n"); + + Instruction[] instructions = new Instruction[lines.length]; + + for(int i = 0; i < lines.length; i++) { + String[] words = lines[i].split(" "); + + switch (words[0]) { + case "ADD": + instructions[i] = new InstructionAdd(Integer.parseInt(words[1])); + break; + case "CADD": + instructions[i] = new InstructionCAdd(Integer.parseInt(words[1])); + break; + case "SUB": + instructions[i] = new InstructionSub(Integer.parseInt(words[1])); + break; + case "CSUB": + instructions[i] = new InstructionCSub(Integer.parseInt(words[1])); + break; + case "MULT": + instructions[i] = new InstructionMult(Integer.parseInt(words[1])); + break; + case "CMULT": + instructions[i] = new InstructionCMult(Integer.parseInt(words[1])); + break; + case "DIV": + instructions[i] = new InstructionDiv(Integer.parseInt(words[1])); + break; + case "CDIV": + instructions[i] = new InstructionCDiv(Integer.parseInt(words[1])); + break; + case "LOAD": + instructions[i] = new InstructionLoad(Integer.parseInt(words[1])); + break; + case "STORE": + instructions[i] = new InstructionStore(Integer.parseInt(words[1])); + break; + case "GOTO": + instructions[i] = new InstructionGoto(Integer.parseInt(words[1])); + break; + case "IFGOTO": + instructions[i] = new InstructionIfGoto(Integer.parseInt(words[1])); + break; + case "END": + instructions[i] = new InstructionEnd(); + break; + } + } + + program = instructions; + } + + public void run() { + int ic; + while(!program[config.getIC()].toString().equals("END")) { + ic = config.getIC(); + program[ic].eval(config); + + System.out.println(program[ic].toString() + " " + config.toString()); + } + } + + public Configuration getConfiguration() { + return config; + } + + public static void main(String[] args) { + Instruction[] prog = { + new InstructionLoad(1), + new InstructionCAdd(32), + new InstructionStore(1), + new InstructionEnd() + }; + + String str = "LOAD 1\nCADD 32\nEND"; + System.out.println(str); + + Machine machine = new Machine(); + machine.setProgram(str); + machine.getConfiguration().setRegister(1, 32); + machine.run(); + + System.out.println(machine.getConfiguration().toString()); + } +}