2 ;;; An implementation of the Memory Array Redcode Simulator (MARS)
12 (define core-size 8000)
13 (define max-steps 10000)
19 (define core (make-vector core-size '()))
25 (define (make-instr opcode modifier addrA modeA addrB modeB))
31 (define (make-player name . ptrs)
34 (define (player-ptrs player)
37 (define (player-name player)
40 (define (player-set-ptrs! player ptrs)
41 (set-cdr! player ptrs))
47 (define (run players step)
49 ((> step max-steps) players) ;Tie between remaining players
50 ((null? players) '()) ;Somehow we have no players remaining
52 (let ((player (car players))
53 (other-players (cdr players)))
55 (run other-players (+ step 1)) ;Player is out
56 (let* ((ptrs (player-ptrs player))
57 (new-ptrs (execute-instr (car ptrs))))
58 (player-set-ptrs! (append (cdr ptrs) new-ptrs))
59 (run (append other-players (list player)) (+ step 1))))))))
61 (define (execute-instr ptr)
62 (let ((instr (vector-ref core ptr)))
63 (case (instr-opcode instr)
80 (error "Unrecognised opcode" (instr-opcode instr))))))