From: plugd Date: Sun, 2 Oct 2022 16:02:57 +0000 (+0200) Subject: Reorganised execution loop to cater for input. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=6f7b0f5e308675a8c11e2d48482d23f10bb99599;p=ez.git Reorganised execution loop to cater for input. --- diff --git a/ez.el b/ez.el index af348a9..7a91e57 100644 --- a/ez.el +++ b/ez.el @@ -26,6 +26,13 @@ ;;; Commentary: +;; There are many Z-machine interpreters, but this one is mine. +;; It only capable of interpreting the subset of Z-code necessary +;; to run the first Zork game in z3 format. +;; +;; It is based entirely on the description of the Z-machine contained +;; Marnix Klooster's wonderful document, "The Z-machine And How to Emulate It." + ;;; Code: ;; Utility functions @@ -312,7 +319,6 @@ (defun ez-get-zstring (base-addr) (cdr (ez-get-zstring-and-length base-addr))) - ;; Call stack (defvar ez-call-stack nil) @@ -572,157 +578,158 @@ ((0op #x0B) new_line ez-op-new-line) ((var #x06) print_num ez-op-print-num) ((var #x05) print_char ez-op-print-char) - ((1op #x0A) print_obj ez-op-print-obj))) + ((1op #x0A) print_obj ez-op-print-obj) + ((var #x04) read ez-op-read))) (defun ez-op-ret (operands &optional operand-types) (let ((retval (car operands))) (message "\tReturning value %x" retval) (ez-pop-call-stack-frame) (ez-set-var (ez-read-pc-byte-and-inc) retval)) - t) + 'run) (defun ez-op-ret-pulled (operands operand-types) (let ((retval (ez-routine-stack-pop))) (message "\tReturning value %x" retval) (ez-op-ret (list retval))) - t) + 'run) (defun ez-op-rtrue (&optional operands operand-types) (ez-op-ret (list 1)) - t) + 'run) (defun ez-op-rfalse (&optional operands operand-types) (ez-op-ret (list 0)) - t) + 'run) (defun ez-op-jz (operands operand-types) (ez-do-branch (= (car operands) 0)) - t) + 'run) (defun ez-op-je (operands operand-types) (ez-do-branch (memq (car operands) (cdr operands))) - t) + 'run) (defun ez-op-jg (operands operand-types) (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types))) (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types)))) (ez-do-branch (> s1 s2))) - t) + 'run) (defun ez-op-jl (operands operand-types) (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types))) (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types)))) (ez-do-branch (< s1 s2))) - t) + 'run) (defun ez-op-inc-jg (operands operand-types) (let ((var (car operands))) (ez-op-inc (list var)) (ez-op-jg (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types)))) - t) + 'run) (defun ez-op-dec-jl (operands operand-types) (let ((var (car operands))) (ez-op-dec (list var)) (ez-op-jl (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types)))) - t) + 'run) (defun ez-op-jin (operands operand-types) (let ((obj (car operands)) (n (cadr operands))) (ez-do-branch (or (= n 0) (= n (ez-get-obj-parent obj))))) - t) + 'run) (defun ez-op-test (operands operand-types) (let ((a (car operands)) (b (cadr operands))) (ez-do-branch (= (logand a b) b))) - t) + 'run) (defun ez-op-jump (operands operand-types) (let ((offset (if (eq (car operand-types) 'b) (ez-decode-signed-byte (car operands)) (ez-decode-signed-word (car operands))))) (ez-set-pc (+ (ez-get-pc) offset -2))) - t) + 'run) (defun ez-op-inc (operands &optional operand-types) (let ((var (car operands))) (ez-set-var var (mod (+ 1 (ez-get-var var)) #x10000))) - t) + 'run) (defun ez-op-dec (operands &optional operand-types) (let ((var (car operands))) (ez-set-var var (mod (+ (ez-get-var var) 1) #x10000))) - t) + 'run) (defun ez-op-store (operands operand-types) (let ((var (car operands)) (a (cadr operands))) (ez-set-var var a)) - t) + 'run) (defun ez-op-load (operands operand-types) (let ((var (car operands))) (ez-set-var (ez-read-pc-byte-and-inc) (ez-get-var var))) - t) + 'run) (defun ez-op-storew (operands operand-types) (let ((baddr (car operands)) (n (cadr operands)) (a (caddr operands))) (ez-mem-set-word (+ baddr (* 2 n)) a)) - t) + 'run) (defun ez-op-loadw (operands operand-types) (let ((baddr (car operands)) (n (cadr operands))) (ez-set-var (ez-read-pc-byte-and-inc) (ez-mem-ref-word (+ baddr (* 2 n))))) - t) + 'run) (defun ez-op-storeb (operands operand-types) (let ((baddr (car operands)) (n (cadr operands)) (a (caddr operands))) (ez-mem-set-byte (+ baddr n) a)) - t) + 'run) (defun ez-op-loadb (operands operand-types) (let ((baddr (car operands)) (n (cadr operands))) (ez-set-var (ez-read-pc-byte-and-inc) (ez-mem-ref-byte (+ baddr n)))) - t) + 'run) (defun ez-op-and (operands operand-types) (let ((a (car operands)) (b (cadr operands))) (ez-set-var (ez-read-pc-byte-and-inc) (logand a b))) - t) + 'run) (defun ez-op-or (operands operand-types) (let ((a (car operands)) (b (cadr operands))) (ez-set-var (ez-read-pc-byte-and-inc) (logior a b))) - t) + 'run) (defun ez-op-add (operands operand-types) (let ((a (car operands)) (b (cadr operands))) (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ a b) #x10000))) - t) + 'run) (defun ez-op-sub (operands operand-types) (let ((a (car operands)) (b (cadr operands))) (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ (- a b) #x10000) #x10000))) - t) + 'run) (defun ez-op-mul (a b) (let ((a (car operands)) (b (cadr operands))) (ez-set-var (ez-read-pc-byte-and-inc) (mod (* a b) #x10000))) - t) + 'run) (defun ez-op-div (a b) (error "Not implemented")) @@ -744,67 +751,67 @@ (if (< i n) (ez-set-local-var (+ i 1) (elt call-operands i)) (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i)))))))) - t) + 'run) (defun ez-op-test-attr (operands operand-types) (let ((obj (car operands)) (attr (cadr operands))) (ez-do-branch (= 1 (ez-get-obj-attr obj attr))) - t)) + 'run)) (defun ez-op-set-attr (operands operand-types) (let ((obj (car operands)) (attr (cadr operands))) (ez-set-obj-attr obj attr 1)) - t) + 'run) (defun ez-op-clear-attr (operands operand-types) (let ((obj (car operands)) (attr (cadr operands))) (ez-set-obj-attr obj attr 0)) - t) + 'run) (defun ez-op-get-sibling (operands operand-types) (let ((sib (ez-get-obj-sibling (car operands)))) (ez-set-var (ez-read-pc-byte-and-inc) sib) (ez-do-branch (> sib 0))) - t) + 'run) (defun ez-op-get-child (operands operand-types) (let ((child (ez-get-obj-child (car operands)))) (ez-set-var (ez-read-pc-byte-and-inc) child) (ez-do-branch (> child 0))) - t) + 'run) (defun ez-op-get-parent (operands operand-types) (let ((parent (ez-get-obj-parent (car operands)))) (ez-set-var (ez-read-pc-byte-and-inc) parent)) - t) + 'run) (defun ez-op-remove-obj (operands operand-types) (let ((obj (car operands))) (ez-remove-obj obj)) - t) + 'run) (defun ez-op-insert-obj (operands operand-types) (let ((obj1 (car operands)) (obj2 (cadr operands))) (ez-insert-obj obj1 obj2)) - t) + 'run) (defun ez-op-put-prop (operands operand-types) (let* ((obj (car operands)) (prop (cadr operands)) (a (caddr operands))) (ez-set-obj-prop obj prop a)) - t) + 'run) (defun ez-op-get-prop (operands operand-types) (let* ((obj (car operands)) (prop (cadr operands))) (ez-set-var (ez-read-pc-byte-and-inc) (ez-get-obj-prop obj prop))) - t) + 'run) (defun ez-op-print (operands operand-types) (let* ((word-count-and-string (ez-get-zstring-and-length (ez-get-pc))) @@ -812,36 +819,49 @@ (string (cdr word-count-and-string))) (ez-print string) (ez-increment-pc (* 2 word-count))) - t) + 'run) (defun ez-op-new-line (operands operand-types) (ez-print "\n") - t) + 'run) (defun ez-op-print-num (operands operand-types) (let ((s (ez-decode-signed-operand (car operands) (car operand-types)))) (ez-print (number-to-string s))) - t) + 'run) (defun ez-op-print-char (operands operand-types) (let ((c (car operands))) (ez-print (string c))) - t) + 'run) (defun ez-op-print-obj (operands operand-types) (let ((obj (car operands))) (ez-print (ez-get-obj-name obj))) - t) + 'run) + +(defun ez-op-read (operands operand-types) + (let ((baddr1 (car operands)) + (baddr2 (cadr operands))) + ) + 'wait-for-input) ;; Execution loop -(defun ez-run (filename) +(defun ez-load-and-run (filename) (ez-load-file filename) (ez-parse-header) (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc))) - (while (ez-execute-instr))) + (ez-run)) +(defvar ez-machine-state nil + "Identifies the current executation state of the Z-machine.") + +(defun ez-run () + (setq ez-machine-state 'run) + (while (eq ez-machine-state 'run) + (setq ez-machine-state (ez-execute-instr)))) ;;; Buffer and I/O ;; @@ -866,6 +886,11 @@ (goto-char ez-input-marker) (insert-before-markers string)))) +(defun ez-enter () + (interactive) + (with-current-buffer "*ez*" + (buffer-substring ez-input-marker (point-max)))) + ;; Mode (defvar ez-mode-map @@ -886,7 +911,7 @@ (switch-to-buffer "*ez*") (ez-mode) (ez-setup-buffer) - (ez-run zfile)) + (ez-load-and-run zfile)) "Started EZ.") (defun ez-debug () @@ -894,6 +919,6 @@ (switch-to-buffer "*ez*") (ez-mode) (ez-setup-buffer) - (ez-run "zork1.z3")) + (ez-load-and-run "zork1.z3")) ;;; ez.el ends here