From 750cd23d7afac165502defa1d259ace00ca0e414 Mon Sep 17 00:00:00 2001
From: dweller <dweller@cabin.digital>
Date: Wed, 26 Mar 2025 20:37:01 +0200
Subject: clean up chip-8 into its own file

---
 sources/meta/exec.c | 34 ++++++++++++++++++++++++++++------
 1 file changed, 28 insertions(+), 6 deletions(-)

(limited to 'sources/meta/exec.c')

diff --git a/sources/meta/exec.c b/sources/meta/exec.c
index b913390..a39aef2 100644
--- a/sources/meta/exec.c
+++ b/sources/meta/exec.c
@@ -1,11 +1,33 @@
-/* FIXME: add a one time init and deinit to generate macro */
-#define BEGIN           \
-do{                     \
-    chip8* c8 = usrdat; \
-    assert(c8);
-
+#define BEGIN do{
 #define END }while(0)
 
+#define X_C8_PRELUDE \
+    chip8* c8 = usrdat; \
+    assert(c8); \
+\
+    if(c8->pc >= sizeof(c8->mem)) \
+    { \
+        report(ERR, "CHIP-8: PC(%04X) Overflow!\n", c8->pc); \
+        c8->running = false; \
+    } \
+\
+    if(c8->pc & 0x0001) \
+    { \
+        report(ERR, "CHIP-8: PC(%04X) is not aligned!\n", c8->pc); \
+        c8->running = false; \
+    }
+
+#define X_C8_EPILOGUE \
+BEGIN \
+    c8->cycles++; \
+    if((c8->cycles % C8_CYCLES_PER_FRAME) == 0) \
+    { \
+        if(c8->dt) c8->dt--; \
+        if(c8->st) c8->st--; \
+    } \
+    c8->pc += 2; \
+END
+
 #define X_C8_ILL \
 BEGIN \
     c8->running = false; \
-- 
cgit v1.2.3