X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=ez.git;a=blobdiff_plain;f=ez.el;h=e899ab46493c68f693418ce9e0baa0331796fb6f;hp=4cea0751162b39b6cbb55bfbdf5d7f5e6f182d97;hb=9d1263da3e1499040fe258ea8dea1c60ae7ccf6a;hpb=9ac00a55f5cf3053b2224fb6d1a6189905e51587 diff --git a/ez.el b/ez.el index 4cea075..e899ab4 100644 --- a/ez.el +++ b/ez.el @@ -28,7 +28,27 @@ ;;; 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 @@ -236,7 +256,7 @@ ((= 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))))) @@ -245,7 +265,7 @@ ((= 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)))) @@ -253,7 +273,8 @@ (ez-get-var (ez-read-pc-byte-and-inc))) (defun ez-execute-instr () - (let ((opbyte (ez-read-pc-byte-and-inc)) + (let ((instr-pc (ez-get-pc)) + (opbyte (ez-read-pc-byte-and-inc)) (optype) (opcode nil) (operands)) @@ -262,34 +283,41 @@ (setq optype '2op opcode opbyte operands (list (ez-read-pc-byte-and-inc) - (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)))) + (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)))) + (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)))) + (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)))) + 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)))) + 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)))) + operands (list (ez-read-pc-var-and-inc)) + operand-types '(w))) ((<= #xB0 opbyte #xBF) (setq optype '0op opcode (- opbyte #xb0))) @@ -297,16 +325,17 @@ (error "Unsupported op" opbyte)) ((<= #xE0 opbyte #xFF) (setq optype 'var - opcode (- opbyte #xe0) - operands (ez-read-var-operands-and-inc)))) + 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" optype opcode)) (let ((mnemonic (elt table-row 1))) - (message "Optype:%s Opcode:%x Operands:%s" - (symbol-name optype) - opcode operands)) - (apply (elt table-row 2) operands)))) + (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)) @@ -328,27 +357,158 @@ nil (list type4))))))))))))) (operands nil)) - (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))) + (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 - '(((var #x0) call_fv ez-op-callf))) + '(((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-callf (raddr &rest operands) - (let* ((r (* 2 raddr)) +(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 operands)) + (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 operands i)) - (ez-set-local-var (+ i 1) (ez-mem-ref-byte (+ r 1 (* 2 i))))))) + (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 @@ -367,33 +527,23 @@ (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc))) (ez-execute-instr) -(binformat #x9b) -"10 01 10 11" +(binformat #x97) +"10010111" + + +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)) + -(* 2 #x2b07) -"10011011" -#b00101111 -(lsh #x2f) -ez-call-stack -(binformat #x2f) -"10 11 11" -(* 2 #x2b07) -(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))))