Skip to content

Commit

Permalink
Added single-step debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdodd4598 committed Jan 10, 2023
1 parent 1ed4630 commit 8914147
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 22 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion run/test.droc1
Original file line number Diff line number Diff line change
@@ -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
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
41 changes: 31 additions & 10 deletions src/drcs/Computer.java
Original file line number Diff line number Diff line change
@@ -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<Short, Short> 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]));
Expand All @@ -25,6 +31,7 @@ else if (i >= MEMORY_SIZE - args.length) {
memory.put(i, (short) 0);
}
}
this.debug = debug;
}

private short read(int address) {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -161,7 +169,7 @@ public void run() {

case CALL:
push(argument);
jump(next(), true);
jump(a, true);
break;
case RET:
jump(pop(), true);
Expand Down Expand Up @@ -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);
}
}
17 changes: 16 additions & 1 deletion src/drcs/Helper.java → src/drcs/Helpers.java
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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));
}
}
}
22 changes: 17 additions & 5 deletions src/drcs/Main.java
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
Expand Down
141 changes: 141 additions & 0 deletions src/drcs/Mnemonics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package drcs;

import static drcs.Opcodes.*;

import java.util.*;

public class Mnemonics {

private static final Map<Short, String> 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);
}
}
10 changes: 5 additions & 5 deletions src/drcs/Opcodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 8914147

Please sign in to comment.