From 9c2842629f2e5f17c6949b5ff37c972f7dd62d4a Mon Sep 17 00:00:00 2001
From: dweller <dweller@cabin.digital>
Date: Wed, 2 Apr 2025 23:12:40 +0300
Subject: just playing around

---
 sources/meta/exec.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

(limited to 'sources/meta')

diff --git a/sources/meta/exec.c b/sources/meta/exec.c
index b5aaba4..4f34d1b 100644
--- a/sources/meta/exec.c
+++ b/sources/meta/exec.c
@@ -34,6 +34,7 @@ BEGIN \
         if(c8->st) c8->st--; \
     } \
     c8->pc += 2; \
+    c8->prevkeys = c8->keys; \
 END
 
 #define X_C8_ILL \
@@ -104,12 +105,12 @@ END
 
 #define X_C8_SKP \
 BEGIN \
-    if(c8->keys & c8->V[x]) c8->pc += 2; \
+    if(c8->keys & bit(c8->V[x])) c8->pc += 2; \
 END
 
 #define X_C8_SKNP \
 BEGIN \
-    if(!(c8->keys & c8->V[x])) c8->pc += 2; \
+    if(!(c8->keys & bit(c8->V[x]))) c8->pc += 2; \
 END
 
 #define X_C8_MVDT \
@@ -117,10 +118,19 @@ BEGIN \
     c8->V[x] = c8->dt; \
 END
 
-/* TODO: implement */
 #define X_C8_LDK \
 BEGIN \
-    if(!c8->keys) c8->pc -= 2; \
+    int i; \
+    if(c8->keys == c8->prevkeys) c8->pc -= 2; \
+    else \
+    { \
+        for(i = 7; i >= 0; i--) \
+            if(c8->keys & bit(i)) \
+            { \
+                c8->V[x] = i; \
+                break; \
+            } \
+    } \
 END
 
 #define X_C8_LDDT \
@@ -227,8 +237,7 @@ BEGIN \
     if(c8->V[x] != c8->V[y]) c8->pc += 2; \
 END
 
-
-/* TODO: actually implement */
+/* FIXME: correct wrapping, test */
 #define X_C8_DRW \
 BEGIN \
     u8 l; \
@@ -236,7 +245,7 @@ BEGIN \
     u8 cy = c8->V[y]; \
     \
     for(l = 0; l < n; l++) \
-        c8->DISPLAY[cy + l * cx] ^= c8->RAM[c8->i + l]; \
+        c8->DISPLAY[(cy + l) % 32] ^= ((u64)c8->RAM[c8->i + l]) << (64 - cx); \
 END
 
 c8_decode_generate(exec)
-- 
cgit v1.2.3