summaryrefslogtreecommitdiff
path: root/sources/meta
diff options
context:
space:
mode:
Diffstat (limited to 'sources/meta')
-rw-r--r--sources/meta/disasm.c72
-rw-r--r--sources/meta/exec.c82
2 files changed, 118 insertions, 36 deletions
diff --git a/sources/meta/disasm.c b/sources/meta/disasm.c
index 2a33aed..66c1478 100644
--- a/sources/meta/disasm.c
+++ b/sources/meta/disasm.c
@@ -1,39 +1,39 @@
-#define X_C8_ILL sprintf(usrdat, "ILL unknown")
-#define X_C8_CLS sprintf(usrdat, "CLS")
-#define X_C8_RET sprintf(usrdat, "RET")
-#define X_C8_SYS sprintf(usrdat, "SYS 0x%03X", nnn)
-#define X_C8_JP sprintf(usrdat, "JP 0x%03X", nnn)
-#define X_C8_CALL sprintf(usrdat, "CALL 0x%03X", nnn)
-#define X_C8_LDI sprintf(usrdat, "LD I, 0x%03X", nnn)
-#define X_C8_JPV sprintf(usrdat, "JP V0, 0x%03X", nnn)
-#define X_C8_SEB sprintf(usrdat, "SE V%x, %d", x, kk)
-#define X_C8_SNEB sprintf(usrdat, "SNE V%x, %d", x, kk)
-#define X_C8_LD sprintf(usrdat, "LD V%x, %d", x, kk)
-#define X_C8_ADDB sprintf(usrdat, "ADD V%x, %d", x, kk)
-#define X_C8_RND sprintf(usrdat, "RND V%x, %d", x, kk)
-#define X_C8_SKP sprintf(usrdat, "SKP V%x", x)
-#define X_C8_SKNP sprintf(usrdat, "SKNP V%x", x)
-#define X_C8_MVDT sprintf(usrdat, "LD V%x, DT", x)
-#define X_C8_LDK sprintf(usrdat, "LD V%x, K", x)
-#define X_C8_LDDT sprintf(usrdat, "LD DT, V%x", x)
-#define X_C8_LDST sprintf(usrdat, "LD ST, V%x", x)
-#define X_C8_ADDI sprintf(usrdat, "ADD I, V%x", x)
-#define X_C8_HEX sprintf(usrdat, "LD F, V%x", x)
-#define X_C8_BCD sprintf(usrdat, "LD B, V%x", x)
-#define X_C8_SAVE sprintf(usrdat, "LD [I], V%x", x)
-#define X_C8_RESTORE sprintf(usrdat, "LD V%x, [I]", x)
-#define X_C8_SE sprintf(usrdat, "SE V%x, V%x", x, y)
-#define X_C8_MOVE sprintf(usrdat, "LD V%x, V%x", x, y)
-#define X_C8_OR sprintf(usrdat, "OR V%x, V%x", x, y)
-#define X_C8_AND sprintf(usrdat, "AND V%x, V%x", x, y)
-#define X_C8_XOR sprintf(usrdat, "XOR V%x, V%x", x, y)
-#define X_C8_ADD sprintf(usrdat, "ADD V%x, V%x", x, y)
-#define X_C8_SUB sprintf(usrdat, "SUB V%x, V%x", x, y)
-#define X_C8_SHR sprintf(usrdat, "SHR V%x, V%x", x, y)
-#define X_C8_SUBN sprintf(usrdat, "SUBN V%x, V%x", x, y)
-#define X_C8_SHL sprintf(usrdat, "SHL V%x, V%x", x, y)
-#define X_C8_SNE sprintf(usrdat, "SNE V%x, V%x", x, y)
-#define X_C8_DRW sprintf(usrdat, "DRW V%x, V%x, %d", x, y, n)
+#define X_C8_ILL sprintf(usrdat, "%04X ILL unknown", instr)
+#define X_C8_CLS sprintf(usrdat, "%04X CLS", instr)
+#define X_C8_RET sprintf(usrdat, "%04X RET", instr)
+#define X_C8_SYS sprintf(usrdat, "%04X SYS 0x%03X", instr, nnn)
+#define X_C8_JP sprintf(usrdat, "%04X JP 0x%03X", instr, nnn)
+#define X_C8_CALL sprintf(usrdat, "%04X CALL 0x%03X", instr, nnn)
+#define X_C8_LDI sprintf(usrdat, "%04X LD I, 0x%03X", instr, nnn)
+#define X_C8_JPV sprintf(usrdat, "%04X JP V0, 0x%03X", instr, nnn)
+#define X_C8_SEB sprintf(usrdat, "%04X SE V%x, %d", instr, x, kk)
+#define X_C8_SNEB sprintf(usrdat, "%04X SNE V%x, %d", instr, x, kk)
+#define X_C8_LD sprintf(usrdat, "%04X LD V%x, %d", instr, x, kk)
+#define X_C8_ADDB sprintf(usrdat, "%04X ADD V%x, %d", instr, x, kk)
+#define X_C8_RND sprintf(usrdat, "%04X RND V%x, %d", instr, x, kk)
+#define X_C8_SKP sprintf(usrdat, "%04X SKP V%x", instr, x)
+#define X_C8_SKNP sprintf(usrdat, "%04X SKNP V%x", instr, x)
+#define X_C8_MVDT sprintf(usrdat, "%04X LD V%x, DT", instr, x)
+#define X_C8_LDK sprintf(usrdat, "%04X LD V%x, K", instr, x)
+#define X_C8_LDDT sprintf(usrdat, "%04X LD DT, V%x", instr, x)
+#define X_C8_LDST sprintf(usrdat, "%04X LD ST, V%x", instr, x)
+#define X_C8_ADDI sprintf(usrdat, "%04X ADD I, V%x", instr, x)
+#define X_C8_HEX sprintf(usrdat, "%04X LD F, V%x", instr, x)
+#define X_C8_BCD sprintf(usrdat, "%04X LD B, V%x", instr, x)
+#define X_C8_SAVE sprintf(usrdat, "%04X LD [I], V%x", instr, x)
+#define X_C8_RESTORE sprintf(usrdat, "%04X LD V%x, [I]", instr, x)
+#define X_C8_SE sprintf(usrdat, "%04X SE V%x, V%x", instr, x, y)
+#define X_C8_MOVE sprintf(usrdat, "%04X LD V%x, V%x", instr, x, y)
+#define X_C8_OR sprintf(usrdat, "%04X OR V%x, V%x", instr, x, y)
+#define X_C8_AND sprintf(usrdat, "%04X AND V%x, V%x", instr, x, y)
+#define X_C8_XOR sprintf(usrdat, "%04X XOR V%x, V%x", instr, x, y)
+#define X_C8_ADD sprintf(usrdat, "%04X ADD V%x, V%x", instr, x, y)
+#define X_C8_SUB sprintf(usrdat, "%04X SUB V%x, V%x", instr, x, y)
+#define X_C8_SHR sprintf(usrdat, "%04X SHR V%x, V%x", instr, x, y)
+#define X_C8_SUBN sprintf(usrdat, "%04X SUBN V%x, V%x", instr, x, y)
+#define X_C8_SHL sprintf(usrdat, "%04X SHL V%x, V%x", instr, x, y)
+#define X_C8_SNE sprintf(usrdat, "%04X SNE V%x, V%x", instr, x, y)
+#define X_C8_DRW sprintf(usrdat, "%04X DRW V%x, V%x, %d", instr, x, y, n)
c8_decode_generate(disasm)
diff --git a/sources/meta/exec.c b/sources/meta/exec.c
new file mode 100644
index 0000000..69e9a8a
--- /dev/null
+++ b/sources/meta/exec.c
@@ -0,0 +1,82 @@
+#define BEGIN \
+do{ \
+ chip8* c8 = usrdat; \
+ assert(c8);
+
+#define END }while(0)
+
+#define X_C8_ILL \
+BEGIN \
+ \
+ c8->running = false; \
+ report(ERR, "Illegal instruction (%04X) @ %04X\n", instr, offset); \
+END
+
+#define X_C8_CLS X_C8_ILL
+#define X_C8_RET X_C8_ILL
+#define X_C8_SYS X_C8_ILL
+
+#define X_C8_JP \
+BEGIN \
+ c8->pc = nnn - 2; \
+END
+
+#define X_C8_CALL X_C8_ILL
+#define X_C8_LDI X_C8_ILL
+#define X_C8_JPV X_C8_ILL
+
+#define X_C8_SEB \
+BEGIN \
+ if(c8->V[x] == kk) c8->pc += 2; \
+END
+
+#define X_C8_SNEB X_C8_ILL
+
+#define X_C8_LD \
+BEGIN \
+ c8->V[x] = kk; \
+END
+
+#define X_C8_ADDB \
+BEGIN \
+ int tmp = (int)c8->V[x] + (int)kk; \
+ c8->V[x] = tmp & 0xFF; \
+END
+
+#define X_C8_RND X_C8_ILL
+#define X_C8_SKP X_C8_ILL
+#define X_C8_SKNP X_C8_ILL
+#define X_C8_MVDT X_C8_ILL
+#define X_C8_LDK X_C8_ILL
+#define X_C8_LDDT X_C8_ILL
+#define X_C8_LDST X_C8_ILL
+#define X_C8_ADDI X_C8_ILL
+#define X_C8_HEX X_C8_ILL
+#define X_C8_BCD X_C8_ILL
+#define X_C8_SAVE X_C8_ILL
+#define X_C8_RESTORE X_C8_ILL
+#define X_C8_SE X_C8_ILL
+#define X_C8_MOVE X_C8_ILL
+#define X_C8_OR X_C8_ILL
+#define X_C8_AND X_C8_ILL
+#define X_C8_XOR X_C8_ILL
+
+#define X_C8_ADD \
+BEGIN \
+ int tmp = (int)c8->V[x] + (int)c8->V[y]; \
+ c8->V[0xf] = (tmp > 255); \
+ c8->V[x] = tmp & 0xFF; \
+END
+
+#define X_C8_SUB X_C8_ILL
+#define X_C8_SHR X_C8_ILL
+#define X_C8_SUBN X_C8_ILL
+#define X_C8_SHL X_C8_ILL
+#define X_C8_SNE X_C8_ILL
+#define X_C8_DRW X_C8_ILL
+
+c8_decode_generate(exec)
+
+#undef BEGIN
+#undef END
+#include "undef.c"