view clojure/com/aurellem/gb/rlm_assembly.clj @ 387:47d44bb54d32

saving progress...
author Robert McIntyre <rlm@mit.edu>
date Thu, 12 Apr 2012 09:21:40 -0500
parents d8cbbf2a3133
children a0d0e1a46b1d
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)
75 0x11 ; \
76 0x43 ; |
77 0xFE ; | load 0xFF44 into DE without repeats
78 0x1C ; |
79 0x14 ; /
80 0x1A ;; (DE) -> A, now A = LY (vertical line coord)
81 ]
82 continue-if-144
83 [0xFE
84 144 ;; compare LY (in A) with 144
85 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
86 (->signed-8-bit
87 (+ -4 (- (count timing-loop))))]
88 spin-loop
89 [;;0x05 ;; dec B, which is 0xFF
90 0x15 ;; dec D, which is 0xFF
91 0x20 ;; spin until B==0
92 0xFD]]
93 (concat timing-loop continue-if-144 spin-loop)))
95 (defn test-frame-metronome
96 "Ensure that frame-metronome ticks exactly once every frame."
97 ([] (test-frame-metronome 151))
98 ([steps]
99 (let [inc-B [0x04 0x18
100 (->signed-8-bit
101 (+ -3 (- (count (frame-metronome)))))]
102 program (concat (frame-metronome) inc-B)
103 count-frames
104 (-> (tick (mid-game))
105 (IE! 0)
106 (BC! 0)
107 (set-memory-range pokemon-list-start program)
108 (PC! pokemon-list-start))
109 B-after-moves (B (run-moves count-frames (repeat steps [])))]
110 (println "B:" B-after-moves)
111 (assert (= steps B-after-moves))
113 (println "B =" B-after-moves "after" steps "steps")
114 count-frames)))
116 (defn read-user-input []
117 [;;0x01 ;\
118 ;;0x01 ; |
119 ;;0xFE ; | load 0xFF00 into BC without repeats
120 ;;0x04 ; |
121 ;;0x0D ;/
123 0x11 ; \
124 0x01 ; |
125 0xFE ; | load 0xFF44 into DE without repeats
126 0x14 ; |
127 0x1D ; /
129 0x3E
130 (Integer/parseInt "00100000" 2) ; prepare to measure d-pad
132 0x12
133 0x1A ;; get D-pad info
135 0xF5 ;; push AF
137 0x3E
138 (Integer/parseInt "00010000" 2) ; prepare to measure buttons
140 0x12
141 0x1A ;; get button info
144 0xE6 ;; select bottom bits of A
145 0x0F
147 0x57 ;; A->D
149 0xF1 ;; pop AF
151 0xE6
152 0x0F ;; select bottom bits of A
154 0xCB
155 0x37 ;; swap A nybbles
157 0x5A ;; D->E \ necessary to
158 0xB3 ;; (or A D) -> A / prevent repeats.
160 0x2F ;; (NOT A) -> A
161 ])
163 (defn test-read-user-input []
164 (let [program
165 (concat
166 (frame-metronome) (read-user-input)
167 [0x47 ;; A->B
168 0x18
169 (->signed-8-bit
170 (+ (- (count (frame-metronome)))
171 (- (count (read-user-input)))
172 (- 3)))])
173 read-input
174 (-> (tick (mid-game))
175 (IE! 0)
176 (set-memory-range pokemon-list-start program)
177 (PC! pokemon-list-start))]
178 (dorun
179 (for [i (range 0x100)]
180 (assert (= (B (step read-input (buttons i))) i))))
181 (println "Tested all inputs.")
182 read-input))
184 (def mode-select-mode (Integer/parseInt "00000000" 2))
185 (def write-mode (Integer/parseInt "00000001" 2))
186 (def jump-mode (Integer/parseInt "10000000" 2))
188 (def input-high-addr-jump (Integer/parseInt "10000010" 2))
189 (def input-high-addr-write (Integer/parseInt "00000010" 2))
190 (def input-low-addr-jump (Integer/parseInt "10000100" 2))
191 (def input-low-addr-write (Integer/parseInt "00000100" 2))
193 (def input-write-num-mode (Integer/parseInt "00001000" 2))
194 (def do-write-mode (Integer/parseInt "00010000" 2))
202 (defn main-bootstrap-program [start-address]
203 (let [[start-high start-low] (disect-bytes-2 start-address)
204 jump-distance (+ (count (frame-metronome)
205 (read-user-input)))
207 init
208 [0xAF 0x4F] ;; 0->A; 0->C;
210 ;; HL = here
211 ;; add C to HL
212 ;; jp HL
214 prepare-HL
215 [0x21] ;; load HL from literal nn
216 ;; nn == here defined below
218 mode-dispatch
219 [0x06 ;\
220 0x01 ; | 0->B without repeats
221 0x05 ;/
222 0x09 ;; add BC to HL
223 0xE9] ;; jp
225 here
226 (disect-bytes-2 (+ 2 start-address (count init)
227 jump-distance prepare-HL
228 (count mode-dispatch)))
232 ;;(here) jr end
234 ;;stuff
235 ;;modify E
236 ;;jr metronome
238 ;;stuff
239 ;;jr metronome
241 input-number
243 [0x47 ;; A->B
244 0x1E ;;
245 input-high-write-jump
246 0x18
247 (jump-distance ??)]
251 input-high-write
252 [0x
260 (concat init (frame-metronome) (read-user-input)
261 prepare-HL here mode-dispatch)
270 ))
281 ;;;;;; TESTS ;;;;;;
283 (defn bootstrap-base []
284 (let [program (main-bootstrap-program pokemon-list-start)]
285 ;; make sure program is valid output for item-writer
286 (bootstrap-pattern program)
287 (-> (tick (mid-game))
288 (set-memory-range pokemon-list-start program)
289 (PC! pokemon-list-start))))
291 (defn test-write-bytes-mode []
292 (let [target-address 0xC00F
293 [target-high target-low] (disect-bytes-2 target-address)
294 assembly [0xF3 0x18 0xFE 0x12]
295 get-mem-region #(subvec (vec (memory %))
296 target-address (+ target-address 20))
297 before (bootstrap-base)
298 after
299 (-> before
300 (step []) ; make sure it can handle blanks
301 (step []) ; at the beginning.
302 (step [])
303 (step [:start]) ; select WRITE-BYTES mode
304 (step (buttons 4)) ; write 4 bytes
305 (step (buttons target-high))
306 (step (buttons target-low))
307 (step (buttons (nth assembly 0)))
308 (step (buttons (nth assembly 1)))
309 (step (buttons (nth assembly 2)))
310 (step (buttons (nth assembly 3)))
311 (step [])
312 (step [])
313 (step []))]
314 (println "before :" (get-mem-region before))
315 (println "after :" (get-mem-region after))
316 (assert (= assembly (take 4 (get-mem-region after))))
317 after))
319 (defn test-jump-mode []
320 (let [target-address 0xC00F
321 [target-high target-low] (disect-bytes-2 target-address)
322 post-jump
323 (-> (test-write-bytes-mode)
324 (step [])
325 (step [])
326 (step [])
327 (step (buttons 0xFF)) ; Select JUMP mode.
328 (step (buttons target-high))
329 (step (buttons target-low)))
330 program-counters
331 (capture-program-counter
332 post-jump
333 10000)]
334 (println program-counters)
335 (assert (contains? (set program-counters) target-address))
336 post-jump))