181a37a444a5f70a8c410bc11a431652c92c2dbd
[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-addr 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 (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 (base-addr)
256   (let* ((word-count-and-chars (ez-get-zstring-chars 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 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
284 ;; Call stack
285
286 (defvar ez-call-stack nil)
287
288 (defun ez-make-call-stack-frame (pc &optional call-method)
289   (list pc
290         nil
291         (make-vector 15 0)
292         call-method))
293
294 (defun ez-add-call-stack-frame (pc &optional call-method)
295   (push (ez-make-call-stack-frame pc call-method) ez-call-stack))
296
297 (defun ez-pop-call-stack-frame ()
298   (pop ez-call-stack))
299
300 (defun ez-routine-stack ()
301   (elt (car ez-call-stack) 1))
302
303 (defun ez-routine-stack-push (val)
304   (let ((frame (car ez-call-stack)))
305     (setf (elt frame 1) (cons val (elt frame 1)))))
306
307 (defun ez-routine-stack-pop ()
308   (let* ((frame (car ez-call-stack))
309          (rs-head (car (elt frame 1))))
310     (setf (elt frame 1) (cdr (elt frame 1)))
311     rs-head))
312
313 (defun ez-get-local-var (lvar)
314   (let ((frame (car ez-call-stack)))
315     (aref (elt frame 2) (- lvar 1))))
316
317 (defun ez-set-local-var (lvar val)
318   (let ((frame (car ez-call-stack)))
319     (aset (elt frame 2) (- lvar 1) val)))
320
321 (defun ez-get-pc ()
322   (caar ez-call-stack))
323
324 (defun ez-set-pc (new-pc)
325   (setf (car (car ez-call-stack)) new-pc))
326
327 (defun ez-increment-pc (inc)
328   (ez-set-pc (+ (ez-get-pc) inc)))
329
330 (defun ez-read-pc-byte-and-inc ()
331   (let ((res (ez-mem-ref-byte (ez-get-pc))))
332     (ez-increment-pc 1)
333     res))
334
335 (defun ez-read-pc-word-and-inc ()
336   (let ((res (ez-mem-ref-word (ez-get-pc))))
337     (ez-increment-pc 2)
338     res))
339
340 ;; Instruction execution
341
342 (defun ez-get-var (var)
343   (cond
344    ((= var 0)
345     (ez-routine-stack-pop))
346    ((< var 16)
347     (ez-get-local-var var))
348    (t
349     (ez-get-global-var (- var 16)))))
350
351 (defun ez-set-var (var val)
352   (cond
353    ((= var 0)
354     (ez-routine-stack-push val))
355    ((< var 16)
356     (ez-set-local-var var val))
357    (t
358     (ez-set-global-var (- var 16) val))))
359
360 (defun ez-read-pc-var-and-inc ()
361   (ez-get-var (ez-read-pc-byte-and-inc)))
362
363 (defun ez-execute-instr ()
364   (let ((instr-pc (ez-get-pc))
365         (opbyte (ez-read-pc-byte-and-inc))
366         (optype) (opcode) (operands))
367     (cond
368      ((<= #x0 opbyte #x1f)
369       (setq optype '2op
370             opcode opbyte
371             operands (list (ez-read-pc-byte-and-inc)
372                            (ez-read-pc-byte-and-inc))
373             operand-types '(b b)))
374      ((<= #x20 opbyte #x3F)
375       (setq optype '2op
376             opcode (- opbyte #x20)
377             operands (list (ez-read-pc-byte-and-inc)
378                            (ez-read-pc-var-and-inc))
379             operand-types '(b w)))
380      ((<= #x40 opbyte #x5F)
381       (setq optype '2op
382             opcode (- opbyte #x40)
383             operands (list (ez-read-pc-var-and-inc)
384                            (ez-read-pc-byte-and-inc))
385             operand-types '(w b)))
386      ((<= #x60 opbyte #x7F)
387       (setq optype '2op
388             opcode (- opbyte #x60)
389             operands (list (ez-read-pc-var-and-inc)
390                            (ez-read-pc-var-and-inc))
391             operand-types '(w w)))
392      ((<= #x80 opbyte #x8F)
393       (setq optype '1op
394             opcode (- opbyte #x80)
395             operands (list (ez-read-pc-word-and-inc))
396             operand-types '(w)))
397      ((<= #x90 opbyte #x9F)
398       (setq optype '1op
399             opcode (- opbyte #x90)
400             operands (list (ez-read-pc-byte-and-inc))
401             operand-types '(b)))
402      ((<= #xA0 opbyte #xAF)
403       (setq optype '1op
404             opcode (- opbyte #xa0)
405             operands (list (ez-read-pc-var-and-inc))
406             operand-types '(w)))
407      ((<= #xB0 opbyte #xBF)
408       (setq optype '0op
409             opcode (- opbyte #xb0)
410             operands '()
411             operand-types '()))
412      ((<= #xC0 opbyte #xDF)
413       (setq optype '2op
414             opcode (- opbyte #xc0))
415       (let ((operands-and-types (ez-read-var-operands-and-inc)))
416         (setq operands (car operands-and-types)
417               operand-types (cdr operands-and-types))))
418      ((<= #xE0 opbyte #xFF)
419       (setq optype 'var
420             opcode (- opbyte #xe0))
421       (let ((operands-and-types (ez-read-var-operands-and-inc)))
422         (setq operands (car operands-and-types)
423               operand-types (cdr operands-and-types)))))
424     (let ((table-row (assoc (list optype opcode) ez-op-table)))
425       (unless table-row
426         (error "Unsupported op PC:%x Optype:%s Opcode:%x Operands:%s Operand-types:%s"
427                instr-pc optype opcode operands operand-types))
428       (let ((mnemonic (elt table-row 1)))
429         (message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s Operand-types:%s"
430                  instr-pc optype opcode mnemonic operands operand-types))
431       (funcall (elt table-row 2) operands operand-types))))
432
433 (defun ez-read-var-operands-and-inc ()
434   (let* ((type-byte (ez-read-pc-byte-and-inc))
435          (types (let ((type1 (lsh type-byte -6)))
436                   (if (= type1 #b11)
437                       nil
438                     (cons type1
439                           (let ((type2 (lsh (logand #b110000 type-byte) -4)))
440                             (if (= type2 #b11)
441                                 nil
442                               (cons type2
443                                     (let ((type3 (lsh (logand #b1100 type-byte) -2)))
444                                       (if (= type3 #b11)
445                                           nil
446                                         (cons type3
447                                               (let ((type4 (logand #b11)))
448                                                 (if (= type4 #b11)
449                                                     nil
450                                                   (list type4))))))))))))))
451     (cons
452      (mapcar
453       (lambda (type)
454         (cond
455          ((= type 0) (ez-read-pc-word-and-inc))
456          ((= type 1) (ez-read-pc-byte-and-inc))
457          ((= type 2) (ez-read-pc-var-and-inc))))
458       types)
459      (mapcar
460       (lambda (type)
461         (if (= type 1)
462             'b
463           'w))
464       types))))
465
466 ;; Branches
467
468 (defun ez-do-branch (branch)
469   (let* ((branch-byte (ez-read-pc-byte-and-inc))
470          (invert (= 0 (logand branch-byte #b10000000)))
471          (single-byte (> (logand branch-byte #b01000000) 0))
472          (offset
473           (if single-byte
474               (logand branch-byte #b00111111)
475             (let ((pos (= (logand branch-byte #b00100000) 0))
476                   (val (+ (* 256 (logand branch-byte #b00011111))
477                           (ez-read-pc-byte-and-inc))))
478               (if pos
479                   val
480                 (- val 8192))))))
481     (if (or (and branch (not invert))
482             (and (not branch) invert))
483         (cond
484          ((= offset 0)
485           (ez-op-rfalse))
486          ((= offset 1)
487           (ez-op-rtrue))
488          (t
489           (ez-set-pc (+ (ez-get-pc) offset -2)))))))
490
491 ;; Operations
492
493 (defvar ez-op-table
494   '(((0op #x00) rtrue ez-op-rtrue)
495     ((0op #x01) rfalse ez-op-rfalse)
496     ((1op #x00) jz ez-op-jz)
497     ((1op #x05) inc ez-op-inc)
498     ((1op #x06) dec ez-op-dec)
499     ((1op #x0B) ret ez-op-ret)
500     ((1op #x0C) jump ez-op-jump)
501     ((2op #x05) inc_jg ez-op-inc-jg)
502     ((2op #x04) dec_jg ez-op-dec-jg)
503     ((2op #x0D) store ez-op-store)
504     ((1op #x0E) load ez-op-load)
505     ((var #x01) storew ez-op-storew)
506     ((2op #x0F) loadw ez-op-loadw)
507     ((2op #x01) je ez-op-je)
508     ((2op #x08) or ez-op-or)
509     ((2op #x09) and ez-op-and)
510     ((2op #x14) add ez-op-add)
511     ((2op #x15) sub ez-op-sub)
512     ((2op #x16) mul ez-op-mul)
513     ((2op #x17) div ez-op-div)
514     ((2op #x18) mod ez-op-mod)
515     ((var #x00) call_fv ez-op-callf)
516     ((1op #x01) get_sibling ez-op-get-sibling)
517     ((1op #x02) get_child ez-op-get-child)
518     ((1op #x03) get_parent ez-op-get-parent)
519     ((2op #x0A) test_attr ez-op-test-attr)
520     ((var #x03) put_prop ez-op-put-prop)
521     ((0op #x02) print ez-op-print)
522     ((0op #x0B) new_line ez-op-new-line)
523     ((var #x06) print_num ez-op-print-num)))
524
525 (defun ez-op-ret (operands &optional operand-types)
526   (let ((retval (car operands)))
527     (ez-pop-call-stack-frame)
528     (ez-set-var (ez-read-pc-byte-and-inc) retval))
529   t)
530
531 (defun ez-op-rtrue (&optional operands operand-types)
532   (ez-op-ret (list 1))
533   t)
534
535 (defun ez-op-rfalse (&optional operands operand-types)
536   (ez-op-ret (list 0))
537   t)
538
539 (defun ez-op-jz (operands operand-types)
540   (ez-do-branch (= (car operands) 0))
541   t)
542
543 (defun ez-op-je (operands operand-types)
544   (ez-do-branch (memq (car operands) (cdr operands)))
545   t)
546
547 (defun ez-op-jg (operands operand-types)
548   (let ((s1 (ez-decode-signed-operand (car operands) (car operand-types)))
549         (s2 (ez-decode-signed-operand (cadr operands) (cadr operand-types))))
550     (ez-do-branch (> s1 s2)))
551   t)
552
553 (defun ez-op-jl (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-inc-jg (operands operand-types)
560   (let ((var (car operands)))
561     (ez-op-inc (list var))
562     (ez-op-jg (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types))))
563   t)
564
565 (defun ez-op-dec-jl (operands operand-types)
566   (let ((var (car operands)))
567     (ez-op-dec (list var))
568     (ez-op-jl (cons (ez-get-var var) (cdr operands)) (cons 'w (cdr operand-types))))
569   t)
570
571
572 (defun ez-op-jump (operands operand-types)
573   (let ((offset (if (eq (car operand-types) 'b)
574                     (ez-decode-signed-byte (car operands))
575                   (ez-decode-signed-word (car operands)))))
576     (ez-set-pc (+ (ez-get-pc) offset -2)))
577   t)
578
579 (defun ez-op-inc (operands &optional operand-types)
580   (let ((var (car operands)))
581     (ez-set-var var (mod (+ 1 (ez-get-var var)) #x10000)))
582   t)
583
584 (defun ez-op-dec (operands &optional operand-types)
585   (let ((var (car operands)))
586     (ez-set-var var (mod (+ (ez-get-var var) 1) #x10000)))
587   t)
588
589 (defun ez-op-store (operands operand-types)
590   (let ((var (car operands))
591         (a (cadr operands)))
592     (ez-set-var var a))
593   t)
594
595 (defun ez-op-load (operands operand-types)
596   (let ((var (car operands)))
597     (ez-set-var (ez-read-pc-byte-and-inc) (ez-get-var var)))
598   t)
599
600 (defun ez-op-storew (operands operand-types)
601   (let ((baddr (car operands))
602         (n (cadr operands))
603         (a (caddr operands)))
604     (ez-mem-set-word (+ baddr (* 2 n)) a))
605   t)
606
607 (defun ez-op-loadw (operands operand-types)
608   (let ((baddr (car operands))
609         (n (cadr operands)))
610     (ez-set-var (ez-read-pc-byte-and-inc) (ez-mem-ref-word (+ baddr (* 2 n)))))
611   t)
612
613 (defun ez-op-and (operands operand-types)
614   (let ((a (car operands))
615         (b (cadr operands)))
616     (ez-set-var (ez-read-pc-byte-and-inc) (logand a b)))
617   t)
618
619 (defun ez-op-or (operands operand-types)
620   (let ((a (car operands))
621         (b (cadr operands)))
622     (ez-set-var (ez-read-pc-byte-and-inc) (logior a b)))
623   t)
624
625 (defun ez-op-add (operands operand-types)
626   (let ((a (car operands))
627         (b (cadr operands)))
628     (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ a b) #x10000)))
629   t)
630
631 (defun ez-op-sub (operands operand-types)
632   (let ((a (car operands))
633         (b (cadr operands)))
634     (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ (- a b) #x10000) #x10000)))
635   t)
636
637 (defun ez-op-mul (a b)
638   (let ((a (car operands))
639         (b (cadr operands)))
640     (ez-set-var (ez-read-pc-byte-and-inc) (mod (* a b) #x10000)))
641   t)
642
643 (defun ez-op-div (a b)
644   (error "Not implemented"))
645
646 (defun ez-op-mod (a b)
647   (error "Not implemented"))
648
649 (defun ez-op-callf (operands operand-types)
650   (let* ((raddr (car operands))
651          (call-operands (cdr operands))
652          (r (* 2 raddr))
653          (L (ez-mem-ref-byte r))
654          (n (length call-operands))
655          (new-pc (+ r 1 (* L 2))))
656     (ez-add-call-stack-frame new-pc)
657     (dotimes (i L)
658       (if (< i n)
659           (ez-set-local-var (+ i 1) (elt call-operands i))
660         (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i)))))))
661   t)
662
663 (defun ez-op-test-attr (operands operand-types)
664   (let ((obj (car operands))
665         (attr (cadr operands)))
666     (ez-do-branch (= 1 (ez-get-obj-attr obj attr)))
667     t))
668
669 (defun ez-op-get-sibling (operands operand-types)
670   (let ((sib (ez-get-obj-sibling (car operands))))
671     (ez-set-var (ez-read-pc-byte-and-inc) sib)
672     (ez-do-branch (> sib 0))
673     t))
674
675 (defun ez-op-get-child (operands operand-types)
676   (let ((child (ez-get-obj-child (car operands))))
677     (ez-set-var (ez-read-pc-byte-and-inc) child)
678     (ez-do-branch (> child 0))
679     t))
680
681 (defun ez-op-get-parent (operands operand-types)
682   (let ((parent (ez-get-obj-parent (car operands))))
683     (ez-set-var (ez-read-pc-byte-and-inc) parent)
684     (ez-do-branch (> parent 0))
685     t))
686
687 (defun ez-op-put-prop (operands operand-types)
688   (let* ((obj (car operands))
689          (prop (cadr operands))
690          (a (caddr operands)))
691     (ez-set-obj-prop obj prop a)
692     t))
693
694 (defun ez-op-print (operands operand-types)
695   (let* ((word-count-and-string (ez-get-zstring (ez-get-pc)))
696          (word-count (car word-count-and-string))
697          (string (cdr word-count-and-string)))
698     (ez-print string)
699     (ez-increment-pc (* 2 word-count)))
700   t)
701
702 (defun ez-op-new-line (operands operand-types)
703   (ez-print "\n")
704   t)
705
706 (defun ez-op-print-num (operands operand-types)
707   (let ((s (ez-decode-signed-operand (car operands) (car operand-types))))
708     (ez-print (number-to-string s)))
709   t)
710
711
712 ;; Execution loop
713
714 (defun ez-run (filename)
715   (ez-load-file filename)
716   (ez-parse-header)
717   (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc)))
718
719   (while (ez-execute-instr)))
720
721
722 ;;; Buffer and I/O
723 ;;
724
725 (defun ez-render-prompt ()
726   (with-current-buffer "*ez*"
727     (let ((update-point (= ez-input-marker (point)))
728           (update-window-points (mapcar (lambda (w)
729                                           (list (= (window-point w) ez-input-marker)
730                                                 w))
731                                         (get-buffer-window-list nil nil t))))
732       (save-excursion
733         (set-marker-insertion-type ez-prompt-marker nil)
734         (set-marker-insertion-type ez-input-marker t)
735         (let ((inhibit-read-only t))
736           (delete-region ez-prompt-marker ez-input-marker)
737           (goto-char ez-prompt-marker)
738           (insert
739            ">"
740            (propertize " " ; Need this to be separate to mark it as rear-nonsticky
741                        'read-only t
742                        'rear-nonsticky t)))
743         (set-marker-insertion-type ez-input-marker nil))
744       (goto-char ez-input-marker))))
745
746 (defvar ez-prompt-marker nil
747   "Marker for prompt position in buffer.")
748
749 (defvar ez-input-marker nil
750   "Marker for prompt position in buffer.")
751
752 (defun ez-setup-buffer ()
753   (with-current-buffer (get-buffer-create "*ez*")
754     (let ((inhibit-read-only t))
755       (delete-region (point-min) (point-max)))
756     (setq-local scroll-conservatively 1)
757     (if (markerp ez-prompt-marker)
758         (set-marker ez-prompt-marker (point-max))
759       (setq ez-prompt-marker (point-max-marker)))
760     (if (markerp ez-input-marker)
761         (set-marker ez-input-marker (point-max))
762       (setq ez-input-marker (point-max-marker)))
763     (goto-char (point-max))
764     (ez-render-prompt)))
765
766
767 (defun ez-print (string)
768   (with-current-buffer "*ez*"
769     (save-excursion
770       (goto-char ez-prompt-marker)
771       (insert-before-markers string))))
772
773 ;; Mode
774
775 (defvar ez-mode-map
776   (let ((map (make-sparse-keymap)))
777     (define-key map (kbd "RET") 'ez-enter)
778     map))
779
780 (define-derived-mode ez-mode text-mode "ez"
781   "Major mode for EZ.")
782
783 (when (fboundp 'evil-set-initial-state)
784   (evil-set-initial-state 'ez-mode 'insert))
785
786 (defun ez (zfile)
787   (interactive "fEnter name of z3 story file: ")
788   (if (get-buffer "*ez*")
789       (switch-to-buffer "*ez*")
790     (switch-to-buffer "*ez*")
791     (ez-mode)
792     (ez-setup-buffer)
793     (ez-run zfile))
794   "Started EZ.")
795
796 (defun ez-debug ()
797   (interactive)
798   (switch-to-buffer "*ez*")
799   (ez-mode)
800   (ez-setup-buffer)
801   (ez-run "zork1.z3"))
802
803 ;;; ez.el ends here