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@404
|
73 (defn frame-metronome** []
|
rlm@404
|
74 (let [init [0xC5] ;; save value of BC
|
rlm@404
|
75 timing-loop
|
rlm@404
|
76 [0x01 ; \
|
rlm@404
|
77 0x43 ; |
|
rlm@404
|
78 0xFE ; | load 0xFF44 into BC without repeats
|
rlm@404
|
79 0x0C ; |
|
rlm@404
|
80 0x04 ; /
|
rlm@404
|
81 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)
|
rlm@404
|
82 continue-if-144
|
rlm@404
|
83 [0xFE
|
rlm@404
|
84 144 ;; compare LY (in A) with 144
|
rlm@404
|
85 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
|
rlm@404
|
86 (->signed-8-bit
|
rlm@404
|
87 (+ -4 (- (count timing-loop))))]
|
rlm@404
|
88 spin-loop
|
rlm@404
|
89 [0x05 ;; dec B, which is 0xFF
|
rlm@404
|
90 0x20 ;; spin until B==0
|
rlm@404
|
91 0xFD]]
|
rlm@404
|
92 (concat init timing-loop continue-if-144 spin-loop)))
|
rlm@404
|
93
|
rlm@404
|
94 (defn frame-metronome* []
|
rlm@404
|
95 [0x3E ;; smallest version, but uses repeated nybbles
|
rlm@404
|
96 0x01
|
rlm@404
|
97 0xE0
|
rlm@404
|
98 0xFF])
|
rlm@404
|
99
|
rlm@404
|
100
|
rlm@404
|
101 (defn frame-metronome []
|
rlm@404
|
102 [0x06 ;; load 0xFE into B
|
rlm@404
|
103 0xFE
|
rlm@404
|
104 0x04 ;; inc B, now B == FF
|
rlm@404
|
105 0x3E
|
rlm@404
|
106 0x01 ;; 1->A
|
rlm@404
|
107
|
rlm@404
|
108 0x48 ;; B->C
|
rlm@404
|
109 0x02]) ;; A->(BC) set exclusive v-blank interrupt
|
rlm@378
|
110
|
rlm@379
|
111 (defn test-frame-metronome
|
rlm@379
|
112 "Ensure that frame-metronome ticks exactly once every frame."
|
rlm@379
|
113 ([] (test-frame-metronome 151))
|
rlm@379
|
114 ([steps]
|
rlm@404
|
115 (let [inc-E [0x1C 0x76 0x18
|
rlm@404
|
116 (->signed-8-bit -4)]
|
rlm@404
|
117
|
rlm@388
|
118 program (concat (frame-metronome) inc-E)
|
rlm@379
|
119 count-frames
|
rlm@379
|
120 (-> (tick (mid-game))
|
rlm@379
|
121 (IE! 0)
|
rlm@388
|
122 (DE! 0)
|
rlm@379
|
123 (set-memory-range pokemon-list-start program)
|
rlm@379
|
124 (PC! pokemon-list-start))
|
rlm@390
|
125 E-after-moves
|
rlm@390
|
126 (E (run-moves count-frames (repeat steps [])))]
|
rlm@388
|
127 (println "E:" E-after-moves)
|
rlm@388
|
128 (assert (= steps E-after-moves))
|
rlm@404
|
129
|
rlm@388
|
130 (println "E =" E-after-moves "after" steps "steps")
|
rlm@379
|
131 count-frames)))
|
rlm@379
|
132
|
rlm@384
|
133 (defn read-user-input []
|
rlm@404
|
134 [0xAF 0x4F 0x47 ;; 0->A; 0->C; 0->B
|
rlm@404
|
135 0xC5 ;; save value of BC
|
rlm@404
|
136
|
rlm@404
|
137 0x3E
|
rlm@388
|
138 0x20 ; prepare to measure d-pad
|
rlm@384
|
139
|
rlm@388
|
140 0x01 ;\
|
rlm@388
|
141 0x01 ; |
|
rlm@388
|
142 0xFE ; | load 0xFF00 into BC without repeats
|
rlm@388
|
143 0x04 ; |
|
rlm@388
|
144 0x0D ;/
|
rlm@386
|
145
|
rlm@388
|
146 0x02
|
rlm@388
|
147 0x0A ;; get D-pad info
|
rlm@385
|
148
|
rlm@385
|
149 0xF5 ;; push AF
|
rlm@385
|
150
|
rlm@385
|
151 0x3E
|
rlm@388
|
152 0x10 ; prepare to measure buttons
|
rlm@388
|
153
|
rlm@388
|
154 0x3F ;; clear carry flag no-op to prevent repeated nybbbles
|
rlm@385
|
155
|
rlm@388
|
156 0x02
|
rlm@388
|
157 0x0A ;; get button info
|
rlm@385
|
158
|
rlm@384
|
159 0xE6 ;; select bottom bits of A
|
rlm@384
|
160 0x0F
|
rlm@384
|
161
|
rlm@388
|
162 0x47 ;; A->B
|
rlm@385
|
163
|
rlm@385
|
164 0xF1 ;; pop AF
|
rlm@385
|
165
|
rlm@385
|
166 0xE6
|
rlm@385
|
167 0x0F ;; select bottom bits of A
|
rlm@385
|
168
|
rlm@384
|
169 0xCB
|
rlm@384
|
170 0x37 ;; swap A nybbles
|
rlm@387
|
171
|
rlm@388
|
172 0xB0 ;; (or A B) -> A
|
rlm@387
|
173
|
rlm@384
|
174 0x2F ;; (NOT A) -> A
|
rlm@384
|
175 ])
|
rlm@384
|
176
|
rlm@384
|
177 (defn test-read-user-input []
|
rlm@384
|
178 (let [program
|
rlm@384
|
179 (concat
|
rlm@384
|
180 (frame-metronome) (read-user-input)
|
rlm@388
|
181 [0x5F ;; A-> E
|
rlm@404
|
182 0x76
|
rlm@384
|
183 0x18
|
rlm@384
|
184 (->signed-8-bit
|
rlm@404
|
185 (+ (- (count (read-user-input)))
|
rlm@404
|
186 (- 4)))])
|
rlm@384
|
187 read-input
|
rlm@384
|
188 (-> (tick (mid-game))
|
rlm@384
|
189 (IE! 0)
|
rlm@384
|
190 (set-memory-range pokemon-list-start program)
|
rlm@384
|
191 (PC! pokemon-list-start))]
|
rlm@384
|
192 (dorun
|
rlm@404
|
193 (for [i (range 0x100)]
|
rlm@404
|
194 (assert (= (E (step read-input (buttons i))) i))))
|
rlm@385
|
195 (println "Tested all inputs.")
|
rlm@384
|
196 read-input))
|
rlm@384
|
197
|
rlm@393
|
198 (def symbol-index
|
rlm@393
|
199 (fn [symbol sequence]
|
rlm@393
|
200 (count (take-while
|
rlm@393
|
201 (partial not= symbol)
|
rlm@393
|
202 sequence))))
|
rlm@385
|
203
|
rlm@403
|
204 (defn main-bootstrap-program
|
rlm@403
|
205 ([] (main-bootstrap-program pokemon-list-start))
|
rlm@403
|
206 ([start-address]
|
rlm@403
|
207 ;; Register Use:
|
rlm@403
|
208
|
rlm@403
|
209 ;; ED non-volitale scratch
|
rlm@403
|
210
|
rlm@403
|
211 ;; A user-input
|
rlm@403
|
212 ;; HL target-address
|
rlm@403
|
213 ;; B bytes-to-write
|
rlm@403
|
214 ;; C non-volatile scratch
|
rlm@385
|
215
|
rlm@403
|
216 ;; Modes (with codes) are:
|
rlm@390
|
217
|
rlm@403
|
218 ;; single-action-modes:
|
rlm@403
|
219 ;; SET-TARGET-HIGH 0x67 ;; A->H
|
rlm@403
|
220 ;; SET-TARGET-LOW 0x6F ;; A->L
|
rlm@403
|
221 ;; JUMP 0xE9 ;; jump to (HL)
|
rlm@390
|
222
|
rlm@403
|
223 ;; multi-action-modes
|
rlm@403
|
224 ;; WRITE 0x47 ;; A->B
|
rlm@390
|
225
|
rlm@404
|
226 (let [header (concat (frame-metronome) (read-user-input))
|
rlm@404
|
227
|
rlm@403
|
228 input
|
rlm@403
|
229 [0xC1 ;; pop BC so it's not volatile
|
rlm@388
|
230
|
rlm@403
|
231 0x5F ;; A->E
|
rlm@403
|
232 0xAF ;; test for output-mode (bytes-to-write > 0)
|
rlm@403
|
233 0xB8 ;; (cp A B)
|
rlm@403
|
234 0x7B ;; E->A
|
rlm@403
|
235 0x20 ;; skip to output section if
|
rlm@403
|
236 :to-output ;; we're not in input mode
|
rlm@403
|
237
|
rlm@403
|
238 :to-be-executed
|
rlm@391
|
239
|
rlm@403
|
240 ;; write mode to instruction-to-be-executed (pun)
|
rlm@403
|
241 0xEA
|
rlm@403
|
242 :to-be-executed-address
|
rlm@388
|
243
|
rlm@403
|
244 ;; protection region -- do not queue this op for
|
rlm@403
|
245 ;; execution if the last one was non-zero
|
rlm@403
|
246 0x79 ;; C->A
|
rlm@403
|
247 0xA7 ;; test A==0
|
rlm@403
|
248 0x28
|
rlm@403
|
249 0x04
|
rlm@403
|
250 0xAF ;; put a no op (0x00) in to-be-executed
|
rlm@403
|
251 0xEA ;;
|
rlm@403
|
252 :to-be-executed-address
|
rlm@403
|
253
|
rlm@403
|
254 0x7B ;; E->A
|
rlm@403
|
255 0x4F ;; A->C now C stores previous instruction
|
rlm@403
|
256 0x18 ;; return
|
rlm@404
|
257 :to-halt]
|
rlm@403
|
258
|
rlm@403
|
259 output
|
rlm@403
|
260 [:output-start ;; just a label
|
rlm@403
|
261 0x54 ;;
|
rlm@403
|
262 0x5D ;; HL->DE \
|
rlm@404
|
263 ;; | This mess is here to do
|
rlm@403
|
264 0x12 ;; A->(DE) | 0x22 (LDI (HL), A) without
|
rlm@404
|
265 ;; | any repeating nybbles
|
rlm@403
|
266 0x23 ;; inc HL /
|
rlm@398
|
267
|
rlm@403
|
268 0x05 ;; DEC bytes-to-write (B)
|
rlm@391
|
269
|
rlm@404
|
270 0x76 ;; HALT, peasant!
|
rlm@403
|
271 0x18
|
rlm@404
|
272 :to-beginning]
|
rlm@404
|
273
|
rlm@403
|
274 symbols
|
rlm@403
|
275 {:to-be-executed-address
|
rlm@403
|
276 (reverse
|
rlm@403
|
277 (disect-bytes-2
|
rlm@404
|
278 (+ start-address
|
rlm@404
|
279 (count header)
|
rlm@403
|
280 (symbol-index :to-be-executed input))))
|
rlm@403
|
281 :to-be-executed 0x00} ;; clear carry flag no-op
|
rlm@392
|
282
|
rlm@403
|
283 program** (flatten
|
rlm@404
|
284 (replace symbols (concat header input output)))
|
rlm@404
|
285
|
rlm@403
|
286 resolve-internal-jumps
|
rlm@403
|
287 {:output-start []
|
rlm@403
|
288 :to-output
|
rlm@403
|
289 (->signed-8-bit
|
rlm@403
|
290 (dec
|
rlm@403
|
291 (- (symbol-index :output-start program**)
|
rlm@403
|
292 (symbol-index :to-output program**))))}
|
rlm@391
|
293
|
rlm@403
|
294 program*
|
rlm@403
|
295 (flatten (replace resolve-internal-jumps program**))
|
rlm@403
|
296
|
rlm@403
|
297 resolve-external-jumps
|
rlm@404
|
298 {:to-halt
|
rlm@404
|
299 (- (- (symbol-index :to-beginning program*)
|
rlm@404
|
300 (symbol-index :to-halt program*)) 3)
|
rlm@404
|
301
|
rlm@404
|
302 :to-beginning
|
rlm@403
|
303 (->signed-8-bit
|
rlm@404
|
304 (+ 2 (count (frame-metronome))
|
rlm@404
|
305 (- (symbol-index :to-beginning program*))))}
|
rlm@389
|
306
|
rlm@403
|
307 program
|
rlm@403
|
308 (replace resolve-external-jumps program*)]
|
rlm@403
|
309 program)))
|
rlm@378
|
310
|
rlm@378
|
311
|
rlm@377
|
312 ;;;;;; TESTS ;;;;;;
|
rlm@377
|
313
|
rlm@401
|
314 (def set-H-mode 0x67)
|
rlm@401
|
315 (def set-L-mode 0x6F)
|
rlm@401
|
316 (def jump-mode 0xE9)
|
rlm@401
|
317 (def write-mode 0x47)
|
rlm@401
|
318
|
rlm@401
|
319
|
rlm@377
|
320 (defn bootstrap-base []
|
rlm@377
|
321 (let [program (main-bootstrap-program pokemon-list-start)]
|
rlm@377
|
322 ;; make sure program is valid output for item-writer
|
rlm@392
|
323 ;;(bootstrap-pattern program)
|
rlm@377
|
324 (-> (tick (mid-game))
|
rlm@377
|
325 (set-memory-range pokemon-list-start program)
|
rlm@400
|
326 (PC! pokemon-list-start)
|
rlm@400
|
327 (step [])
|
rlm@400
|
328 (step []))))
|
rlm@377
|
329
|
rlm@400
|
330 (defn test-set-H []
|
rlm@400
|
331 (letfn [(test-H [state n]
|
rlm@400
|
332 (let [after
|
rlm@400
|
333 (-> state
|
rlm@402
|
334 (step (buttons set-H-mode))
|
rlm@400
|
335 (step (buttons n))
|
rlm@400
|
336 (step []))]
|
rlm@401
|
337 ;;(println "desired H =" n "actual =" (H after))
|
rlm@400
|
338 (assert (= n (H after)))
|
rlm@400
|
339 after))]
|
rlm@403
|
340 (let [result (reduce test-H (bootstrap-base) (range 0x100))]
|
rlm@403
|
341 (println "tested all H values")
|
rlm@403
|
342 result)))
|
rlm@400
|
343
|
rlm@401
|
344 (defn test-write-bytes []
|
rlm@377
|
345 (let [target-address 0xC00F
|
rlm@377
|
346 [target-high target-low] (disect-bytes-2 target-address)
|
rlm@377
|
347 assembly [0xF3 0x18 0xFE 0x12]
|
rlm@377
|
348 get-mem-region #(subvec (vec (memory %))
|
rlm@377
|
349 target-address (+ target-address 20))
|
rlm@377
|
350 before (bootstrap-base)
|
rlm@377
|
351 after
|
rlm@377
|
352 (-> before
|
rlm@392
|
353 (step []) ; make sure it can handle blanks
|
rlm@392
|
354 (step []) ; at the beginning.
|
rlm@377
|
355 (step [])
|
rlm@402
|
356 (step (buttons set-H-mode)) ; select set-H
|
rlm@401
|
357 (step (buttons target-high))
|
rlm@401
|
358 (step [])
|
rlm@402
|
359 (step (buttons set-L-mode))
|
rlm@401
|
360 (step (buttons target-low))
|
rlm@401
|
361 (step [])
|
rlm@401
|
362 (step (buttons write-mode))
|
rlm@392
|
363 (step (buttons 4)) ; write 4 bytes
|
rlm@377
|
364 (step (buttons (nth assembly 0)))
|
rlm@377
|
365 (step (buttons (nth assembly 1)))
|
rlm@377
|
366 (step (buttons (nth assembly 2)))
|
rlm@377
|
367 (step (buttons (nth assembly 3)))
|
rlm@377
|
368 (step [])
|
rlm@377
|
369 (step [])
|
rlm@377
|
370 (step []))]
|
rlm@377
|
371 (println "before :" (get-mem-region before))
|
rlm@377
|
372 (println "after :" (get-mem-region after))
|
rlm@402
|
373 (assert (= assembly (take 4 (get-mem-region after))))
|
rlm@377
|
374 after))
|
rlm@377
|
375
|
rlm@402
|
376 (defn test-jump []
|
rlm@377
|
377 (let [target-address 0xC00F
|
rlm@377
|
378 [target-high target-low] (disect-bytes-2 target-address)
|
rlm@377
|
379 post-jump
|
rlm@402
|
380 (-> (test-write-bytes)
|
rlm@402
|
381 (step (buttons set-H-mode)) ; select set-H
|
rlm@402
|
382 (step (buttons target-high))
|
rlm@377
|
383 (step [])
|
rlm@402
|
384 (step (buttons set-L-mode))
|
rlm@402
|
385 (step (buttons target-low))
|
rlm@377
|
386 (step [])
|
rlm@402
|
387 (step (buttons jump-mode))) ; Select JUMP mode.
|
rlm@377
|
388 program-counters
|
rlm@377
|
389 (capture-program-counter
|
rlm@377
|
390 post-jump
|
rlm@377
|
391 10000)]
|
rlm@377
|
392 (assert (contains? (set program-counters) target-address))
|
rlm@402
|
393 (println "jump test passed")
|
rlm@377
|
394 post-jump))
|
rlm@402
|
395
|
rlm@402
|
396
|
rlm@402
|
397 (defn run-all-tests []
|
rlm@402
|
398 (test-frame-metronome)
|
rlm@402
|
399 (test-read-user-input)
|
rlm@402
|
400 (test-set-H)
|
rlm@402
|
401 (test-write-bytes)
|
rlm@402
|
402 (test-jump))
|