Fleshing out object functions.
[ez.git] / ez.el
diff --git a/ez.el b/ez.el
index 906b370..f1ef37e 100644 (file)
--- a/ez.el
+++ b/ez.el
 
 ;;; Code:
 
-;; Character set tables
+;; Utility functions
+
+(defun ez-decode-signed-bits (n nbits)
+  (if (= 0 (lsh n (- 1 nbits)))
+      n
+    (- n (lsh 1 nbits))))
+
+(defun ez-decode-signed-byte (b)
+  (ez-decode-signed-bits b 8))
+
+(defun ez-decode-signed-word (w)
+  (ez-decode-signed-bits w 16))
+
+(defun binformat (n &optional s)
+  (unless s
+    (setq s ""))
+  (let ((d (/ n 2))
+        (new-s (concat (number-to-string (mod n 2)) s)))
+    (if (= d 0)
+        new-s
+      (binformat d new-s))))
 
 ;; Memory
 
 
 ;; Object tree
 
-(defun ez-make-obj (&optional name attribs props parent first-child next-sibling)
-    (list name attribs props parent first-child next-sibling))
-
-(defun ez-obj-name (obj) (elt obj 0))
-(defun ez-obj-attribs (obj) (elt obj 1))
-(defun ez-obj-props (obj) (elt obj 2))
-(defun ez-obj-parent (obj) (elt obj 3))
-(defun ez-obj-first-child (obj) (elt obj 4))
-(defun ez-obj-next-sibling (obj) (elt obj 5))
-
 (defvar ez-property-defaults nil)
 
 (defun ez-load-property-defaults ()
   (dotimes (i 31)
     (aset ez-property-defaults i (aref ez-memory (+ ez-objtab-addr (* 2 i))))))
 
+(defun ez-get-obj-addr (obj-id)
+  (+ ez-objtab-addr (* 2 31) (* 9 (- obj-id 1))))
+
+(defun ez-get-obj-parent (obj-id)
+  (let ((addr (ez-get-obj-addr obj-id)))
+    (ez-mem-ref-byte (+ addr 4))))
+
+(defun ez-get-obj-sibling (obj-id)
+  (let ((addr (ez-get-obj-addr obj-id)))
+    (ez-mem-ref-byte (+ addr 5))))
+
+(defun ez-get-obj-child (obj-id)
+  (let ((addr (ez-get-obj-addr obj-id)))
+    (ez-mem-ref-byte (+ addr 6))))
+
+(defun ez-get-obj-plist-addr (obj-id)
+  (let ((addr (ez-get-obj-addr obj-id)))
+    (ez-mem-ref-word (+ addr 7))))
+
+(defun ez-get-obj-name (obj-id)
+  (let ((plist-addr (ez-get-obj-plist-addr obj-id)))
+    (if (> (ez-mem-ref-byte plist-addr) 0)
+        (ez-get-zstring (+ 1 (ez-get-obj-plist-addr obj-id)))
+      nil)))
+
 (defun ez-get-obj (obj-id)
   (let ((addr (+ ez-objtab-addr
                  (* 2 31)
      (let ((plist-addr (ez-mem-ref-word (+ addr 7))))
        (if (> (ez-mem-ref-byte plist-addr) 0)
            (ez-get-zstring (+ 1 plist-addr))
-         "")))))
+         nil)))))
 
 
 ;; Z-strings
        ((memq char '(1 2 3)) ;Abbreviation
         (setq abbrev-char char))
        ((= char 4)
-        (setq cur (mod (+ cur 1) 2)))
+        (setq cur (mod (+ cur 1) 3)))
        ((= char 5)
-        (setq cur (mod (+ 2 (- cur 1)) 2)))
+        (setq cur (mod (+ 3 (- cur 1)) 3)))
        (t 
         (setq s (concat s (substring (elt ez-zstring-alphabets cur)
                                      char (+ char 1))))
 (defun ez-increment-pc (inc)
   (ez-set-pc (+ (ez-get-pc) inc)))
 
+(defun ez-read-pc-byte-and-inc ()
+  (let ((res (ez-mem-ref-byte (ez-get-pc))))
+    (ez-increment-pc 1)
+    res))
+
+(defun ez-read-pc-word-and-inc ()
+  (let ((res (ez-mem-ref-word (ez-get-pc))))
+    (ez-increment-pc 2)
+    res))
 
 ;; Instruction execution
 
    ((= var 0)
     (ez-routine-stack-pop))
    ((< var 16)
-    (ez-get-local-var (- var 1)))
+    (ez-get-local-var var))
    (t
     (ez-get-global-var (- var 16)))))
 
    ((= var 0)
     (ez-routine-stack-push val))
    ((< var 16)
-    (ez-set-local-var (- var 1) val))
+    (ez-set-local-var var val))
    (t
     (ez-set-global-var (- var 16) val))))
 
