(('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))))
(define (mode-string mode)
(case mode
((immediate) "#")
- ((direct) "")
+ ((direct) "$")
((indirect-A "*"))
((indirect-B "@"))
((pre-indirect-A "{"))
((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
(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)
(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))))
(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))))
(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))))))
(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)))))