view clojure/com/aurellem/gb/rlm_assembly.clj @ 388:a0d0e1a46b1d

had to change programs to prevent repeats.
author Robert McIntyre <rlm@mit.edu>
date Thu, 12 Apr 2012 10:20:38 -0500
parents 47d44bb54d32
children bb8978d370d8
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 [timing-loop
68 [0x01 ; \
69 0x43 ; |
70 0xFE ; | load 0xFF44 into BC without repeats
71 0x0C ; |
72 0x04 ; /
73 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)
74 continue-if-144
75 [0xFE
76 144 ;; compare LY (in A) with 144
77 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
78 (->signed-8-bit
79 (+ -4 (- (count timing-loop))))]
80 spin-loop
81 [0x05 ;; dec B, which is 0xFF
82 0x20 ;; spin until B==0
83 0xFD]]
84 (concat timing-loop continue-if-144 spin-loop)))
86 (defn test-frame-metronome
87 "Ensure that frame-metronome ticks exactly once every frame."
88 ([] (test-frame-metronome 151))
89 ([steps]
90 (let [inc-E [0x1C 0x18
91 (->signed-8-bit
92 (+ -3 (- (count (frame-metronome)))))]
93 program (concat (frame-metronome) inc-E)
94 count-frames
95 (-> (tick (mid-game))
96 (IE! 0)
97 (DE! 0)
98 (set-memory-range pokemon-list-start program)
99 (PC! pokemon-list-start))
100 E-after-moves (E (run-moves count-frames (repeat steps [])))]
101 (println "E:" E-after-moves)
102 (assert (= steps E-after-moves))
104 (println "E =" E-after-moves "after" steps "steps")
105 count-frames)))
107 (defn read-user-input []
108 [0x3E
109 0x20 ; prepare to measure d-pad
111 0x01 ;\
112 0x01 ; |
113 0xFE ; | load 0xFF00 into BC without repeats
114 0x04 ; |
115 0x0D ;/
117 0x02
118 0x0A ;; get D-pad info
120 0xF5 ;; push AF
122 0x3E
123 0x10 ; prepare to measure buttons
125 0x3F ;; clear carry flag no-op to prevent repeated nybbbles
127 0x02
128 0x0A ;; get button info
130 0xE6 ;; select bottom bits of A
131 0x0F
133 0x47 ;; A->B
135 0xF1 ;; pop AF
137 0xE6
138 0x0F ;; select bottom bits of A
140 0xCB
141 0x37 ;; swap A nybbles
143 0xB0 ;; (or A B) -> A
145 0x2F ;; (NOT A) -> A
146 ])
148 (defn test-read-user-input []
149 (let [program
150 (concat
151 (frame-metronome) (read-user-input)
152 [0x5F ;; A-> E
153 0x18
154 (->signed-8-bit
155 (+ (- (count (frame-metronome)))
156 (- (count (read-user-input)))
157 (- 3)))])
158 read-input
159 (-> (tick (mid-game))
160 (IE! 0)
161 (set-memory-range pokemon-list-start program)
162 (PC! pokemon-list-start))]
163 (dorun
164 (for [i (range 0x100)]
165 (assert (= (E (step read-input (buttons i))) i))))
166 (println "Tested all inputs.")
167 read-input))
169 (def mode-select-mode (Integer/parseInt "00000000" 2))
170 (def write-mode (Integer/parseInt "00000001" 2))
171 (def jump-mode (Integer/parseInt "10000000" 2))
173 (def input-high-addr-jump (Integer/parseInt "10000010" 2))
174 (def input-high-addr-write (Integer/parseInt "00000010" 2))
175 (def input-low-addr-jump (Integer/parseInt "10000100" 2))
176 (def input-low-addr-write (Integer/parseInt "00000100" 2))
178 (def input-write-num-mode (Integer/parseInt "00001000" 2))
179 (def do-write-mode (Integer/parseInt "00010000" 2))
181 (defn main-bootstrap-program [start-address]
182 (let [[start-high start-low] (disect-bytes-2 start-address)
183 jump-distance (+ (count (frame-metronome)
184 (read-user-input)))
186 init
187 [0xAF 0x4F] ;; 0->A; 0->C;
191 ;; HL = here
192 ;; add C to HL
193 ;; jp HL
195 prepare-HL
196 [0xD1 ;; pop DE causes D and E to be non-volitale
198 0x21 ;; load HL from literal nn
199 :dispatch-high
200 :dispatch-low]
202 mode-dispatch
203 [0xC5 ;; push BC
204 0x06 ;\
205 0x01 ; | 0->B without repeats
206 0x05 ;/
207 0x09 ;; add BC to HL
208 0xC1 ;; pop BC
209 0xE9] ;; jp
211 mode-select
212 [0x4F ;; A->C (this is the address of dispatch above)
213 0x18 ;; return
214 :jump-to-end]
216 input-bytes-to-write
217 [0x47 ;; A->B
218 0x18 ;; return
219 :jump-to-end]
225 cleanup
226 [0xD5 ;; push DE
227 0x18
228 0x??];; jump all the way back to frame-metronome
231 ;; (concat init (frame-metronome) (read-user-input)
232 ;; prepare-HL here mode-dispatch)
241 ;; ))
250 (comment
252 ;;;;;; TESTS ;;;;;;
254 (defn bootstrap-base []
255 (let [program (main-bootstrap-program pokemon-list-start)]
256 ;; make sure program is valid output for item-writer
257 (bootstrap-pattern program)
258 (-> (tick (mid-game))
259 (set-memory-range pokemon-list-start program)
260 (PC! pokemon-list-start))))
262 (defn test-write-bytes-mode []
263 (let [target-address 0xC00F
264 [target-high target-low] (disect-bytes-2 target-address)
265 assembly [0xF3 0x18 0xFE 0x12]
266 get-mem-region #(subvec (vec (memory %))
267 target-address (+ target-address 20))
268 before (bootstrap-base)
269 after
270 (-> before
271 (step []) ; make sure it can handle blanks
272 (step []) ; at the beginning.
273 (step [])
274 (step [:start]) ; select WRITE-BYTES mode
275 (step (buttons 4)) ; write 4 bytes
276 (step (buttons target-high))
277 (step (buttons target-low))
278 (step (buttons (nth assembly 0)))
279 (step (buttons (nth assembly 1)))
280 (step (buttons (nth assembly 2)))
281 (step (buttons (nth assembly 3)))
282 (step [])
283 (step [])
284 (step []))]
285 (println "before :" (get-mem-region before))
286 (println "after :" (get-mem-region after))
287 (assert (= assembly (take 4 (get-mem-region after))))
288 after))
290 (defn test-jump-mode []
291 (let [target-address 0xC00F
292 [target-high target-low] (disect-bytes-2 target-address)
293 post-jump
294 (-> (test-write-bytes-mode)
295 (step [])
296 (step [])
297 (step [])
298 (step (buttons 0xFF)) ; Select JUMP mode.
299 (step (buttons target-high))
300 (step (buttons target-low)))
301 program-counters
302 (capture-program-counter
303 post-jump
304 10000)]
305 (println program-counters)
306 (assert (contains? (set program-counters) target-address))
307 post-jump))