diff --git a/org.eclipse.jdt.annotation_2.2.600.v20200408-1511.jar b/org.eclipse.jdt.annotation_2.2.600.v20200408-1511.jar new file mode 100644 index 0000000..3d3fecd Binary files /dev/null and b/org.eclipse.jdt.annotation_2.2.600.v20200408-1511.jar differ diff --git a/run/test.droc1 b/run/test.droc1 index 106e648..8f584ca 100644 --- a/run/test.droc1 +++ b/run/test.droc1 @@ -1 +1 @@ -257 16448 16704 1892 29446 65280 16704 1537 16448 258 16449 16704 16450 12303 38 16451 16705 18243 29460 28703 16704 57921 29208 28700 16705 1537 16449 28688 357 16449 28688 16705 1381 29219 28674 16704 4096 28674 16706 1537 2305 16453 256 16454 16706 53829 16454 17989 2305 16453 16706 53829 16454 17989 2305 16453 16706 53829 16454 17989 2305 16453 1537 12544 \ No newline at end of file +8445 8701 16895 16437 256 16438 273 16439 28685 16693 4096 300 12301 16693 29961 256 65280 8704 9216 9729 33026 1793 29979 33026 23040 10752 28713 33026 1793 14848 273 12320 9473 40961 33026 1794 14848 273 12327 9473 42497 9473 8960 12544 16694 1537 16438 14848 16695 12338 9473 16437 12544 \ No newline at end of file diff --git a/src/drcs/Computer.java b/src/drcs/Computer.java index d8f099f..2fe4ca6 100644 --- a/src/drcs/Computer.java +++ b/src/drcs/Computer.java @@ -1,19 +1,25 @@ package drcs; -import static drcs.Helper.*; +import static drcs.Helpers.*; +import static drcs.Main.READER; +import static drcs.Mnemonics.get; import static drcs.Opcodes.*; +import java.io.IOException; import java.util.*; public class Computer { private static final short MEMORY_SIZE = 256; + private static final short HEX_LENGTH = 2; private final Map memory = new HashMap<>(); + private final boolean debug; + private short a = 0, b = 0, bp = 0, sp = 0, pc = 0; - public Computer(String[] code, String[] args) { + public Computer(String[] code, String[] args, boolean debug) { for (short i = 0; i < MEMORY_SIZE; ++i) { if (i < code.length) { memory.put(i, (short) Integer.parseInt(code[i])); @@ -25,6 +31,7 @@ else if (i >= MEMORY_SIZE - args.length) { memory.put(i, (short) 0); } } + this.debug = debug; } private short read(int address) { @@ -58,15 +65,16 @@ private void out() { } public void run() { - short data, instruction, argument; + short prevpc, data, instruction, argument; while (true) { + prevpc = pc; data = next(); instruction = high(data); argument = low(data); switch (instruction) { case HLT: - System.out.println("HALT"); + System.out.println("HALT " + a); return; case NOP: @@ -161,7 +169,7 @@ public void run() { case CALL: push(argument); - jump(next(), true); + jump(a, true); break; case RET: jump(pop(), true); @@ -374,25 +382,38 @@ public void run() { a /= read(bp - argument); break; - case MODI: + case REMI: a %= argument; break; - case MODLI: + case REMLI: a %= next(); break; - case MOD: + case REM: a %= read(argument); break; - case MODPB: + case REMPB: a %= read(bp + argument); break; - case MODNB: + case REMNB: a %= read(bp - argument); break; default: break; } + + if (debug) { + try { + if (READER.readLine() != null) { + System.out.println(hex(prevpc) + "\t" + get(instruction) + "\t" + hex(argument) + "\nA:\tB:\tBP:\tSP:\tPC:\n" + a + "\t" + b + "\t" + bp + "\t" + sp + "\t" + pc); + } + } + catch (IOException e) {} + } } } + + private static String hex(short value) { + return Helpers.hex(value, HEX_LENGTH); + } } diff --git a/src/drcs/Helper.java b/src/drcs/Helpers.java similarity index 50% rename from src/drcs/Helper.java rename to src/drcs/Helpers.java index d34152f..4b37bbe 100644 --- a/src/drcs/Helper.java +++ b/src/drcs/Helpers.java @@ -1,6 +1,12 @@ package drcs; -public class Helper { +import java.util.Locale; + +public class Helpers { + + public static String lowerCase(String s) { + return s.toLowerCase(Locale.ROOT); + } public static short low(short value) { return (short) (value & 0xFF); @@ -25,4 +31,13 @@ public static short mod(short a, short b) { public static short bool(boolean bool) { return (short) (bool ? 1 : 0); } + + public static String hex(short value, short length) { + if (value < 0) { + return "-0x".concat(String.format("%" + length + "s", Integer.toHexString(-value)).replace(' ', '0').toUpperCase(Locale.ROOT)); + } + else { + return "0x".concat(String.format("%" + length + "s", Integer.toHexString(value)).replace(' ', '0').toUpperCase(Locale.ROOT)); + } + } } diff --git a/src/drcs/Main.java b/src/drcs/Main.java index f9c1f37..64684e6 100644 --- a/src/drcs/Main.java +++ b/src/drcs/Main.java @@ -1,23 +1,31 @@ package drcs; -import java.io.IOException; +import java.io.*; import java.nio.charset.Charset; import java.nio.file.*; import java.util.Arrays; public class Main { + public static final BufferedReader READER = new BufferedReader(new InputStreamReader(System.in)); + public static void main(String[] args) { if (args.length == 0) { StringBuilder builder = new StringBuilder(); builder.append("ERROR: input file not specified!\n"); - builder.append("PARAMETERS: input [params...]\n"); - builder.append("EXAMPLE: code.droc1 45 98\n"); + builder.append("PARAMETERS: [\"-s\"] input [params...]\n"); + builder.append("INFO: optionally run in single-step debug mode with \"-s\"\n"); + builder.append("INFO: optionally specify parameters after the input file name\n"); + builder.append("EXAMPLE: program.droc1 45 98\n"); err(builder.toString()); } try { - String[] input = readFile(args[0]).trim().split("\\s+"); - new Computer(input, Arrays.copyOfRange(args, 1, args.length)).run(); + boolean debug = trim(args[0]).equals("s"); + if (debug) { + System.out.println("INFO: Single-step debug mode was enabled!"); + } + String[] input = readFile(args[debug ? 1 : 0]).trim().split("\\s+"); + new Computer(input, Arrays.copyOfRange(args, debug ? 2 : 1, args.length), debug).run(); } catch (Exception e) { e.printStackTrace(); @@ -29,6 +37,10 @@ private static String readFile(String path) throws IOException { return new String(Files.readAllBytes(Paths.get(path)), Charset.defaultCharset()); } + private static String trim(String arg) { + return Helpers.lowerCase(arg.replaceAll("-|_", "")); + } + private static void err(String string) { System.err.print(string); System.exit(1); diff --git a/src/drcs/Mnemonics.java b/src/drcs/Mnemonics.java new file mode 100644 index 0000000..ceef7af --- /dev/null +++ b/src/drcs/Mnemonics.java @@ -0,0 +1,141 @@ +package drcs; + +import static drcs.Opcodes.*; + +import java.util.*; + +public class Mnemonics { + + private static final Map MAP = new HashMap<>(); + + static { + put(HLT, "HLT"); + + put(NOP, "NOP"); + put(LDAI, "LDAI"); + put(NOTI, "NOTI"); + put(ANDI, "ANDI"); + put(ORI, "ORI"); + put(XORI, "XORI"); + put(ADDI, "ADDI"); + put(SUBI, "SUBI"); + put(LSHI, "LSHI"); + put(RSHI, "RSHI"); + + put(OUT, "OUT"); + put(LDALI, "LDALI"); + put(NOTLI, "NOTLI"); + put(ANDLI, "ANDLI"); + put(ORLI, "ORLI"); + put(XORLI, "XORLI"); + put(ADDLI, "ADDLI"); + put(SUBLI, "SUBLI"); + + put(LDBP, "LDBP"); + put(LDSP, "LDSP"); + put(PSHBP, "PSHBP"); + put(POPBP, "POPBP"); + put(MSPBP, "MSPBP"); + put(ADDSP, "ADDSP"); + put(SUBSP, "SUBSP"); + + put(DEA, "DEA"); + put(DEB, "DEB"); + put(STATB, "STATB"); + put(STBTA, "STBTA"); + + put(CALL, "CALL"); + put(RET, "RET"); + + put(PSHA, "PSHA"); + put(POPA, "POPA"); + + put(STA, "STA"); + put(LDA, "LDA"); + put(NOT, "NOT"); + put(AND, "AND"); + put(OR, "OR"); + put(XOR, "XOR"); + put(ADD, "ADD"); + put(SUB, "SUB"); + put(LSH, "LSH"); + put(RSH, "RSH"); + + put(STB, "STB"); + put(LDB, "LDB"); + + put(LDEZ, "LDEZ"); + put(LDNEZ, "LDNEZ"); + put(LDLZ, "LDLZ"); + put(LDLEZ, "LDLEZ"); + put(LDMZ, "LDMZ"); + put(LDMEZ, "LDMEZ"); + + put(LDNOT, "LDNOT"); + put(LDNEG, "LDNEG"); + + put(JMP, "JMP"); + put(JEZ, "JEZ"); + put(JNEZ, "JNEZ"); + put(JLZ, "JLZ"); + put(JLEZ, "JLEZ"); + put(JMZ, "JMZ"); + put(JMEZ, "JMEZ"); + + put(STAPB, "STAPB"); + put(LDAPB, "LDAPB"); + put(NOTPB, "NOTPB"); + put(ANDPB, "ANDPB"); + put(ORPB, "ORPB"); + put(XORPB, "XORPB"); + put(ADDPB, "ADDPB"); + put(SUBPB, "SUBPB"); + put(LSHPB, "LSHPB"); + put(RSHPB, "RSHPB"); + + put(STBPB, "STBPB"); + put(LDBPB, "LDBPB"); + put(LDIPB, "LDIPB"); + + put(STANB, "STANB"); + put(LDANB, "LDANB"); + put(NOTNB, "NOTNB"); + put(ANDNB, "ANDNB"); + put(ORNB, "ORNB"); + put(XORNB, "XORNB"); + put(ADDNB, "ADDNB"); + put(SUBNB, "SUBNB"); + put(LSHNB, "LSHNB"); + put(RSHNB, "RSHNB"); + + put(STBNB, "STBNB"); + put(LDBNB, "LDBNB"); + put(LDINB, "LDINB"); + + put(MULI, "MULI"); + put(MULLI, "MULLI"); + put(MUL, "MUL"); + put(MULPB, "MULPB"); + put(MULNB, "MULNB"); + + put(DIVI, "DIVI"); + put(DIVLI, "DIVLI"); + put(DIV, "DIV"); + put(DIVPB, "DIVPB"); + put(DIVNB, "DIVNB"); + + put(REMI, "REMI"); + put(REMLI, "REMLI"); + put(REM, "REM"); + put(REMPB, "REMPB"); + put(REMNB, "REMNB"); + } + + private static void put(short opcode, String mnemonic) { + MAP.put(opcode, mnemonic); + } + + public static String get(short opcode) { + return MAP.get(opcode); + } +} diff --git a/src/drcs/Opcodes.java b/src/drcs/Opcodes.java index a694dd2..9f15431 100644 --- a/src/drcs/Opcodes.java +++ b/src/drcs/Opcodes.java @@ -117,9 +117,9 @@ public class Opcodes { public static final short DIVPB = 0xD3; public static final short DIVNB = 0xD4; - public static final short MODI = 0xE0; - public static final short MODLI = 0xE1; - public static final short MOD = 0xE2; - public static final short MODPB = 0xE3; - public static final short MODNB = 0xE4; + public static final short REMI = 0xE0; + public static final short REMLI = 0xE1; + public static final short REM = 0xE2; + public static final short REMPB = 0xE3; + public static final short REMNB = 0xE4; }