view clojure/com/aurellem/gb/rlm_assembly.clj @ 379:f86bd04bd9fc

simplifying frame-metronome
author Robert McIntyre <rlm@mit.edu>
date Wed, 11 Apr 2012 12:35:31 -0500
parents 5c4a30521d09
children 4d2767423266
line wrap: on
line source
1 (ns com.aurellem.gb.rlm-assembly
2 "Version of main bootstrap program that is valid output for the
3 item-writer program."
4 (:use (com.aurellem.gb gb-driver assembly util vbm constants))
5 (:use (com.aurellem.run bootstrap-1))
6 (:import [com.aurellem.gb.gb_driver SaveState]))
8 ;; Specs for Main Bootstrap Program
10 ;; Number-Input
11 ;; Number input works using all eight buttons to
12 ;; spell out an 8 bit number. The order of buttons is
13 ;; [:d :u :l :r :start :select :b :a] --> 11111111
14 ;; [ :l :start :a] --> 00101001
16 ;;; MODE-SELECT
17 ;; The bootstrap program starts in MODE-SELECT mode.
18 ;; MODE-SELECT transitions to one of three modes depending
19 ;; on which buttons are pressed:
20 ;; 0 (no-buttons) : MODE-SELECT
21 ;; 8 [:start] : WRITE-BYTES
22 ;; 0xFF (all-buttons) : JUMP
24 ;;; WRITE-BYTES
26 ;; WRITE-BYTES mode writes sequences of arbitray values to
27 ;; arbitray memory locations. It expects you to enter a
28 ;; header of three bytes describing what to write:
30 ;; Byte 0 : Number of Bytes to Write
31 ;; Byte 1 : Start Address High Byte
32 ;; Byte 1 : Start Address Low Byte
34 ;; Then, you enter the number of bytes specified in Byte 0
35 ;; they are written to the start address in
36 ;; sequence. After the last byte is written control
37 ;; returns to MODE-SELECT mode.
39 ;; Example: to write the sequence [1 2 3 4] starting at
40 ;; address 0xC01F enter
41 ;; Byte 0 : 4 (will write four bytes)
42 ;; Byte 1 : 0xC0 (high byte of 0xC01F)
43 ;; Byte 2 : 0x1F (low byte of 0xC01F)
44 ;; Byte 3 : 1 (write 1 to 0xC01F)
45 ;; Byte 4 : 2 (write 2 to 0xC020)
46 ;; Byte 5 : 3 (write 3 to 0xC021)
47 ;; Byte 6 : 4 (write 4 to 0xC022)
49 ;;; JUMP
50 ;; JUMP mode jumps program control to any arbitray
51 ;; location. It expects you to enter two bytes which
52 ;; correspond to the high and low bytes of the memory
53 ;; address to which you want to jump.
54 ;; Byte 0 : Jump Address High Byte
55 ;; Byte 1 : Jump Address Low Byte
57 ;; Example: to jump to address 0x1234 enter
58 ;; Byte 0 : 0x12 (high byte of 0x1234)
59 ;; Byte 1 : 0x34 (low byte of 0x1234)
62 (defn ->signed-8-bit [n]
63 (if (< n 0)
64 (+ 256 n) n))
66 (defn frame-metronome []
67 (let [init [0x06 155] ;; init B to out of LY range
68 timing-loop
69 [0x47 ;; A->B
71 0x26
72 0xFE ;; load FF into H without repeats
73 0x24
75 0x2E
76 0x43 ;; load 44 into L without repeats
77 0x2C
79 0x7E] ;; (HL) -> A, now A = LY (vertical line coord)
80 continue-if-different
81 [0xB8 ;; compare A with B
82 0x28
83 (->signed-8-bit
84 (+ -3 (- (count timing-loop))))
85 ]
86 continue-if-144
87 [0xFE
88 144 ;; compare LY (in A) with 144
89 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
90 (->signed-8-bit
91 (+ -4 (- (count timing-loop))
92 (- (count continue-if-different))))]]
94 (concat init timing-loop continue-if-different continue-if-144)))
97 (defn test-frame-metronome
98 "Ensure that frame-metronome ticks exactly once every frame."
99 ([] (test-frame-metronome 151))
100 ([steps]
101 (let [inc-C [0x0C 0x18
102 (->signed-8-bit
103 (+ -3 (- (count (frame-metronome)))))]
104 program (concat (frame-metronome) inc-C)
105 count-frames
106 (-> (tick (mid-game))
107 (IE! 0)
108 (BC! 0)
109 (set-memory-range pokemon-list-start program)
110 (PC! pokemon-list-start))
111 C-after-moves (C (run-moves count-frames (repeat steps [])))]
112 (println "C:" C-after-moves)
113 (assert (= steps C-after-moves))
115 (println "C =" steps "after" steps "steps")
116 count-frames)))
118 (defn main-bootstrap-program [start-address]
119 (let [[start-high start-low] (disect-bytes-2 start-address)
120 ]
121 ))
132 ;;;;;; TESTS ;;;;;;
134 (defn bootstrap-base []
135 (let [program (main-bootstrap-program pokemon-list-start)]
136 ;; make sure program is valid output for item-writer
137 (bootstrap-pattern program)
138 (-> (tick (mid-game))
139 (set-memory-range pokemon-list-start program)
140 (PC! pokemon-list-start))))
142 (defn test-write-bytes-mode []
143 (let [target-address 0xC00F
144 [target-high target-low] (disect-bytes-2 target-address)
145 assembly [0xF3 0x18 0xFE 0x12]
146 get-mem-region #(subvec (vec (memory %))
147 target-address (+ target-address 20))
148 before (bootstrap-base)
149 after
150 (-> before
151 (step []) ; make sure it can handle blanks
152 (step []) ; at the beginning.
153 (step [])
154 (step [:start]) ; select WRITE-BYTES mode
155 (step (buttons 4)) ; write 4 bytes
156 (step (buttons target-high))
157 (step (buttons target-low))
158 (step (buttons (nth assembly 0)))
159 (step (buttons (nth assembly 1)))
160 (step (buttons (nth assembly 2)))
161 (step (buttons (nth assembly 3)))
162 (step [])
163 (step [])
164 (step []))]
165 (println "before :" (get-mem-region before))
166 (println "after :" (get-mem-region after))
167 (assert (= assembly (take 4 (get-mem-region after))))
168 after))
170 (defn test-jump-mode []
171 (let [target-address 0xC00F
172 [target-high target-low] (disect-bytes-2 target-address)
173 post-jump
174 (-> (test-write-bytes-mode)
175 (step [])
176 (step [])
177 (step [])
178 (step (buttons 0xFF)) ; Select JUMP mode.
179 (step (buttons target-high))
180 (step (buttons target-low)))
181 program-counters
182 (capture-program-counter
183 post-jump
184 10000)]
185 (println program-counters)
186 (assert (contains? (set program-counters) target-address))
187 post-jump))