view clojure/com/aurellem/gb/rlm_assembly.clj @ 404:41647cb85901

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