view clojure/com/aurellem/gb/rlm_assembly.clj @ 385:3f3cfc89be91

working on mode-dispatch
author Robert McIntyre <rlm@mit.edu>
date Thu, 12 Apr 2012 06:20:17 -0500
parents 8013915a07b3
children d8cbbf2a3133
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)
74 continue-if-144
75 [0xFE
76 144 ;; compare LY (in A) with 144
77 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
78 (->signed-8-bit
79 (+ -4 (- (count timing-loop))))]
80 spin-loop
81 [0x05 ;; dec B, which is 0xFF
82 0x20 ;; spin until B==0
83 0xFD]]
84 (concat timing-loop continue-if-144 spin-loop)))
86 (defn test-frame-metronome
87 "Ensure that frame-metronome ticks exactly once every frame."
88 ([] (test-frame-metronome 151))
89 ([steps]
90 (let [inc-D [0x14 0x18
91 (->signed-8-bit
92 (+ -3 (- (count (frame-metronome)))))]
93 program (concat (frame-metronome) inc-D)
94 count-frames
95 (-> (tick (mid-game))
96 (IE! 0)
97 (DE! 0)
98 (set-memory-range pokemon-list-start program)
99 (PC! pokemon-list-start))
100 D-after-moves (D (run-moves count-frames (repeat steps [])))]
101 (println "D:" D-after-moves)
102 (assert (= steps D-after-moves))
104 (println "D =" D-after-moves "after" steps "steps")
105 count-frames)))
107 (defn read-user-input []
108 [0x01 ;\
109 0x01 ;|
110 0xFE ;| load 0xFF00 into BC without repeats
111 0x04 ;|
112 0x0D ;/
114 0x3E
115 (Integer/parseInt "00100000" 2) ; prepare to measure d-pad
117 0x02
118 0x0A ;; get D-pad info
120 0xF5 ;; push AF
122 0x3E
123 (Integer/parseInt "00010000" 2) ; prepare to measure buttons
125 0x02
126 0x0A ;; get button info
129 0xE6 ;; select bottom bits of A
130 0x0F
132 0x47 ;; A->B
134 0xF1 ;; pop AF
136 0xE6
137 0x0F ;; select bottom bits of A
139 0xCB
140 0x37 ;; swap A nybbles
142 0xB0 ;; (AND A B) -> A
144 0x2F ;; (NOT A) -> A
145 ])
147 (defn test-read-user-input []
148 (let [program
149 (concat
150 (frame-metronome) (read-user-input)
151 [0x5F ;; A-> E
152 0x18
153 (->signed-8-bit
154 (+ (- (count (frame-metronome)))
155 (- (count (read-user-input)))
156 (- 3)))])
157 read-input
158 (-> (tick (mid-game))
159 (IE! 0)
160 (set-memory-range pokemon-list-start program)
161 (PC! pokemon-list-start))]
162 (dorun
163 (for [i (range 0x100)]
164 (assert (= (E (step read-input (buttons i))) i))))
165 (println "Tested all inputs.")
166 read-input))
168 (def mode-select-mode (Integer/parseInt "00000000" 2))
169 (def write-mode (Integer/parseInt "00000001" 2))
170 (def jump-mode (Integer/parseInt "10000000" 2))
172 (def input-high-addr-jump (Integer/parseInt "10000010" 2))
173 (def input-high-addr-write (Integer/parseInt "00000010" 2))
174 (def input-low-addr-jump (Integer/parseInt "10000100" 2))
175 (def input-low-addr-write (Integer/parseInt "00000100" 2))
177 (def input-write-num-mode (Integer/parseInt "00001000" 2))
178 (def do-write-mode (Integer/parseInt "00010000" 2))
186 (defn main-bootstrap-program [start-address]
187 (let [[start-high start-low] (disect-bytes-2 start-address)
188 jump-distance (+ (count (frame-metronome)
189 (read-user-input)))
191 init
192 [0x1E 0xFF 0x1C] ;; 0-> E without repeats
195 ;; HL -> BC
196 ;; HL = here
197 ;; add E to HL
198 ;; jp HL
200 mode-dispatch
201 [0x44
205 ;;(here) jr metronome
207 ;;stuff
208 ;;modify E
209 ;;jr metronome
211 ;;stuff
212 ;;jr metronome
220 mode-dispatch
221 [0xCB
222 0x43 ;; test bit 0
224 0xCB
225 0x4B ;; test bit 1
227 0xCB
228 0x53 ;; test bit 2
230 0xCB
231 0x5B ;; test bit 3
233 0xCB
234 0x63 ;; test bit 4
236 0xCB
237 0x6B ;; test bit 5
239 ];;default
242 mode-select
243 [0x5F] ;; A->E
245 ;; delayed inputs
246 input-high 0x67 ;; A->H
247 input-low 0x6F ;; A->L
248 input-num 0x57 ;; A->D
250 ;; final-actions
251 jump 0xE9 ;; jp (HL)
252 write 0x22 ;; A->(HL); inc HL
253 input-mode 0x00 ;; no-op
257 input-high
258 [0x67 ;; A->H
259 0x1E ;; change mode
260 input-low-mode]
265 mode-dispatch
273 ))
284 ;;;;;; TESTS ;;;;;;
286 (defn bootstrap-base []
287 (let [program (main-bootstrap-program pokemon-list-start)]
288 ;; make sure program is valid output for item-writer
289 (bootstrap-pattern program)
290 (-> (tick (mid-game))
291 (set-memory-range pokemon-list-start program)
292 (PC! pokemon-list-start))))
294 (defn test-write-bytes-mode []
295 (let [target-address 0xC00F
296 [target-high target-low] (disect-bytes-2 target-address)
297 assembly [0xF3 0x18 0xFE 0x12]
298 get-mem-region #(subvec (vec (memory %))
299 target-address (+ target-address 20))
300 before (bootstrap-base)
301 after
302 (-> before
303 (step []) ; make sure it can handle blanks
304 (step []) ; at the beginning.
305 (step [])
306 (step [:start]) ; select WRITE-BYTES mode
307 (step (buttons 4)) ; write 4 bytes
308 (step (buttons target-high))
309 (step (buttons target-low))
310 (step (buttons (nth assembly 0)))
311 (step (buttons (nth assembly 1)))
312 (step (buttons (nth assembly 2)))
313 (step (buttons (nth assembly 3)))
314 (step [])
315 (step [])
316 (step []))]
317 (println "before :" (get-mem-region before))
318 (println "after :" (get-mem-region after))
319 (assert (= assembly (take 4 (get-mem-region after))))
320 after))
322 (defn test-jump-mode []
323 (let [target-address 0xC00F
324 [target-high target-low] (disect-bytes-2 target-address)
325 post-jump
326 (-> (test-write-bytes-mode)
327 (step [])
328 (step [])
329 (step [])
330 (step (buttons 0xFF)) ; Select JUMP mode.
331 (step (buttons target-high))
332 (step (buttons target-low)))
333 program-counters
334 (capture-program-counter
335 post-jump
336 10000)]
337 (println program-counters)
338 (assert (contains? (set program-counters) target-address))
339 post-jump))