1 ;;; ez.el --- Emacs Z-machine
3 ;; Copyright (C) 2021 Tim Vaughan
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 13 Oct 2021
9 ;; Homepage: http://thelambdalab.xyz/ez
10 ;; Package-Requires: ((emacs "26"))
12 ;; This file is not part of GNU Emacs.
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file. If not, see <http://www.gnu.org/licenses/>.
33 (defun ez-decode-signed-byte (b)
39 "Memory of z-machine.")
41 (defvar ez-version nil)
43 (defvar ez-start-pc nil)
44 (defvar ez-himem-base nil)
45 (defvar ez-dict-addr nil)
46 (defvar ez-objtab-addr nil)
47 (defvar ez-abbrevtab-addr nil)
48 (defvar ez-globalvartab-addr nil)
50 (defun ez-mem-ref-byte (addr)
51 (aref ez-memory addr))
53 (defun ez-mem-set-byte (addr val)
54 (aset ez-memory addr val))
56 (defun ez-mem-ref-word (addr)
57 (+ (* 256 (aref ez-memory addr))
58 (aref ez-memory (+ addr 1))))
60 (defun ez-mem-set-word (addr val)
61 (let ((byte-high (/ val 256))
62 (byte-low (mod val 256)))
63 (aset ez-memory addr byte-high)
64 (aset ez-memory (+ addr 1) byte-low)))
66 (defun ez-parse-header ()
67 (setq ez-version (ez-mem-ref-byte #x0))
68 (setq ez-himem-addr (ez-mem-ref-word #x4))
69 (setq ez-start-pc (ez-mem-ref-word #x6))
70 (setq ez-dict-base (ez-mem-ref-word #x8))
71 (setq ez-objtab-addr (ez-mem-ref-word #xA))
72 (setq ez-globalvartab-addr (ez-mem-ref-word #xC))
73 (setq ez-abbrevtab-addr (ez-mem-ref-word #x18)))
75 (defun ez-load-file (filename)
76 "Load story file into memory."
78 (insert-file-contents-literally filename)
79 (setq ez-memory (encode-coding-string (buffer-string) 'raw-text))))
83 (defun ez-get-global-var (gvar)
84 (ez-mem-ref-word (+ (* 2 gvar) (ez-mem-ref-word ez-globalvartab-addr))))
86 (defun ez-set-global-var (gvar val)
87 (ez-mem-set-word (+ (* 2 gvar) (ez-mem-ref-word ez-globalvartab-addr)) val))
91 (defun ez-make-obj (&optional name attribs props parent first-child next-sibling)
92 (list name attribs props parent first-child next-sibling))
94 (defun ez-obj-name (obj) (elt obj 0))
95 (defun ez-obj-attribs (obj) (elt obj 1))
96 (defun ez-obj-props (obj) (elt obj 2))
97 (defun ez-obj-parent (obj) (elt obj 3))
98 (defun ez-obj-first-child (obj) (elt obj 4))
99 (defun ez-obj-next-sibling (obj) (elt obj 5))
101 (defvar ez-property-defaults nil)
103 (defun ez-load-property-defaults ()
104 (setq ez-property-defaults (make-vector 31 0))
106 (aset ez-property-defaults i (aref ez-memory (+ ez-objtab-addr (* 2 i))))))
108 (defun ez-get-obj (obj-id)
109 (let ((addr (+ ez-objtab-addr
111 (* 9 (- obj-id 1)))))
114 (ez-mem-ref-byte (+ addr 4))
115 (ez-mem-ref-byte (+ addr 5))
116 (ez-mem-ref-byte (+ addr 6))
117 (ez-mem-ref-word (+ addr 7))
118 (let ((plist-addr (ez-mem-ref-word (+ addr 7))))
119 (if (> (ez-mem-ref-byte plist-addr) 0)
120 (ez-get-zstring (+ 1 plist-addr))
126 (defvar ez-zstring-alphabets
129 (list " abcdefghijklmnopqrstuvwxyz"
130 " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
131 " \n0123456789.,!?_#'\"/\\-:()")
132 "Alphabets used by V3")
134 (defun ez-parse-zstring-word (word)
136 (logand (lsh word -10) #b11111)
137 (logand (lsh word -5) #b11111)
138 (logand word #b11111)))
140 (defun ez-get-zstring-chars (base-addr)
141 (let ((addr base-addr)
145 (let ((components (ez-parse-zstring-word (ez-mem-ref-word addr))))
146 (setq chars (append chars (cdr components)))
147 (setq addr (+ addr 2))
148 (when (= (car components) 1)
149 (setq not-done nil))))
152 (defun ez-get-zstring (base-addr)
153 (let ((chars (ez-get-zstring-chars base-addr))
162 (* 2 (ez-mem-ref-word (+ ez-abbrevtab-addr
163 (* 2 (+ (* (- abbrev-char 1) 32) char)))))))
164 (setq s (concat s (ez-get-zstring abbrev-addr))))
165 (setq abbrev-char nil))
166 ((memq char '(1 2 3)) ;Abbreviation
167 (setq abbrev-char char))
169 (setq cur (mod (+ cur 1) 2)))
171 (setq cur (mod (+ 2 (- cur 1)) 2)))
173 (setq s (concat s (substring (elt ez-zstring-alphabets cur)
181 (defvar ez-call-stack nil)
183 (defun ez-make-call-stack-frame (pc &optional call-method)
189 (defun ez-add-call-stack-frame (pc &optional call-method)
190 (push (ez-make-call-stack-frame pc call-method) ez-call-stack))
192 (defun ez-pop-call-stack-frame ()
195 (defun ez-routine-stack ()
196 (elt (car ez-call-stack) 1))
198 (defun ez-routine-stack-push (val)
199 (let ((frame (car ez-call-stack)))
200 (setf (elt frame 1) (cons val (elt frame 1)))))
202 (defun ez-routine-stack-pop ()
203 (let* ((frame (car ez-call-stack))
204 (rs-head (car (elt frame 1))))
205 (setf (elt frame 1) (cdr (elt frame 1)))
208 (defun ez-get-local-var (lvar)
209 (let ((frame (car ez-call-stack)))
210 (aref (elt frame 2) (- lvar 1))))
212 (defun ez-set-local-var (lvar val)
213 (let ((frame (car ez-call-stack)))
214 (aset (elt frame 2) (- lvar 1) val)))
217 (caar ez-call-stack))
219 (defun ez-set-pc (new-pc)
220 (setf (car (car ez-call-stack)) new-pc))
222 (defun ez-increment-pc (inc)
223 (ez-set-pc (+ (ez-get-pc) inc)))
225 (defun ez-read-pc-byte-and-inc ()
226 (let ((res (ez-mem-ref-byte (ez-get-pc))))
230 (defun ez-read-pc-word-and-inc ()
231 (let ((res (ez-mem-ref-word (ez-get-pc))))
235 ;; Instruction execution
237 (defun ez-get-var (var)
240 (ez-routine-stack-pop))
242 (ez-get-local-var var))
244 (ez-get-global-var (- var 16)))))
246 (defun ez-set-var (var val)
249 (ez-routine-stack-push val))
251 (ez-set-local-var var val))
253 (ez-set-global-var (- var 16) val))))
255 (defun ez-read-pc-var-and-inc ()
256 (ez-get-var (ez-read-pc-byte-and-inc)))
258 (defun ez-execute-instr ()
259 (let ((instr-pc (ez-get-pc))
260 (opbyte (ez-read-pc-byte-and-inc))
265 ((<= #x0 opbyte #x1f)
268 operands (list (ez-read-pc-byte-and-inc)
269 (ez-read-pc-byte-and-inc))
270 operand-types '(b b)))
271 ((<= #x20 opbyte #x3F)
273 opcode (- opbyte #x20)
274 operands (list (ez-read-pc-byte-and-inc)
275 (ez-read-pc-var-and-inc))
276 operand-types '(b w)))
277 ((<= #x40 opbyte #x5F)
279 opcode (- opbyte #x40)
280 operands (list (ez-read-pc-var-and-inc)
281 (ez-read-pc-byte-and-inc))
282 operand-types '(w b)))
283 ((<= #x60 opbyte #x7F)
285 opcode (- opbyte #x60)
286 operands (list (ez-read-pc-var-and-inc)
287 (ez-read-pc-var-and-inc))
288 operand-types '(w w)))
289 ((<= #x80 opbyte #x8F)
291 opcode (- opbyte #x80)
292 operands (list (ez-read-pc-word-and-inc))
294 ((<= #x90 opbyte #x9F)
296 opcode (- opbyte #x90)
297 operands (list (ez-read-pc-byte-and-inc))
299 ((<= #xA0 opbyte #xAF)
301 opcode (- opbyte #xa0)
302 operands (list (ez-read-pc-var-and-inc))
304 ((<= #xB0 opbyte #xBF)
306 opcode (- opbyte #xb0)))
307 ((<= #xC0 opbyte #xDF)
308 (error "Unsupported op" opbyte))
309 ((<= #xE0 opbyte #xFF)
311 opcode (- opbyte #xe0))
312 (let ((operands-and-types (ez-read-var-operands-and-inc)))
313 (setq operands (car operands-and-types)
314 operand-types (cdr operands-and-types)))))
315 (let ((table-row (assoc (list optype opcode) ez-op-table)))
317 (error "Unsupported op" optype opcode))
318 (let ((mnemonic (elt table-row 1)))
319 (message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s"
320 instr-pc optype opcode mnemonic operands))
321 (funcall (elt table-row 2) operands operand-types))))
323 (defun ez-read-var-operands-and-inc ()
324 (let* ((type-byte (ez-read-pc-byte-and-inc))
325 (bleh (message (binformat type-byte)))
326 (types (let ((type1 (lsh type-byte -6)))
330 (let ((type2 (lsh (logand #b110000 type-byte) -4)))
334 (let ((type3 (lsh (logand #b1100 type-byte) -2)))
338 (let ((type4 (logand #b11)))
341 (list type4)))))))))))))
347 ((= type 0) (ez-read-pc-word-and-inc))
348 ((= type 1) (ez-read-pc-byte-and-inc))
349 ((= type 2) (ez-read-pc-var-and-inc))))
359 '(((2op #x14) add ez-op-add)
360 ((2op #x15) sub ez-op-sub)
361 ((2op #x16) mul ez-op-mul)
362 ((2op #x17) div ez-op-div)
363 ((2op #x18) mod ez-op-mod)
364 ((var #x00) call_fv ez-op-callf)))
366 (defun ez-op-add (operands operand-types)
367 (let ((a (car operands))
369 (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ a b) #x10000))))
371 (defun ez-op-sub (operands operand-types)
372 (let ((a (car operands))
374 (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ (- a b) #x10000) #x10000))))
376 (defun ez-op-mul (a b)
377 (let ((a (car operands))
379 (ez-set-var (ez-read-pc-byte-and-inc) (mod (* a b) #x10000))))
381 (defun ez-op-div (a b))
383 (defun ez-op-mod (a b))
385 (defun ez-op-callf (operands operand-types)
386 (let* ((raddr (car operands))
387 (call-operands (cdr operands))
389 (L (ez-mem-ref-byte r))
390 (n (length call-operands))
391 (new-pc (+ r 1 (* L 2))))
392 (ez-add-call-stack-frame new-pc)
395 (ez-set-local-var (+ i 1) (elt call-operands i))
396 (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i)))))))
401 (defun ez-run (filename)
402 (ez-load-file filename)
404 (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc)))
406 (while (ez-execute-inst)))
410 (ez-load-file "zork1.z3")
412 (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc)))
416 (defun binformat (n &optional s)
420 (new-s (concat (number-to-string (mod n 2)) s)))
423 (binformat d new-s))))