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@128
|
69 (defn B [state]
|
rlm@128
|
70 (bit-shift-right (bit-and 0x0000FF00 (BC state)) 8))
|
rlm@128
|
71
|
rlm@128
|
72 (defn C [state]
|
rlm@128
|
73 (bit-and 0xFF (BC state)))
|
rlm@128
|
74
|
rlm@110
|
75 (defn binary-str [num]
|
rlm@110
|
76 (format "%08d"
|
rlm@110
|
77 (Integer/parseInt
|
rlm@110
|
78 (Integer/toBinaryString num) 10)))
|
rlm@110
|
79
|
rlm@110
|
80 (defn view-register [state name reg-fn]
|
rlm@110
|
81 (println (format "%s: %s" name
|
rlm@110
|
82 (binary-str (reg-fn state))))
|
rlm@110
|
83 state)
|
rlm@110
|
84
|
rlm@107
|
85 (defn view-memory [state mem]
|
rlm@110
|
86 (println (format "mem 0x%04X = %s" mem
|
rlm@110
|
87 (binary-str (aget (memory state) mem))))
|
rlm@107
|
88 state)
|
rlm@107
|
89
|
rlm@118
|
90 (defn trace [state]
|
rlm@124
|
91 (loop [program-counters [(first (registers @current-state)) ]
|
rlm@124
|
92 opcodes [(aget (memory @current-state) (PC @current-state))]]
|
rlm@118
|
93 (let [frame-boundary?
|
rlm@118
|
94 (com.aurellem.gb.Gb/tick)]
|
rlm@118
|
95 (if frame-boundary?
|
rlm@118
|
96 [program-counters opcodes]
|
rlm@118
|
97 (recur
|
rlm@118
|
98 (conj program-counters
|
rlm@118
|
99 (first (registers @current-state)))
|
rlm@118
|
100 (conj opcodes
|
rlm@118
|
101 (aget (memory @current-state)
|
rlm@118
|
102 (PC @current-state))))))))
|
rlm@124
|
103
|
rlm@124
|
104 (defn print-trace [state n]
|
rlm@124
|
105 (let [[program-counters opcodes] (trace state)]
|
rlm@124
|
106 (dorun (map (fn [pc op] (println (format "%04X: 0x%02X" pc op)))
|
rlm@124
|
107 (take n program-counters)
|
rlm@124
|
108 (take n opcodes)))))
|
rlm@124
|
109
|
rlm@118
|
110 (defn good-trace []
|
rlm@118
|
111 (-> (mid-game) (tick) (IE! 0)
|
rlm@118
|
112 (set-inv-mem [0x00 0x00 0X00 0x00])
|
rlm@118
|
113 (PC! item-list-start)(print-interrupt)
|
rlm@118
|
114 (info) (tick) (info) (tick) (info)))
|
rlm@118
|
115
|
rlm@113
|
116 (defn read-down-button []
|
rlm@110
|
117 (-> (tick (mid-game))
|
rlm@110
|
118 (IE! 0) ; disable interrupts
|
rlm@110
|
119 (inject-item-assembly
|
rlm@118
|
120 ;; write 00010000 to 0xFF00 to select joypad
|
rlm@118
|
121 [0x18 ;D31D ; jump over
|
rlm@118
|
122 0x01 ;D31E ; the next 8 bits
|
rlm@118
|
123 ;D31F
|
rlm@118
|
124 (Integer/parseInt "00100000" 2) ; data section
|
rlm@118
|
125
|
rlm@118
|
126 0xFA ;D320 ; load (D31F) into A
|
rlm@118
|
127 0x1F ;D321 -->
|
rlm@118
|
128 0xD3 ;D322 --> D31F
|
rlm@107
|
129
|
rlm@118
|
130 0xEA ;D323 ; load (A), which is
|
rlm@118
|
131 0x00 ;D324 --> ; 00010000, into FF00
|
rlm@118
|
132 0xFF ;D325 --> FF00
|
rlm@118
|
133
|
rlm@118
|
134 0x18 ;D326 ; this is the place where
|
rlm@118
|
135 0x01 ;D327 ; we will store whether
|
rlm@118
|
136 0x00 ;D328 ; "down" is pressed.
|
rlm@107
|
137
|
rlm@118
|
138 0xFA ;D329 ; (FF00) -> A
|
rlm@118
|
139 0x00 ;D32A
|
rlm@118
|
140 0xFF ;D32B
|
rlm@110
|
141
|
rlm@118
|
142 0xCB ;D32C ; Test whether "down"
|
rlm@118
|
143 0x5F ;D32D ; is pressed.
|
rlm@111
|
144
|
rlm@118
|
145 0x28 ;D32E ; if down is pressed,
|
rlm@118
|
146 0x03 ;D32F ; skip the next section
|
rlm@118
|
147 ; of code.
|
rlm@118
|
148 ;; down-is-not-pressed
|
rlm@118
|
149 0xC3 ;D330
|
rlm@118
|
150 0x1D ;D331 ; return to beginning
|
rlm@118
|
151 0xD3 ;D332
|
rlm@118
|
152
|
rlm@118
|
153 ;; down-is-pressed
|
rlm@118
|
154 0xEA ;D334 ; write A to D328 if
|
rlm@118
|
155 0x28 ;D335 ; "down" was pressed
|
rlm@118
|
156 0xD3 ;D336
|
rlm@111
|
157
|
rlm@118
|
158 0xC3 ;D330
|
rlm@118
|
159 0x1D ;D331 ; return to beginning
|
rlm@118
|
160 0xD3 ;D332
|
rlm@118
|
161 ])))
|
rlm@111
|
162
|
rlm@118
|
163 (defn test-read-down []
|
rlm@118
|
164 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)
|
rlm@118
|
165 (view-memory (step (step (read-down-button))) 0xD328)))
|
rlm@113
|
166
|
rlm@113
|
167 (defn count-frames []
|
rlm@113
|
168 (-> (tick (mid-game))
|
rlm@113
|
169 (IE! 0) ; disable interrupts
|
rlm@113
|
170 (inject-item-assembly
|
rlm@113
|
171 [0x18 ;D31D ; jump over
|
rlm@113
|
172 0x02 ;D31E ; the next 2 bytes
|
rlm@113
|
173 0x00 ;D31F ; frame-count
|
rlm@113
|
174 0x00 ;D320 ; v-blank-prev
|
rlm@113
|
175
|
rlm@113
|
176 0xFA ;D321
|
rlm@113
|
177 0x41 ;D322 ; load (FF41) into A
|
rlm@113
|
178 0xFF ;D323 ; this contains mode flags
|
rlm@113
|
179
|
rlm@113
|
180 ;; if we're in v-blank, the bit-1 is 0
|
rlm@113
|
181 ;; and bit-2 is 1 Otherwise, it is not v-blank.
|
rlm@113
|
182 0xCB ;D324 ; test bit-1 of A
|
rlm@113
|
183 0x4F ;D325
|
rlm@113
|
184
|
rlm@113
|
185 0xC2 ;D326 ; if bit-1 is not 0
|
rlm@115
|
186 0x44 ;D327 ; GOTO not-v-blank
|
rlm@113
|
187 0xD3 ;D328
|
rlm@113
|
188
|
rlm@113
|
189 0xCB ;D329 ; test bit-0 of A
|
rlm@113
|
190 0x47 ;D32A
|
rlm@113
|
191
|
rlm@113
|
192 0xCA ;D32B ; if bit-0 is not 1
|
rlm@115
|
193 0x44 ;D32C ; GOTO not-v-blank
|
rlm@113
|
194 0xD3 ;D32D
|
rlm@114
|
195 ;;; in v-blank mode
|
rlm@113
|
196 ;; if v-blank-prev was 0,
|
rlm@113
|
197 ;; increment frame-count
|
rlm@113
|
198
|
rlm@113
|
199 0xFA ;D32E ; load v-blank-prev to A
|
rlm@113
|
200 0x20 ;D32F
|
rlm@113
|
201 0xD3 ;D330
|
rlm@113
|
202
|
rlm@113
|
203 0xCB ;D331
|
rlm@113
|
204 0x47 ;D332 ; test bit-0 of A
|
rlm@113
|
205
|
rlm@113
|
206 0x20 ;D333 ; skip next section
|
rlm@113
|
207 0x07 ;D334 ; if v-blank-prev was not zero
|
rlm@113
|
208
|
rlm@113
|
209 ;; v-blank was 0, increment frame-count
|
rlm@113
|
210 0xFA ;D335 ; load frame-count into A
|
rlm@113
|
211 0x1F ;D336
|
rlm@113
|
212 0xD3 ;D337
|
rlm@113
|
213
|
rlm@113
|
214 0x3C ;D338 ; inc A
|
rlm@113
|
215
|
rlm@113
|
216 0xEA ;D339 ; load A into frame-count
|
rlm@113
|
217 0x1F ;D33A
|
rlm@113
|
218 0xD3 ;D33B
|
rlm@113
|
219
|
rlm@114
|
220 ;; set v-blank-prev to 1
|
rlm@113
|
221 0x3E ;D33C ; load 1 into A
|
rlm@113
|
222 0x01 ;D33D
|
rlm@113
|
223
|
rlm@113
|
224 0xEA ;D33E ; load A into v-blank-prev
|
rlm@113
|
225 0x20 ;D33F
|
rlm@113
|
226 0xD3 ;D340
|
rlm@113
|
227
|
rlm@115
|
228 0xC3 ;D341 ; return to beginning
|
rlm@115
|
229 0x1D ;D342
|
rlm@115
|
230 0xD3 ;D343
|
rlm@113
|
231
|
rlm@114
|
232 ;;; not in v-blank mode
|
rlm@114
|
233 ;; set v-blank-prev to 0
|
rlm@115
|
234 0x3E ;D344 ; load 0 into A
|
rlm@115
|
235 0x00 ;D345
|
rlm@113
|
236
|
rlm@115
|
237 0xEA ;D346 ; load A into v-blank-prev
|
rlm@115
|
238 0x20 ;D347
|
rlm@115
|
239 0xD3 ;D348
|
rlm@115
|
240
|
rlm@115
|
241 0xC3 ;D349 ; return to beginning
|
rlm@115
|
242 0x1D ;D34A
|
rlm@115
|
243 0xD3 ;D34B
|
rlm@113
|
244 ])))
|
rlm@113
|
245
|
rlm@114
|
246 (defn step-count-frames []
|
rlm@113
|
247 (-> (read-down-button)
|
rlm@110
|
248 (info)
|
rlm@110
|
249 (tick) ;; skip over data section
|
rlm@110
|
250 (info)
|
rlm@110
|
251 (view-register "Register A" A)
|
rlm@110
|
252 (tick) ;; load-data into A
|
rlm@110
|
253 (view-register "Register A" A)
|
rlm@110
|
254 (info)
|
rlm@110
|
255 (view-memory 0xFF00)
|
rlm@110
|
256 (tick) ;; load A into 0xFF00
|
rlm@110
|
257 (view-memory 0xFF00)
|
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 (info)
|
rlm@111
|
265 (tick)
|
rlm@111
|
266 (info)
|
rlm@111
|
267 (tick)
|
rlm@111
|
268 (info)
|
rlm@111
|
269 (tick)
|
rlm@111
|
270 (print-inventory)))
|
rlm@111
|
271
|
rlm@115
|
272 (defn test-count-frames []
|
rlm@115
|
273 (= 255 (aget (memory ((apply comp (repeat 255 step))
|
rlm@115
|
274 (count-frames)))
|
rlm@115
|
275 0xD31F)))
|
rlm@115
|
276
|
rlm@115
|
277 ;; specs for main bootstrap program
|
rlm@115
|
278 ;; starts in "mode-select" mode
|
rlm@115
|
279 ;; Each button press takes place in a single frame.
|
rlm@115
|
280 ;; mode-select-mode takes one of the main buttons
|
rlm@115
|
281 ;; which selects one of up to eight modes
|
rlm@115
|
282 ;; mode 1 activated by the "A" button
|
rlm@115
|
283 ;; the next two button presses indicates the start
|
rlm@115
|
284 ;; memory location which to which the bootstrap
|
rlm@115
|
285 ;; program will write.
|
rlm@115
|
286 ;; This is done by using each of the eight buttons to
|
rlm@115
|
287 ;; spell out an 8 bit number. The order of buttons is
|
rlm@116
|
288 ;; [:d :u :l :r :start :select :b :a]
|
rlm@116
|
289 ;; [:a :start :l] --> 00101001
|
rlm@114
|
290
|
rlm@115
|
291 ;; the next button press determines how many bytes are to be
|
rlm@115
|
292 ;; written, starting at the start position.
|
rlm@114
|
293
|
rlm@115
|
294 ;; then, the actual bytes are entered and are written to the
|
rlm@115
|
295 ;; start address in sequence.
|
rlm@114
|
296
|
rlm@118
|
297 (defn input-number-assembly []
|
rlm@118
|
298 [0x18 ;D31D ; jump over
|
rlm@118
|
299 0x02 ;D31E ; the next 2 bytes
|
rlm@118
|
300 0x00 ;D31F ; frame-count
|
rlm@118
|
301 0x00 ;D320 ; v-blank-prev
|
rlm@118
|
302
|
rlm@118
|
303 0xFA ;D321
|
rlm@118
|
304 0x41 ;D322 ; load (FF41) into A
|
rlm@118
|
305 0xFF ;D323 ; this contains mode flags
|
rlm@118
|
306
|
rlm@118
|
307 ;; if we're in v-blank, the bit-1 is 0
|
rlm@118
|
308 ;; and bit-2 is 1 Otherwise, it is not v-blank.
|
rlm@118
|
309 0xCB ;D324 ; test bit-1 of A
|
rlm@118
|
310 0x4F ;D325
|
rlm@114
|
311
|
rlm@118
|
312 0xC2 ;D326 ; if bit-1 is not 0
|
rlm@118
|
313 0x44 ;D327 ; GOTO not-v-blank
|
rlm@118
|
314 0xD3 ;D328
|
rlm@118
|
315
|
rlm@118
|
316 0xCB ;D329 ; test bit-0 of A
|
rlm@118
|
317 0x47 ;D32A
|
rlm@118
|
318
|
rlm@118
|
319 0xCA ;D32B ; if bit-0 is not 1
|
rlm@118
|
320 0x44 ;D32C ; GOTO not-v-blank
|
rlm@118
|
321 0xD3 ;D32D
|
rlm@118
|
322
|
rlm@118
|
323 ;;; in v-blank mode
|
rlm@118
|
324
|
rlm@118
|
325 ;; if v-blank-prev was 0,
|
rlm@118
|
326 ;; increment frame-count
|
rlm@118
|
327
|
rlm@118
|
328 0xFA ;D32E ; load v-blank-prev to A
|
rlm@118
|
329 0x20 ;D32F
|
rlm@118
|
330 0xD3 ;D330
|
rlm@118
|
331
|
rlm@118
|
332 0xCB ;D331
|
rlm@118
|
333 0x47 ;D332 ; test bit-0 of A
|
rlm@118
|
334
|
rlm@118
|
335 0x20 ;D333 ; skip next section
|
rlm@118
|
336 0x07 ;D334 ; if v-blank-prev was not zero
|
rlm@118
|
337
|
rlm@118
|
338 ;; v-blank was 0, increment frame-count
|
rlm@118
|
339 0xFA ;D335 ; load frame-count into A
|
rlm@118
|
340 0x1F ;D336
|
rlm@118
|
341 0xD3 ;D337
|
rlm@118
|
342
|
rlm@118
|
343 0x3C ;D338 ; inc A
|
rlm@118
|
344
|
rlm@118
|
345 0xEA ;D339 ; load A into frame-count
|
rlm@118
|
346 0x1F ;D33A
|
rlm@118
|
347 0xD3 ;D33B
|
rlm@118
|
348
|
rlm@118
|
349 ;; set v-blank-prev to 1
|
rlm@118
|
350 0x3E ;D33C ; load 1 into A
|
rlm@118
|
351 0x01 ;D33D
|
rlm@118
|
352
|
rlm@118
|
353 0xEA ;D33E ; load A into v-blank-prev
|
rlm@118
|
354 0x20 ;D33F
|
rlm@118
|
355 0xD3 ;D340
|
rlm@118
|
356
|
rlm@118
|
357 0xC3 ;D341 ; GOTO input handling code
|
rlm@118
|
358 0x4E ;D342
|
rlm@118
|
359 0xD3 ;D343
|
rlm@118
|
360
|
rlm@118
|
361 ;;; not in v-blank mode
|
rlm@118
|
362 ;; set v-blank-prev to 0
|
rlm@118
|
363 0x3E ;D344 ; load 0 into A
|
rlm@118
|
364 0x00 ;D345
|
rlm@118
|
365
|
rlm@118
|
366 0xEA ;D346 ; load A into v-blank-prev
|
rlm@118
|
367 0x20 ;D347
|
rlm@118
|
368 0xD3 ;D348
|
rlm@118
|
369
|
rlm@118
|
370 0xC3 ;D349 ; return to beginning
|
rlm@118
|
371 0x1D ;D34A
|
rlm@118
|
372 0xD3 ;D34B
|
rlm@118
|
373
|
rlm@118
|
374 0x00 ;D34C ; these are here
|
rlm@118
|
375 0x00 ;D34D ; for glue
|
rlm@118
|
376
|
rlm@118
|
377
|
rlm@118
|
378 ;;; calculate input number based on button presses
|
rlm@118
|
379 0x18 ;D34E ; skip next 3 bytes
|
rlm@118
|
380 0x03 ;D34F
|
rlm@118
|
381 ;D350
|
rlm@118
|
382 (Integer/parseInt "00100000" 2) ; select directional pad
|
rlm@118
|
383 ;D351
|
rlm@118
|
384 (Integer/parseInt "00010000" 2) ; select buttons
|
rlm@118
|
385 0x00 ;D352 ; input-number
|
rlm@118
|
386
|
rlm@118
|
387 ;; select directional pad, store low bits in B
|
rlm@118
|
388
|
rlm@118
|
389 0xFA ;D353 ; load (D350) into A
|
rlm@118
|
390 0x50 ;D354 -->
|
rlm@118
|
391 0xD3 ;D355 --> D31F
|
rlm@118
|
392
|
ocsenave@135
|
393 0xEA ;D356 ; load A, which is
|
rlm@118
|
394 0x00 ;D357 --> ; 00010000, into FF00
|
rlm@118
|
395 0xFF ;D358 --> FF00
|
rlm@118
|
396
|
rlm@118
|
397 0x06 ;D359
|
rlm@118
|
398 ;D35A
|
rlm@118
|
399 (Integer/parseInt "11110000" 2) ; "11110000" -> B
|
rlm@118
|
400 0xFA ;D35B ; (FF00) -> A
|
rlm@118
|
401 0x00 ;D35C
|
rlm@118
|
402 0xFF ;D35D
|
rlm@118
|
403
|
rlm@118
|
404 0xCB ;D35E ; swap nybbles on A
|
rlm@118
|
405 0x37 ;D35F
|
rlm@118
|
406 0xA0 ;D360 ; (AND A B) -> A
|
rlm@118
|
407 0x47 ;D361 ; A -> B
|
rlm@118
|
408
|
rlm@118
|
409 ;; select buttons store bottom bits in C
|
rlm@118
|
410
|
rlm@118
|
411 0xFA ; ; load (D351) into A
|
rlm@118
|
412 0x51 ; -->
|
rlm@118
|
413 0xD3 ; --> D31F
|
rlm@118
|
414
|
rlm@118
|
415 0xEA ; ; load (A), which is
|
rlm@118
|
416 0x00 ; --> ; 00001000, into FF00
|
rlm@118
|
417 0xFF ; --> FF00
|
rlm@118
|
418
|
rlm@118
|
419 0x0E ;
|
rlm@118
|
420 (Integer/parseInt "00001111" 2) ; "00001111" -> C
|
rlm@118
|
421
|
rlm@118
|
422 0xFA ; ; (FF00) -> A
|
rlm@118
|
423 0x00 ;
|
rlm@118
|
424 0xFF ;
|
rlm@118
|
425
|
rlm@118
|
426 0xA1 ; ; (AND A C) -> A
|
rlm@118
|
427 0x4F ; ; A -> C
|
rlm@118
|
428
|
rlm@118
|
429 ;; combine the B and C registers into the input number
|
rlm@118
|
430 0x79 ; ; C -> A
|
rlm@118
|
431 0xB0 ; ; (OR A B) -> A
|
rlm@118
|
432 0x2F ; ; negate A
|
rlm@118
|
433
|
rlm@118
|
434 0xEA ; ; store A into input-number
|
rlm@118
|
435 0x52 ;
|
rlm@118
|
436 0xD3 ;
|
rlm@118
|
437
|
rlm@118
|
438 0xC3 ; ; return to beginning
|
rlm@118
|
439 0x1D ;
|
rlm@118
|
440 0xD3 ;
|
rlm@118
|
441 ])
|
rlm@114
|
442
|
rlm@128
|
443
|
rlm@128
|
444 (defn print-pc [state]
|
rlm@128
|
445 (println (format "PC: 0x%04X" (PC state)))
|
rlm@128
|
446 state)
|
rlm@128
|
447
|
rlm@128
|
448 (defn print-op [state]
|
rlm@128
|
449 (println (format "OP: 0x%02X" (aget (memory state) (PC state))))
|
rlm@128
|
450 state)
|
rlm@128
|
451
|
rlm@128
|
452 (defn d-tick
|
rlm@128
|
453 ([state]
|
rlm@128
|
454 (-> state print-pc print-op tick)))
|
rlm@128
|
455
|
rlm@115
|
456 (defn input-number []
|
rlm@115
|
457 (-> (tick (mid-game))
|
rlm@115
|
458 (IE! 0) ; disable interrupts
|
rlm@118
|
459 (inject-item-assembly (input-number-assembly))))
|
rlm@115
|
460
|
rlm@117
|
461 (defn test-input-number
|
rlm@117
|
462 "Input freestyle buttons and observe the effects at the repl."
|
rlm@117
|
463 []
|
rlm@117
|
464 (set-state! (input-number))
|
rlm@117
|
465 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
|
rlm@115
|
466
|
rlm@128
|
467
|
rlm@128
|
468
|
rlm@128
|
469
|
ocsenave@135
|
470
|
ocsenave@135
|
471
|
ocsenave@135
|
472
|
ocsenave@135
|
473
|
ocsenave@135
|
474
|
ocsenave@135
|
475
|
ocsenave@135
|
476
|
ocsenave@135
|
477
|
ocsenave@135
|
478
|
ocsenave@135
|
479
|
ocsenave@135
|
480
|
ocsenave@135
|
481
|
ocsenave@135
|
482
|
ocsenave@135
|
483
|
ocsenave@135
|
484
|
ocsenave@135
|
485
|
ocsenave@135
|
486
|
ocsenave@135
|
487
|
ocsenave@135
|
488
|
ocsenave@135
|
489
|
ocsenave@135
|
490
|
ocsenave@135
|
491
|
ocsenave@135
|
492
|
ocsenave@135
|
493
|
ocsenave@135
|
494
|
ocsenave@136
|
495 (defn write-memory-assembly*
|
ocsenave@136
|
496 "Currently, grabs input from the user each frame."
|
ocsenave@136
|
497 []
|
rlm@128
|
498 [
|
ocsenave@135
|
499 ;; --------- FRAME METRONOME
|
ocsenave@136
|
500 0x00 ;; v-blank-prev D31D
|
rlm@128
|
501
|
rlm@128
|
502 0xFA ;; load modes into A
|
rlm@128
|
503 0x41
|
rlm@128
|
504 0xFF
|
ocsenave@135
|
505
|
ocsenave@135
|
506 0x47 ;; A -> B
|
ocsenave@135
|
507 0xCB ;; rotate A
|
rlm@128
|
508 0x2F
|
ocsenave@135
|
509 0x2F ;; invert A
|
rlm@128
|
510
|
rlm@128
|
511 0xA0
|
ocsenave@135
|
512 0x47 ;; now B_0 contains (VB==1)
|
rlm@128
|
513
|
rlm@128
|
514 0xFA ;; load v-blank-prev
|
ocsenave@136
|
515 0x1D
|
rlm@128
|
516 0xD3
|
rlm@128
|
517
|
ocsenave@135
|
518 0x2F ;; complement v-blank-prev
|
ocsenave@135
|
519
|
ocsenave@135
|
520 0xA0 ;; A & B --> A
|
ocsenave@135
|
521 0x4F ;; now C_0 contains increment?
|
ocsenave@135
|
522
|
rlm@128
|
523
|
rlm@128
|
524 0x78 ;; B->A
|
rlm@128
|
525
|
rlm@128
|
526 0xEA ;; spit A --> vbprev
|
ocsenave@136
|
527 0x1D
|
rlm@128
|
528 0xD3
|
rlm@128
|
529
|
ocsenave@136
|
530 0xCB ;test C_0
|
ocsenave@136
|
531 0x41
|
ocsenave@136
|
532 0x20 ; JUMP ahead to button input if nonzero
|
ocsenave@136
|
533 0x03
|
ocsenave@137
|
534 0x18 ; JUMP back to frame metronome (D31E)
|
ocsenave@137
|
535 0xE7
|
ocsenave@135
|
536
|
ocsenave@135
|
537 ;; -------- GET BUTTON INPUT
|
ocsenave@137
|
538
|
ocsenave@137
|
539 ;; btw, C_0 is now 1
|
ocsenave@137
|
540 0x00 ;; var: which-input D337
|
ocsenave@135
|
541 ;; prepare to select bits
|
ocsenave@135
|
542
|
ocsenave@137
|
543 0x06 ;; load 0x00 into B
|
ocsenave@135
|
544 0x00
|
ocsenave@137
|
545
|
ocsenave@137
|
546 0x3E ;; load 0x20 into A, to measure dpad
|
ocsenave@135
|
547 0x20
|
ocsenave@135
|
548
|
ocsenave@135
|
549
|
ocsenave@137
|
550 0xE0 ;; load A into [FF00] ;; D33C
|
ocsenave@135
|
551 0x00
|
ocsenave@137
|
552
|
ocsenave@137
|
553 0xF0 ;; load A from [FF00]
|
ocsenave@135
|
554 0x00
|
ocsenave@135
|
555
|
ocsenave@135
|
556 0xE6 ;; bitmask 00001111
|
ocsenave@135
|
557 0x0F
|
ocsenave@135
|
558
|
ocsenave@135
|
559 0xB0 ;; A or B --> A
|
ocsenave@135
|
560 0xCB
|
ocsenave@135
|
561 0x41 ;; test bit 0 of C
|
ocsenave@137
|
562 0x28 ;; JUMP forward if 0
|
ocsenave@135
|
563 0x08
|
ocsenave@135
|
564
|
ocsenave@135
|
565 0x47 ;; A -> B
|
ocsenave@135
|
566 0xCB ;; swap B nybbles
|
ocsenave@135
|
567 0x30
|
ocsenave@135
|
568 0x0C ;; increment C
|
ocsenave@137
|
569 0x3E ;; load 0x10 into A, to measure btns
|
ocsenave@135
|
570 0x10
|
ocsenave@135
|
571 0x18 ;; JUMP back to "load A into [FF00]" [20 steps?]
|
ocsenave@137
|
572 0xED
|
ocsenave@135
|
573
|
ocsenave@135
|
574 ;; now A contains the pressed keys
|
ocsenave@137
|
575
|
ocsenave@137
|
576
|
ocsenave@137
|
577
|
ocsenave@137
|
578
|
ocsenave@137
|
579
|
ocsenave@137
|
580
|
ocsenave@137
|
581
|
ocsenave@137
|
582
|
ocsenave@137
|
583 0xC3 ;; todo replace with relative jump
|
ocsenave@137
|
584 0x1E
|
ocsenave@135
|
585 0xD3
|
ocsenave@137
|
586 ;;0x18 ;;JUMP back to "metronome" in one hop [D31E]
|
ocsenave@137
|
587 ;;0xCA ;; E1
|
ocsenave@135
|
588
|
rlm@128
|
589 ]
|
rlm@128
|
590 )
|
rlm@128
|
591
|
rlm@128
|
592 (defn write-mem-dyl []
|
rlm@128
|
593 (-> (tick (mid-game))
|
rlm@128
|
594 (IE! 0)
|
rlm@128
|
595 (inject-item-assembly (write-memory-assembly*))))
|
rlm@131
|
596
|
rlm@131
|
597
|
ocsenave@135
|
598 (defn dylan []
|
ocsenave@135
|
599 (->
|
ocsenave@135
|
600 (write-mem-dyl)
|
ocsenave@135
|
601 (tick)
|
ocsenave@135
|
602 (tick)
|
ocsenave@135
|
603 (tick)
|
ocsenave@135
|
604 (tick)
|
ocsenave@135
|
605 (tick)
|
ocsenave@135
|
606 (tick)
|
ocsenave@135
|
607 (tick)
|
ocsenave@135
|
608 (tick)
|
ocsenave@135
|
609 (tick)
|
ocsenave@135
|
610 (tick)
|
ocsenave@135
|
611 (tick)
|
ocsenave@135
|
612 (tick)
|
ocsenave@135
|
613 (tick)
|
ocsenave@135
|
614 (tick)
|
ocsenave@136
|
615 (tick) ;; first loop
|
ocsenave@136
|
616
|
ocsenave@136
|
617
|
ocsenave@135
|
618 (tick)
|
ocsenave@135
|
619 (tick)
|
ocsenave@135
|
620 (tick)
|
ocsenave@135
|
621 (tick)
|
ocsenave@135
|
622 (tick)
|
ocsenave@135
|
623 (tick)
|
ocsenave@135
|
624 (tick)
|
ocsenave@135
|
625 (tick)
|
ocsenave@135
|
626 (tick)
|
ocsenave@135
|
627 (tick)
|
ocsenave@135
|
628 (tick)
|
ocsenave@135
|
629 (tick)
|
ocsenave@136
|
630 (tick) ;; dpad bits
|
ocsenave@136
|
631
|
ocsenave@135
|
632 (tick)
|
ocsenave@135
|
633 (tick)
|
ocsenave@136
|
634 (tick)
|
ocsenave@136
|
635 (tick)
|
ocsenave@136
|
636 (tick)
|
ocsenave@136
|
637 (tick)
|
ocsenave@136
|
638 (tick)
|
ocsenave@136
|
639 (tick)
|
ocsenave@136
|
640 (d-tick)
|
ocsenave@135
|
641
|
ocsenave@135
|
642
|
ocsenave@135
|
643
|
ocsenave@135
|
644 (view-register "A" A)
|
ocsenave@135
|
645 (view-register "B" B)
|
ocsenave@135
|
646 (view-register "C" C)
|
ocsenave@135
|
647
|
ocsenave@135
|
648 ))
|
ocsenave@135
|
649
|
ocsenave@135
|
650
|
ocsenave@135
|
651
|
ocsenave@135
|
652
|
rlm@131
|
653 (defn d2 []
|
rlm@131
|
654 (->
|
rlm@131
|
655 (write-mem-dyl)
|
rlm@131
|
656 (view-memory 0xD31F)
|
rlm@131
|
657 step step step step step
|
rlm@131
|
658 (view-memory 0xD31F)))
|
rlm@131
|
659
|
rlm@131
|
660
|
rlm@131
|
661
|
ocsenave@135
|
662
|
ocsenave@135
|
663
|
ocsenave@135
|
664
|
ocsenave@135
|
665
|
ocsenave@135
|
666
|
ocsenave@135
|
667
|
ocsenave@135
|
668
|
ocsenave@135
|
669
|
ocsenave@135
|
670
|
ocsenave@135
|
671
|
ocsenave@135
|
672
|
ocsenave@135
|
673
|
ocsenave@135
|
674
|
rlm@131
|
675
|
rlm@131
|
676
|
rlm@128
|
677
|
rlm@128
|
678
|
rlm@118
|
679 (defn write-memory-assembly []
|
rlm@129
|
680 [
|
rlm@123
|
681 ;; Main Timing Loop
|
rlm@123
|
682 ;; Constantly check for v-blank and Trigger main state machine on
|
rlm@123
|
683 ;; every transtion from v-blank to non-v-blank.
|
rlm@129
|
684
|
rlm@123
|
685 0x18 ; D31D ; Variable declaration
|
rlm@123
|
686 0x02 ; D31E
|
rlm@123
|
687 0x00 ; D31F ; frame-count
|
rlm@123
|
688 0x00 ; D320 ; v-blank-prev
|
rlm@129
|
689
|
rlm@129
|
690 0xF0 ; D321 ; load v-blank mode flags into A
|
rlm@129
|
691 0x41
|
rlm@129
|
692 0x00
|
rlm@123
|
693
|
rlm@123
|
694
|
rlm@123
|
695 ;; Branch dependent on v-blank. v-blank happens when the last two
|
rlm@123
|
696 ;; bits in A are "01"
|
rlm@123
|
697 0xCB ; D324
|
rlm@123
|
698 0x4F ; D325
|
rlm@123
|
699
|
rlm@123
|
700 0xC2 ; D326 ; if bit-1 is not 0, then
|
rlm@124
|
701 0x3E ; D327 ; GOTO non-v-blank.
|
rlm@123
|
702 0xD3 ; D328
|
rlm@123
|
703
|
rlm@123
|
704 0xCB ; D329
|
rlm@123
|
705 0x47 ; D32A
|
rlm@123
|
706
|
rlm@123
|
707 0xCA ; D32B ; if bit-0 is not 1, then
|
rlm@124
|
708 0x3E ; D32C ; GOTO non-v-blank.
|
rlm@123
|
709 0xD3 ; D32D
|
rlm@123
|
710
|
rlm@123
|
711 ;; V-Blank
|
rlm@123
|
712 ;; Activate state-machine if this is a transition event.
|
rlm@123
|
713
|
rlm@123
|
714 0xFA ; D32E ; load v-bank-prev into A
|
rlm@123
|
715 0x20 ; D32F
|
rlm@123
|
716 0xD3 ; D330
|
rlm@123
|
717
|
rlm@124
|
718 0xFE ; D331 ; compare A to 0. >--------\
|
rlm@124
|
719 0x00 ; D332 \
|
rlm@124
|
720 ; |
|
rlm@124
|
721 ;; set v-blank-prev to 1. |
|
rlm@124
|
722 0x3E ; D333 ; load 1 into A. |
|
rlm@124
|
723 0x01 ; D334 |
|
rlm@124
|
724 ; |
|
rlm@124
|
725 0xEA ; D335 ; load A into v-blank-prev |
|
rlm@124
|
726 0x20 ; D336 |
|
rlm@124
|
727 0xD3 ; D337 |
|
rlm@124
|
728 ; /
|
rlm@124
|
729 ;; if v-blank-prev was 0, activate state-machine <------/
|
rlm@124
|
730 0xCA ; D338 ; if v-blank-prev
|
rlm@123
|
731 0x46 ; D339 ; was 0,
|
rlm@123
|
732 0xD3 ; D33A ; GOTO state-machine
|
rlm@123
|
733
|
rlm@123
|
734 0xC3 ; D33B
|
rlm@123
|
735 0x1D ; D33C
|
rlm@123
|
736 0xD3 ; D33D ; GOTO beginning
|
rlm@123
|
737 ;; END V-blank
|
rlm@123
|
738
|
rlm@123
|
739 ;; Non-V-Blank
|
rlm@123
|
740 ;; Set v-blank-prev to 0
|
rlm@123
|
741 0x3E ; D33E ; load 0 into A
|
rlm@123
|
742 0x00 ; D33F
|
rlm@123
|
743
|
rlm@123
|
744 0xEA ; D340 ; load A into v-blank-prev
|
rlm@123
|
745 0x20 ; D341
|
rlm@123
|
746 0xD3 ; D342
|
rlm@118
|
747
|
rlm@123
|
748 0xC3 ; D343
|
rlm@123
|
749 0x1D ; D344
|
rlm@123
|
750 0xD3 ; D345 ; GOTO beginning
|
rlm@123
|
751 ;; END Not-V-Blank
|
rlm@115
|
752
|
rlm@124
|
753
|
rlm@123
|
754 ;; Main State Machine -- Input Section
|
rlm@123
|
755 ;; This is called once every frame.
|
rlm@123
|
756 ;; It collects input and uses it to drive the
|
rlm@123
|
757 ;; state transitions.
|
rlm@123
|
758
|
rlm@123
|
759 ;; Increment frame-count
|
rlm@123
|
760 0xFA ; D346 ; load frame-count into A
|
rlm@123
|
761 0x1F ; D347
|
rlm@123
|
762 0xD3 ; D348
|
rlm@118
|
763
|
rlm@123
|
764 0x3C ; D349 ; inc A
|
rlm@115
|
765
|
rlm@123
|
766 0xEA ; D34A
|
rlm@123
|
767 0x1F ; D34B ; load A into frame-count
|
rlm@123
|
768 0xD3 ; D34C
|
rlm@118
|
769
|
rlm@123
|
770 0x00 ; D34D ; glue :)
|
rlm@118
|
771
|
rlm@118
|
772 0x18 ;D34E ; skip next 3 bytes
|
rlm@118
|
773 0x03 ;D34F
|
rlm@123
|
774 ;D350
|
rlm@118
|
775 (Integer/parseInt "00100000" 2) ; select directional pad
|
rlm@123
|
776 ;D351
|
rlm@118
|
777 (Integer/parseInt "00010000" 2) ; select buttons
|
rlm@118
|
778 0x00 ;D352 ; input-number
|
rlm@118
|
779
|
rlm@123
|
780 ;; select directional pad; store low bits in B
|
rlm@118
|
781
|
rlm@118
|
782 0xFA ;D353 ; load (D350) into A
|
rlm@118
|
783 0x50 ;D354 -->
|
rlm@123
|
784 0xD3 ;D355 --> D350
|
rlm@118
|
785
|
rlm@129
|
786 0xE0 ;D356 ; load (A), which is
|
rlm@118
|
787 0x00 ;D357 --> ; 00010000, into FF00
|
rlm@129
|
788 0x00 ;D358 --> FF00 ;; NO-OP
|
rlm@118
|
789
|
rlm@118
|
790 0x06 ;D359
|
rlm@123
|
791 ;D35A
|
rlm@118
|
792 (Integer/parseInt "11110000" 2) ; "11110000" -> B
|
rlm@129
|
793 0xF0 ;D35B ; (FF00) -> A
|
rlm@118
|
794 0x00 ;D35C
|
rlm@129
|
795 0x00 ;D35D ;; NO-OP
|
rlm@118
|
796
|
rlm@118
|
797 0xCB ;D35E ; swap nybbles on A
|
rlm@118
|
798 0x37 ;D35F
|
rlm@118
|
799 0xA0 ;D360 ; (AND A B) -> A
|
rlm@118
|
800 0x47 ;D361 ; A -> B
|
rlm@118
|
801
|
rlm@123
|
802 ;; select buttons; store bottom bits in C
|
rlm@118
|
803
|
rlm@118
|
804 0xFA ;D362 ; load (D351) into A
|
rlm@118
|
805 0x51 ;D363 -->
|
rlm@123
|
806 0xD3 ;D364 --> D351
|
rlm@118
|
807
|
rlm@129
|
808 0xE0 ;D365 ; load (A), which is
|
rlm@118
|
809 0x00 ;D366 --> ; 00001000, into FF00
|
rlm@129
|
810 0x00 ;D367 --> FF00 ;; NO-OP
|
rlm@118
|
811
|
rlm@118
|
812 0x0E ;D368
|
rlm@118
|
813 ;D369
|
rlm@118
|
814 (Integer/parseInt "00001111" 2) ; "00001111" -> C
|
rlm@118
|
815
|
rlm@129
|
816 0xF0 ;D36A ; (FF00) -> A
|
rlm@118
|
817 0x00 ;D36B
|
rlm@129
|
818 0x00 ;D36C
|
rlm@118
|
819
|
rlm@118
|
820 0xA1 ;D36D ; (AND A C) -> A
|
rlm@118
|
821 0x4F ;D36E ; A -> C
|
rlm@118
|
822
|
rlm@118
|
823 ;; combine the B and C registers into the input number
|
rlm@118
|
824 0x79 ;D36F ; C -> A
|
rlm@118
|
825 0xB0 ;D370 ; (OR A B) -> A
|
rlm@118
|
826 0x2F ;D371 ; negate A
|
rlm@118
|
827
|
rlm@118
|
828 0xEA ;D372 ; store A into input-number
|
rlm@118
|
829 0x52 ;D373
|
rlm@118
|
830 0xD3 ;D374
|
rlm@118
|
831
|
rlm@124
|
832 0x00 ;D375
|
rlm@124
|
833 0x00 ;D376
|
rlm@124
|
834 0x00 ;D377
|
rlm@118
|
835 0x00 ;D378
|
rlm@118
|
836 0x00 ;D379
|
rlm@118
|
837 0x00 ;D37A
|
rlm@118
|
838 0x00 ;D37B ; these are here because
|
rlm@118
|
839 0x00 ;D37C ; I messed up :(
|
rlm@118
|
840 0x00 ;D37D
|
rlm@118
|
841 0x00 ;D37E
|
rlm@118
|
842 0x00 ;D37F
|
rlm@118
|
843
|
rlm@118
|
844 ;; beginning of main state machine
|
rlm@118
|
845 0x18 ;D380 ; Declaration of variables
|
rlm@118
|
846 0x05 ;D381 ; 5 variables:
|
rlm@118
|
847 0x00 ;D382 ; current-mode
|
rlm@122
|
848 0x00 ;D383 ; bytes-to-write
|
rlm@126
|
849 0x00 ;D384 ; bytes-written
|
rlm@126
|
850 0x00 ;D385 ; start-point-high
|
rlm@126
|
851 0x00 ;D386 ; start-point-low
|
rlm@118
|
852
|
rlm@118
|
853
|
rlm@118
|
854 ;; banch on current mode
|
rlm@118
|
855 0xFA ;D387 ; load current-mode (0xD382)
|
rlm@118
|
856 0x82 ;D388 ; into A
|
rlm@118
|
857 0xD3 ;D389
|
rlm@118
|
858 0x00 ;D38A
|
rlm@118
|
859
|
rlm@120
|
860
|
rlm@120
|
861 ;; GOTO Mode 0 (input-mode) if current-mode is 0
|
rlm@118
|
862 0xFE ;D38B
|
rlm@118
|
863 0x00 ;D38C ; compare A with 0x00
|
rlm@118
|
864
|
rlm@120
|
865 0xCA ;D38D ; goto Mode 0 if A == 0
|
rlm@118
|
866 0xA8 ;D38E
|
rlm@118
|
867 0xD3 ;D38F
|
rlm@118
|
868
|
rlm@121
|
869 ;; GOTO Mode 1 (set-length) if current-mode is 1
|
rlm@120
|
870 0xFE ;D390
|
rlm@120
|
871 0x01 ;D391 ; compare A with 0x01
|
rlm@120
|
872
|
rlm@120
|
873 0xCA ;D392
|
rlm@120
|
874 0xB1 ;D393
|
rlm@120
|
875 0xD3 ;D394 ; goto Mode 1 if A == 1
|
rlm@120
|
876
|
rlm@126
|
877 ;; GOTO Mode 2 (set-start-point-high) if current mode is 2
|
rlm@121
|
878 0xFE ;D395
|
rlm@121
|
879 0x02 ;D396 ; compare A with 0x02
|
rlm@121
|
880
|
rlm@121
|
881 0xCA ;D397
|
rlm@121
|
882 0xBF ;D398
|
rlm@121
|
883 0xD3 ;D399 ; goto Mode 2 if A == 2
|
rlm@121
|
884
|
rlm@126
|
885 ;; GOTO Mode 3 (set-start-point-low) if current mode is 3
|
rlm@125
|
886 0xFE ;D39A
|
rlm@125
|
887 0x03 ;D39B
|
rlm@125
|
888
|
rlm@126
|
889 0xCA ;D39C
|
rlm@126
|
890 0xCD ;D39D
|
rlm@126
|
891 0xD3 ;D39E ; goto Mode 3 if A == 3
|
rlm@126
|
892
|
rlm@127
|
893 ;; GOTO Mode 4 (write-memory) if current mode is 4
|
rlm@127
|
894 0xFE ;D39F
|
rlm@127
|
895 0x04 ;D3A0
|
rlm@127
|
896
|
rlm@127
|
897 0xCA ;D3A1
|
rlm@127
|
898 0xDB ;D3A2
|
rlm@127
|
899 0xD3 ;D3A3
|
rlm@127
|
900
|
rlm@118
|
901 0x00 ;D3A4
|
rlm@118
|
902 ;; End of Mode checking, goto beginning
|
rlm@118
|
903 0xC3 ;D3A5
|
rlm@118
|
904 0x1D ;D3A6
|
rlm@118
|
905 0xD3 ;D3A7
|
rlm@120
|
906
|
rlm@120
|
907
|
rlm@120
|
908 ;; Mode 0 -- input-mode mode
|
rlm@120
|
909 ;; means that we are waiting for a mode, so set the mode to
|
rlm@120
|
910 ;; whatever is currently in input-number. If nothing is
|
rlm@120
|
911 ;; entered, then the program stays in input-mode mode
|
rlm@120
|
912
|
rlm@118
|
913 ;; set current-mode to input-number
|
rlm@118
|
914 0xFA ;D3A8 ; load input-number (0xD352)
|
rlm@118
|
915 0x52 ;D3A9 ; into A
|
rlm@118
|
916 0xD3 ;D3AA
|
rlm@118
|
917
|
rlm@118
|
918 0xEA ;D3AB ; load A into current-mode
|
rlm@118
|
919 0x82 ;D3AC ; (0xD382)
|
rlm@118
|
920 0xD3 ;D3AD
|
rlm@118
|
921
|
rlm@118
|
922 0xC3 ;D3AE ; go back to beginning
|
rlm@118
|
923 0x1D ;D3AF
|
rlm@118
|
924 0xD3 ;D3B0
|
rlm@120
|
925 ;; End Mode 0
|
rlm@118
|
926
|
rlm@120
|
927
|
rlm@121
|
928 ;; Mode 1 -- set-length mode
|
rlm@121
|
929 ;; This is the header for writing things to memory.
|
rlm@121
|
930 ;; User specifies the number of bytes to write.
|
rlm@120
|
931 ;; Mode is auto advanced to Mode 2 after this mode
|
rlm@120
|
932 ;; completes.
|
rlm@120
|
933
|
rlm@121
|
934 ;; Set bytes left to write to input-number;
|
rlm@120
|
935 ;; set current-mode to 0x02.
|
rlm@120
|
936 0xFA ;D3B1 ; load input-number (0xD352)
|
rlm@120
|
937 0x52 ;D3B2 ; into A
|
rlm@120
|
938 0xD3 ;D3B3
|
rlm@120
|
939
|
rlm@120
|
940 0xEA ;D3B4 ; load A into bytes-left-to-write
|
rlm@120
|
941 0x83 ;D3B5 ; (0xD383)
|
rlm@120
|
942 0xD3 ;D3B6
|
rlm@120
|
943
|
rlm@121
|
944 0x3E ;D3B7 ; load 0x02 into A.
|
rlm@120
|
945 0x02 ;D3B8
|
rlm@120
|
946
|
rlm@120
|
947 0xEA ;D3B9 ; load A to current-mode
|
rlm@120
|
948 0x82 ;D3BA ; advancing from Mode 1 to
|
rlm@120
|
949 0xD3 ;D3BB ; Mode 2
|
rlm@120
|
950
|
rlm@120
|
951 0xC3 ;D3BC ; go back to beginning
|
rlm@120
|
952 0x1D ;D3BD
|
rlm@120
|
953 0xD3 ;D3BE
|
rlm@120
|
954 ;; End Mode 1
|
rlm@121
|
955
|
rlm@121
|
956
|
rlm@126
|
957 ;; Mode 2 -- set start-point-high mode
|
rlm@126
|
958 ;; Middle part of the header for writing things to memory.
|
rlm@121
|
959 ;; User specifies the start location in RAM to which
|
rlm@121
|
960 ;; data will be written.
|
rlm@121
|
961 ;; Mode is auto advanced to Mode 3 after this mode completes.
|
rlm@121
|
962
|
rlm@126
|
963 ;; Set start-point-high to input-number;
|
rlm@121
|
964 ;; set current mode to 0x03.
|
rlm@121
|
965 0xFA ;D3BF ; load input-number (0xD352)
|
rlm@121
|
966 0x52 ;D3C0 ; into A
|
rlm@121
|
967 0xD3 ;D3C1
|
rlm@121
|
968
|
rlm@126
|
969 0xEA ;D3C2 ; load A into start-point-high
|
rlm@126
|
970 0x85 ;D3C3 ; (0xD385)
|
rlm@121
|
971 0xD3 ;D3C4
|
rlm@121
|
972
|
rlm@121
|
973 0x3E ;D3C5 ; load 0x03 into A.
|
rlm@121
|
974 0x03 ;D3C6
|
rlm@121
|
975
|
rlm@121
|
976 0xEA ;D3C7 ; load A to current-mode,
|
rlm@121
|
977 0x82 ;D3C8 ; advancing from Mode 2 to
|
rlm@121
|
978 0xD3 ;D3C9 ; Mode 3.
|
rlm@120
|
979
|
rlm@121
|
980 0xC3 ;D3CA ; go back to beginning
|
rlm@121
|
981 0x1D ;D3CB
|
rlm@121
|
982 0xD3 ;D3CC
|
rlm@121
|
983 ;;End Mode 2
|
rlm@125
|
984
|
rlm@118
|
985
|
rlm@126
|
986 ;; Mode 3 -- set-start-point-low mode
|
rlm@126
|
987 ;; Final part of header for writing things to memory.
|
rlm@126
|
988 ;; User specifies the low bytes of 16 bit start-point.
|
rlm@126
|
989
|
rlm@126
|
990 ;; Set start-point-low to input-number;
|
rlm@126
|
991 ;; set current mode to 0x04
|
rlm@126
|
992 0xFA ;D3CD ; load input-number into A
|
rlm@126
|
993 0x52 ;D3CE
|
rlm@126
|
994 0xD3 ;D3CF
|
rlm@126
|
995
|
rlm@126
|
996 0xEA ;D3D0 ; load A into start-point-low
|
rlm@126
|
997 0x86 ;D3D1
|
rlm@126
|
998 0xD3 ;D3D2
|
rlm@126
|
999
|
rlm@126
|
1000 0x3E ;D3D3 ; load 0x04 into A.
|
rlm@126
|
1001 0x04 ;D3D4
|
rlm@126
|
1002
|
rlm@126
|
1003 0xEA ;D3D5 ; load A to current-mode,
|
rlm@126
|
1004 0x82 ;D3D6 ; advancing from Mode 3 to
|
rlm@126
|
1005 0xD3 ;D3D7 ; Mode 4.
|
rlm@126
|
1006
|
rlm@126
|
1007 0xC3 ;D3D8 ; go back to beginning
|
rlm@126
|
1008 0x1D ;D3D9
|
rlm@126
|
1009 0xD3 ;D3DA
|
rlm@126
|
1010
|
rlm@126
|
1011 ;; Mode 4 -- write bytes mode
|
rlm@125
|
1012
|
rlm@125
|
1013 ;; This is where RAM manipulation happens. User supplies
|
rlm@125
|
1014 ;; bytes every frame, which are written sequentially to
|
rlm@125
|
1015 ;; start-point until bytes-to-write have been written. Once
|
rlm@125
|
1016 ;; bytes-to-write have been written, the mode is reset to 0.
|
rlm@125
|
1017
|
rlm@125
|
1018 ;; compare bytes-written with bytes-to-write.
|
rlm@125
|
1019 ;; if they are the same, then reset mode to 0
|
rlm@125
|
1020
|
rlm@126
|
1021 0xFA ;D3DB ; load bytes-to-write into A
|
rlm@126
|
1022 0x83 ;D3DC
|
rlm@126
|
1023 0xD3 ;D3DD
|
rlm@127
|
1024
|
rlm@126
|
1025 0x47 ;D3DE ; load A into B
|
rlm@127
|
1026
|
rlm@126
|
1027 0xFA ;D3DF ; load bytes-written into A
|
rlm@126
|
1028 0x84 ;D3E0
|
rlm@126
|
1029 0xD3 ;D3E1
|
rlm@127
|
1030
|
rlm@126
|
1031 0xB8 ;D3E2 ; compare A with B
|
rlm@127
|
1032
|
rlm@126
|
1033 0xCA ;D3E3 ; if they are equal, go to cleanup
|
rlm@127
|
1034 0x07 ;D3E4
|
rlm@127
|
1035 0xD4 ;D3E5
|
rlm@125
|
1036
|
rlm@125
|
1037 ;; Write Memory Section
|
rlm@125
|
1038 ;; Write the input-number, interpreted as an 8-bit number,
|
rlm@125
|
1039 ;; into the current target register, determined by
|
rlm@125
|
1040 ;; (+ start-point bytes-written).
|
rlm@125
|
1041 ;; Then, increment bytes-written by 1.
|
rlm@125
|
1042
|
rlm@127
|
1043 0xFA ;D3E6 ; load start-point-high into A
|
rlm@127
|
1044 0x85 ;D3E7
|
rlm@127
|
1045 0xD3 ;D3E8
|
rlm@127
|
1046
|
rlm@127
|
1047 0x67 ;D3E9 ; load A into H
|
rlm@127
|
1048
|
rlm@127
|
1049 0xFA ;D3EA ; load start-point-low into A
|
rlm@127
|
1050 0x86 ;D3EB
|
rlm@127
|
1051 0xD3 ;D3EC
|
rlm@127
|
1052
|
rlm@127
|
1053 0x6F ;D3ED ; load A into L
|
rlm@127
|
1054
|
rlm@127
|
1055 0xFA ;D3EE ; load bytes-written into A
|
rlm@127
|
1056 0x84 ;D3EF
|
rlm@127
|
1057 0xD3 ;D3F0
|
rlm@127
|
1058
|
rlm@127
|
1059 0x00 ;D3F1 ; These are here because
|
rlm@127
|
1060 0x00 ;D3F2 ; I screwed up again.
|
rlm@125
|
1061 0x00 ;D3F3
|
rlm@125
|
1062
|
rlm@127
|
1063 0x85 ;D3F4 ; add L to A; store A in L.
|
rlm@127
|
1064 0x6F ;D3F5
|
rlm@121
|
1065
|
rlm@127
|
1066 0x30 ;D3F6 ; If the addition overflowed,
|
rlm@127
|
1067 0x01 ;D3F7
|
rlm@127
|
1068 0x24 ;D3F8 ; increment H.
|
rlm@118
|
1069
|
rlm@127
|
1070 ;; Now, HL points to the correct place in memory
|
rlm@127
|
1071
|
rlm@127
|
1072 0xFA ;D3F9 ; load input-number into A
|
rlm@127
|
1073 0x52 ;D3FA
|
rlm@127
|
1074 0xD3 ;D3FB
|
rlm@118
|
1075
|
rlm@127
|
1076 0x77 ;D3FC ; load A into (HL)
|
rlm@118
|
1077
|
rlm@127
|
1078 0xFA ;D3FD ; load bytes-written into A
|
rlm@127
|
1079 0x84 ;D3FE
|
rlm@127
|
1080 0xD3 ;D3FF
|
rlm@118
|
1081
|
rlm@127
|
1082 0x3C ;D400 ; increment A
|
rlm@127
|
1083
|
rlm@127
|
1084 0xEA ;D401 ; load A into bytes-written
|
rlm@127
|
1085 0x84 ;D402
|
rlm@127
|
1086 0xD3 ;D403
|
rlm@127
|
1087
|
rlm@127
|
1088 0xC3 ;D404 ; go back to beginning.
|
rlm@127
|
1089 0x1D ;D405
|
rlm@127
|
1090 0xD3 ;D406
|
rlm@127
|
1091 ;; End Write Memory Section
|
rlm@127
|
1092
|
rlm@127
|
1093 ;; Mode 4 Cleanup Section
|
rlm@127
|
1094 ;; reset bytes-written to 0
|
rlm@127
|
1095 ;; set mode to 0
|
rlm@127
|
1096 0x3E ;D407 ; load 0 into A
|
rlm@127
|
1097 0x00 ;D408
|
rlm@127
|
1098
|
rlm@127
|
1099 0xEA ;D409 ; load A into bytes-written
|
rlm@127
|
1100 0x84 ;D40A
|
rlm@127
|
1101 0xD3 ;D40B
|
rlm@127
|
1102
|
rlm@127
|
1103 0xEA ;D40C ; load A into current-mode
|
rlm@127
|
1104 0x82 ;D40D
|
rlm@127
|
1105 0xD3 ;D40E
|
rlm@127
|
1106
|
rlm@127
|
1107 0xC3 ;D40F ; go back to beginning
|
rlm@127
|
1108 0x1D ;D410
|
rlm@127
|
1109 0xD3 ;D411
|
rlm@127
|
1110
|
rlm@127
|
1111 ;; End Mode 4
|
rlm@118
|
1112
|
rlm@118
|
1113 ])
|
rlm@118
|
1114
|
rlm@118
|
1115
|
rlm@123
|
1116
|
rlm@118
|
1117 (def frame-count 0xD31F)
|
rlm@118
|
1118 (def input 0xD352)
|
rlm@127
|
1119 (def current-mode 0xD382)
|
rlm@127
|
1120 (def bytes-to-write 0xD383)
|
rlm@127
|
1121 (def bytes-written 0xD384)
|
rlm@127
|
1122 (def start-point-high 0xD385)
|
rlm@127
|
1123 (def start-point-low 0xD386)
|
rlm@126
|
1124
|
rlm@127
|
1125
|
rlm@118
|
1126
|
rlm@118
|
1127 (defn write-memory []
|
rlm@118
|
1128 (-> (tick (mid-game))
|
rlm@118
|
1129 (IE! 0) ; disable interrupts
|
rlm@118
|
1130 (inject-item-assembly (write-memory-assembly))))
|
rlm@119
|
1131
|
rlm@119
|
1132 (defn test-write-memory []
|
rlm@119
|
1133 (set-state! (write-memory))
|
rlm@119
|
1134 (dorun
|
rlm@119
|
1135 (dotimes [_ 5000]
|
rlm@119
|
1136 (view-memory (step @current-state) current-mode))))
|
rlm@122
|
1137
|
rlm@122
|
1138 (def bytes-to-write 0xD383)
|
rlm@122
|
1139 (def start-point 0xD384)
|
rlm@122
|
1140
|
rlm@125
|
1141 (defn print-blank-assembly
|
rlm@125
|
1142 [start end]
|
rlm@125
|
1143 (dorun
|
rlm@125
|
1144 (map
|
rlm@125
|
1145 #(println (format "0x00 ;%04X " %))
|
rlm@125
|
1146 (range start end))))
|
rlm@125
|
1147
|
rlm@122
|
1148 (defn test-mode-2 []
|
rlm@122
|
1149 (->
|
rlm@122
|
1150 (write-memory)
|
rlm@123
|
1151 (view-memory frame-count)
|
rlm@122
|
1152 (step)
|
rlm@122
|
1153 (step [:a])
|
rlm@122
|
1154 (step [:b])
|
rlm@122
|
1155 (step [:start])
|
rlm@122
|
1156 (step [])
|
rlm@123
|
1157 (view-memory frame-count)))
|
rlm@123
|
1158
|
rlm@128
|
1159 (defn test-mode-4
|
rlm@128
|
1160 ([] (test-mode-4 (write-memory)))
|
rlm@128
|
1161 ([target-state]
|
rlm@128
|
1162 (->
|
rlm@128
|
1163 target-state
|
rlm@128
|
1164 (#(do (println "memory from 0xC00F to 0xC01F:"
|
rlm@128
|
1165 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
|
rlm@128
|
1166 (view-memory current-mode)
|
rlm@128
|
1167 (step [])
|
rlm@128
|
1168 (step [])
|
rlm@128
|
1169 (step [])
|
rlm@128
|
1170 (#(do (println "after three steps") %))
|
rlm@128
|
1171 (view-memory current-mode)
|
rlm@127
|
1172
|
rlm@128
|
1173 ;; Activate memory writing mode
|
rlm@128
|
1174
|
rlm@128
|
1175 (#(do (println "step with [:a]") %))
|
rlm@128
|
1176 (step [:a])
|
rlm@128
|
1177 (view-memory current-mode)
|
rlm@128
|
1178 (view-memory bytes-to-write)
|
rlm@128
|
1179 (view-memory start-point-high)
|
rlm@128
|
1180 (view-memory start-point-low)
|
rlm@127
|
1181
|
rlm@128
|
1182 ;; Specify four bytes to be written
|
rlm@128
|
1183
|
rlm@128
|
1184 (#(do (println "step with [:select]")%))
|
rlm@128
|
1185 (step [:select])
|
rlm@128
|
1186 (view-memory current-mode)
|
rlm@128
|
1187 (view-memory bytes-to-write)
|
rlm@128
|
1188 (view-memory start-point-high)
|
rlm@128
|
1189 (view-memory start-point-low)
|
rlm@127
|
1190
|
rlm@128
|
1191 ;; Specify target memory address as 0xC00F
|
rlm@128
|
1192
|
rlm@128
|
1193 (#(do (println "step with [:u :d]")%))
|
rlm@128
|
1194 (step [:u :d])
|
rlm@128
|
1195 (view-memory current-mode)
|
rlm@128
|
1196 (view-memory bytes-to-write)
|
rlm@128
|
1197 (view-memory start-point-high)
|
rlm@128
|
1198 (view-memory start-point-low)
|
rlm@127
|
1199
|
rlm@128
|
1200 (#(do (println "step with [:a :b :start :select]")%))
|
rlm@128
|
1201 (step [:a :b :start :select])
|
rlm@128
|
1202 (view-memory current-mode)
|
rlm@128
|
1203 (view-memory bytes-to-write)
|
rlm@128
|
1204 (view-memory start-point-high)
|
rlm@128
|
1205 (view-memory start-point-low)
|
rlm@127
|
1206
|
rlm@128
|
1207 ;; Start reprogramming memory
|
rlm@127
|
1208
|
rlm@128
|
1209 (#(do (println "step with [:a]")%))
|
rlm@128
|
1210 (step [:a])
|
rlm@128
|
1211 (view-memory current-mode)
|
rlm@128
|
1212 (view-memory bytes-written)
|
rlm@127
|
1213
|
rlm@128
|
1214 (#(do (println "step with [:b]")%))
|
rlm@128
|
1215 (step [:b])
|
rlm@128
|
1216 (view-memory current-mode)
|
rlm@128
|
1217 (view-memory bytes-written)
|
rlm@127
|
1218
|
rlm@128
|
1219 (#(do (println "step with [:a :b]")%))
|
rlm@128
|
1220 (step [:a :b])
|
rlm@128
|
1221 (view-memory current-mode)
|
rlm@128
|
1222 (view-memory bytes-written)
|
rlm@127
|
1223
|
rlm@128
|
1224 (#(do (println "step with [:select]")%))
|
rlm@128
|
1225 (step [:select])
|
rlm@128
|
1226 (view-memory current-mode)
|
rlm@128
|
1227 (view-memory bytes-written)
|
rlm@127
|
1228
|
rlm@128
|
1229 ;; Reprogramming done, program ready for more commands.
|
rlm@127
|
1230
|
rlm@128
|
1231 (#(do (println "step with []")%))
|
rlm@128
|
1232 (step [])
|
rlm@128
|
1233 (view-memory current-mode)
|
rlm@128
|
1234 (view-memory bytes-written)
|
rlm@128
|
1235
|
rlm@128
|
1236 (#(do (println "memory from 0xC00F to 0xC01F:"
|
rlm@128
|
1237 (subvec (vec (memory %)) 0xC00F 0xC01F)) %)))))
|
rlm@123
|
1238
|