(chicken string)
(chicken random)
(chicken condition)
- matchable)
+ matchable
+ wish)
;;; Constants
;;
;;; 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 (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 (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 "Unknown mode."))))
+
+(define (instr->string opcode modifier A-mode A-num B-mode B-num name)
+ (conc opcode
+ "." modifier
+ " " (mode->string A-mode) A-num
+ " " (mode->string B-mode) B-num
+ " ;" name))
+
(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 (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 '())))
+ (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 '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))
+ (('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))))))))
(define (addr+ . args)
(foldl (lambda (a b)
(define (prog-instrs prog) (list-ref prog 1))
(define (prog-offset prog) (list-ref prog 2))
-(define (install-prog prog addr)
+(define (install-prog core prog addr)
(let loop ((ptr addr)
(instrs (prog-instrs prog)))
(unless (null? instrs)
- (core-set! ptr ((car instrs) 'copy (prog-name prog)))
+ (core ptr 'copy-from-instr! (car instrs))
(loop (addr+ ptr 1) (cdr instrs))))
(make-player (prog-name prog)
(addr+ addr (prog-offset prog))))
-(define (can-install-prog? prog-len addr)
+(define (can-install-prog? core prog-len addr)
(let loop ((ptr addr)
(remaining prog-len))
(if (= remaining 0)
#t
- (if (null? ((core-get ptr) 'name))
+ (if (null? (core ptr 'name))
(loop (addr+ ptr 1)
(- remaining 1))
#f))))
-(define (install-progs progs)
+(define (install-progs core 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)
+ (if (can-install-prog? core (length (prog-instrs prog)) addr)
+ (loop (cons (install-prog core prog addr) players)
(cdr progs-left))
(loop players progs-left))))))
((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)))))
+ (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)))))
+ (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)