From: plugd Date: Fri, 15 Nov 2019 08:51:09 +0000 (+0100) Subject: Working on executive function. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=fa939a053e40051209bd6c708f57423adb6801de;p=jars.git Working on executive function. --- diff --git a/mars.scm b/mars.scm index 5ea1fba..7dac46d 100644 --- a/mars.scm +++ b/mars.scm @@ -28,7 +28,7 @@ (('B-num) B-num) (('name) name) (('print) (print opcode - (if (null? modifier) "" (conc "." modifier)) + "." modifier " " (mode-string A-mode) A-num ", " (mode-string B-mode) B-num (if (null? name) "" (conc " ; " name)))) @@ -42,7 +42,7 @@ (define (mode-string mode) (case mode ((immediate) "#") - ((direct) "") + ((direct) "$") ((indirect-A "*")) ((indirect-B "@")) ((pre-indirect-A "{")) @@ -51,7 +51,7 @@ ((post-indirect-B ">")))) (define initial-instruction - (make-instr 'DAT '() 'immediate 0 'immediate 0 '())) + (make-instr 'DAT 'F 'immediate 0 'immediate 0 '())) ;;; Memory setup and addressing @@ -59,16 +59,22 @@ (define core (make-vector core-size '())) +(define (core-get i) + (vector-ref core i)) + +(define (core-set! i x) + (vector-set! core i x)) + (define (initialize-core) (let loop ((i 0)) (unless (>= i core-size) - (vector-set! core i (initial-instruction 'copy '())) + (core-set! i (initial-instruction 'copy '())) (loop (+ i 1))))) (define (core-dump) (let loop ((i 0)) (unless (>= i core-size) - ((vector-ref core i) 'print) + ((core-get i) 'print) (loop (+ i 1))))) (define (addr+ . args) @@ -90,7 +96,7 @@ (let loop ((ptr addr) (instrs (prog-instrs prog))) (unless (null? instrs) - (vector-set! core ptr ((car instrs) 'copy (prog-name prog))) + (core-set! ptr ((car instrs) 'copy (prog-name prog))) (loop (addr+ ptr 1) (cdr instrs)))) (make-player (prog-name prog) (addr+ addr (prog-offset prog)))) @@ -100,7 +106,7 @@ (remaining prog-len)) (if (= remaining 0) #t - (if (null? ((vector-ref core ptr) 'name)) + (if (null? ((core-get ptr) 'name)) (loop (addr+ ptr 1) (- remaining 1)) #f)))) @@ -143,26 +149,48 @@ (run (append other-players (list player)) (+ step 1))))))))) (define (execute-instr ptr) - (let* ((instr (vector-ref core ptr)) - (A-pointer (eval-operand (instr-A-mode instr) (instr-A-num) ptr)) - (B-pointer (eval-operand (instr-B-mode instr) (instr-B-num) ptr)) + (let* ((instr (core-get ptr)) + (A-pointer (eval-operand (instr 'A-mode) (instr 'A-num) ptr)) + (B-pointer (eval-operand (instr 'B-mode) (instr 'B-num) ptr)) (modifier (instr-modifier instr))) (case (instr-opcode instr) ((DAT) '()) ;Game over, man, game over! - ((MOV)) - ((ADD)) - ((SUB)) + ((MOV) + (case modifier + ((A)) + ((B)) + ((AB)) + ((BA)) + ((F)) + ((X)))) + ((ADD) + (case modifier + ((A)) + ((B)) + ((AB)) + ((BA)) + ((F)) + ((X)))) + ((SUB) + (case modifier + ((A)) + ((B)) + ((AB)) + ((BA)) + ((F)) + ((X)))) ((MUL)) ((DIV)) ((MOD)) ((JMP)) + ((JMZ)) ((JMN)) ((DJN)) ((SEQ CMP)) ((SNE)) ((SLT)) ((SPL)) - ((NOP)) + ((NOP) (list (addr+ ptr 1))) (else (error "Unrecognised opcode" (instr-opcode instr)))))) @@ -171,25 +199,25 @@ (case mode ((immediate) 0) ((direct) num) - ((indirect-A) (addr+ num (instr-A-num (vector-ref core (addr+ ptr num))))) - ((indirect-B) (addr+ num (instr-B-num (vector-ref core (addr+ ptr num))))) + ((indirect-A) (addr+ num ((core-get (addr+ ptr num)) 'A-num))) + ((indirect-B) (addr+ num ((core-get (addr+ ptr num)) 'B-num))) ((pre-indirect-A) - (let ((aux-instr (vector-ref core (addr+ ptr num)))) - (instr-set-A-num! aux-instr (addr+ -1 (instr-A-num aux-instr))) - (addr+ num (instr-A-num aux-instr)))) + (let ((aux-instr (core-get (addr+ ptr num)))) + (instr-set-A-num! aux-instr (addr+ -1 (aux-instr 'A-num))) + (addr+ num (aux-instr 'A-num)))) ((pre-indirect-B) - (let ((aux-instr (vector-ref core (addr+ ptr num)))) - (instr-set-B-num! aux-instr (addr+ -1 (instr-B-num aux-instr))) - (addr+ num (instr-B-num aux-instr)))) + (let ((aux-instr (core-get (addr+ ptr num)))) + (instr-set-B-num! aux-instr (addr+ -1 (aux-instr 'B-num))) + (addr+ num (aux-instr 'B-num)))) ((post-indirect-A) - (let* ((aux-instr (vector-ref core (addr+ ptr num))) - (old-A-num (instr-A-num aux-instr))) - (instr-set-A-num! aux-instr (addr+ 1 (instr-A-num aux-instr))) + (let* ((aux-instr (core-get (addr+ ptr num))) + (old-A-num (aux-instr 'A-num))) + (instr-set-A-num! aux-instr (addr+ 1 (aux-instr 'A-num))) (addr+ num old-A-num))) ((post-indirect-B) - (let* ((aux-instr (vector-ref core (addr+ ptr num))) - (old-B-num (instr-B-num aux-instr))) - (instr-set-B-num! aux-instr (addr+ 1 (instr-B-num aux-instr))) + (let* ((aux-instr (core-get (addr+ ptr num))) + (old-B-num (aux-instr 'B-num))) + (instr-set-B-num! aux-instr (addr+ 1 (aux-instr 'B-num))) (addr+ num old-B-num))) (else (error "Unrecognized mode" mode)))))