X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=jars.git;a=blobdiff_plain;f=mars.scm;h=d2d73e32d29f7d08fe279f4a74034b40ebb9b96c;hp=66ec04689749457b5c9a9185cb9b6aeea0c0d1fc;hb=4ccb7fe4e20053cd189864142aeb3d1d6c59c118;hpb=bc3e11db08cc62b131771395a1815a1b91c7ac88 diff --git a/mars.scm b/mars.scm index 66ec046..d2d73e3 100644 --- a/mars.scm +++ b/mars.scm @@ -2,346 +2,381 @@ ;;; An implementation of the Memory Array Redcode Simulator (MARS) ;;; -(import (chicken io) - (chicken string) - (chicken random) - (chicken condition) - matchable) - -;;; Constants -;; - -(define core-size 20) -(define max-steps 10000) - - -;;; Instructions -;; - -(define ((make-instr opcode modifier A-mode A-num B-mode B-num name) . args) - (match args - (('copy n) (make-instr opcode modifier A-mode A-num B-mode B-num n)) - (('copy-from! other n) - (set! opcode (other 'opcode)) - (set! modifier (other 'modifier)) - (set! A-mode (other 'A-mode)) - (set! A-num (other 'A-num)) - (set! B-mode (other 'B-mode)) - (set! B-num (other 'B-num)) - (set! name n)) - (('opcode) opcode) - (('modifier) modifier) - (('A-mode) A-mode) - (('A-num) A-num) - (('B-mode) B-mode) - (('B-num) B-num) - (('name) name) - (('print) (print opcode - "." modifier - " " (mode-string A-mode) A-num - ", " (mode-string B-mode) B-num - (if (null? name) "" (conc " ; " name)))) - (('set-opcode! x n) (set! opcode x) (set! name n)) - (('set-modifier! x n) (set! modifier x) (set! name n)) - (('set-A-mode! x n) (set! A-mode x) (set! name n)) - (('set-A-num! x n) (set! A-num x) (set! name n)) - (('set-B-mode! x n) (set! B-mode x) (set! name n)) - (('set-B-num! x n) (set! B-num x) (set! name n)))) - -(define (mode-string mode) - (case mode - ((immediate) "#") - ((direct) "$") - ((indirect-A) "*") - ((indirect-B) "@") - ((pre-indirect-A) "{") - ((pre-indirect-B) "<") - ((post-indirect-A) "}") - ((post-indirect-B) ">"))) - -(define initial-instruction - (make-instr 'DAT 'F 'immediate 0 'immediate 0 '())) - - -;;; Memory setup and addressing -;; - -(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) - (core-set! i (initial-instruction 'copy '())) - (loop (+ i 1))))) - -(define (dump-core) - (let loop ((i 0)) - (unless (>= i core-size) - (print* i ":\t") - ((core-get i) 'print) - (loop (+ i 1))))) - -(define (addr+ . args) - (foldl (lambda (a b) - (modulo (+ a b core-size) core-size)) - 0 args)) - -;;; Programmes -;; - -(define (make-prog name instrs offset) - (list name instrs offset)) - -(define (prog-name prog) (list-ref prog 0)) -(define (prog-instrs prog) (list-ref prog 1)) -(define (prog-offset prog) (list-ref prog 2)) - -(define (install-prog prog addr) - (let loop ((ptr addr) - (instrs (prog-instrs prog))) - (unless (null? instrs) - (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)))) - -(define (can-install-prog? prog-len addr) - (let loop ((ptr addr) - (remaining prog-len)) - (if (= remaining 0) - #t - (if (null? ((core-get ptr) 'name)) - (loop (addr+ ptr 1) - (- remaining 1)) - #f)))) - -(define (install-progs progs) - (let loop ((players '()) - (progs-left progs)) - (if (null? progs-left) - players - (let ((addr (pseudo-random-integer core-size)) - (prog (car progs-left))) - (if (can-install-prog? (length (prog-instrs prog)) addr) - (loop (cons (install-prog prog addr) players) - (cdr progs-left)) - (loop players progs-left)))))) - -(define (make-player name ptr) - (list name ptr)) - -(define (player-name player) (car player)) -(define (player-ptrs player) (cdr player)) - -(define (player-set-ptrs! player ptrs) - (set-cdr! player ptrs)) - - -;;; Executive function -;; - -(define (run players steps-left) - (cond - ((<= steps-left 0) players) ;Tie between remaining players - ;; ((<= (length players) 1) players) ;There's only one player left who thus wins - (else - (let* ((player (car players)) - (other-players (cdr players)) - (ptrs (player-ptrs player)) - (new-ptrs (execute-instr (car ptrs) (player-name player)))) - (if (null? new-ptrs) - (run other-players (- steps-left 1)) - (begin - (player-set-ptrs! player (append (cdr ptrs) new-ptrs)) - (run (append other-players (list player)) (- steps-left 1)))))))) - -(define (execute-instr ptr name) - (let* ((instr (core-get ptr)) - (A-ptr (eval-operand (instr 'A-mode) (instr 'A-num) ptr)) - (B-ptr (eval-operand (instr 'B-mode) (instr 'B-num) ptr)) - (modifier (instr 'modifier))) - (case (instr 'opcode) - ((DAT) - '()) ;Game over, man, game over! - ((MOV) - (if (eq? modifier 'I) - ((core-get B-ptr) 'copy-from! (core-get A-ptr) name) - (combine A-ptr B-ptr modifier name (lambda (x y) y))) - (list (addr+ ptr 1))) - ((ADD) - (combine-and-store A-ptr B-ptr modifier name addr+) - (list (addr+ ptr 1))) - ((SUB) - (combine-and-store A-ptr B-ptr modifier name - (lambda (x y) (addr+ x (- y)))) - (list (addr+ ptr 1))) - ((MUL) - (combine-and-store A-ptr B-ptr modifier name - (lambda (x y) (modulo (* (addr+ x core-size) - (addr+ y core-size)) - core-size))) - (list (addr+ ptr 1))) - ((DIV) - (condition-case - (begin - (combine-and-store A-ptr B-ptr modifier name - (lambda (x y) (quotient (addr x core-size) - (addr y core-size)))) - (list (addr+ ptr 1))) - ((exn arithmetic) '()))) - ((MOD) - (condition-case - (begin - (combine-and-store A-ptr B-ptr modifier name - (lambda (x y) (remainder (addr x core-size) - (addr y core-size)))) - (list (addr+ ptr 1))) - ((exn arithmetic) '()))) - ((JMP) - (list (addr+ ptr ((core-get A-ptr) 'A-num)))) - ((JMZ) - (list (addr+ ptr (if (instr-zero? B-ptr modifier #f) - ((core-get A-ptr) 'A-num) - 1)))) - ((JMN) - (list (addr+ ptr (if (not (instr-zero? B-ptr modifier #f) - ((core-get A-ptr) 'A-num) - 1))))) - ((DJN) - (list (addr+ ptr (if (not (instr-zero? B-ptr modifier #t) - ((core-get A-ptr) 'A-num) - 1))))) - ((SEQ CMP) - (list (addr+ ptr (if (compare-instrs A-ptr B-ptr modifier =) 2 1)))) - ((SNE) - (list (addr+ ptr (if (compare-instrs A-ptr B-ptr modifier =) 1 2)))) - ((SLT) - (list (addr+ ptr (if (compare-instrs A-ptr B-ptr modifier <) 2 1)))) - ((SPL) - (list (addr+ ptr 1) (addr+ ptr ((core-get A-ptr) 'A-num)))) - ((NOP) - (list (addr+ ptr 1))) +(module mars + (make-instr + make-prog + prog-name + prog-author + prog-instrs + prog-offset + prog->string + dump-prog + install-progs + make-queue + queue-owner + queue-ptrs + dump-queues + make-core + run-mars) + + (import scheme + (chicken base) + (chicken io) + (chicken string) + (chicken random) + (chicken condition) + (chicken process-context) + matchable) + + + ;;; Instructions + ;; + + (define (make-instr opcode modifier A-mode A-num B-mode B-num) + (lambda args + (match args + (('opcode) opcode) + (('modifier) modifier) + (('A-mode) A-mode) + (('A-num) A-num) + (('B-mode) B-mode) + (('B-num) B-num) + (('make-copy) (make-instr opcode modifier A-mode A-num B-mode B-num)) + (('set! 'opcode x) (set! opcode x)) + (('set! 'modifier x) (set! modifier x)) + (('set! 'A-mode x) (set! A-mode x)) + (('set! 'A-num x) (set! A-num x)) + (('set! 'B-mode x) (set! B-mode x)) + (('set! 'B-num x) (set! B-num x)) + (('set-from! other) + (set! opcode (other 'opcode)) + (set! modifier (other 'modifier)) + (set! A-mode (other 'A-mode)) + (set! A-num (other 'A-num)) + (set! B-mode (other 'B-mode)) + (set! B-num (other 'B-num))) + (('->string) + (conc opcode + "." modifier + "\t" (mode->string A-mode) A-num + ", " (mode->string B-mode) B-num)) + (else + (error "Invalid instr arguments" args))))) + + (define (mode->string mode) + (case mode + ((immediate) "#") + ((direct) "$") + ((indirect-A) "*") + ((indirect-B) "@") + ((pre-indirect-A) "{") + ((pre-indirect-B) "<") + ((post-indirect-A) "}") + ((post-indirect-B) ">") (else - (error "Unrecognised opcode" (instr 'opcode)))))) - -(define (compare-instrs A-ptr B-ptr modifier test) - (let ((A-instr (core-get A-ptr)) - (B-instr (core-get B-ptr))) + (error "Unknown mode.")))) + + + ;;; Memory setup and addressing + ;; + + (define INITIAL-INSTR (make-instr 'DAT 'F 'immediate 0 'immediate 0)) + + (define (make-core core-size . set-functions) + (let ((core-vec (make-vector core-size '())) + (names-vec (make-vector core-size '()))) + (define (norm-addr i) + (if (< i 0) + (norm-addr (+ i core-size)) + (modulo i core-size))) + (define (norm-ref v i) + (vector-ref v (norm-addr i))) + (define (norm-set! v i x) + (vector-set! v (norm-addr i) + (if (integer? x) + (norm-addr x) + x))) + (define (run-set-functions i n) + (let loop ((remaining-fns set-functions)) + (unless (null? remaining-fns) + ((car remaining-fns) i n)))) + (define (dump i) + (print* i ":\t" ((norm-ref core-vec i) '->string)) + (let ((n (norm-ref names-vec i))) + (unless (null? n) + (print* "\t;" n))) + (print)) + (let loop ((i 0)) + (unless (>= i core-size) + (vector-set! core-vec i (INITIAL-INSTR 'make-copy)) + (loop (+ i 1)))) + (lambda args + (match args + ((i 'set-from! j n) + ((norm-ref core-vec i) 'set-from! (norm-ref core-vec j)) + (norm-set! names-vec i n) + (run-set-functions i n)) + ((i 'set-from-instr! instr n) + ((norm-ref core-vec i) 'set-from! instr) + (norm-set! names-vec i n) + (run-set-functions i n)) + ((i 'set! v x n) + ((norm-ref core-vec i) 'set! v x) + (norm-set! names-vec i n) + (run-set-functions i n)) + ((i 'name) (norm-ref names-vec i)) + ((i 'dump) + (let ((i1 (- i 4)) + (i2 (+ i 4))) + (let loop ((idx i1)) + (unless (> idx i2) + (if (= idx i) + (print* "*")) + (dump idx) + (loop (+ idx 1)))))) + (('size) core-size) + (((? integer? i) v) ((norm-ref core-vec i) v)) + (('->addr (? integer? i)) (norm-addr i)))))) + + + + ;;; Programmes and task queues + ;; + + (define (make-prog name author instrs offset) + (list name author instrs offset)) + + (define (prog-name prog) (list-ref prog 0)) + (define (prog-author prog) (list-ref prog 1)) + (define (prog-instrs prog) (list-ref prog 2)) + (define (prog-offset prog) (list-ref prog 3)) + + (define (install-prog core prog addr) + (let loop ((ptr addr) + (instrs (prog-instrs prog))) + (unless (null? instrs) + (core ptr 'set-from-instr! (car instrs) (prog-name prog)) + (loop (core '->addr (+ ptr 1)) (cdr instrs)))) + (make-queue (prog-name prog) + (core '->addr (+ addr (prog-offset prog))))) + + (define (can-install-prog? core prog-len addr) + (let loop ((ptr addr) + (remaining prog-len)) + (if (= remaining 0) + #t + (if (null? (core ptr 'name)) + (loop (core '->addr (+ ptr 1)) + (- remaining 1)) + #f)))) + + (define (install-progs core progs) + (let loop ((queues '()) + (progs-left progs)) + (if (null? progs-left) + queues + (let ((addr (pseudo-random-integer (core 'size))) + (prog (car progs-left))) + (if (can-install-prog? core (length (prog-instrs prog)) addr) + (loop (cons (install-prog core prog addr) queues) + (cdr progs-left)) + (loop queues progs-left)))))) + + (define (make-queue name ptr) + (list name ptr)) + + (define (queue-owner queue) (car queue)) + (define (queue-ptrs queue) (cdr queue)) + + (define (queue-set-ptrs! queue ptrs) + (set-cdr! queue ptrs)) + + (define (dump-queues queues core) + (for-each (lambda (queue) + (print ";" (queue-owner queue)) + (for-each (lambda (ptr) + (core ptr 'dump) + (print)) + (cdr queue)) + (print)) + queues)) + + (define (prog->string prog) + (conc ";redcode\n\n" + ";name\t" (prog-name prog) "\n" + (if (not (null? (prog-author prog))) + (conc ";author\t" (prog-author prog) "\n\n") + "\n") + "ORG\t" (prog-offset prog) "\t; Execution offset\n\n" + (apply conc (map (lambda (instr) (conc (instr '->string) "\n")) (prog-instrs prog))))) + + (define (dump-prog prog) + (print (prog->string prog))) + + + ;;; Executive function + ;; + + (define (run-mars core queues steps-left min-queue-count) + (if (or (<= steps-left 0) + (< (length queues) min-queue-count)) + queues + (let* ((queue (car queues)) + (remaining-queues (cdr queues)) + (ptrs (queue-ptrs queue)) + (new-ptrs (execute-instr core (car ptrs) (queue-owner queue)))) + (if (null? new-ptrs) + (run-mars core remaining-queues (- steps-left 1) min-queue-count) + (begin + (queue-set-ptrs! queue (append (cdr ptrs) new-ptrs)) + (run-mars core (append remaining-queues (list queue)) + (- steps-left 1) min-queue-count)))))) + + (define (execute-instr core ptr name) + ;; (print ptr "\t" (core ptr '->string) "\t(" name ")") + (let* ((A-ptr (eval-operand core (core ptr 'A-mode) (core ptr 'A-num) ptr name)) + (B-ptr (eval-operand core (core ptr 'B-mode) (core ptr 'B-num) ptr name)) + (modifier (core ptr 'modifier))) + (case (core ptr 'opcode) + ((DAT) + '()) ;Game over, man, game over! + ((MOV) + (if (eq? modifier 'I) + (core B-ptr 'set-from! A-ptr name) + (combine-and-store core A-ptr B-ptr modifier name (lambda (x y) y))) + (list (core '->addr (+ ptr 1)))) + ((ADD) + (combine-and-store core A-ptr B-ptr modifier name +) + (list (core '->addr (+ ptr 1)))) + ((SUB) + (combine-and-store core A-ptr B-ptr modifier name -) + (list (core '->addr (+ ptr 1)))) + ((MUL) + (combine-and-store core A-ptr B-ptr modifier name *) + (list (core '->addr (+ ptr 1)))) + ((DIV) + (condition-case + (begin + (combine-and-store core A-ptr B-ptr modifier name quotient) + + (list (core '->addr (+ ptr 1)))) + ((exn arithmetic) '()))) + ((MOD) + (condition-case + (begin + (combine-and-store core A-ptr B-ptr modifier name modulo) + (list (core '->addr (+ ptr 1)))) + ((exn arithmetic) '()))) + ((JMP) + (list (core '->addr A-ptr))) + ((JMZ) + (list (core '->addr (if (instr-zero? core B-ptr modifier #f name) + A-ptr + (+ ptr 1))))) + ((JMN) + (list (core '->addr (if (not (instr-zero? core B-ptr modifier #f name)) + A-ptr + (+ ptr 1))))) + ((DJN) + (list (core '->addr (if (not (instr-zero? core B-ptr modifier #t name)) + A-ptr + (+ ptr 1))))) + ((SEQ CMP) + (list (core '->addr (+ ptr (if (compare-instrs core A-ptr B-ptr modifier =) 2 1))))) + ((SNE) + (list (core '->addr (+ ptr (if (compare-instrs core A-ptr B-ptr modifier =) 1 2))))) + ((SLT) + (list (core '->addr (+ ptr (if (compare-instrs core A-ptr B-ptr modifier <) 2 1))))) + ((SPL) + (list (core '->addr (+ ptr 1)) (core '->addr A-ptr))) + ((NOP) + (list (core '->addr (+ ptr 1)))) + (else + (error "Unrecognised opcode" (core ptr 'opcode)))))) + + (define (compare-instrs core A-ptr B-ptr modifier test) (case modifier - ((A) (test (A-instr 'A-num) (B-instr 'A-num))) - ((B) (test (A-instr 'B-num) (B-instr 'B-num))) - ((AB) (test (A-instr 'A-num) (B-instr 'B-num))) - ((BA) (test (A-instr 'B-num) (B-instr 'A-num))) + ((A) (test (core A-ptr 'A-num) (core B-ptr 'A-num))) + ((B) (test (core A-ptr 'B-num) (core B-ptr 'B-num))) + ((AB) (test (core A-ptr 'A-num) (core B-ptr 'B-num))) + ((BA) (test (core A-ptr 'B-num) (core B-ptr 'A-num))) ((F) (and - (test (A-instr 'A-num) (B-instr 'A-num)) - (test (A-instr 'B-num) (B-instr 'B-num)))) + (test (core A-ptr 'A-num) (core B-ptr 'A-num)) + (test (core A-ptr 'B-num) (core B-ptr 'B-num)))) ((X) (and - (test (A-instr 'A-num) (B-instr 'B-num)) - (test (A-instr 'B-num) (B-instr 'A-num)))) + (test (core A-ptr 'A-num) (core B-ptr 'B-num)) + (test (core A-ptr 'B-num) (core B-ptr 'A-num)))) ((I) (and (if (eq? test =) (and - (eq? (A-instr 'opcode) (B-instr 'opcode)) - (eq? (A-instr 'modifier) (B-instr 'modifier)) - (eq? (A-instr 'A-mode) (B-instr 'B-mode)) - (eq? (A-instr 'B-mode) (B-instr 'A-mode))) + (eq? (core A-ptr 'opcode) (core B-ptr 'opcode)) + (eq? (core A-ptr 'modifier) (core B-ptr 'modifier)) + (eq? (core A-ptr 'A-mode) (core B-ptr 'B-mode)) + (eq? (core A-ptr 'B-mode) (core B-ptr 'A-mode))) #t) - (test (A-instr 'A-num) (B-instr 'B-num)) - (test (A-instr 'B-num) (B-instr 'A-num))))))) + (test (core A-ptr 'A-num) (core B-ptr 'B-num)) + (test (core A-ptr 'B-num) (core B-ptr 'A-num)))))) -(define (instr-zero? ptr modifier decrement) - (let ((instr (core-get ptr))) + (define (instr-zero? core ptr modifier decrement name) (case modifier ((A AB) - (if decrement (instr 'set-A-num! (addr+ (instr 'A-num) -1))) - (= 0 (instr 'A-num))) + (if decrement (core ptr 'set! 'A-num (- (core ptr 'A-num) 1) name)) + (= 0 (core ptr 'A-num))) ((A AB) - (if decrement (instr 'set-B-num! (addr+ (instr 'B-num) -1))) - (= 0 (instr 'B-num))) + (if decrement (core ptr 'set! 'B-num (- (core ptr 'B-num) 1) name)) + (= 0 (core ptr 'B-num))) ((X I F) (if decrement (begin - (instr 'set-A-num! (addr+ (instr 'A-num) -1)) - (instr 'set-B-num! (addr+ (instr 'B-num) -1)))) - (and (= 0 (instr 'A-num)) - (= 0 (instr 'B-num))))))) - -(define (combine-and-store A-ptr B-ptr modifier name f) - (case modifier - ((A) ((core-get B-ptr) 'set-A-num! - (f ((core-get B-ptr) 'A-num) ((core-get A-ptr) 'A-num)) name)) - ((B) ((core-get B-ptr) 'set-B-num! - (f ((core-get B-ptr) 'B-num) ((core-get A-ptr) 'B-num)) name)) - ((AB) ((core-get B-ptr) 'set-B-num! - (f ((core-get B-ptr) 'B-num) ((core-get A-ptr) 'A-num)) name)) - ((BA) ((core-get B-ptr) 'set-A-num! - (f ((core-get B-ptr) 'A-num) ((core-get A-ptr) 'B-num)) name)) - ((F I) ((core-get B-ptr) 'set-A-num! - (f ((core-get B-ptr) 'A-num) ((core-get A-ptr) 'A-num))) name - ((core-get B-ptr) 'set-B-num! - (f ((core-get B-ptr) 'B-num) ((core-get A-ptr) 'B-num)) name)) - ((X) ((core-get B-ptr) 'set-A-num! - (f ((core-get B-ptr) 'A-num) ((core-get A-ptr) 'B-num)) name) - ((core-get B-ptr) 'set-B-num! - (f ((core-get B-ptr) 'B-num) ((core-get A-ptr) 'A-num)) name)))) - -(define (eval-operand mode num ptr) - (addr+ ptr - (case mode - ((immediate) 0) - ((direct) 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 (core-get (addr+ ptr num)))) - ((aux-instr set-A-num! (addr+ -1 (aux-instr 'A-num)))) - (addr+ num (aux-instr 'A-num)))) - ((pre-indirect-B) - (let ((aux-instr (core-get (addr+ ptr num)))) - (aux-instr set-B-num!(addr+ -1 (aux-instr 'B-num))) - (addr+ num (aux-instr 'B-num)))) - ((post-indirect-A) - (let* ((aux-instr (core-get (addr+ ptr num))) - (old-A-num (aux-instr 'A-num))) - (aux-instr set-A-num! (addr+ 1 (aux-instr 'A-num))) - (addr+ num old-A-num))) - ((post-indirect-B) - (let* ((aux-instr (core-get (addr+ ptr num))) - (old-B-num (aux-instr 'B-num))) - (aux-instr set-B-num! (addr+ 1 (aux-instr 'B-num))) - (addr+ num old-B-num))) - (else - (error "Unrecognized mode" mode))))) - -;;; TEST CODE - -(define addressing-test - (make-prog 'at (list - (make-instr 'DAT 'F 'immediate 42 'immediate 53 'at) - (make-instr 'DAT 'F 'immediate 123 'immediate 256 'at) - (make-instr 'MOV 'A 'indirect-B 4 'direct 7 'at) - (make-instr 'NOP 'I 'immediate 0 'immediate 0 'at) - (make-instr 'NOP 'I 'immediate 0 'immediate 0 'at) - (make-instr 'NOP 'I 'immediate 0 'immediate 0 'at) - (make-instr 'DAT 'F 'immediate -5 'immediate -6 'at)) 2)) - -(define imp - (make-prog 'imp (list (make-instr 'MOV 'I 'direct 0 'direct 1 'imp)) 0)) - -(define dwarf - (make-prog 'dwarf (list - (make-instr 'DAT 'F 'immediate 0 'immediate -1 'dwarf) - (make-instr 'ADD 'AB 'immediate 5 'direct -1 'dwarf) - (make-instr 'MOV 'I 'direct -2 'indirect-B -2 'dwarf) - (make-instr 'JMP 'I 'immediate -2 'immediate 0 'dwarf)) 1)) - -(initialize-core) -(define players (install-progs (list dwarf imp))) - -(dump-core) + (core ptr 'set! 'A-num (- (core ptr 'A-num) 1) name) + (core ptr 'set! 'B-num (- (core ptr 'B-num) 1) name))) + (and (= 0 (core ptr 'A-num)) + (= 0 (core ptr 'B-num)))))) + + (define (combine-and-store core A-ptr B-ptr modifier name f) + (case modifier + ((A) (core B-ptr 'set! 'A-num + (f (core B-ptr 'A-num) (core A-ptr 'A-num)) name)) + ((B) (core B-ptr 'set! 'B-num + (f (core B-ptr 'B-num) (core A-ptr 'B-num)) name)) + ((AB) (core B-ptr 'set! 'B-num + (f (core B-ptr 'B-num) (core A-ptr 'A-num)) name)) + ((BA) (core B-ptr 'set! 'A-num + (f (core B-ptr 'A-num) (core A-ptr 'B-num)) name)) + ((F I) (core B-ptr 'set! 'A-num + (f (core B-ptr 'A-num) (core A-ptr 'A-num)) name) + (core B-ptr 'set! 'B-num + (f (core B-ptr 'B-num) (core A-ptr 'B-num)) name)) + ((X) (core B-ptr 'set! 'A-num + (f (core B-ptr 'A-num) (core A-ptr 'B-num)) name) + (core B-ptr 'set! 'B-num + (f (core B-ptr 'B-num) (core A-ptr 'A-num)) name)))) + + (define (eval-operand core mode num ptr name) + (core '->addr (+ ptr + (case mode + ((immediate) 0) + ((direct) num) + ((indirect-A) (+ num (core (+ ptr num) 'A-num))) + ((indirect-B) (+ num (core (+ ptr num) 'B-num))) + ((pre-indirect-A) + (let ((aux-ptr (+ ptr num))) + (core aux-ptr 'set! 'A-num (- (core aux-ptr 'A-num) 1) name) + (+ num (core aux-ptr 'A-num)))) + ((pre-indirect-B) + (let ((aux-ptr (+ ptr num))) + (core aux-ptr 'set! 'B-num (- (core aux-ptr 'B-num) 1) name) + (+ num (core aux-ptr 'B-num)))) + ((post-indirect-A) + (let* ((aux-ptr (+ ptr num)) + (old-A-num (core aux-ptr 'A-num))) + (core aux-ptr 'set! 'A-num (+ (core aux-ptr 'A-num) 1) name) + (+ num old-A-num))) + ((post-indirect-B) + (let* ((aux-ptr (+ ptr num)) + (old-B-num (core aux-ptr 'B-num))) + (core aux-ptr 'set! 'B-num (+ (core aux-ptr 'B-num) 1) name) + (+ num old-B-num))) + (else + (error "Unrecognized mode" mode))))))) +