rlm@377
|
1 (ns com.aurellem.gb.rlm-assembly
|
rlm@377
|
2 "Version of main bootstrap program that is valid output for the
|
rlm@377
|
3 item-writer program."
|
rlm@377
|
4 (:use (com.aurellem.gb gb-driver assembly util vbm constants))
|
rlm@377
|
5 (:use (com.aurellem.run bootstrap-1))
|
rlm@377
|
6 (:import [com.aurellem.gb.gb_driver SaveState]))
|
rlm@377
|
7
|
rlm@390
|
8
|
rlm@390
|
9 ;; MODE-SELECT
|
rlm@390
|
10 ;; SET-LENGTH
|
rlm@390
|
11 ;; SET-TARGET
|
rlm@390
|
12 ;; WRITE
|
rlm@390
|
13 ;; JUMP
|
rlm@390
|
14
|
rlm@377
|
15 ;; Specs for Main Bootstrap Program
|
rlm@377
|
16
|
rlm@377
|
17 ;; Number-Input
|
rlm@377
|
18 ;; Number input works using all eight buttons to
|
rlm@377
|
19 ;; spell out an 8 bit number. The order of buttons is
|
rlm@377
|
20 ;; [:d :u :l :r :start :select :b :a] --> 11111111
|
rlm@377
|
21 ;; [ :l :start :a] --> 00101001
|
rlm@377
|
22
|
rlm@377
|
23 ;;; MODE-SELECT
|
rlm@377
|
24 ;; The bootstrap program starts in MODE-SELECT mode.
|
rlm@377
|
25 ;; MODE-SELECT transitions to one of three modes depending
|
rlm@377
|
26 ;; on which buttons are pressed:
|
rlm@377
|
27 ;; 0 (no-buttons) : MODE-SELECT
|
rlm@377
|
28 ;; 8 [:start] : WRITE-BYTES
|
rlm@377
|
29 ;; 0xFF (all-buttons) : JUMP
|
rlm@377
|
30
|
rlm@377
|
31 ;;; WRITE-BYTES
|
rlm@377
|
32
|
rlm@377
|
33 ;; WRITE-BYTES mode writes sequences of arbitray values to
|
rlm@377
|
34 ;; arbitray memory locations. It expects you to enter a
|
rlm@377
|
35 ;; header of three bytes describing what to write:
|
rlm@377
|
36
|
rlm@377
|
37 ;; Byte 0 : Number of Bytes to Write
|
rlm@377
|
38 ;; Byte 1 : Start Address High Byte
|
rlm@377
|
39 ;; Byte 1 : Start Address Low Byte
|
rlm@377
|
40
|
rlm@377
|
41 ;; Then, you enter the number of bytes specified in Byte 0
|
rlm@377
|
42 ;; they are written to the start address in
|
rlm@377
|
43 ;; sequence. After the last byte is written control
|
rlm@377
|
44 ;; returns to MODE-SELECT mode.
|
rlm@377
|
45
|
rlm@377
|
46 ;; Example: to write the sequence [1 2 3 4] starting at
|
rlm@377
|
47 ;; address 0xC01F enter
|
rlm@377
|
48 ;; Byte 0 : 4 (will write four bytes)
|
rlm@377
|
49 ;; Byte 1 : 0xC0 (high byte of 0xC01F)
|
rlm@377
|
50 ;; Byte 2 : 0x1F (low byte of 0xC01F)
|
rlm@377
|
51 ;; Byte 3 : 1 (write 1 to 0xC01F)
|
rlm@377
|
52 ;; Byte 4 : 2 (write 2 to 0xC020)
|
rlm@377
|
53 ;; Byte 5 : 3 (write 3 to 0xC021)
|
rlm@377
|
54 ;; Byte 6 : 4 (write 4 to 0xC022)
|
rlm@377
|
55
|
rlm@377
|
56 ;;; JUMP
|
rlm@377
|
57 ;; JUMP mode jumps program control to any arbitray
|
rlm@377
|
58 ;; location. It expects you to enter two bytes which
|
rlm@377
|
59 ;; correspond to the high and low bytes of the memory
|
rlm@377
|
60 ;; address to which you want to jump.
|
rlm@377
|
61 ;; Byte 0 : Jump Address High Byte
|
rlm@377
|
62 ;; Byte 1 : Jump Address Low Byte
|
rlm@377
|
63
|
rlm@377
|
64 ;; Example: to jump to address 0x1234 enter
|
rlm@377
|
65 ;; Byte 0 : 0x12 (high byte of 0x1234)
|
rlm@377
|
66 ;; Byte 1 : 0x34 (low byte of 0x1234)
|
rlm@377
|
67
|
rlm@377
|
68
|
rlm@378
|
69 (defn ->signed-8-bit [n]
|
rlm@378
|
70 (if (< n 0)
|
rlm@378
|
71 (+ 256 n) n))
|
rlm@378
|
72
|
rlm@403
|
73 (defn frame-metronome
|
rlm@403
|
74 ([] (frame-metronome true))
|
rlm@403
|
75 ([spin-loop?]
|
rlm@403
|
76 (let [init [0xC5] ;; save value of BC
|
rlm@403
|
77 timing-loop
|
rlm@403
|
78 [0x01 ; \
|
rlm@403
|
79 0x43 ; |
|
rlm@403
|
80 0xFE ; | load 0xFF44 into BC without repeats
|
rlm@403
|
81 0x0C ; |
|
rlm@403
|
82 0x04 ; /
|
rlm@403
|
83 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)
|
rlm@403
|
84 continue-if-144
|
rlm@403
|
85 [0xFE
|
rlm@403
|
86 144 ;; compare LY (in A) with 144
|
rlm@403
|
87 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
|
rlm@403
|
88 (->signed-8-bit
|
rlm@403
|
89 (+ -4 (- (count timing-loop))))]
|
rlm@403
|
90 spin-loop
|
rlm@403
|
91 [0x05 ;; dec B, which is 0xFF
|
rlm@403
|
92 0x20 ;; spin until B==0
|
rlm@403
|
93 0xFD]]
|
rlm@403
|
94 (concat init timing-loop continue-if-144
|
rlm@403
|
95 (if spin-loop?
|
rlm@403
|
96 spin-loop [])))))
|
rlm@378
|
97
|
rlm@379
|
98 (defn test-frame-metronome
|
rlm@379
|
99 "Ensure that frame-metronome ticks exactly once every frame."
|
rlm@379
|
100 ([] (test-frame-metronome 151))
|
rlm@379
|
101 ([steps]
|
rlm@388
|
102 (let [inc-E [0x1C 0x18
|
rlm@379
|
103 (->signed-8-bit
|
rlm@379
|
104 (+ -3 (- (count (frame-metronome)))))]
|
rlm@388
|
105 program (concat (frame-metronome) inc-E)
|
rlm@379
|
106 count-frames
|
rlm@379
|
107 (-> (tick (mid-game))
|
rlm@379
|
108 (IE! 0)
|
rlm@388
|
109 (DE! 0)
|
rlm@379
|
110 (set-memory-range pokemon-list-start program)
|
rlm@379
|
111 (PC! pokemon-list-start))
|
rlm@390
|
112 E-after-moves
|
rlm@390
|
113 (E (run-moves count-frames (repeat steps [])))]
|
rlm@388
|
114 (println "E:" E-after-moves)
|
rlm@388
|
115 (assert (= steps E-after-moves))
|
rlm@379
|
116
|
rlm@388
|
117 (println "E =" E-after-moves "after" steps "steps")
|
rlm@379
|
118 count-frames)))
|
rlm@379
|
119
|
rlm@384
|
120 (defn read-user-input []
|
rlm@388
|
121 [0x3E
|
rlm@388
|
122 0x20 ; prepare to measure d-pad
|
rlm@384
|
123
|
rlm@388
|
124 0x01 ;\
|
rlm@388
|
125 0x01 ; |
|
rlm@388
|
126 0xFE ; | load 0xFF00 into BC without repeats
|
rlm@388
|
127 0x04 ; |
|
rlm@388
|
128 0x0D ;/
|
rlm@386
|
129
|
rlm@388
|
130 0x02
|
rlm@388
|
131 0x0A ;; get D-pad info
|
rlm@385
|
132
|
rlm@385
|
133 0xF5 ;; push AF
|
rlm@385
|
134
|
rlm@385
|
135 0x3E
|
rlm@388
|
136 0x10 ; prepare to measure buttons
|
rlm@388
|
137
|
rlm@388
|
138 0x3F ;; clear carry flag no-op to prevent repeated nybbbles
|
rlm@385
|
139
|
rlm@388
|
140 0x02
|
rlm@388
|
141 0x0A ;; get button info
|
rlm@385
|
142
|
rlm@384
|
143 0xE6 ;; select bottom bits of A
|
rlm@384
|
144 0x0F
|
rlm@384
|
145
|
rlm@388
|
146 0x47 ;; A->B
|
rlm@385
|
147
|
rlm@385
|
148 0xF1 ;; pop AF
|
rlm@385
|
149
|
rlm@385
|
150 0xE6
|
rlm@385
|
151 0x0F ;; select bottom bits of A
|
rlm@385
|
152
|
rlm@384
|
153 0xCB
|
rlm@384
|
154 0x37 ;; swap A nybbles
|
rlm@387
|
155
|
rlm@388
|
156 0xB0 ;; (or A B) -> A
|
rlm@387
|
157
|
rlm@384
|
158 0x2F ;; (NOT A) -> A
|
rlm@390
|
159
|
rlm@384
|
160 ])
|
rlm@384
|
161
|
rlm@384
|
162 (defn test-read-user-input []
|
rlm@384
|
163 (let [program
|
rlm@384
|
164 (concat
|
rlm@384
|
165 (frame-metronome) (read-user-input)
|
rlm@388
|
166 [0x5F ;; A-> E
|
rlm@384
|
167 0x18
|
rlm@384
|
168 (->signed-8-bit
|
rlm@384
|
169 (+ (- (count (frame-metronome)))
|
rlm@384
|
170 (- (count (read-user-input)))
|
rlm@384
|
171 (- 3)))])
|
rlm@384
|
172 read-input
|
rlm@384
|
173 (-> (tick (mid-game))
|
rlm@384
|
174 (IE! 0)
|
rlm@384
|
175 (set-memory-range pokemon-list-start program)
|
rlm@384
|
176 (PC! pokemon-list-start))]
|
rlm@384
|
177 (dorun
|
rlm@385
|
178 (for [i (range 0x100)]
|
rlm@388
|
179 (assert (= (E (step read-input (buttons i))) i))))
|
rlm@385
|
180 (println "Tested all inputs.")
|
rlm@384
|
181 read-input))
|
rlm@384
|
182
|
rlm@393
|
183 (def symbol-index
|
rlm@393
|
184 (fn [symbol sequence]
|
rlm@393
|
185 (count (take-while
|
rlm@393
|
186 (partial not= symbol)
|
rlm@393
|
187 sequence))))
|
rlm@385
|
188
|
rlm@403
|
189 (defn main-bootstrap-program
|
rlm@403
|
190 ([] (main-bootstrap-program pokemon-list-start))
|
rlm@403
|
191 ([start-address]
|
rlm@403
|
192 ;; Register Use:
|
rlm@403
|
193
|
rlm@403
|
194 ;; ED non-volitale scratch
|
rlm@403
|
195
|
rlm@403
|
196 ;; A user-input
|
rlm@403
|
197 ;; HL target-address
|
rlm@403
|
198 ;; B bytes-to-write
|
rlm@403
|
199 ;; C non-volatile scratch
|
rlm@385
|
200
|
rlm@403
|
201 ;; Modes (with codes) are:
|
rlm@390
|
202
|
rlm@403
|
203 ;; single-action-modes:
|
rlm@403
|
204 ;; SET-TARGET-HIGH 0x67 ;; A->H
|
rlm@403
|
205 ;; SET-TARGET-LOW 0x6F ;; A->L
|
rlm@403
|
206 ;; JUMP 0xE9 ;; jump to (HL)
|
rlm@390
|
207
|
rlm@403
|
208 ;; multi-action-modes
|
rlm@403
|
209 ;; WRITE 0x47 ;; A->B
|
rlm@390
|
210
|
rlm@403
|
211 (let [[start-high start-low] (disect-bytes-2 start-address)
|
rlm@403
|
212 jump-distance (+ (count (frame-metronome))
|
rlm@403
|
213 (count (read-user-input)))
|
rlm@390
|
214
|
rlm@403
|
215 init
|
rlm@403
|
216 [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B
|
rlm@385
|
217
|
rlm@403
|
218 input
|
rlm@403
|
219 [0xC1 ;; pop BC so it's not volatile
|
rlm@388
|
220
|
rlm@403
|
221 0x5F ;; A->E
|
rlm@403
|
222 0xAF ;; test for output-mode (bytes-to-write > 0)
|
rlm@403
|
223 0xB8 ;; (cp A B)
|
rlm@403
|
224 0x7B ;; E->A
|
rlm@403
|
225 0x20 ;; skip to output section if
|
rlm@403
|
226 :to-output ;; we're not in input mode
|
rlm@403
|
227
|
rlm@403
|
228 :to-be-executed
|
rlm@391
|
229
|
rlm@403
|
230 ;; write mode to instruction-to-be-executed (pun)
|
rlm@403
|
231 0xEA
|
rlm@403
|
232 :to-be-executed-address
|
rlm@388
|
233
|
rlm@403
|
234 ;; protection region -- do not queue this op for
|
rlm@403
|
235 ;; execution if the last one was non-zero
|
rlm@403
|
236 0x79 ;; C->A
|
rlm@403
|
237 0xA7 ;; test A==0
|
rlm@403
|
238 0x28
|
rlm@403
|
239 0x04
|
rlm@403
|
240 0xAF ;; put a no op (0x00) in to-be-executed
|
rlm@403
|
241 0xEA ;;
|
rlm@403
|
242 :to-be-executed-address
|
rlm@403
|
243
|
rlm@403
|
244 0x7B ;; E->A
|
rlm@403
|
245 0x4F ;; A->C now C stores previous instruction
|
rlm@403
|
246 0x18 ;; return
|
rlm@403
|
247 :to-beginning-1]
|
rlm@403
|
248
|
rlm@403
|
249 output
|
rlm@403
|
250 [:output-start ;; just a label
|
rlm@403
|
251 0x54 ;;
|
rlm@403
|
252 0x5D ;; HL->DE \
|
rlm@403
|
253 ;; | This mess is here to do
|
rlm@403
|
254 0x12 ;; A->(DE) | 0x22 (LDI (HL), A) without
|
rlm@403
|
255 ;; | any repeating nybbles
|
rlm@403
|
256 0x23 ;; inc HL /
|
rlm@398
|
257
|
rlm@403
|
258 0x05 ;; DEC bytes-to-write (B)
|
rlm@391
|
259
|
rlm@403
|
260 0x18
|
rlm@403
|
261 :to-beginning-2]
|
rlm@403
|
262
|
rlm@403
|
263 symbols
|
rlm@403
|
264 {:to-be-executed-address
|
rlm@403
|
265 (reverse
|
rlm@403
|
266 (disect-bytes-2
|
rlm@403
|
267 (+ start-address jump-distance
|
rlm@403
|
268 (count init)
|
rlm@403
|
269 (symbol-index :to-be-executed input))))
|
rlm@403
|
270 :to-be-executed 0x00} ;; clear carry flag no-op
|
rlm@392
|
271
|
rlm@403
|
272 program** (flatten
|
rlm@403
|
273 (replace
|
rlm@403
|
274 symbols
|
rlm@403
|
275 (concat init (frame-metronome)
|
rlm@403
|
276 (read-user-input)
|
rlm@403
|
277 input output)))
|
rlm@403
|
278 resolve-internal-jumps
|
rlm@403
|
279 {:output-start []
|
rlm@403
|
280 :to-output
|
rlm@403
|
281 (->signed-8-bit
|
rlm@403
|
282 (dec
|
rlm@403
|
283 (- (symbol-index :output-start program**)
|
rlm@403
|
284 (symbol-index :to-output program**))))}
|
rlm@391
|
285
|
rlm@403
|
286 program*
|
rlm@403
|
287 (flatten (replace resolve-internal-jumps program**))
|
rlm@403
|
288
|
rlm@403
|
289 resolve-external-jumps
|
rlm@403
|
290 {:to-beginning-1
|
rlm@403
|
291 (->signed-8-bit
|
rlm@403
|
292 (+ (count init)
|
rlm@403
|
293 -2 (- (dec (symbol-index :to-beginning-1 program*)))))
|
rlm@403
|
294 :to-beginning-2
|
rlm@403
|
295 (->signed-8-bit
|
rlm@403
|
296 (+ (count init)
|
rlm@403
|
297 -2 (- (dec (symbol-index :to-beginning-2 program*)))))}
|
rlm@389
|
298
|
rlm@403
|
299 program
|
rlm@403
|
300 (replace resolve-external-jumps program*)]
|
rlm@403
|
301 program)))
|
rlm@378
|
302
|
rlm@378
|
303
|
rlm@377
|
304 ;;;;;; TESTS ;;;;;;
|
rlm@377
|
305
|
rlm@401
|
306 (def set-H-mode 0x67)
|
rlm@401
|
307 (def set-L-mode 0x6F)
|
rlm@401
|
308 (def jump-mode 0xE9)
|
rlm@401
|
309 (def write-mode 0x47)
|
rlm@401
|
310
|
rlm@401
|
311
|
rlm@377
|
312 (defn bootstrap-base []
|
rlm@377
|
313 (let [program (main-bootstrap-program pokemon-list-start)]
|
rlm@377
|
314 ;; make sure program is valid output for item-writer
|
rlm@392
|
315 ;;(bootstrap-pattern program)
|
rlm@377
|
316 (-> (tick (mid-game))
|
rlm@377
|
317 (set-memory-range pokemon-list-start program)
|
rlm@400
|
318 (PC! pokemon-list-start)
|
rlm@400
|
319 (step [])
|
rlm@400
|
320 (step []))))
|
rlm@377
|
321
|
rlm@400
|
322 (defn test-set-H []
|
rlm@400
|
323 (letfn [(test-H [state n]
|
rlm@400
|
324 (let [after
|
rlm@400
|
325 (-> state
|
rlm@402
|
326 (step (buttons set-H-mode))
|
rlm@400
|
327 (step (buttons n))
|
rlm@400
|
328 (step []))]
|
rlm@401
|
329 ;;(println "desired H =" n "actual =" (H after))
|
rlm@400
|
330 (assert (= n (H after)))
|
rlm@400
|
331 after))]
|
rlm@403
|
332 (let [result (reduce test-H (bootstrap-base) (range 0x100))]
|
rlm@403
|
333 (println "tested all H values")
|
rlm@403
|
334 result)))
|
rlm@400
|
335
|
rlm@401
|
336 (defn test-write-bytes []
|
rlm@377
|
337 (let [target-address 0xC00F
|
rlm@377
|
338 [target-high target-low] (disect-bytes-2 target-address)
|
rlm@377
|
339 assembly [0xF3 0x18 0xFE 0x12]
|
rlm@377
|
340 get-mem-region #(subvec (vec (memory %))
|
rlm@377
|
341 target-address (+ target-address 20))
|
rlm@377
|
342 before (bootstrap-base)
|
rlm@377
|
343 after
|
rlm@377
|
344 (-> before
|
rlm@392
|
345 (step []) ; make sure it can handle blanks
|
rlm@392
|
346 (step []) ; at the beginning.
|
rlm@377
|
347 (step [])
|
rlm@402
|
348 (step (buttons set-H-mode)) ; select set-H
|
rlm@401
|
349 (step (buttons target-high))
|
rlm@401
|
350 (step [])
|
rlm@402
|
351 (step (buttons set-L-mode))
|
rlm@401
|
352 (step (buttons target-low))
|
rlm@401
|
353 (step [])
|
rlm@401
|
354 (step (buttons write-mode))
|
rlm@392
|
355 (step (buttons 4)) ; write 4 bytes
|
rlm@377
|
356 (step (buttons (nth assembly 0)))
|
rlm@377
|
357 (step (buttons (nth assembly 1)))
|
rlm@377
|
358 (step (buttons (nth assembly 2)))
|
rlm@377
|
359 (step (buttons (nth assembly 3)))
|
rlm@377
|
360 (step [])
|
rlm@377
|
361 (step [])
|
rlm@377
|
362 (step []))]
|
rlm@377
|
363 (println "before :" (get-mem-region before))
|
rlm@377
|
364 (println "after :" (get-mem-region after))
|
rlm@402
|
365 (assert (= assembly (take 4 (get-mem-region after))))
|
rlm@377
|
366 after))
|
rlm@377
|
367
|
rlm@402
|
368 (defn test-jump []
|
rlm@377
|
369 (let [target-address 0xC00F
|
rlm@377
|
370 [target-high target-low] (disect-bytes-2 target-address)
|
rlm@377
|
371 post-jump
|
rlm@402
|
372 (-> (test-write-bytes)
|
rlm@402
|
373 (step (buttons set-H-mode)) ; select set-H
|
rlm@402
|
374 (step (buttons target-high))
|
rlm@377
|
375 (step [])
|
rlm@402
|
376 (step (buttons set-L-mode))
|
rlm@402
|
377 (step (buttons target-low))
|
rlm@377
|
378 (step [])
|
rlm@402
|
379 (step (buttons jump-mode))) ; Select JUMP mode.
|
rlm@377
|
380 program-counters
|
rlm@377
|
381 (capture-program-counter
|
rlm@377
|
382 post-jump
|
rlm@377
|
383 10000)]
|
rlm@377
|
384 (assert (contains? (set program-counters) target-address))
|
rlm@402
|
385 (println "jump test passed")
|
rlm@377
|
386 post-jump))
|
rlm@402
|
387
|
rlm@402
|
388
|
rlm@402
|
389 (defn run-all-tests []
|
rlm@402
|
390 (test-frame-metronome)
|
rlm@402
|
391 (test-read-user-input)
|
rlm@402
|
392 (test-set-H)
|
rlm@402
|
393 (test-write-bytes)
|
rlm@402
|
394 (test-jump))
|