Improved zstring function names.
[ez.git] / ez.el
1 ;;; ez.el --- Emacs Z-machine
2
3 ;; Copyright (C) 2021 Tim Vaughan
4
5 ;; Author: Tim Vaughan <timv@ughan.xyz>
6 ;; Created: 13 Oct 2021
7 ;; Version: 1.0
8 ;; Keywords: game
9 ;; Homepage: http://thelambdalab.xyz/ez
10 ;; Package-Requires: ((emacs "26"))
11
12 ;; This file is not part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;; Utility functions
32
33 (defun ez-decode-signed-bits (n nbits)
34   (if (= 0 (lsh n (- 1 nbits)))
35       n
36     (- n (lsh 1 nbits))))
37
38 (defun ez-decode-signed-byte (b)
39   (ez-decode-signed-bits b 8))
40
41 (defun ez-decode-signed-word (w)
42   (ez-decode-signed-bits w 16))
43
44 (defun ez-decode-signed-operand (operand operand-type)
45   (if (eq operand-type 'b)
46       (ez-decode-signed-byte operand)
47     (ez-decode-signed-word operand)))
48
49 (defun binformat (n &optional s)
50   (unless s
51     (setq s ""))
52   (let ((d (/ n 2))
53         (new-s (concat (number-to-string (mod n 2)) s)))
54     (if (= d 0)
55         new-s
56       (binformat d new-s))))
57
58 ;; Memory
59
60 (defvar ez-memory nil
61   "Memory of z-machine.")
62
63 (defvar ez-version nil)
64
65 (defvar ez-start-pc nil)
66 (defvar ez-himem-base nil)
67 (defvar ez-dict-base nil)
68 (defvar ez-objtab-addr nil)
69 (defvar ez-abbrevtab-addr nil)
70 (defvar ez-globalvartab-addr nil)
71
72 (defun ez-mem-ref-byte (addr)
73   (aref ez-memory addr))
74
75 (defun ez-mem-set-byte (addr val)
76   (aset ez-memory addr val))
77
78 (defun ez-mem-ref-word (addr)
79   (+ (* 256 (aref ez-memory addr))
80      (aref ez-memory (+ addr 1))))
81
82 (defun ez-mem-set-word (addr val)
83   (let ((byte-high (/ val 256))
84         (byte-low (mod val 256)))
85     (aset ez-memory addr byte-high)
86     (aset ez-memory (+ addr 1) byte-low)))
87
88 (defun ez-parse-header ()
89   (setq ez-version (ez-mem-ref-byte #x0))
90   (setq ez-himem-addr (ez-mem-ref-word #x4))
91   (setq ez-start-pc (ez-mem-ref-word #x6))
92   (setq ez-dict-base (ez-mem-ref-word #x8))
93   (setq ez-objtab-addr (ez-mem-ref-word #xA))
94   (setq ez-globalvartab-addr (ez-mem-ref-word #xC))
95   (setq ez-abbrevtab-addr (ez-mem-ref-word #x18)))
96
97 (defun ez-load-file (filename)
98   "Load story file into memory."
99   (with-temp-buffer
100     (insert-file-contents-literally filename)
101     (setq ez-memory (encode-coding-string (buffer-string) 'raw-text)))
102   'done)
103
104 ;; Global variables
105
106 (defun ez-get-global-var (gvar)
107   (if (> gvar 239)
108       (error "Invalid global variable %d" gvar))
109   (ez-mem-ref-word (+ (* 2 gvar) ez-globalvartab-addr)))
110
111 (defun ez-set-global-var (gvar val)
112   (ez-mem-set-word (+ (* 2 gvar) ez-globalvartab-addr) val))
113
114 ;; Object tree
115
116 (defvar ez-property-defaults nil)
117
118 (defun ez-load-property-defaults ()
119   (setq ez-property-defaults (make-vector 31 0))
120   (dotimes (i 31)
121     (aset ez-property-defaults i (aref ez-memory (+ ez-objtab-addr (* 2 i))))))
122
123 (defun ez-get-obj-addr (obj-id)
124   (+ ez-objtab-addr (* 2 31) (* 9 (- obj-id 1))))
125
126 (defun ez-get-obj-parent (obj-id)
127   (let ((addr (ez-get-obj-addr obj-id)))
128     (ez-mem-ref-byte (+ addr 4))))
129
130 (defun ez-get-obj-sibling (obj-id)
131   (let ((addr (ez-get-obj-addr obj-id)))
132     (ez-mem-ref-byte (+ addr 5))))
133
134 (defun ez-get-obj-child (obj-id)
135   (let ((addr (ez-get-obj-addr obj-id)))
136     (ez-mem-ref-byte (+ addr 6))))
137
138 (defun ez-get-obj-plist-addr (obj-id)
139   (let ((addr (ez-get-obj-addr obj-id)))
140     (ez-mem-ref-word (+ addr 7))))
141
142 (defun ez-get-obj-name (obj-id)
143   (let ((plist-addr (ez-get-obj-plist-addr obj-id)))
144     (if (> (ez-mem-ref-byte plist-addr) 0)
145         (ez-get-zstring (+ 1 (ez-get-obj-plist-addr obj-id)))
146       nil)))
147
148 (defun ez-get-obj (obj-id)
149   (let ((addr (+ ez-objtab-addr
150                  (* 2 31)
151                  (* 9 (- obj-id 1)))))
152     (list
153      obj-id
154      (ez-mem-ref-byte (+ addr 4))
155      (ez-mem-ref-byte (+ addr 5))
156      (ez-mem-ref-byte (+ addr 6))
157      (ez-mem-ref-word (+ addr 7))
158      (let ((plist-addr (ez-mem-ref-word (+ addr 7))))
159        (if (> (ez-mem-ref-byte plist-addr) 0)
160            (ez-get-zstring (+ 1 plist-addr))
161          nil)))))
162
163 (defun ez-get-prop-default (prop)
164   (ez-mem-ref-word (+ ez-objtab-addr (* 2 (+ 1 prop)))))
165
166 (defun ez-get-prop-len (prop-size-byte)
167   (lsh prop-size-byte -5))
168
169 (defun ez-get-prop-num (prop-size-byte)
170   (logand #b00011111 prop-size-byte))
171
172 (defun ez-get-obj-prop-addr (obj prop)
173   (let* ((plist-addr (ez-get-obj-plist-addr obj))
174          (prop-addr (+ plist-addr 1 (* 2 (ez-mem-ref-byte plist-addr))))
175          (size-byte (ez-mem-ref-byte prop-addr)))
176     (while (not (or (= size-byte 0)
177                     (= prop (ez-get-prop-num size-byte))))
178       (setq prop-addr (+ prop-addr (ez-get-prop-len size-byte) 2)
179             size-byte (ez-mem-ref-byte prop-addr)))
180     prop-addr))
181
182 (defun ez-get-obj-prop (obj prop)
183   (let* ((prop-addr (ez-get-obj-prop-addr obj prop))
184          (size-byte (ez-mem-ref-byte prop-addr)))
185     (if (= size-byte 0)
186         (ez-get-prop-default prop)
187       (let ((prop-len (ez-get-prop-len size-byte))
188             (data-addr (+ prop-addr 1)))
189         (cond
190          ((= prop-len 0)
191           (ez-mem-ref-byte data-addr))
192          ((>= prop-len 1)
193           (ez-mem-ref-word data-addr)))))))
194
195 (defun ez-set-obj-prop (obj prop value)
196   (let* ((prop-addr (ez-get-obj-prop-addr obj prop))
197          (size-byte (ez-mem-ref-byte prop-addr)))
198     (cond
199      ((= size-byte 0)
200       (error "Tried to set non-existant property"))
201      ((= (ez-get-prop-len size-byte) 1)
202       (ez-mem-set-byte (+ prop-addr 1) value))
203      (t
204       (ez-mem-set-word (+ prop-addr 1) value)))))
205
206 (defun ez-get-obj-attr (obj attr)
207   (let* ((byte-num (/ attr 8))
208          (bit-num (mod attr 8))
209          (bit-mask (lsh 1 (- 7 bit-num))))
210     (if (> (logand bit-mask (ez-mem-ref-byte (+ (ez-get-obj-addr obj) byte-num))) 0)
211         1
212       0)))
213
214 (defun ez-set-obj-attr (obj attr val)
215   (let* ((byte-num (/ attr 8))
216          (bit-num (mod attr 8))
217          (bit-mask (lsh 1 (- 7 bit-num)))
218          (byte-addr (+ (ez-get-obj-addr obj) byte-num))
219          (byte (ez-mem-ref-byte byte-addr))
220          (current-set (> (logand bit-mask byte) 0)))
221     (if (or (and current-set (= val 0))
222             (and (not current-set) (> val 0)))
223         (ez-mem-set-byte byte-addr (logxor byte bit-mask)))))
224
225 ;; Z-strings
226
227 (defvar ez-zstring-alphabets
228   ;                1    1    2    2    3
229   ;      0    5    0    5    0    5    0
230   (list "      abcdefghijklmnopqrstuvwxyz"
231         "      ABCDEFGHIJKLMNOPQRSTUVWXYZ"
232         "       \n0123456789.,!?_#'\"/\\-:()")
233   "Alphabets used by V3")
234
235 (defun ez-parse-zstring-word (word)
236   (list (lsh word -15)
237         (logand (lsh word -10) #b11111)
238         (logand (lsh word -5) #b11111)
239         (logand word #b11111)))
240
241 (defun ez-get-zstring-chars-and-length (base-addr)
242   (let ((addr base-addr)
243         (chars nil)
244         (not-done t)
245         (word-count 0))
246     (while not-done
247       (let ((components (ez-parse-zstring-word (ez-mem-ref-word addr))))
248         (setq chars (append chars (cdr components)))
249         (setq addr (+ addr 2))
250         (setq word-count (+ word-count 1))
251         (when (= (car components) 1)
252           (setq not-done nil))))
253     (cons word-count chars)))
254
255 (defun ez-get-zstring-and-length (base-addr)
256   (let* ((word-count-and-chars (ez-get-zstring-chars-and-length base-addr))
257          (word-count (car word-count-and-chars))
258          (chars (cdr word-count-and-chars))
259          (cur 0)
260          (lock 0)
261          (abbrev-char nil)
262          (s ""))
263     (dolist (char chars)
264       (cond
265        (abbrev-char
266         (let ((abbrev-addr
267                (* 2 (ez-mem-ref-word (+ ez-abbrevtab-addr
268                                         (* 2 (+ (* (- abbrev-char 1) 32) char)))))))
269           (setq s (concat s (cdr (ez-get-zstring-and-length abbrev-addr)))))
270         (setq abbrev-char nil))
271        ((memq char '(1 2 3)) ;Abbreviation
272         (setq abbrev-char char))
273        ((= char 4)
274         (setq cur (mod (+ cur 1) 3)))
275        ((= char 5)
276         (setq cur (mod (+ 3 (- cur 1)) 3)))
277        (t 
278         (setq s (concat s (substring (elt ez-zstring-alphabets cur)
279                                      char (+ char 1))))
280         (setq cur lock))))
281     (cons word-count s)))
282
283 (defun ez-get-zstring (base-addr)
284   (cdr (ez-get-zstring-and-length base-addr)))
285
286
287 ;; Call stack
288
289 (defvar ez-call-stack nil)
290
291 (defun ez-make-call-stack-frame (pc &optional call-method)
292   (list pc
293         nil
294         (make-vector 15 0)
295         call-method))
296
297 (defun ez-add-call-stack-frame (pc &optional call-method)
298   (push (ez-make-call-stack-frame pc call-method) ez-call-stack))
299
300 (defun ez-pop-call-stack-frame ()
301   (pop ez-call-stack))
302
303 (defun ez-routine-stack ()
304   (elt (car ez-call-stack) 1))
305
306 (defun ez-routine-stack-push (val)
307   (let ((frame (car ez-call-stack)))
308     (setf (elt frame 1) (cons val (elt frame 1)))))
309
310 (defun ez-routine-stack-pop ()
311   (let* ((frame (car ez-call-stack))
312          (rs-head (car (elt frame 1))))
313     (setf (elt frame 1) (cdr (elt frame 1)))
314     rs-head))
315
316 (defun ez-get-local-var (lvar)
317   (let ((frame (car ez-call-stack)))
318     (aref (elt frame 2) (- lvar 1))))
319
320 (defun ez-set-local-var (lvar val)
321   (let ((frame (car ez-call-stack)))
322     (aset (elt frame 2) (- lvar 1) val)))
323
324 (defun ez-get-pc ()
325   (caar ez-call-stack))
326
327 (defun ez-set-pc (new-pc)
328   (setf (car (car ez-call-stack)) new-pc))
329
330 (defun ez-increment-pc (inc)
331   (ez-set-pc (+ (ez-get-pc) inc)))
332
333 (defun ez-read-pc-byte-and-inc ()
334   (let ((res (ez-mem-ref-byte (ez-get-pc))))
335     (ez-increment-pc 1)
336     res))
337
338 (defun ez-read-pc-word-and-inc ()
339   (let ((res (ez-mem-ref-word (ez-get-pc))))
340     (ez-increment-pc 2)
341     res))
342
343 ;; Instruction execution
344
345 (defun ez-get-var (var)
346   (cond
347    ((= var 0)
348     (ez-routine-stack-pop))
349    ((< var 16)
350     (ez-get-local-var var))
351    (t
352     (ez-get-global-var (- var 16)))))
353
354 (defun ez-set-var (var val)
355   (cond
356    ((= var 0)
357     (ez-routine-stack-push val))
358    ((< var 16)
359     (ez-set-local-var var val))
360    (t
361     (ez-set-global-var (- var 16) val))))
362
363 (defun ez-read-pc-var-and-inc ()
364   (ez-get-var (ez-read-pc-byte-and-inc)))
365
366 (defun ez-execute-instr ()
367   (let ((instr-pc (ez-get-pc))
368         (opbyte (ez-read-pc-byte-and-inc))
369         (optype) (opcode) (operands))
370     (cond
371      ((<= #x0 opbyte #x1f)
372       (setq optype '2op
373             opcode opbyte
374             operands (list (ez-read-pc-byte-and-inc)
375                            (ez-read-pc-byte-and-inc))
376             operand-types '(b b)))
377      ((<= #x20 opbyte #x3F)
378       (setq optype '2op
379             opcode (- opbyte #x20)
380             operands (list (ez-read-pc-byte-and-inc)
381                            (ez-read-pc-var-and-inc))
382             operand-types '(b w)))
383      ((<= #x40 opbyte #x5F)
384       (setq optype '2op
385             opcode (- opbyte #x40)
386             operands (list (ez-read-pc-var-and-inc)
387                            (ez-read-pc-byte-and-inc))
388             operand-types '(w b)))
389      ((<= #x60 opbyte #x7F)
390       (setq optype '2op
391             opcode (- opbyte #x60)
392             operands (list (ez-read-pc-var-and-inc)
393                            (ez-read-pc-var-and-inc))
394             operand-types '(w w)))
395      ((<= #x80 opbyte #x8F)
396       (setq optype '1op
397             opcode (- opbyte #x80)
398             operands (list (ez-read-pc-word-and-inc))
399             operand-types '(w)))
400      ((<= #x90 opbyte #x9F)
401       (setq optype '1op
402             opcode (- opbyte #x90)
403             operands (list (ez-read-pc-byte-and-inc))
404             operand-types '(b)))
405      ((<= #xA0 opbyte #xAF)
406       (setq optype '1op
407             opcode (- opbyte #xa0)
408             operands (list (ez-read-pc-var-and-inc))
409             operand-types '(w)))
410      ((<= #xB0 opbyte #xBF)
411       (setq optype '0op
412             opcode (- opbyte #xb0)
413             operands '()
414             operand-types '()))
415      ((<= #xC0 opbyte #xDF)
416       (setq optype '2op
417             opcode (- opbyte #xc0))
418       (let ((operands-and-types (ez-read-var-operands-and-inc)))
419         (setq operands (car operands-and-types)
420               operand-types (cdr operands-and-types))))
421      ((<= #xE0 opbyte #xFF)
422       (setq optype 'var
423             opcode (- opbyte #xe0))
424       (let ((operands-and-types (ez-read-var-operands-and-inc)))
425         (setq operands (car operands-and-types)
426               operand-types (cdr operands-and-types)))))
427     (let ((table-row (assoc (list optype opcode) ez-op-table)))
428       (unless table-row
429         (error "Unsupported op PC:%x Optype:%s Opcode:%x Operands:%s Operand-types:%s"
430                instr-pc optype opcode operands operand-types))
431       (let ((mnemonic (elt table-row 1)))
432         (message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s Operand-types:%s"
433                  instr-pc optype opcode mnemonic operands operand-types))
434       (funcall (elt table-row 2) operands operand-types))))
435
436 (defun ez-read-var-operands-and-inc ()
437   (let* ((type-byte (ez-read-pc-byte-and-inc))
438          (types (let ((type1 (lsh type-byte -6)))
439                   (if (= type1 #b11)
440                       nil
441                     (cons type1
442                           (let ((type2 (lsh (logand #b110000 type-byte) -4)))
443                             (if (= type2 #b11)
444                                 nil
445                               (cons type2
446                                     (let ((type3 (lsh (logand #b1100 type-byte) -2)))
447                                       (if (= type3 #b11)
448                                           nil
449                                         (cons type3
450                                               (let ((type4 (logand #b11)))
451                                                 (if (= type4 #b11)
452                                                     nil
453                                                   (list type4))))))))))))))
454     (cons
455      (mapcar
456       (lambda (type)
457         (cond
458          ((= type 0) (ez-read-pc-word-and-inc))
459          ((= type 1) (ez-read-pc-byte-and-inc))
460          ((= type 2) (ez-read-pc-var-and-inc))))
461       types)
462      (mapcar
463       (lambda (type)
464         (if (= type 1)
465             'b
466           'w))
467       types))))
468
469 ;; Branches
470
471 (defun ez-do-branch (branch)
472   (let* ((branch-byte (ez-read-pc-byte-and-inc))
473          (invert (= 0 (logand branch-byte #b10000000)))
474          (single-byte (> (logand branch-byte #b01000000) 0))
475          (offset
476           (if single-byte
477               (logand branch-byte #b00111111)
478             (let ((pos (= (logand branch-byte #b00100000) 0))
479                   (val (+ (* 256 (logand branch-byte #b00011111))
480                           (ez-read-pc-byte-and-inc))))
481               (if pos
482                   val
483                 (- val 8192))))))
484     (if (or (and branch (not invert))
485             (and (not branch) invert))
486         (cond
487          ((= offset 0)
488           (ez-op-rfalse))
489          ((= offset 1)
490           (ez-op-rtrue))
491          (t
492           (ez-set-pc (+ (ez-get-pc) offset -2)))))))
493
494 ;; Operations
495
496 (defvar ez-op-table
497   '(((0op #x00) rtrue ez-op-rtrue)
498     ((0op #x01) rfalse ez-op-rfalse)
499     ((1op #x00) jz ez-op-jz)
500     ((1op #x05) inc ez-op-inc)
501     ((1op #x06) dec ez-op-dec)
502     ((1op #x0B) ret ez-op-ret)
503     ((1op #x0C) jump ez-op-jump)
504     ((2op #x05) inc_jg ez-op-inc-jg)
505     ((2op #x04) dec_jg ez-op-dec-jg)
506     ((2op #x0D) store ez-op-store)
507     ((1op #x0E) load ez-op-load)
508     ((var #x01) storew ez-op-storew)
509     ((2op #x0F) loadw ez-op-loadw)
510     ((var #x02) storeb ez-op-storeb)
511     ((2op #x10) loadb ez-op-loadb)
512     ((2op #x01) je ez-op-je)
513     ((2op #x08) or ez-op-or)
514     ((2op #x09) and ez-op-and)
515     ((2op #x14) add ez-op-add)
516     ((2op #x15) sub ez-op-sub)
517     ((2op #x16) mul ez-op-mul)
518     ((2op #x17) div ez-op-div)
519     ((2op #x18) mod ez-op-mod)
520     ((var #x00) call_fv ez-op-callf)
521     ((1op #x01) get_sibling ez-op-get-sibling)
522     ((1op #x02) get_child ez-op-get-child)
523     ((1op #x03) get_parent ez-op-get-parent)
524     ((2op #x0A) test_attr ez-op-test-attr)
525     ((var #x03) put_prop ez-op-put-prop)
526     ((0op #x02) print ez-op-print)
527     ((0op #x0B) new_line ez-op-new-line)
528     ((var #x06) print_num ez-op-print-num)
529     ((var #x05) print_char ez-op-print-char)))
530
531 (defun ez-op-ret (operands &optional operand-types)
532   (let ((retval (car operands)))
533     (ez-pop-call-stack-frame)
534     (ez-set-var (ez-read-pc-byte-and-inc) retval))
535   t)
536
537 (defun ez-op-rtrue (&optional operands operand-types)
538   (ez-op-ret (list 1))
539   t)
540
541 (defun ez-op-rfalse (&optional operands operand-types)
542   (ez-op-ret (list 0))
543   t)
544
545 (defun ez-op-jz (operands operand-types)
546   (ez-do-branch (= (car operands) 0))
547   t)
548
549 (defun ez-op-je (operands operand-types)
550   (ez-do-branch (memq (car operands) (cdr operands)))
551   t)
552
553 (defun ez-op-jg (operands operand-types)
554   (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types)))
555         (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types))))
556     (ez-do-branch (> s1 s2)))
557   t)
558
559 (defun ez-op-jl (operands operand-types)
560   (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types)))
561         (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types))))
562     (ez-do-branch (< s1 s2)))
563   t)
564
565 (defun ez-op-inc-jg (operands operand-types)
566   (let ((var (car operands)))
567     (ez-op-inc (list var))
568     (ez-op-jg (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types))))
569   t)
570
571 (defun ez-op-dec-jl (operands operand-types)
572   (let ((var (car operands)))
573     (ez-op-dec (list var))
574     (ez-op-jl (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types))))
575   t)
576
577
578 (defun ez-op-jump (operands operand-types)
579   (let ((offset (if (eq (car operand-types) 'b)
580                     (ez-decode-signed-byte (car operands))
581                   (ez-decode-signed-word (car operands)))))
582     (ez-set-pc (+ (ez-get-pc) offset -2)))
583   t)
584
585 (defun ez-op-inc (operands &optional operand-types)
586   (let ((var (car operands)))
587     (ez-set-var var (mod (+ 1 (ez-get-var var)) #x10000)))
588   t)
589
590 (defun ez-op-dec (operands &optional operand-types)
591   (let ((var (car operands)))
592     (ez-set-var var (mod (+ (ez-get-var var) 1) #x10000)))
593   t)
594
595 (defun ez-op-store (operands operand-types)
596   (let ((var (car operands))
597         (a (cadr operands)))
598     (ez-set-var var a))
599   t)
600
601 (defun ez-op-load (operands operand-types)
602   (let ((var (car operands)))
603     (ez-set-var (ez-read-pc-byte-and-inc) (ez-get-var var)))
604   t)
605
606 (defun ez-op-storew (operands operand-types)
607   (let ((baddr (car operands))
608         (n (cadr operands))
609         (a (caddr operands)))
610     (ez-mem-set-word (+ baddr (* 2 n)) a))
611   t)
612
613 (defun ez-op-loadw (operands operand-types)
614   (let ((baddr (car operands))
615         (n (cadr operands)))
616     (ez-set-var (ez-read-pc-byte-and-inc) (ez-mem-ref-word (+ baddr (* 2 n)))))
617   t)
618
619 (defun ez-op-storeb (operands operand-types)
620   (let ((baddr (car operands))
621         (n (cadr operands))
622         (a (caddr operands)))
623     (ez-mem-set-byte (+ baddr n) a))
624   t)
625
626 (defun ez-op-loadb (operands operand-types)
627   (let ((baddr (car operands))
628         (n (cadr operands)))
629     (ez-set-var (ez-read-pc-byte-and-inc) (ez-mem-ref-byte (+ baddr n))))
630   t)
631
632 (defun ez-op-and (operands operand-types)
633   (let ((a (car operands))
634         (b (cadr operands)))
635     (ez-set-var (ez-read-pc-byte-and-inc) (logand a b)))
636   t)
637
638 (defun ez-op-or (operands operand-types)
639   (let ((a (car operands))
640         (b (cadr operands)))
641     (ez-set-var (ez-read-pc-byte-and-inc) (logior a b)))
642   t)
643
644 (defun ez-op-add (operands operand-types)
645   (let ((a (car operands))
646         (b (cadr operands)))
647     (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ a b) #x10000)))
648   t)
649
650 (defun ez-op-sub (operands operand-types)
651   (let ((a (car operands))
652         (b (cadr operands)))
653     (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ (- a b) #x10000) #x10000)))
654   t)
655
656 (defun ez-op-mul (a b)
657   (let ((a (car operands))
658         (b (cadr operands)))
659     (ez-set-var (ez-read-pc-byte-and-inc) (mod (* a b) #x10000)))
660   t)
661
662 (defun ez-op-div (a b)
663   (error "Not implemented"))
664
665 (defun ez-op-mod (a b)
666   (error "Not implemented"))
667
668 (defun ez-op-callf (operands operand-types)
669   (let* ((raddr (car operands))
670          (call-operands (cdr operands))
671          (r (* 2 raddr))
672          (L (ez-mem-ref-byte r))
673          (n (length call-operands))
674          (new-pc (+ r 1 (* L 2))))
675     (ez-add-call-stack-frame new-pc)
676     (dotimes (i L)
677       (if (< i n)
678           (ez-set-local-var (+ i 1) (elt call-operands i))
679         (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i)))))))
680   t)
681
682 (defun ez-op-test-attr (operands operand-types)
683   (let ((obj (car operands))
684         (attr (cadr operands)))
685     (ez-do-branch (= 1 (ez-get-obj-attr obj attr)))
686     t))
687
688 (defun ez-op-get-sibling (operands operand-types)
689   (let ((sib (ez-get-obj-sibling (car operands))))
690     (ez-set-var (ez-read-pc-byte-and-inc) sib)
691     (ez-do-branch (> sib 0))
692     t))
693
694 (defun ez-op-get-child (operands operand-types)
695   (let ((child (ez-get-obj-child (car operands))))
696     (ez-set-var (ez-read-pc-byte-and-inc) child)
697     (ez-do-branch (> child 0))
698     t))
699
700 (defun ez-op-get-parent (operands operand-types)
701   (let ((parent (ez-get-obj-parent (car operands))))
702     (ez-set-var (ez-read-pc-byte-and-inc) parent)
703     (ez-do-branch (> parent 0))
704     t))
705
706 (defun ez-op-put-prop (operands operand-types)
707   (let* ((obj (car operands))
708          (prop (cadr operands))
709          (a (caddr operands)))
710     (ez-set-obj-prop obj prop a)
711     t))
712
713 (defun ez-op-print (operands operand-types)
714   (let* ((word-count-and-string (ez-get-zstring-and-length (ez-get-pc)))
715          (word-count (car word-count-and-string))
716          (string (cdr word-count-and-string)))
717     (ez-print string)
718     (ez-increment-pc (* 2 word-count)))
719   t)
720
721 (defun ez-op-new-line (operands operand-types)
722   (ez-print "\n")
723   t)
724
725 (defun ez-op-print-num (operands operand-types)
726   (let ((s (ez-decode-signed-operand (car operands) (car operand-types))))
727     (ez-print (number-to-string s)))
728   t)
729
730 (defun ez-op-print-char (operands operand-types)
731   (let ((c (car operands)))
732     (ez-print (string c)))
733   t)
734
735
736 ;; Execution loop
737
738 (defun ez-run (filename)
739   (ez-load-file filename)
740   (ez-parse-header)
741   (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc)))
742
743   (while (ez-execute-instr)))
744
745
746 ;;; Buffer and I/O
747 ;;
748
749 (defun ez-render-prompt ()
750   (with-current-buffer "*ez*"
751     (let ((update-point (= ez-input-marker (point)))
752           (update-window-points (mapcar (lambda (w)
753                                           (list (= (window-point w) ez-input-marker)
754                                                 w))
755                                         (get-buffer-window-list nil nil t))))
756       (save-excursion
757         (set-marker-insertion-type ez-prompt-marker nil)
758         (set-marker-insertion-type ez-input-marker t)
759         (let ((inhibit-read-only t))
760           (delete-region ez-prompt-marker ez-input-marker)
761           (goto-char ez-prompt-marker)
762           (insert
763            ">"
764            (propertize " " ; Need this to be separate to mark it as rear-nonsticky
765                        'read-only t
766                        'rear-nonsticky t)))
767         (set-marker-insertion-type ez-input-marker nil))
768       (goto-char ez-input-marker))))
769
770 (defvar ez-prompt-marker nil
771   "Marker for prompt position in buffer.")
772
773 (defvar ez-input-marker nil
774   "Marker for prompt position in buffer.")
775
776 (defun ez-setup-buffer ()
777   (with-current-buffer (get-buffer-create "*ez*")
778     (let ((inhibit-read-only t))
779       (delete-region (point-min) (point-max)))
780     (setq-local scroll-conservatively 1)
781     (if (markerp ez-prompt-marker)
782         (set-marker ez-prompt-marker (point-max))
783       (setq ez-prompt-marker (point-max-marker)))
784     (if (markerp ez-input-marker)
785         (set-marker ez-input-marker (point-max))
786       (setq ez-input-marker (point-max-marker)))
787     (goto-char (point-max))
788     (ez-render-prompt)))
789
790
791 (defun ez-print (string)
792   (with-current-buffer "*ez*"
793     (save-excursion
794       (goto-char ez-prompt-marker)
795       (insert-before-markers string))))
796
797 ;; Mode
798
799 (defvar ez-mode-map
800   (let ((map (make-sparse-keymap)))
801     (define-key map (kbd "RET") 'ez-enter)
802     map))
803
804 (define-derived-mode ez-mode text-mode "ez"
805   "Major mode for EZ.")
806
807 (when (fboundp 'evil-set-initial-state)
808   (evil-set-initial-state 'ez-mode 'insert))
809
810 (defun ez (zfile)
811   (interactive "fEnter name of z3 story file: ")
812   (if (get-buffer "*ez*")
813       (switch-to-buffer "*ez*")
814     (switch-to-buffer "*ez*")
815     (ez-mode)
816     (ez-setup-buffer)
817     (ez-run zfile))
818   "Started EZ.")
819
820 (defun ez-debug ()
821   (interactive)
822   (switch-to-buffer "*ez*")
823   (ez-mode)
824   (ez-setup-buffer)
825   (ez-run "zork1.z3"))
826
827 ;;; ez.el ends here