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