From: plugd Date: Fri, 15 Oct 2021 15:22:31 +0000 (+0200) Subject: Added several more instructions. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=9d1263da3e1499040fe258ea8dea1c60ae7ccf6a;p=ez.git Added several more instructions. --- diff --git a/ez.el b/ez.el index 22a4c78..e899ab4 100644 --- a/ez.el +++ b/ez.el @@ -30,8 +30,25 @@ ;; 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) - (lsh 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 @@ -353,34 +370,132 @@ (if (= type 1) 'b 'w)) - types))))) + 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 - '(((2op #x14) add ez-op-add) + '(((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)))) + (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)))) + (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)))) + (ez-set-var (ez-read-pc-byte-and-inc) (mod (* a b) #x10000))) + t) -(defun ez-op-div (a b)) +(defun ez-op-div (a b) + (error "Not implemented")) -(defun ez-op-mod (a b)) +(defun ez-op-mod (a b) + (error "Not implemented")) (defun ez-op-callf (operands operand-types) (let* ((raddr (car operands)) @@ -411,16 +526,24 @@ (ez-parse-header) (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc))) (ez-execute-instr) + +(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)) + + + + + + + + -(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))))