From: plugd Date: Sun, 17 Nov 2019 22:06:27 +0000 (+0100) Subject: Converting to new core implementation. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=5ea3ebb09473ba9941d5a3cafdc4cb17d489c3b0;p=jars.git Converting to new core implementation. --- diff --git a/mars.scm b/mars.scm index f2c78c0..9952774 100644 --- a/mars.scm +++ b/mars.scm @@ -6,8 +6,8 @@ (chicken string) (chicken random) (chicken condition) - matchable - wish) + (chicken process context) + matchable) ;;; Constants ;; @@ -19,16 +19,31 @@ ;;; Instructions ;; -(define (make-instr opcode modifier A-mode A-num B-mode B-num name) - (list opcode modifier A-mod A-num B-mode B-num name)) - -(define (instr-opcode instr) (list-ref instr 0)) -(define (instr-modifier instr) (list-ref instr 1)) -(define (instr-A-mode instr) (list-ref instr 2)) -(define (instr-A-num instr) (list-ref instr 3)) -(define (instr-B-mode instr) (list-ref instr 4)) -(define (instr-B-num instr) (list-ref instr 5)) -(define (instr-name instr) (list-ref instr 6)) +(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 'opcode)) + (set! A-mode (other 'A-mode)) + (set! A-num (other 'A-num)) + (set! B-mode (other 'B-mode)) + (set! B-num (other 'B-num))) + (else + (error "Invalid instr arguments" args))))) (define (mode->string mode) (case mode @@ -43,77 +58,54 @@ (else (error "Unknown mode.")))) -(define (instr->string opcode modifier A-mode A-num B-mode B-num name) +(define (instr->string instr) (conc opcode "." modifier " " (mode->string A-mode) A-num - " " (mode->string B-mode) B-num - " ;" name)) - + " " (mode->string B-mode) B-num)) + (define initial-instruction - (make-instr 'DAT 'F 'immediate 0 'immediate 0 '())) + (make-instr 'DAT 'F 'immediate 0 'immediate 0)) ;;; Memory setup and addressing ;; -(define (make-core core-size opcode modifier A-mode A-num B-mode B-num) - (let ((opcodes (make-vector core-size opcode)) - (modifiers (make-vector core-size modifier)) - (A-modes (make-vector core-size A-mode)) - (A-nums (make-vector core-size A-num)) - (B-modes (make-vector core-size B-mode)) - (B-nums (make-vector core-size B-num)) - (names (make-vector core-size '()))) +(define (make-core core-size initial-instr) + (let ((core-vec (make-vector core-size '())) + (names-vec (make-vector core-size '()))) + (let loop ((i 0)) + (unless (>= i core-size) + (vector-set! core-vec i (initial-instr 'copy)) + (loop (+ i 1)))) (lambda args (match args - ((i 'copy-to j n) - (vector-set! opcodes j (vector-ref opcodes i)) - (vector-set! modifiers j (vector-ref modifiers i)) - (vector-set! A-modes j (vector-ref A-modes i)) - (vector-set! A-nums j (vector-ref A-nums i)) - (vector-set! B-modes j (vector-ref B-modes i)) - (vector-set! B-nums j (vector-ref B-nums i)) - (vector-set! names j n)) - ((i 'copy-from-instr! instr) - (vector-set! opcodes i (instr-opcode instr)) - (vector-set! modifiers i (instr-modifier instr)) - (vector-set! A-mode i (instr-A-mode instr)) - (vector-set! A-num i (instr-A-num instr)) - (vector-set! B-mode i (instr-B-mode instr)) - (vector-set! B-num i (instr-B-num instr)) - (vector-set! names i (instr-name instr))) - ((i 'opcode) (vector-ref opcodes i)) - ((i 'modifier) (vector-ref modifiers i)) - ((i 'A-mode) (vector-ref A-modes i)) - ((i 'A-num) (vector-ref A-nums i)) - ((i 'B-mode) (vector-ref B-modes i)) - ((i 'B-num) (vector-ref B-nums i)) + ((i 'set-from! j n) + ((vector-ref core-vec i) 'set-from! (vector-ref core-vec j)) + (vector-set! names-vec i n)) + ((i 'set-from-instr! instr n) + ((vector-ref core-vec i) 'set-from! instr) + (vector-set! names-vec i n)) + ((i 'set! v x n) + ((vector-ref core-vec i) 'set! v x) + (vector-set! names-vec i n)) ((i 'name) (vector-ref names i)) - ((i 'set-opcode! x n) (vector-set! opcodes i x) (vector-set! names i n)) - ((i 'set-modifier! x n) (vector-set! modifiers i x) (vector-set! names i n)) - ((i 'set-A-mode! x n) (vector-set! A-modes i x) (vector-set! names i n)) - ((i 'set-A-num! x n) (vector-set! A-nums i x) (vector-set! names i n)) - ((i 'set-B-mode! x n) (vector-set! B-modes i x) (vector-set! names i n)) - ((i 'set-B-num! x n) (vector-set! B-nums i x) (vector-set! names i n)) + ((i v) ((vector-ref core-vec i) v)) (('dump) (let loop ((i 0)) - (unless (>= i core-size)) - (print* i ":\t") - (instr->string (vector-ref opcodes i) - (vector-ref modifiers i) - (vector-ref A-modes i) - (vector-ref A-nums i) - (vector-ref B-modes i) - (vector-ref B-nums i) - (vector-ref names i)))))))) + (unless (>= i core-size) + (print* i ":\t") + (let ((n (vector-ref names-vec i))) + (unless (null? n) + (print* "\t;" n))) + (print)))))))) (define (addr+ . args) (foldl (lambda (a b) (modulo (+ a b core-size) core-size)) 0 args)) -;;; Programmes +;;; Programmes and task queues ;; (define (make-prog name instrs offset) @@ -127,7 +119,7 @@ (let loop ((ptr addr) (instrs (prog-instrs prog))) (unless (null? instrs) - (core ptr 'copy-from-instr! (car instrs)) + (core ptr 'set-from-instr! (car instrs) (prog-name prog)) (loop (addr+ ptr 1) (cdr instrs)))) (make-player (prog-name prog) (addr+ addr (prog-offset prog)))) @@ -154,56 +146,55 @@ (cdr progs-left)) (loop players progs-left)))))) -(define (make-player name ptr) +(define (make-queue name ptr) (list name ptr)) -(define (player-name player) (car player)) -(define (player-ptrs player) (cdr player)) +(define (queue-owner queue) (car player)) +(define (queue-ptrs queue) (cdr queue)) -(define (player-set-ptrs! player ptrs) - (set-cdr! player ptrs)) +(define (queue-set-ptrs! queue ptrs) + (set-cdr! queue ptrs)) ;;; Executive function ;; -(define (run players steps-left) +(define (run core queues steps-left) (cond - ((<= steps-left 0) players) ;Tie between remaining players - ;; ((<= (length players) 1) players) ;There's only one player left who thus wins + ((<= steps-left 0) queues) ;Tie between remaining players + ;; ((<= (length queues) 1) queues) ;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)))) + (let* ((queue (car queues)) + (remaining-queues (cdr queues)) + (ptrs (queue-ptrs queues)) + (new-ptrs (execute-instr core (car ptrs) (queue-owner queue)))) (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) +(define (execute-instr core ptr 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-get B-ptr) 'copy-from! (core-get A-ptr) name) - (combine A-ptr B-ptr modifier name (lambda (x y) y))) + (core B-ptr 'set-from! A-ptr name) + (combine-and-store core 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+) + (combine-and-store core A-ptr B-ptr modifier name addr+) (list (addr+ ptr 1))) ((SUB) - (combine-and-store A-ptr B-ptr modifier name + (combine-and-store core 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 + (combine-and-store core A-ptr B-ptr modifier name (lambda (x y) (modulo (* (addr+ x core-size) (addr+ y core-size)) core-size))) @@ -211,7 +202,7 @@ ((DIV) (condition-case (begin - (combine-and-store A-ptr B-ptr modifier name + (combine-and-store core A-ptr B-ptr modifier name (lambda (x y) (quotient (addr x core-size) (addr y core-size)))) (list (addr+ ptr 1))) @@ -219,13 +210,13 @@ ((MOD) (condition-case (begin - (combine-and-store A-ptr B-ptr modifier name + (combine-and-store core 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)))) + (list (addr+ ptr (core A-ptr 'A-num)))) ((JMZ) (list (addr+ ptr (if (instr-zero? B-ptr modifier #f) ((core-get A-ptr) 'A-num) @@ -293,53 +284,56 @@ (and (= 0 (instr 'A-num)) (= 0 (instr 'B-num))))))) -(define (combine-and-store A-ptr B-ptr modifier name f) +(define (combine-and-store core 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) + ((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) (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))) + ((indirect-A) (addr+ num (core (addr+ ptr num) 'A-num))) + ((indirect-B) (addr+ num (core (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)))) + (let ((aux-ptr (addr+ ptr num))) + (core aux-ptr 'set! 'A-num (addr+ -1 (core aux-ptr 'A-num)) name) + (addr+ num (core aux-ptr '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)))) + (let ((aux-ptr (addr+ ptr num))) + (core aux-ptr 'set! 'B-num (addr+ -1 (core aux-ptr 'B-num)) name) + (addr+ num (core aux-ptr '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))) + (let* ((aux-ptr (addr+ ptr num)) + (old-A-num (core aux-ptr 'A-num))) + (core aux-ptr 'set! 'A-num (addr+ 1 (core aux-ptr 'A-num)) name) (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))) + (let* ((aux-ptr (addr+ ptr num)) + (old-B-num (core aux-ptr 'B-num))) + (core aux-ptr 'set! 'B-num (addr+ 1 (core aux-ptr 'B-num)) name) (addr+ num old-B-num))) (else (error "Unrecognized mode" mode))))) +;;; Main procedure +;; + ;;; TEST CODE (define addressing-test