rlm@105
|
1 (ns com.aurellem.assembly
|
rlm@105
|
2 (:use (com.aurellem gb-driver vbm title items))
|
rlm@105
|
3 (:import [com.aurellem.gb_driver SaveState]))
|
rlm@105
|
4
|
rlm@106
|
5 (defn mid-game []
|
rlm@106
|
6 (read-state "mid-game"))
|
rlm@105
|
7
|
rlm@105
|
8 (defn inject-assembly
|
rlm@107
|
9 ([^SaveState state
|
rlm@105
|
10 program-counter registers
|
rlm@105
|
11 assembly-code]
|
rlm@105
|
12 (let [scratch-memory (memory state)]
|
rlm@105
|
13 ;; inject assembly code
|
rlm@105
|
14 (dorun (map (fn [index val]
|
rlm@105
|
15 (aset scratch-memory index val))
|
rlm@105
|
16 (range program-counter
|
rlm@105
|
17 (+ program-counter (count assembly-code)))
|
rlm@105
|
18 assembly-code))
|
rlm@106
|
19 (-> state
|
rlm@106
|
20 (write-memory! scratch-memory)
|
rlm@106
|
21 (write-registers! registers)
|
rlm@112
|
22 (PC! program-counter)))))
|
rlm@105
|
23
|
rlm@105
|
24 (defn inject-item-assembly
|
rlm@105
|
25 ([^SaveState state assembly-code]
|
rlm@105
|
26 (inject-assembly state (inc item-list-start)
|
rlm@105
|
27 (registers state)
|
rlm@105
|
28 assembly-code))
|
rlm@105
|
29 ([assembly-code]
|
rlm@105
|
30 (inject-item-assembly @current-state assembly-code)))
|
rlm@105
|
31
|
rlm@105
|
32 (defn info
|
rlm@105
|
33 ([^SaveState state]
|
rlm@107
|
34 (println (format "PC: 0x%04X" (PC state)))
|
rlm@106
|
35 (println "Instruction:"
|
rlm@106
|
36 (format "0x%02X" (aget (memory state) (PC state))))
|
rlm@105
|
37 state))
|
rlm@105
|
38
|
rlm@107
|
39 (defn print-interrupt
|
rlm@107
|
40 [^SaveState state]
|
rlm@107
|
41 (println (format "IE: %d" (IE state)))
|
rlm@107
|
42 state)
|
rlm@107
|
43
|
rlm@118
|
44 (defn print-listing [state begin end]
|
rlm@118
|
45 (dorun (map
|
rlm@118
|
46 (fn [opcode line]
|
rlm@118
|
47 (println (format "0x%04X: 0x%02X" line opcode)))
|
rlm@118
|
48 (subvec (vec (memory state)) begin end)
|
rlm@118
|
49 (range begin end)))
|
rlm@118
|
50 state)
|
rlm@118
|
51
|
rlm@105
|
52 (defn run-assembly
|
rlm@105
|
53 ([info-fn assembly n]
|
rlm@105
|
54 (let [final-state
|
rlm@105
|
55 (reduce (fn [state _]
|
rlm@105
|
56 (tick (info-fn state)))
|
rlm@107
|
57 (inject-item-assembly
|
rlm@107
|
58 (mid-game) assembly)
|
rlm@105
|
59 (range n))]
|
rlm@105
|
60 final-state))
|
rlm@105
|
61 ([assembly n]
|
rlm@105
|
62 (run-assembly info assembly n)))
|
rlm@107
|
63
|
rlm@107
|
64 (def buttons-port 0xFF00)
|
rlm@107
|
65
|
rlm@107
|
66 (defn A [state]
|
rlm@107
|
67 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
|
rlm@107
|
68
|
rlm@110
|
69 (defn binary-str [num]
|
rlm@110
|
70 (format "%08d"
|
rlm@110
|
71 (Integer/parseInt
|
rlm@110
|
72 (Integer/toBinaryString num) 10)))
|
rlm@110
|
73
|
rlm@110
|
74 (defn view-register [state name reg-fn]
|
rlm@110
|
75 (println (format "%s: %s" name
|
rlm@110
|
76 (binary-str (reg-fn state))))
|
rlm@110
|
77 state)
|
rlm@110
|
78
|
rlm@107
|
79 (defn view-memory [state mem]
|
rlm@110
|
80 (println (format "mem 0x%04X = %s" mem
|
rlm@110
|
81 (binary-str (aget (memory state) mem))))
|
rlm@107
|
82 state)
|
rlm@107
|
83
|
rlm@118
|
84 (defn trace [state]
|
rlm@118
|
85 (loop [program-counters []
|
rlm@118
|
86 opcodes []]
|
rlm@118
|
87 (let [frame-boundary?
|
rlm@118
|
88 (com.aurellem.gb.Gb/tick)]
|
rlm@118
|
89 (println (count opcodes))
|
rlm@118
|
90 (if frame-boundary?
|
rlm@118
|
91 [program-counters opcodes]
|
rlm@118
|
92 (recur
|
rlm@118
|
93 (conj program-counters
|
rlm@118
|
94 (first (registers @current-state)))
|
rlm@118
|
95 (conj opcodes
|
rlm@118
|
96 (aget (memory @current-state)
|
rlm@118
|
97 (PC @current-state))))))))
|
rlm@118
|
98
|
rlm@118
|
99 (defn good-trace []
|
rlm@118
|
100 (-> (mid-game) (tick) (IE! 0)
|
rlm@118
|
101 (set-inv-mem [0x00 0x00 0X00 0x00])
|
rlm@118
|
102 (PC! item-list-start)(print-interrupt)
|
rlm@118
|
103 (info) (tick) (info) (tick) (info)))
|
rlm@118
|
104
|
rlm@113
|
105 (defn read-down-button []
|
rlm@110
|
106 (-> (tick (mid-game))
|
rlm@110
|
107 (IE! 0) ; disable interrupts
|
rlm@110
|
108 (inject-item-assembly
|
rlm@118
|
109 ;; write 00010000 to 0xFF00 to select joypad
|
rlm@118
|
110 [0x18 ;D31D ; jump over
|
rlm@118
|
111 0x01 ;D31E ; the next 8 bits
|
rlm@118
|
112 ;D31F
|
rlm@118
|
113 (Integer/parseInt "00100000" 2) ; data section
|
rlm@118
|
114
|
rlm@118
|
115 0xFA ;D320 ; load (D31F) into A
|
rlm@118
|
116 0x1F ;D321 -->
|
rlm@118
|
117 0xD3 ;D322 --> D31F
|
rlm@107
|
118
|
rlm@118
|
119 0xEA ;D323 ; load (A), which is
|
rlm@118
|
120 0x00 ;D324 --> ; 00010000, into FF00
|
rlm@118
|
121 0xFF ;D325 --> FF00
|
rlm@118
|
122
|
rlm@118
|
123 0x18 ;D326 ; this is the place where
|
rlm@118
|
124 0x01 ;D327 ; we will store whether
|
rlm@118
|
125 0x00 ;D328 ; "down" is pressed.
|
rlm@107
|
126
|
rlm@118
|
127 0xFA ;D329 ; (FF00) -> A
|
rlm@118
|
128 0x00 ;D32A
|
rlm@118
|
129 0xFF ;D32B
|
rlm@110
|
130
|
rlm@118
|
131 0xCB ;D32C ; Test whether "down"
|
rlm@118
|
132 0x5F ;D32D ; is pressed.
|
rlm@111
|
133
|
rlm@118
|
134 0x28 ;D32E ; if down is pressed,
|
rlm@118
|
135 0x03 ;D32F ; skip the next section
|
rlm@118
|
136 ; of code.
|
rlm@118
|
137 ;; down-is-not-pressed
|
rlm@118
|
138 0xC3 ;D330
|
rlm@118
|
139 0x1D ;D331 ; return to beginning
|
rlm@118
|
140 0xD3 ;D332
|
rlm@118
|
141
|
rlm@118
|
142 ;; down-is-pressed
|
rlm@118
|
143 0xEA ;D334 ; write A to D328 if
|
rlm@118
|
144 0x28 ;D335 ; "down" was pressed
|
rlm@118
|
145 0xD3 ;D336
|
rlm@111
|
146
|
rlm@118
|
147 0xC3 ;D330
|
rlm@118
|
148 0x1D ;D331 ; return to beginning
|
rlm@118
|
149 0xD3 ;D332
|
rlm@118
|
150 ])))
|
rlm@111
|
151
|
rlm@118
|
152 (defn test-read-down []
|
rlm@118
|
153 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)
|
rlm@118
|
154 (view-memory (step (step (read-down-button))) 0xD328)))
|
rlm@113
|
155
|
rlm@113
|
156 (defn count-frames []
|
rlm@113
|
157 (-> (tick (mid-game))
|
rlm@113
|
158 (IE! 0) ; disable interrupts
|
rlm@113
|
159 (inject-item-assembly
|
rlm@113
|
160 [0x18 ;D31D ; jump over
|
rlm@113
|
161 0x02 ;D31E ; the next 2 bytes
|
rlm@113
|
162 0x00 ;D31F ; frame-count
|
rlm@113
|
163 0x00 ;D320 ; v-blank-prev
|
rlm@113
|
164
|
rlm@113
|
165 0xFA ;D321
|
rlm@113
|
166 0x41 ;D322 ; load (FF41) into A
|
rlm@113
|
167 0xFF ;D323 ; this contains mode flags
|
rlm@113
|
168
|
rlm@113
|
169 ;; if we're in v-blank, the bit-1 is 0
|
rlm@113
|
170 ;; and bit-2 is 1 Otherwise, it is not v-blank.
|
rlm@113
|
171 0xCB ;D324 ; test bit-1 of A
|
rlm@113
|
172 0x4F ;D325
|
rlm@113
|
173
|
rlm@113
|
174 0xC2 ;D326 ; if bit-1 is not 0
|
rlm@115
|
175 0x44 ;D327 ; GOTO not-v-blank
|
rlm@113
|
176 0xD3 ;D328
|
rlm@113
|
177
|
rlm@113
|
178 0xCB ;D329 ; test bit-0 of A
|
rlm@113
|
179 0x47 ;D32A
|
rlm@113
|
180
|
rlm@113
|
181 0xCA ;D32B ; if bit-0 is not 1
|
rlm@115
|
182 0x44 ;D32C ; GOTO not-v-blank
|
rlm@113
|
183 0xD3 ;D32D
|
rlm@114
|
184 ;;; in v-blank mode
|
rlm@113
|
185 ;; if v-blank-prev was 0,
|
rlm@113
|
186 ;; increment frame-count
|
rlm@113
|
187
|
rlm@113
|
188 0xFA ;D32E ; load v-blank-prev to A
|
rlm@113
|
189 0x20 ;D32F
|
rlm@113
|
190 0xD3 ;D330
|
rlm@113
|
191
|
rlm@113
|
192 0xCB ;D331
|
rlm@113
|
193 0x47 ;D332 ; test bit-0 of A
|
rlm@113
|
194
|
rlm@113
|
195 0x20 ;D333 ; skip next section
|
rlm@113
|
196 0x07 ;D334 ; if v-blank-prev was not zero
|
rlm@113
|
197
|
rlm@113
|
198 ;; v-blank was 0, increment frame-count
|
rlm@113
|
199 0xFA ;D335 ; load frame-count into A
|
rlm@113
|
200 0x1F ;D336
|
rlm@113
|
201 0xD3 ;D337
|
rlm@113
|
202
|
rlm@113
|
203 0x3C ;D338 ; inc A
|
rlm@113
|
204
|
rlm@113
|
205 0xEA ;D339 ; load A into frame-count
|
rlm@113
|
206 0x1F ;D33A
|
rlm@113
|
207 0xD3 ;D33B
|
rlm@113
|
208
|
rlm@114
|
209 ;; set v-blank-prev to 1
|
rlm@113
|
210 0x3E ;D33C ; load 1 into A
|
rlm@113
|
211 0x01 ;D33D
|
rlm@113
|
212
|
rlm@113
|
213 0xEA ;D33E ; load A into v-blank-prev
|
rlm@113
|
214 0x20 ;D33F
|
rlm@113
|
215 0xD3 ;D340
|
rlm@113
|
216
|
rlm@115
|
217 0xC3 ;D341 ; return to beginning
|
rlm@115
|
218 0x1D ;D342
|
rlm@115
|
219 0xD3 ;D343
|
rlm@113
|
220
|
rlm@114
|
221 ;;; not in v-blank mode
|
rlm@114
|
222 ;; set v-blank-prev to 0
|
rlm@115
|
223 0x3E ;D344 ; load 0 into A
|
rlm@115
|
224 0x00 ;D345
|
rlm@113
|
225
|
rlm@115
|
226 0xEA ;D346 ; load A into v-blank-prev
|
rlm@115
|
227 0x20 ;D347
|
rlm@115
|
228 0xD3 ;D348
|
rlm@115
|
229
|
rlm@115
|
230 0xC3 ;D349 ; return to beginning
|
rlm@115
|
231 0x1D ;D34A
|
rlm@115
|
232 0xD3 ;D34B
|
rlm@113
|
233 ])))
|
rlm@113
|
234
|
rlm@114
|
235 (defn step-count-frames []
|
rlm@113
|
236 (-> (read-down-button)
|
rlm@110
|
237 (info)
|
rlm@110
|
238 (tick) ;; skip over data section
|
rlm@110
|
239 (info)
|
rlm@110
|
240 (view-register "Register A" A)
|
rlm@110
|
241 (tick) ;; load-data into A
|
rlm@110
|
242 (view-register "Register A" A)
|
rlm@110
|
243 (info)
|
rlm@110
|
244 (view-memory 0xFF00)
|
rlm@110
|
245 (tick) ;; load A into 0xFF00
|
rlm@110
|
246 (view-memory 0xFF00)
|
rlm@111
|
247 (info)
|
rlm@111
|
248 (tick)
|
rlm@111
|
249 (info)
|
rlm@111
|
250 (tick)
|
rlm@111
|
251 (info)
|
rlm@111
|
252 (tick)
|
rlm@111
|
253 (info)
|
rlm@111
|
254 (tick)
|
rlm@111
|
255 (info)
|
rlm@111
|
256 (tick)
|
rlm@111
|
257 (info)
|
rlm@111
|
258 (tick)
|
rlm@111
|
259 (print-inventory)))
|
rlm@111
|
260
|
rlm@115
|
261 (defn test-count-frames []
|
rlm@115
|
262 (= 255 (aget (memory ((apply comp (repeat 255 step))
|
rlm@115
|
263 (count-frames)))
|
rlm@115
|
264 0xD31F)))
|
rlm@115
|
265
|
rlm@115
|
266 ;; specs for main bootstrap program
|
rlm@115
|
267 ;; starts in "mode-select" mode
|
rlm@115
|
268 ;; Each button press takes place in a single frame.
|
rlm@115
|
269 ;; mode-select-mode takes one of the main buttons
|
rlm@115
|
270 ;; which selects one of up to eight modes
|
rlm@115
|
271 ;; mode 1 activated by the "A" button
|
rlm@115
|
272 ;; the next two button presses indicates the start
|
rlm@115
|
273 ;; memory location which to which the bootstrap
|
rlm@115
|
274 ;; program will write.
|
rlm@115
|
275 ;; This is done by using each of the eight buttons to
|
rlm@115
|
276 ;; spell out an 8 bit number. The order of buttons is
|
rlm@116
|
277 ;; [:d :u :l :r :start :select :b :a]
|
rlm@116
|
278 ;; [:a :start :l] --> 00101001
|
rlm@114
|
279
|
rlm@115
|
280 ;; the next button press determines how many bytes are to be
|
rlm@115
|
281 ;; written, starting at the start position.
|
rlm@114
|
282
|
rlm@115
|
283 ;; then, the actual bytes are entered and are written to the
|
rlm@115
|
284 ;; start address in sequence.
|
rlm@114
|
285
|
rlm@118
|
286 (defn input-number-assembly []
|
rlm@118
|
287 [0x18 ;D31D ; jump over
|
rlm@118
|
288 0x02 ;D31E ; the next 2 bytes
|
rlm@118
|
289 0x00 ;D31F ; frame-count
|
rlm@118
|
290 0x00 ;D320 ; v-blank-prev
|
rlm@118
|
291
|
rlm@118
|
292 0xFA ;D321
|
rlm@118
|
293 0x41 ;D322 ; load (FF41) into A
|
rlm@118
|
294 0xFF ;D323 ; this contains mode flags
|
rlm@118
|
295
|
rlm@118
|
296 ;; if we're in v-blank, the bit-1 is 0
|
rlm@118
|
297 ;; and bit-2 is 1 Otherwise, it is not v-blank.
|
rlm@118
|
298 0xCB ;D324 ; test bit-1 of A
|
rlm@118
|
299 0x4F ;D325
|
rlm@114
|
300
|
rlm@118
|
301 0xC2 ;D326 ; if bit-1 is not 0
|
rlm@118
|
302 0x44 ;D327 ; GOTO not-v-blank
|
rlm@118
|
303 0xD3 ;D328
|
rlm@118
|
304
|
rlm@118
|
305 0xCB ;D329 ; test bit-0 of A
|
rlm@118
|
306 0x47 ;D32A
|
rlm@118
|
307
|
rlm@118
|
308 0xCA ;D32B ; if bit-0 is not 1
|
rlm@118
|
309 0x44 ;D32C ; GOTO not-v-blank
|
rlm@118
|
310 0xD3 ;D32D
|
rlm@118
|
311
|
rlm@118
|
312 ;;; in v-blank mode
|
rlm@118
|
313
|
rlm@118
|
314 ;; if v-blank-prev was 0,
|
rlm@118
|
315 ;; increment frame-count
|
rlm@118
|
316
|
rlm@118
|
317 0xFA ;D32E ; load v-blank-prev to A
|
rlm@118
|
318 0x20 ;D32F
|
rlm@118
|
319 0xD3 ;D330
|
rlm@118
|
320
|
rlm@118
|
321 0xCB ;D331
|
rlm@118
|
322 0x47 ;D332 ; test bit-0 of A
|
rlm@118
|
323
|
rlm@118
|
324 0x20 ;D333 ; skip next section
|
rlm@118
|
325 0x07 ;D334 ; if v-blank-prev was not zero
|
rlm@118
|
326
|
rlm@118
|
327 ;; v-blank was 0, increment frame-count
|
rlm@118
|
328 0xFA ;D335 ; load frame-count into A
|
rlm@118
|
329 0x1F ;D336
|
rlm@118
|
330 0xD3 ;D337
|
rlm@118
|
331
|
rlm@118
|
332 0x3C ;D338 ; inc A
|
rlm@118
|
333
|
rlm@118
|
334 0xEA ;D339 ; load A into frame-count
|
rlm@118
|
335 0x1F ;D33A
|
rlm@118
|
336 0xD3 ;D33B
|
rlm@118
|
337
|
rlm@118
|
338 ;; set v-blank-prev to 1
|
rlm@118
|
339 0x3E ;D33C ; load 1 into A
|
rlm@118
|
340 0x01 ;D33D
|
rlm@118
|
341
|
rlm@118
|
342 0xEA ;D33E ; load A into v-blank-prev
|
rlm@118
|
343 0x20 ;D33F
|
rlm@118
|
344 0xD3 ;D340
|
rlm@118
|
345
|
rlm@118
|
346 0xC3 ;D341 ; GOTO input handling code
|
rlm@118
|
347 0x4E ;D342
|
rlm@118
|
348 0xD3 ;D343
|
rlm@118
|
349
|
rlm@118
|
350 ;;; not in v-blank mode
|
rlm@118
|
351 ;; set v-blank-prev to 0
|
rlm@118
|
352 0x3E ;D344 ; load 0 into A
|
rlm@118
|
353 0x00 ;D345
|
rlm@118
|
354
|
rlm@118
|
355 0xEA ;D346 ; load A into v-blank-prev
|
rlm@118
|
356 0x20 ;D347
|
rlm@118
|
357 0xD3 ;D348
|
rlm@118
|
358
|
rlm@118
|
359 0xC3 ;D349 ; return to beginning
|
rlm@118
|
360 0x1D ;D34A
|
rlm@118
|
361 0xD3 ;D34B
|
rlm@118
|
362
|
rlm@118
|
363 0x00 ;D34C ; these are here
|
rlm@118
|
364 0x00 ;D34D ; for glue
|
rlm@118
|
365
|
rlm@118
|
366
|
rlm@118
|
367 ;;; calculate input number based on button presses
|
rlm@118
|
368 0x18 ;D34E ; skip next 3 bytes
|
rlm@118
|
369 0x03 ;D34F
|
rlm@118
|
370 ;D350
|
rlm@118
|
371 (Integer/parseInt "00100000" 2) ; select directional pad
|
rlm@118
|
372 ;D351
|
rlm@118
|
373 (Integer/parseInt "00010000" 2) ; select buttons
|
rlm@118
|
374 0x00 ;D352 ; input-number
|
rlm@118
|
375
|
rlm@118
|
376 ;; select directional pad, store low bits in B
|
rlm@118
|
377
|
rlm@118
|
378 0xFA ;D353 ; load (D350) into A
|
rlm@118
|
379 0x50 ;D354 -->
|
rlm@118
|
380 0xD3 ;D355 --> D31F
|
rlm@118
|
381
|
rlm@118
|
382 0xEA ;D356 ; load (A), which is
|
rlm@118
|
383 0x00 ;D357 --> ; 00010000, into FF00
|
rlm@118
|
384 0xFF ;D358 --> FF00
|
rlm@118
|
385
|
rlm@118
|
386 0x06 ;D359
|
rlm@118
|
387 ;D35A
|
rlm@118
|
388 (Integer/parseInt "11110000" 2) ; "11110000" -> B
|
rlm@118
|
389 0xFA ;D35B ; (FF00) -> A
|
rlm@118
|
390 0x00 ;D35C
|
rlm@118
|
391 0xFF ;D35D
|
rlm@118
|
392
|
rlm@118
|
393 0xCB ;D35E ; swap nybbles on A
|
rlm@118
|
394 0x37 ;D35F
|
rlm@118
|
395 0xA0 ;D360 ; (AND A B) -> A
|
rlm@118
|
396 0x47 ;D361 ; A -> B
|
rlm@118
|
397
|
rlm@118
|
398 ;; select buttons store bottom bits in C
|
rlm@118
|
399
|
rlm@118
|
400 0xFA ; ; load (D351) into A
|
rlm@118
|
401 0x51 ; -->
|
rlm@118
|
402 0xD3 ; --> D31F
|
rlm@118
|
403
|
rlm@118
|
404 0xEA ; ; load (A), which is
|
rlm@118
|
405 0x00 ; --> ; 00001000, into FF00
|
rlm@118
|
406 0xFF ; --> FF00
|
rlm@118
|
407
|
rlm@118
|
408 0x0E ;
|
rlm@118
|
409 (Integer/parseInt "00001111" 2) ; "00001111" -> C
|
rlm@118
|
410
|
rlm@118
|
411 0xFA ; ; (FF00) -> A
|
rlm@118
|
412 0x00 ;
|
rlm@118
|
413 0xFF ;
|
rlm@118
|
414
|
rlm@118
|
415 0xA1 ; ; (AND A C) -> A
|
rlm@118
|
416 0x4F ; ; A -> C
|
rlm@118
|
417
|
rlm@118
|
418 ;; combine the B and C registers into the input number
|
rlm@118
|
419 0x79 ; ; C -> A
|
rlm@118
|
420 0xB0 ; ; (OR A B) -> A
|
rlm@118
|
421 0x2F ; ; negate A
|
rlm@118
|
422
|
rlm@118
|
423 0xEA ; ; store A into input-number
|
rlm@118
|
424 0x52 ;
|
rlm@118
|
425 0xD3 ;
|
rlm@118
|
426
|
rlm@118
|
427 0xC3 ; ; return to beginning
|
rlm@118
|
428 0x1D ;
|
rlm@118
|
429 0xD3 ;
|
rlm@118
|
430 ])
|
rlm@114
|
431
|
rlm@115
|
432 (defn input-number []
|
rlm@115
|
433 (-> (tick (mid-game))
|
rlm@115
|
434 (IE! 0) ; disable interrupts
|
rlm@118
|
435 (inject-item-assembly (input-number-assembly))))
|
rlm@115
|
436
|
rlm@117
|
437 (defn test-input-number
|
rlm@117
|
438 "Input freestyle buttons and observe the effects at the repl."
|
rlm@117
|
439 []
|
rlm@117
|
440 (set-state! (input-number))
|
rlm@117
|
441 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
|
rlm@115
|
442
|
rlm@118
|
443 (defn write-memory-assembly []
|
rlm@123
|
444 [
|
rlm@123
|
445 ;; Main Timing Loop
|
rlm@123
|
446 ;; Constantly check for v-blank and Trigger main state machine on
|
rlm@123
|
447 ;; every transtion from v-blank to non-v-blank.
|
rlm@118
|
448
|
rlm@123
|
449 0x18 ; D31D ; Variable declaration
|
rlm@123
|
450 0x02 ; D31E
|
rlm@123
|
451 0x00 ; D31F ; frame-count
|
rlm@123
|
452 0x00 ; D320 ; v-blank-prev
|
rlm@123
|
453
|
rlm@123
|
454 0xFA ; D321 ; load v-blank mode flags into A
|
rlm@123
|
455 0x41 ; D322
|
rlm@123
|
456 0xFF ; D323
|
rlm@123
|
457
|
rlm@123
|
458 ;; Branch dependent on v-blank. v-blank happens when the last two
|
rlm@123
|
459 ;; bits in A are "01"
|
rlm@123
|
460 0xCB ; D324
|
rlm@123
|
461 0x4F ; D325
|
rlm@123
|
462
|
rlm@123
|
463 0xC2 ; D326 ; if bit-1 is not 0, then
|
rlm@123
|
464 0x44 ; D327 ; GOTO non-v-blank.
|
rlm@123
|
465 0xD3 ; D328
|
rlm@123
|
466
|
rlm@123
|
467 0xCB ; D329
|
rlm@123
|
468 0x47 ; D32A
|
rlm@123
|
469
|
rlm@123
|
470 0xCA ; D32B ; if bit-0 is not 1, then
|
rlm@123
|
471 0x44 ; D32C ; GOTO non-v-blank.
|
rlm@123
|
472 0xD3 ; D32D
|
rlm@123
|
473
|
rlm@123
|
474 ;; V-Blank
|
rlm@123
|
475 ;; Activate state-machine if this is a transition event.
|
rlm@123
|
476
|
rlm@123
|
477 0xFA ; D32E ; load v-bank-prev into A
|
rlm@123
|
478 0x20 ; D32F
|
rlm@123
|
479 0xD3 ; D330
|
rlm@123
|
480
|
rlm@123
|
481 0xFE ; D331 ; compare A to 0.
|
rlm@123
|
482 0x00 ; D332
|
rlm@123
|
483
|
rlm@123
|
484 ;; set v-blank-prev to 1.
|
rlm@123
|
485 0x3E ; D333 ; load 1 into A.
|
rlm@123
|
486 0x01 ; D334
|
rlm@118
|
487
|
rlm@123
|
488 0xEA ; D335 ; load A into v-blank-prev
|
rlm@123
|
489 0x20 ; D336
|
rlm@123
|
490 0xD3 ; D337
|
rlm@115
|
491
|
rlm@123
|
492 ;; if v-blank-prev was 0, activate state-machine
|
rlm@123
|
493 0xC2 ; D338 ; if v-blank-prev
|
rlm@123
|
494 0x46 ; D339 ; was 0,
|
rlm@123
|
495 0xD3 ; D33A ; GOTO state-machine
|
rlm@123
|
496
|
rlm@123
|
497 0xC3 ; D33B
|
rlm@123
|
498 0x1D ; D33C
|
rlm@123
|
499 0xD3 ; D33D ; GOTO beginning
|
rlm@123
|
500 ;; END V-blank
|
rlm@123
|
501
|
rlm@123
|
502 ;; Non-V-Blank
|
rlm@123
|
503 ;; Set v-blank-prev to 0
|
rlm@123
|
504 0x3E ; D33E ; load 0 into A
|
rlm@123
|
505 0x00 ; D33F
|
rlm@123
|
506
|
rlm@123
|
507 0xEA ; D340 ; load A into v-blank-prev
|
rlm@123
|
508 0x20 ; D341
|
rlm@123
|
509 0xD3 ; D342
|
rlm@118
|
510
|
rlm@123
|
511 0xC3 ; D343
|
rlm@123
|
512 0x1D ; D344
|
rlm@123
|
513 0xD3 ; D345 ; GOTO beginning
|
rlm@123
|
514 ;; END Not-V-Blank
|
rlm@115
|
515
|
rlm@123
|
516 ;; Main State Machine -- Input Section
|
rlm@123
|
517 ;; This is called once every frame.
|
rlm@123
|
518 ;; It collects input and uses it to drive the
|
rlm@123
|
519 ;; state transitions.
|
rlm@123
|
520
|
rlm@123
|
521 ;; Increment frame-count
|
rlm@123
|
522 0xFA ; D346 ; load frame-count into A
|
rlm@123
|
523 0x1F ; D347
|
rlm@123
|
524 0xD3 ; D348
|
rlm@118
|
525
|
rlm@123
|
526 0x3C ; D349 ; inc A
|
rlm@115
|
527
|
rlm@123
|
528 0xEA ; D34A
|
rlm@123
|
529 0x1F ; D34B ; load A into frame-count
|
rlm@123
|
530 0xD3 ; D34C
|
rlm@118
|
531
|
rlm@123
|
532 0x00 ; D34D ; glue :)
|
rlm@123
|
533
|
rlm@123
|
534
|
rlm@123
|
535 ;;;;;;;;; BEGIN RLM DEBUG
|
rlm@123
|
536 0xC3 ;; jump to beginning
|
rlm@123
|
537 0x1D
|
rlm@123
|
538 0xD3
|
rlm@118
|
539
|
rlm@123
|
540 ;;;;;;;;; END RLM DEBUG
|
rlm@118
|
541
|
rlm@118
|
542
|
rlm@118
|
543 0x18 ;D34E ; skip next 3 bytes
|
rlm@118
|
544 0x03 ;D34F
|
rlm@123
|
545 ;D350
|
rlm@118
|
546 (Integer/parseInt "00100000" 2) ; select directional pad
|
rlm@123
|
547 ;D351
|
rlm@118
|
548 (Integer/parseInt "00010000" 2) ; select buttons
|
rlm@118
|
549 0x00 ;D352 ; input-number
|
rlm@118
|
550
|
rlm@123
|
551 ;; select directional pad; store low bits in B
|
rlm@118
|
552
|
rlm@118
|
553 0xFA ;D353 ; load (D350) into A
|
rlm@118
|
554 0x50 ;D354 -->
|
rlm@123
|
555 0xD3 ;D355 --> D350
|
rlm@118
|
556
|
rlm@118
|
557 0xEA ;D356 ; load (A), which is
|
rlm@118
|
558 0x00 ;D357 --> ; 00010000, into FF00
|
rlm@118
|
559 0xFF ;D358 --> FF00
|
rlm@118
|
560
|
rlm@118
|
561 0x06 ;D359
|
rlm@123
|
562 ;D35A
|
rlm@118
|
563 (Integer/parseInt "11110000" 2) ; "11110000" -> B
|
rlm@118
|
564 0xFA ;D35B ; (FF00) -> A
|
rlm@118
|
565 0x00 ;D35C
|
rlm@118
|
566 0xFF ;D35D
|
rlm@118
|
567
|
rlm@118
|
568 0xCB ;D35E ; swap nybbles on A
|
rlm@118
|
569 0x37 ;D35F
|
rlm@118
|
570 0xA0 ;D360 ; (AND A B) -> A
|
rlm@118
|
571 0x47 ;D361 ; A -> B
|
rlm@118
|
572
|
rlm@123
|
573 ;; select buttons; store bottom bits in C
|
rlm@118
|
574
|
rlm@118
|
575 0xFA ;D362 ; load (D351) into A
|
rlm@118
|
576 0x51 ;D363 -->
|
rlm@123
|
577 0xD3 ;D364 --> D351
|
rlm@118
|
578
|
rlm@118
|
579 0xEA ;D365 ; load (A), which is
|
rlm@118
|
580 0x00 ;D366 --> ; 00001000, into FF00
|
rlm@118
|
581 0xFF ;D367 --> FF00
|
rlm@118
|
582
|
rlm@118
|
583 0x0E ;D368
|
rlm@118
|
584 ;D369
|
rlm@118
|
585 (Integer/parseInt "00001111" 2) ; "00001111" -> C
|
rlm@118
|
586
|
rlm@118
|
587 0xFA ;D36A ; (FF00) -> A
|
rlm@118
|
588 0x00 ;D36B
|
rlm@118
|
589 0xFF ;D36C
|
rlm@118
|
590
|
rlm@118
|
591 0xA1 ;D36D ; (AND A C) -> A
|
rlm@118
|
592 0x4F ;D36E ; A -> C
|
rlm@118
|
593
|
rlm@118
|
594 ;; combine the B and C registers into the input number
|
rlm@118
|
595 0x79 ;D36F ; C -> A
|
rlm@118
|
596 0xB0 ;D370 ; (OR A B) -> A
|
rlm@118
|
597 0x2F ;D371 ; negate A
|
rlm@118
|
598
|
rlm@118
|
599 0xEA ;D372 ; store A into input-number
|
rlm@118
|
600 0x52 ;D373
|
rlm@118
|
601 0xD3 ;D374
|
rlm@118
|
602
|
rlm@123
|
603 0xC3 ;D375 ; secret jump :)
|
rlm@123
|
604 0x1D ;D376
|
rlm@118
|
605 0xD3 ;D377
|
rlm@118
|
606 0x00 ;D378
|
rlm@118
|
607 0x00 ;D379
|
rlm@118
|
608 0x00 ;D37A
|
rlm@118
|
609 0x00 ;D37B ; these are here because
|
rlm@118
|
610 0x00 ;D37C ; I messed up :(
|
rlm@118
|
611 0x00 ;D37D
|
rlm@118
|
612 0x00 ;D37E
|
rlm@118
|
613 0x00 ;D37F
|
rlm@118
|
614
|
rlm@118
|
615 ;; beginning of main state machine
|
rlm@118
|
616 0x18 ;D380 ; Declaration of variables
|
rlm@118
|
617 0x05 ;D381 ; 5 variables:
|
rlm@118
|
618 0x00 ;D382 ; current-mode
|
rlm@122
|
619 0x00 ;D383 ; bytes-to-write
|
rlm@121
|
620 0x00 ;D384 ; start-point
|
rlm@118
|
621 0x00 ;D385 ; unused
|
rlm@118
|
622 0x00 ;D386 ; unused
|
rlm@118
|
623
|
rlm@118
|
624
|
rlm@118
|
625 ;; banch on current mode
|
rlm@118
|
626 0xFA ;D387 ; load current-mode (0xD382)
|
rlm@118
|
627 0x82 ;D388 ; into A
|
rlm@118
|
628 0xD3 ;D389
|
rlm@118
|
629 0x00 ;D38A
|
rlm@118
|
630
|
rlm@120
|
631
|
rlm@120
|
632 ;; GOTO Mode 0 (input-mode) if current-mode is 0
|
rlm@118
|
633 0xFE ;D38B
|
rlm@118
|
634 0x00 ;D38C ; compare A with 0x00
|
rlm@118
|
635
|
rlm@120
|
636 0xCA ;D38D ; goto Mode 0 if A == 0
|
rlm@118
|
637 0xA8 ;D38E
|
rlm@118
|
638 0xD3 ;D38F
|
rlm@118
|
639
|
rlm@121
|
640 ;; GOTO Mode 1 (set-length) if current-mode is 1
|
rlm@120
|
641 0xFE ;D390
|
rlm@120
|
642 0x01 ;D391 ; compare A with 0x01
|
rlm@120
|
643
|
rlm@120
|
644 0xCA ;D392
|
rlm@120
|
645 0xB1 ;D393
|
rlm@120
|
646 0xD3 ;D394 ; goto Mode 1 if A == 1
|
rlm@120
|
647
|
rlm@121
|
648 ;; GOTO Mode 2 (set-start-point) if current mode is 2
|
rlm@121
|
649 0xFE ;D395
|
rlm@121
|
650 0x02 ;D396 ; compare A with 0x02
|
rlm@121
|
651
|
rlm@121
|
652 0xCA ;D397
|
rlm@121
|
653 0xBF ;D398
|
rlm@121
|
654 0xD3 ;D399 ; goto Mode 2 if A == 2
|
rlm@121
|
655
|
rlm@118
|
656 0x00 ;D39A
|
rlm@118
|
657 0x00 ;D39B
|
rlm@118
|
658 0x00 ;D39C
|
rlm@118
|
659 0x00 ;D39D
|
rlm@118
|
660 0x00 ;D39E
|
rlm@118
|
661 0x00 ;D39F
|
rlm@118
|
662 0x00 ;D3A0
|
rlm@118
|
663 0x00 ;D3A1
|
rlm@118
|
664 0x00 ;D3A2
|
rlm@118
|
665 0x00 ;D3A3
|
rlm@118
|
666 0x00 ;D3A4
|
rlm@118
|
667 ;; End of Mode checking, goto beginning
|
rlm@118
|
668 0xC3 ;D3A5
|
rlm@118
|
669 0x1D ;D3A6
|
rlm@118
|
670 0xD3 ;D3A7
|
rlm@120
|
671
|
rlm@120
|
672
|
rlm@120
|
673 ;; Mode 0 -- input-mode mode
|
rlm@120
|
674 ;; means that we are waiting for a mode, so set the mode to
|
rlm@120
|
675 ;; whatever is currently in input-number. If nothing is
|
rlm@120
|
676 ;; entered, then the program stays in input-mode mode
|
rlm@120
|
677
|
rlm@118
|
678 ;; set current-mode to input-number
|
rlm@118
|
679 0xFA ;D3A8 ; load input-number (0xD352)
|
rlm@118
|
680 0x52 ;D3A9 ; into A
|
rlm@118
|
681 0xD3 ;D3AA
|
rlm@118
|
682
|
rlm@118
|
683 0xEA ;D3AB ; load A into current-mode
|
rlm@118
|
684 0x82 ;D3AC ; (0xD382)
|
rlm@118
|
685 0xD3 ;D3AD
|
rlm@118
|
686
|
rlm@118
|
687 0xC3 ;D3AE ; go back to beginning
|
rlm@118
|
688 0x1D ;D3AF
|
rlm@118
|
689 0xD3 ;D3B0
|
rlm@120
|
690 ;; End Mode 0
|
rlm@118
|
691
|
rlm@120
|
692
|
rlm@121
|
693 ;; Mode 1 -- set-length mode
|
rlm@121
|
694 ;; This is the header for writing things to memory.
|
rlm@121
|
695 ;; User specifies the number of bytes to write.
|
rlm@120
|
696 ;; Mode is auto advanced to Mode 2 after this mode
|
rlm@120
|
697 ;; completes.
|
rlm@120
|
698
|
rlm@121
|
699 ;; Set bytes left to write to input-number;
|
rlm@120
|
700 ;; set current-mode to 0x02.
|
rlm@120
|
701 0xFA ;D3B1 ; load input-number (0xD352)
|
rlm@120
|
702 0x52 ;D3B2 ; into A
|
rlm@120
|
703 0xD3 ;D3B3
|
rlm@120
|
704
|
rlm@120
|
705 0xEA ;D3B4 ; load A into bytes-left-to-write
|
rlm@120
|
706 0x83 ;D3B5 ; (0xD383)
|
rlm@120
|
707 0xD3 ;D3B6
|
rlm@120
|
708
|
rlm@121
|
709 0x3E ;D3B7 ; load 0x02 into A.
|
rlm@120
|
710 0x02 ;D3B8
|
rlm@120
|
711
|
rlm@120
|
712 0xEA ;D3B9 ; load A to current-mode
|
rlm@120
|
713 0x82 ;D3BA ; advancing from Mode 1 to
|
rlm@120
|
714 0xD3 ;D3BB ; Mode 2
|
rlm@120
|
715
|
rlm@120
|
716 0xC3 ;D3BC ; go back to beginning
|
rlm@120
|
717 0x1D ;D3BD
|
rlm@120
|
718 0xD3 ;D3BE
|
rlm@120
|
719 ;; End Mode 1
|
rlm@121
|
720
|
rlm@121
|
721
|
rlm@121
|
722 ;; Mode 2 -- set start-point mode
|
rlm@121
|
723 ;; Final part of the header for writing things to memory.
|
rlm@121
|
724 ;; User specifies the start location in RAM to which
|
rlm@121
|
725 ;; data will be written.
|
rlm@121
|
726 ;; Mode is auto advanced to Mode 3 after this mode completes.
|
rlm@121
|
727
|
rlm@121
|
728 ;; Set start-point to input-number;
|
rlm@121
|
729 ;; set current mode to 0x03.
|
rlm@121
|
730 0xFA ;D3BF ; load input-number (0xD352)
|
rlm@121
|
731 0x52 ;D3C0 ; into A
|
rlm@121
|
732 0xD3 ;D3C1
|
rlm@121
|
733
|
rlm@121
|
734 0xEA ;D3C2 ; load A into start-point
|
rlm@121
|
735 0x84 ;D3C3 ; (0xD384)
|
rlm@121
|
736 0xD3 ;D3C4
|
rlm@121
|
737
|
rlm@121
|
738 0x3E ;D3C5 ; load 0x03 into A.
|
rlm@121
|
739 0x03 ;D3C6
|
rlm@121
|
740
|
rlm@121
|
741 0xEA ;D3C7 ; load A to current-mode,
|
rlm@121
|
742 0x82 ;D3C8 ; advancing from Mode 2 to
|
rlm@121
|
743 0xD3 ;D3C9 ; Mode 3.
|
rlm@120
|
744
|
rlm@121
|
745 0xC3 ;D3CA ; go back to beginning
|
rlm@121
|
746 0x1D ;D3CB
|
rlm@121
|
747 0xD3 ;D3CC
|
rlm@121
|
748 ;;End Mode 2
|
rlm@121
|
749
|
rlm@118
|
750 0x00 ;D3CD
|
rlm@118
|
751 0x00 ;D3CE
|
rlm@118
|
752 0x00 ;D3CF
|
rlm@118
|
753 0x00 ;D3D0
|
rlm@118
|
754 0x00 ;D3D1
|
rlm@118
|
755 0x00 ;D3D2
|
rlm@118
|
756 0x00 ;D3D3
|
rlm@118
|
757 0x00 ;D3D4
|
rlm@118
|
758 0x00 ;D3D5
|
rlm@118
|
759 0x00 ;D3D6
|
rlm@118
|
760
|
rlm@121
|
761 ;; Mode 3 -- write bytes mode
|
rlm@121
|
762 ;; This is where RAM manipulation happens.
|
rlm@121
|
763 ;; User supplies bytes every frame, which are written
|
rlm@121
|
764 ;; sequentially to
|
rlm@121
|
765
|
rlm@118
|
766
|
rlm@118
|
767
|
rlm@118
|
768
|
rlm@118
|
769 0xC3 ; ; Complete Loop
|
rlm@118
|
770 0x1D ;
|
rlm@118
|
771 0xD3 ;
|
rlm@118
|
772
|
rlm@118
|
773
|
rlm@118
|
774
|
rlm@118
|
775 ])
|
rlm@118
|
776
|
rlm@118
|
777
|
rlm@123
|
778
|
rlm@118
|
779 (def frame-count 0xD31F)
|
rlm@118
|
780 (def input 0xD352)
|
rlm@118
|
781 (def current-mode 0xD382)
|
rlm@118
|
782
|
rlm@118
|
783 (defn write-memory []
|
rlm@118
|
784 (-> (tick (mid-game))
|
rlm@118
|
785 (IE! 0) ; disable interrupts
|
rlm@118
|
786 (inject-item-assembly (write-memory-assembly))))
|
rlm@119
|
787
|
rlm@119
|
788 (defn test-write-memory []
|
rlm@119
|
789 (set-state! (write-memory))
|
rlm@119
|
790 (dorun
|
rlm@119
|
791 (dotimes [_ 5000]
|
rlm@119
|
792 (view-memory (step @current-state) current-mode))))
|
rlm@122
|
793
|
rlm@122
|
794 (def bytes-to-write 0xD383)
|
rlm@122
|
795 (def start-point 0xD384)
|
rlm@122
|
796
|
rlm@122
|
797 (defn test-mode-2 []
|
rlm@122
|
798 (->
|
rlm@122
|
799 (write-memory)
|
rlm@123
|
800 (view-memory frame-count)
|
rlm@122
|
801 (step)
|
rlm@122
|
802 (step [:a])
|
rlm@122
|
803 (step [:b])
|
rlm@122
|
804 (step [:start])
|
rlm@122
|
805 (step [])
|
rlm@123
|
806 (view-memory frame-count)))
|
rlm@123
|
807
|
rlm@123
|
808
|
rlm@123
|
809
|
rlm@123
|
810
|