Added get/set obj attribute functions.
[ez.git] / ez.el
diff --git a/ez.el b/ez.el
index 4ec4346..549b8f0 100644 (file)
--- a/ez.el
+++ b/ez.el
 (defun ez-get-obj-prop (obj prop)
   (let* ((prop-addr (ez-get-obj-prop-addr obj prop))
          (size-byte (ez-mem-ref-byte prop-addr)))
-    (if (= prop-addr 0)
+    (if (= size-byte 0)
         (ez-get-prop-default prop)
       (let ((prop-len (ez-get-prop-len size-byte))
             (data-addr (+ prop-addr 1)))
      (t
       (ez-mem-set-word (+ prop-addr 1) value)))))
 
+(defun ez-get-obj-attr (obj attr)
+  (let* ((byte-num (/ attr 8))
+         (bit-num (mod attr 8))
+         (bit-mask (lsh 1 (- 7 bit-num))))
+    (if (> (logand bit-mask (ez-mem-ref-byte (+ (ez-get-obj-addr obj) byte-num))) 0)
+        1
+      0)))
+
+(defun ez-set-obj-attr (obj attr val)
+  (let* ((byte-num (/ attr 8))
+         (bit-num (mod attr 8))
+         (bit-mask (lsh 1 (- 7 bit-num)))
+         (byte-addr (+ (ez-get-obj-addr obj) byte-num))
+         (byte (ez-mem-ref-byte byte-addr))
+         (current-set (> (logand bit-mask byte) 0)))
+    (if (or (and current-set (= val 0))
+            (and (not current-set) (> val 0)))
+        (ez-mem-set-byte byte-addr (logxor byte bit-mask)))))
+
 ;; Z-strings
 
 (defvar ez-zstring-alphabets
             operand-types '(w)))
      ((<= #xB0 opbyte #xBF)
       (setq optype '0op
-            opcode (- opbyte #xb0)))
+            opcode (- opbyte #xb0)
+            operands '()
+            operand-types '()))
      ((<= #xC0 opbyte #xDF)
-      (error "Unsupported op" opbyte))
+      (error "Unsupported op %x" opbyte))
      ((<= #xE0 opbyte #xFF)
       (setq optype 'var
             opcode (- opbyte #xe0))
               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))
+        (error "Unsupported op PC:%x Optype:%s Opcode:%x Operands:%s Operand-types:%s"
+               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))
+        (message "PC:%x Optype:%s Opcode:%x Mnemonic:%s Operands:%s Operand-types:%s"
+                 instr-pc optype opcode mnemonic operands operand-types))
       (funcall (elt table-row 2) operands operand-types))))
 
 (defun ez-read-var-operands-and-inc ()
                                               (let ((type4 (logand #b11)))
                                                 (if (= type4 #b11)
                                                     nil
-                                                  (list type4)))))))))))))
-         (operands nil))
+                                                  (list type4))))))))))))))
     (cons
      (mapcar
       (lambda (type)
 (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)))
+         (single-byte (> (logand branch-byte #b01000000) 0))
          (offset
           (if single-byte
               (logand branch-byte #b00111111)
               (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))))))
+    (if (or (and branch (not invert))
+            (and (not branch) invert))
+        (cond
+         ((= offset 0)
+          (ez-op-rfalse))
+         ((= offset 1)
+          (ez-op-rtrue))
+         (t
+          (ez-set-pc (+ (ez-get-pc) offset -2)))))))
 
 ;; Operations
 
     ((2op #x0D) store ez-op-store)
     ((1op #x0E) load ez-op-load)
     ((var #x01) storew ez-op-storew)
+    ((2op #x0F) loadw ez-op-loadw)
     ((2op #x01) je ez-op-je)
     ((2op #x08) or ez-op-or)
     ((2op #x09) and ez-op-and)
     ((1op #x01) get_sibling ez-op-get-sibling)
     ((1op #x02) get_child ez-op-get-child)
     ((1op #x03) get_parent ez-op-get-parent)
+    ((2op #x0A) test_attr ez-op-test-attr)
     ((var #x03) put_prop ez-op-put-prop)))
 
 (defun ez-op-ret (operands &optional operand-types)
   t)
 
 (defun ez-op-rtrue (&optional operands operand-types)
-  (ez-op-ret (list 1)))
+  (ez-op-ret (list 1))
+  t)
 
 (defun ez-op-rfalse (&optional operands operand-types)
-  (ez-op-ret (list 0)))
+  (ez-op-ret (list 0))
+  t)
 
 (defun ez-op-jz (operands operand-types)
   (ez-do-branch (= (car operands) 0))
     (ez-mem-set-word (+ baddr (* 2 n)) a))
   t)
 
+(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)
+
 (defun ez-op-and (operands operand-types)
   (let ((a (car operands))
         (b (cadr operands)))
         (ez-set-local-var (+ i 1) (ez-mem-ref-word (+ r 1 (* 2 i)))))))
   t)
 
+(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))
+
 (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))))
+    (ez-do-branch (> sib 0))
+    t))
 
 (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))))
+    (ez-do-branch (> child 0))
+    t))
 
 (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)
-    (ez-do-branch (> parent 0))))
+    (ez-do-branch (> parent 0))
+    t))
 
 (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)))
+    (ez-set-obj-prop obj prop a)
+    t))
 
 ;; Main