The Lambda Lab
/
projects
/
jars.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
8c9a5e7
)
Thinking about operand evaluation.
author
plugd
<plugd@thelambdalab.xyz>
Thu, 14 Nov 2019 12:44:38 +0000
(13:44 +0100)
committer
plugd
<plugd@thelambdalab.xyz>
Thu, 14 Nov 2019 13:11:59 +0000
(14:11 +0100)
mars.scm
patch
|
blob
|
history
diff --git
a/mars.scm
b/mars.scm
index
c77e2d3
..
2f4db9d
100644
(file)
--- a/
mars.scm
+++ b/
mars.scm
@@
-22,7
+22,13
@@
;;; Instructions
;;
;;; Instructions
;;
-(define (make-instr opcode modifier addrA modeA addrB modeB))
+(define (make-instr opcode modifier A-mode A-num B-mode B-num))
+(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))
;;; Players
;;; Players
@@
-46,20
+52,23
@@
(define (run players step)
(cond
(define (run players step)
(cond
- ((> step max-steps) players) ;Tie between remaining players
- ((
null? players) '()) ;Somehow we have no players remaining
+ ((> step max-steps) players)
;Tie between remaining players
+ ((
<= (length players) 1) players) ;There's only one player left who thus wins
(else
(let ((player (car players))
(else
(let ((player (car players))
- (other-players (cdr players)))
- (if (null? ptrs)
- (run other-players (+ step 1)) ;Player is out
- (let* ((ptrs (player-ptrs player))
- (new-ptrs (execute-instr (car ptrs))))
- (player-set-ptrs! (append (cdr ptrs) new-ptrs))
- (run (append other-players (list player)) (+ step 1))))))))
+ (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)
(define (execute-instr ptr)
- (let ((instr (vector-ref core ptr)))
+ (let* ((instr (vector-ref core ptr))
+ (A-operand (eval-operand (instr-A-mode instr) (instr-A-num) ptr))
+ (B-operand (eval-operand (instr-B-mode instr) (instr-B-num) ptr)))
(case (instr-opcode instr)
((DAT))
((MOV))
(case (instr-opcode instr)
((DAT))
((MOV))
@@
-79,3
+88,13
@@
(else
(error "Unrecognised opcode" (instr-opcode instr))))))
(else
(error "Unrecognised opcode" (instr-opcode instr))))))
+(define (eval-operand mode num ptr)
+ (case mode
+ ((immediate))
+ ((direct))
+ ((indirect-A))
+ ((indirect-B))
+ ((pre-indirect-A))
+ ((pre-indirect-B))
+ ((post-indirect-A))
+ ((post-indirect-B))))