+(defun ez-read-pc-var-and-inc ()
+  (ez-get-var (ez-read-pc-byte-and-inc)))
+
 (defun ez-execute-instr ()
-  (let ((op-byte (ez-mem-ref-byte (ez-get-pc))))
-    (ez-inc-pc 1)
+  (let ((instr-pc (ez-get-pc))
+        (opbyte (ez-read-pc-byte-and-inc))
+        (optype)
+        (opcode nil)
+        (operands))
     (cond
-     ((<= #x0 op-byte #x1f) (list '2op op-byte 'b 'b))
-     ((<= #x20 op-byte #x3F) (list '2op (- op-byte #x20) 'b 'v))
-     ((<= #x40 op-byte #x5F) (list '2op (- op-byte #x40) 'v 'b))
-     ((<= #x60 op-byte #x7F) (list '2op (- op-byte #x60) 'v 'v))
-     ((<= #x80 op-byte #x8F) (list '1op (- op-byte #x80) 'w))
-     ((<= #x90 op-byte #x9F) (list '1op (- op-byte #x90) 'b))
-     ((<= #xA0 op-byte #xAF) (list '1op (- op-byte #xa0) 'v))
-     ((<= #xB0 op-byte #xBF) (list '0op (- op-byte #xb0)))
-     ((<= #xC0 op-byte #xDF) (list '2op (- op-byte #xc0) 'var-instr-format))
-     ((<= #xE0 op-byte #xFF) (list 'var (- op-byte #xe0))
-      (let ((opcode (- op-byte #xe0))
-            (types (ez-mem-ref-byte (ez-get-pc))))
-        ))
-     )))
-
-(ez-get-instr ez-start-pc)
-(ez-mem-ref-byte ez-start-pc)
-
-(binformat #xe0)
-(binformat #x03)
-
+     ((<= #x0 opbyte #x1f)
+      (setq optype '2op
+            opcode opbyte
+            operands (list (ez-read-pc-byte-and-inc)
+                           (ez-read-pc-byte-and-inc))
+            operand-types '(b b)))
+     ((<= #x20 opbyte #x3F)
+      (setq optype '2op
+            opcode (- opbyte #x20)
+            operands (list (ez-read-pc-byte-and-inc)
+                           (ez-read-pc-var-and-inc))
+            operand-types '(b w)))
+     ((<= #x40 opbyte #x5F)
+      (setq optype '2op
+            opcode (- opbyte #x40)
+            operands (list (ez-read-pc-var-and-inc)
+                           (ez-read-pc-byte-and-inc))
+            operand-types '(w b)))
+     ((<= #x60 opbyte #x7F)
+      (setq optype '2op
+            opcode (- opbyte #x60)
+            operands (list (ez-read-pc-var-and-inc)
+                           (ez-read-pc-var-and-inc))
+            operand-types '(w w)))
+     ((<= #x80 opbyte #x8F)
+      (setq optype '1op
+            opcode (- opbyte #x80)
+            operands (list (ez-read-pc-word-and-inc))
+            operand-types '(w)))
+     ((<= #x90 opbyte #x9F)
+      (setq optype '1op
+            opcode (- opbyte #x90)
+            operands (list (ez-read-pc-byte-and-inc))
+            operand-types '(b)))
+     ((<= #xA0 opbyte #xAF)
+      (setq optype '1op
+            opcode (- opbyte #xa0)
+            operands (list (ez-read-pc-var-and-inc))
+            operand-types '(w)))
+     ((<= #xB0 opbyte #xBF)
+      (setq optype '0op
+            opcode (- opbyte #xb0)))
+     ((<= #xC0 opbyte #xDF)
+      (error "Unsupported op" opbyte))
+     ((<= #xE0 opbyte #xFF)
+      (setq optype 'var
+            opcode (- opbyte #xe0))
+      (let ((operands-and-types (ez-read-var-operands-and-inc)))
+        (setq operands (car operands-and-types)
+              operand-types (cdr operands-and-types)))))
+    (let ((table-row (assoc (list optype opcode) ez-op-table)))
+      (unless table-row
+        (error "Unsupported op" instr-pc optype opcode operands operand-types))
+      (let ((mnemonic (elt table-row 1)))
+        (message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s"
+                 instr-pc optype opcode mnemonic operands))
+      (funcall (elt table-row 2) operands operand-types))))
+
+(defun ez-read-var-operands-and-inc ()
+  (let* ((type-byte (ez-read-pc-byte-and-inc))
+         (bleh (message (binformat type-byte)))
+         (types (let ((type1 (lsh type-byte -6)))
+                  (if (= type1 #b11)
+                      nil
+                    (cons type1
+                          (let ((type2 (lsh (logand #b110000 type-byte) -4)))
+                            (if (= type2 #b11)
+                                nil
+                              (cons type2
+                                    (let ((type3 (lsh (logand #b1100 type-byte) -2)))
+                                      (if (= type3 #b11)
+                                          nil
+                                        (cons type3
+                                              (let ((type4 (logand #b11)))
+                                                (if (= type4 #b11)
+                                                    nil
+                                                  (list type4)))))))))))))
+         (operands nil))
+    (cons
+     (mapcar
+      (lambda (type)
+        (cond
+         ((= type 0) (ez-read-pc-word-and-inc))
+         ((= type 1) (ez-read-pc-byte-and-inc))
+         ((= type 2) (ez-read-pc-var-and-inc))))
+      types)
+     (mapcar
+      (lambda (type)
+        (if (= type 1)
+            'b
+          'w))
+      types))))
+
+;; Branches
+
+(defun ez-do-branch (branch)
+  (let* ((branch-byte (ez-read-pc-byte-and-inc))
+         (invert (= 0 (logand branch-byte #b10000000)))
+         (single-byte (= 1 (logand branch-byte #b01000000)))
+         (offset
+          (if single-byte
+              (logand branch-byte #b00111111)
+            (let ((pos (= (logand branch-byte #b00100000) 0))
+                  (val (+ (* 256 (logand branch-byte #b00011111))
+                          (ez-read-pc-byte-and-inc))))
+              (if pos
+                  val
+                (- val 8192))))))
+    (cond
+     ((= offset 0)
+      (ez-op-rfalse))
+     ((= offset 1)
+      (ez-op-rtrue))
+     (t
+      (ez-set-pc (+ (ez-get-pc) offset -2))))))
+
+;; Operations
+
+(defvar ez-op-table
+  '(((0op #x00) rtrue ez-op-rtrue)
+    ((0op #x01) rfalse ez-op-rfalse)
+    ((1op #x00) jz ez-op-jz)
+    ((1op #x0B) ret ez-op-ret)
+    ((1op #x0C) jump ez-op-jump)
+    ((2op #x0D) store ez-op-store)
+    ((1op #x0E) load ez-op-load)
+    ((var #x01) storew ez-op-storew)
+    ((2op #x01) je ez-op-je)
+    ((2op #x08) or ez-op-or)
+    ((2op #x09) and ez-op-and)
+    ((2op #x14) add ez-op-add)
+    ((2op #x15) sub ez-op-sub)
+    ((2op #x16) mul ez-op-mul)
+    ((2op #x17) div ez-op-div)
+    ((2op #x18) mod ez-op-mod)
+    ((var #x00) call_fv ez-op-callf)))
+
+(defun ez-op-ret (operands &optional operand-types)
+  (let ((retval (car operands)))
+    (ez-pop-call-stack-frame)
+    (ez-set-var (ez-read-pc-byte-and-inc) retval))
+  t)
+
+(defun ez-op-rtrue (&optional operands operand-types)
+  (ez-op-ret (list 1)))
+
+(defun ez-op-rfalse (&optional operands operand-types)
+  (ez-op-ret (list 0)))
+
+(defun ez-op-jz (operands operand-types)
+  (ez-do-branch (= (car operands) 0))
+  t)
+
+(defun ez-op-je (operands operand-types)
+  (ez-do-branch (memq (car operands) (cdr operands)))
+  t)
+
+(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)
+
+(defun ez-op-store (operands operand-types)
+  (let ((var (car operands))
+        (a (cadr operands)))
+    (ez-set-var var a))
+  t)
+
+(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)
+
+(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)
+
+(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)
+
+(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)
+
+(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)
+
+(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)
+
+(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)
+
+(defun ez-op-div (a b)
+  (error "Not implemented"))
+
+(defun ez-op-mod (a b)
+  (error "Not implemented"))
+
+(defun ez-op-callf (operands operand-types)
+  (let* ((raddr (car operands))
+         (call-operands (cdr operands))
+         (r (* 2 raddr))
+         (L (ez-mem-ref-byte r))
+         (n (length call-operands))
+         (new-pc (+ r 1 (* L 2))))
+    (ez-add-call-stack-frame new-pc)
+    (dotimes (i L)
+      (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)
 
 ;; Main
 
 (ez-load-file "zork1.z3")
 (ez-parse-header)
 (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc)))
+(ez-execute-instr)
+
+(while (ez-execute-instr))
+
+(binformat #x97)
+"10010111"
+(ez-get-obj 208)
+(binformat #x57)
+"01010111"
+
+
+
+ez-call-stack
+((28281 nil [33807 0 180 47872 0 0 0 0 0 0 0 0 ...] nil) (22022 nil [33807 65535 0 0 0 0 0 0 0 0 0 0 ...] nil) (20715 nil [0 0 0 0 0 0 0 0 0 0 0 0 ...] nil))
+
+((22049 nil [33807 0 180 47872 0 0 0 0 0 0 0 0 ...] nil) (22022 nil [33807 65535 0 0 0 0 0 0 0 0 0 0 ...] nil) (20715 nil [0 0 0 0 0 0 0 0 0 0 0 0 ...] nil))
+
+
+
+
+
+
 
-(ez-get-obj 1)
 
-(defun binformat (n &optional s)
-  (unless s
-    (setq s ""))
-  (let ((d (/ n 2))
-        (new-s (concat (number-to-string (mod n 2)) s)))
-    (if (= d 0)
-        new-s
-      (binformat d new-s))))
 
 
-(binformat (ez-mem-ref-byte ez-start-pc))
-"11100000"
 
 
 ;;; ez.el ends here