From 647ca0c2fad0d4749792698ca09adfa9c6d1b23b Mon Sep 17 00:00:00 2001 From: Ekaitz Zarraga Date: Tue, 19 Jul 2022 14:20:17 +0200 Subject: Fix instructions: The program counter must be reset after each frame, and it's not supposed to be set after each instruction execution. We were setting the PC from the C value of the instructions but that's not what we need to do. The C value is only used for a jump and then, in the next frame the PC must be reloaded from its value in the memory. The only way to change the PC in the program is using it in the B argument of the instructions, which would overwrite it with an arbitrary value (stored in A). --- bytepusher.scm | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/bytepusher.scm b/bytepusher.scm index e822073..54c7e28 100644 --- a/bytepusher.scm +++ b/bytepusher.scm @@ -173,7 +173,7 @@ (lambda () (load-instruction instruction-addr)) (lambda (a b c) (set-byte! b (get-byte a)) - (set-pc! c)))) + c))) (define (load-instruction instruction-addr) (let ((a (get-addr (+ 0 instruction-addr))) @@ -185,9 +185,11 @@ (values a b c))) (define (loop-frame!) - (let loop ((count 65535)) - (execute! (get-pc)) - (unless (= count 0) (loop (- count 1))))) + (let loop ((count 65536) + (nextpc (get-pc))) + (unless (= count 0) + (loop (- count 1) + (execute! nextpc))))) (define (handle-key! action! key) (let ((index (hashq-ref key-ids key))) @@ -240,6 +242,7 @@ (call-with-renderer (make-renderer w '(accelerated)) loop!))) (sdl-quit)) +(main) ;; ---- main operation -- cgit v1.2.3