From ec1e7e3d69c8f2c81defca8bf98178a65d76b8fd Mon Sep 17 00:00:00 2001 From: d_m Date: Mon, 20 Jan 2025 00:29:59 -0500 Subject: [PATCH] clean up code and get drifloon test passing --- vm.scm | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/vm.scm b/vm.scm index 87aa547..2bd5ec9 100644 --- a/vm.scm +++ b/vm.scm @@ -111,6 +111,7 @@ (lo (vector-ref mem (u16 (+ pc 1))))) (s16 (hilo->u16 hi lo)))) +; restore a stack pointer when in keep mode (define (restore k) (if (null? k) '() @@ -126,6 +127,7 @@ (define (jmp n) (set! pc (if _2 n (+ pc (s8 n))))) + (define (jmi) (let ((offset (_load-s16))) (set! pc (+ pc offset 2)))) @@ -141,7 +143,7 @@ (define (jmi-impl k) (jmi)) (define (jsi-impl k) (_rpush2 (+ pc 2)) (jmi)) (define (lit-impl k) (_read pc 65536) (set! pc (+ pc (if _2 2 1)))) -(define (inc-impl k) (let* ((a (_pop)) (_ (restore k))) (_push (+ a 1)))) +(define (inc-impl k) (let* ((a (_pop))) (restore k) (_push (+ a 1)))) (define (pop-impl k) (_pop) (restore k)) (define (nip-impl k) (let* ((a (_pop)) (_ (_pop))) (restore k) (_push a))) (define (swp-impl k) (let* ((b (_pop)) (a (_pop))) (restore k) (_push b) (_push a))) @@ -159,7 +161,7 @@ (define (ldz-impl k) (let* ((zp (_pop1))) (restore k) (_read zp 256))) (define (stz-impl k) (let* ((zp (_pop1)) (n (_pop))) (restore k) (_write zp n 256))) (define (ldr-impl k) (let* ((r (_pop1))) (restore k) (_read (+ pc (s8 r)) 65536))) -(define (str-impl k) (let* ((off (_pop1)) (n (_pop))) (restore k) (_write (+ pc (s8 off)) n 65536))) +(define (str-impl k) (let* ((r (_pop1)) (n (_pop))) (restore k) (_write (+ pc (s8 r)) n 65536))) (define (lda-impl k) (let ((n (_pop2))) (restore k) (_read n 65536))) (define (sta-impl k) (let* ((addr (_pop2)) (n (_pop))) (restore k) (_write addr n 65536))) (define (add-impl k) (let* ((b (_pop)) (a (_pop))) (restore k) (_push (+ a b)))) @@ -186,15 +188,6 @@ (define (deo-impl k) (let* ((dev (_pop1)) (n (_pop))) (restore k) - ;; (display "deo-impl 2=") - ;; (display _2) - ;; (display " k=") - ;; (display k) - ;; (display " n=") - ;; (display n) - ;; (display " dev=") - ;; (display dev) - ;; (display "\n") (if _2 (begin (_deo (u16->hi n) dev) @@ -226,7 +219,7 @@ (vector-set! instructions (+ base 96) (op impl #t #f #t)) ; 2-r (vector-set! instructions (+ base 128) (op impl #f #t #f)) ; -k- (vector-set! instructions (+ base 160) (op impl #t #t #f)) ; 2k- - (vector-set! instructions (+ base 192) (op impl #t #f #t)) ; 2-r + (vector-set! instructions (+ base 192) (op impl #f #t #t)) ; -kr (vector-set! instructions (+ base 224) (op impl #t #t #t))) ; 2kr ; hardcoded instructions for base 0x00 @@ -322,7 +315,8 @@ (define (u:read-stdin port) (let ((c (read-char port))) - (if (eof-object? c) '() + (if (eof-object? c) + (u:send-input #\newline 4) (begin (u:send-input c 1) (u:read-stdin port))))) @@ -345,6 +339,6 @@ (vector-set! dev 23 (if (null? rom-args) 0 1)) (u:load-rom rom start) (u:run start) - (u:read-args rom-args) + (if (null? rom-args) '() (u:read-args rom-args)) (u:read-stdin (current-input-port)) (exit 0))))