+ ((<= #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))
+ (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)
+ ((1op #x01) get_sibling ez-op-get-sibling)
+ ((1op #x02) get_child ez-op-get-child)
+ ((1op #x03) get_parent ez-op-get-parent)
+ ((var #x03) put_prop ez-op-put-prop)))
+
+(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)
+
+(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))))
+
+(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))))
+
+(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))))
+
+(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)))