X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=ez.git;a=blobdiff_plain;f=ez.el;h=22a4c78fd739fec9166a24e3f89136bccfe927c9;hp=906b370b826771805fe3517ca9f77f3c3acb4d96;hb=d8227335527231b5d5205aca8113c54fa5dd2423;hpb=c270f548992eba116567e046d935f5d435285535 diff --git a/ez.el b/ez.el index 906b370..22a4c78 100644 --- a/ez.el +++ b/ez.el @@ -28,7 +28,10 @@ ;;; Code: -;; Character set tables +;; Utility functions + +(defun ez-decode-signed-byte (b) + (lsh b )) ;; Memory @@ -219,6 +222,15 @@ (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 @@ -227,7 +239,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))))) @@ -236,35 +248,153 @@ ((= 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" optype opcode)) + (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))))) + +(defvar ez-op-table + '(((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-add (operands operand-types) + (let ((a (car operands)) + (b (cadr operands))) + (ez-set-var (ez-read-pc-byte-and-inc) (mod (+ a b) #x10000)))) + +(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)))) + +(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)))) + +(defun ez-op-div (a b)) + +(defun ez-op-mod (a b)) + +(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 @@ -280,8 +410,8 @@ (ez-load-file "zork1.z3") (ez-parse-header) (setq ez-call-stack (list (ez-make-call-stack-frame ez-start-pc))) - -(ez-get-obj 1) +(ez-execute-instr) +ez-call-stack (defun binformat (n &optional s) (unless s @@ -293,8 +423,5 @@ (binformat d new-s)))) -(binformat (ez-mem-ref-byte ez-start-pc)) -"11100000" - ;;; ez.el ends here