- (let ((player (car players))
- (other-players (cdr players))
- (ptrs (player-ptrs player)))
- (let ((new-ptrs (execute-instr (car ptrs))))
- (if (null? new-ptrs)
- (run other-players (+ step 1))
- (begin
- (player-set-ptrs! (append (cdr ptrs) new-ptrs))
- (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))
- (modifier (instr-modifier instr)))
- (case (instr-opcode instr)
- ((DAT) '()) ;Game over, man, game over!
- ((MOV))
- ((ADD))
- ((SUB))
- ((MUL))
- ((DIV))
- ((MOD))
- ((JMP))
+ (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))