view clojure/com/aurellem/gb/rlm_assembly.clj @ 386:d8cbbf2a3133

changed scratch ragisters from BC to DE.
author Robert McIntyre <rlm@mit.edu>
date Thu, 12 Apr 2012 06:27:03 -0500
parents 3f3cfc89be91
children 47d44bb54d32
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 0x47 ;; A->B
149 0xF1 ;; pop AF
151 0xE6
152 0x0F ;; select bottom bits of A
154 0xCB
155 0x37 ;; swap A nybbles
157 0xB0 ;; (AND A B) -> A
159 0x2F ;; (NOT A) -> A
160 ])
162 (defn test-read-user-input []
163 (let [program
164 (concat
165 (frame-metronome) (read-user-input)
166 [0x47 ;; A->B
167 0x18
168 (->signed-8-bit
169 (+ (- (count (frame-metronome)))
170 (- (count (read-user-input)))
171 (- 3)))])
172 read-input
173 (-> (tick (mid-game))
174 (IE! 0)
175 (set-memory-range pokemon-list-start program)
176 (PC! pokemon-list-start))]
177 (dorun
178 (for [i (range 0x100)]
179 (assert (= (B (step read-input (buttons i))) i))))
180 (println "Tested all inputs.")
181 read-input))
183 (def mode-select-mode (Integer/parseInt "00000000" 2))
184 (def write-mode (Integer/parseInt "00000001" 2))
185 (def jump-mode (Integer/parseInt "10000000" 2))
187 (def input-high-addr-jump (Integer/parseInt "10000010" 2))
188 (def input-high-addr-write (Integer/parseInt "00000010" 2))
189 (def input-low-addr-jump (Integer/parseInt "10000100" 2))
190 (def input-low-addr-write (Integer/parseInt "00000100" 2))
192 (def input-write-num-mode (Integer/parseInt "00001000" 2))
193 (def do-write-mode (Integer/parseInt "00010000" 2))
201 (defn main-bootstrap-program [start-address]
202 (let [[start-high start-low] (disect-bytes-2 start-address)
203 jump-distance (+ (count (frame-metronome)
204 (read-user-input)))
206 init
207 [0x1E 0xFF 0x1C] ;; 0-> E without repeats
210 ;; HL -> BC
211 ;; HL = here
212 ;; add E to HL
213 ;; jp HL
215 mode-dispatch
216 [0x44
220 ;;(here) jr metronome
222 ;;stuff
223 ;;modify E
224 ;;jr metronome
226 ;;stuff
227 ;;jr metronome
235 mode-dispatch
236 [0xCB
237 0x43 ;; test bit 0
239 0xCB
240 0x4B ;; test bit 1
242 0xCB
243 0x53 ;; test bit 2
245 0xCB
246 0x5B ;; test bit 3
248 0xCB
249 0x63 ;; test bit 4
251 0xCB
252 0x6B ;; test bit 5
254 ];;default
257 mode-select
258 [0x5F] ;; A->E
260 ;; delayed inputs
261 input-high 0x67 ;; A->H
262 input-low 0x6F ;; A->L
263 input-num 0x57 ;; A->D
265 ;; final-actions
266 jump 0xE9 ;; jp (HL)
267 write 0x22 ;; A->(HL); inc HL
268 input-mode 0x00 ;; no-op
272 input-high
273 [0x67 ;; A->H
274 0x1E ;; change mode
275 input-low-mode]
280 mode-dispatch
288 ))
299 ;;;;;; TESTS ;;;;;;
301 (defn bootstrap-base []
302 (let [program (main-bootstrap-program pokemon-list-start)]
303 ;; make sure program is valid output for item-writer
304 (bootstrap-pattern program)
305 (-> (tick (mid-game))
306 (set-memory-range pokemon-list-start program)
307 (PC! pokemon-list-start))))
309 (defn test-write-bytes-mode []
310 (let [target-address 0xC00F
311 [target-high target-low] (disect-bytes-2 target-address)
312 assembly [0xF3 0x18 0xFE 0x12]
313 get-mem-region #(subvec (vec (memory %))
314 target-address (+ target-address 20))
315 before (bootstrap-base)
316 after
317 (-> before
318 (step []) ; make sure it can handle blanks
319 (step []) ; at the beginning.
320 (step [])
321 (step [:start]) ; select WRITE-BYTES mode
322 (step (buttons 4)) ; write 4 bytes
323 (step (buttons target-high))
324 (step (buttons target-low))
325 (step (buttons (nth assembly 0)))
326 (step (buttons (nth assembly 1)))
327 (step (buttons (nth assembly 2)))
328 (step (buttons (nth assembly 3)))
329 (step [])
330 (step [])
331 (step []))]
332 (println "before :" (get-mem-region before))
333 (println "after :" (get-mem-region after))
334 (assert (= assembly (take 4 (get-mem-region after))))
335 after))
337 (defn test-jump-mode []
338 (let [target-address 0xC00F
339 [target-high target-low] (disect-bytes-2 target-address)
340 post-jump
341 (-> (test-write-bytes-mode)
342 (step [])
343 (step [])
344 (step [])
345 (step (buttons 0xFF)) ; Select JUMP mode.
346 (step (buttons target-high))
347 (step (buttons target-low)))
348 program-counters
349 (capture-program-counter
350 post-jump
351 10000)]
352 (println program-counters)
353 (assert (contains? (set program-counters) target-address))
354 post-jump))