view clojure/com/aurellem/assembly.clj @ 127:901ee6b648da

preliminary memory bootstrapping program complete.
author Robert McIntyre <rlm@mit.edu>
date Sat, 17 Mar 2012 03:43:42 -0500
parents 86f92deacd2a
children 203d64e16156
line wrap: on
line source
1 (ns com.aurellem.assembly
2 (:use (com.aurellem gb-driver vbm title items))
3 (:import [com.aurellem.gb_driver SaveState]))
5 (defn mid-game []
6 (read-state "mid-game"))
8 (defn inject-assembly
9 ([^SaveState state
10 program-counter registers
11 assembly-code]
12 (let [scratch-memory (memory state)]
13 ;; inject assembly code
14 (dorun (map (fn [index val]
15 (aset scratch-memory index val))
16 (range program-counter
17 (+ program-counter (count assembly-code)))
18 assembly-code))
19 (-> state
20 (write-memory! scratch-memory)
21 (write-registers! registers)
22 (PC! program-counter)))))
24 (defn inject-item-assembly
25 ([^SaveState state assembly-code]
26 (inject-assembly state (inc item-list-start)
27 (registers state)
28 assembly-code))
29 ([assembly-code]
30 (inject-item-assembly @current-state assembly-code)))
32 (defn info
33 ([^SaveState state]
34 (println (format "PC: 0x%04X" (PC state)))
35 (println "Instruction:"
36 (format "0x%02X" (aget (memory state) (PC state))))
37 state))
39 (defn print-interrupt
40 [^SaveState state]
41 (println (format "IE: %d" (IE state)))
42 state)
44 (defn print-listing [state begin end]
45 (dorun (map
46 (fn [opcode line]
47 (println (format "0x%04X: 0x%02X" line opcode)))
48 (subvec (vec (memory state)) begin end)
49 (range begin end)))
50 state)
52 (defn run-assembly
53 ([info-fn assembly n]
54 (let [final-state
55 (reduce (fn [state _]
56 (tick (info-fn state)))
57 (inject-item-assembly
58 (mid-game) assembly)
59 (range n))]
60 final-state))
61 ([assembly n]
62 (run-assembly info assembly n)))
64 (def buttons-port 0xFF00)
66 (defn A [state]
67 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
69 (defn binary-str [num]
70 (format "%08d"
71 (Integer/parseInt
72 (Integer/toBinaryString num) 10)))
74 (defn view-register [state name reg-fn]
75 (println (format "%s: %s" name
76 (binary-str (reg-fn state))))
77 state)
79 (defn view-memory [state mem]
80 (println (format "mem 0x%04X = %s" mem
81 (binary-str (aget (memory state) mem))))
82 state)
84 (defn trace [state]
85 (loop [program-counters [(first (registers @current-state)) ]
86 opcodes [(aget (memory @current-state) (PC @current-state))]]
87 (let [frame-boundary?
88 (com.aurellem.gb.Gb/tick)]
89 (if frame-boundary?
90 [program-counters opcodes]
91 (recur
92 (conj program-counters
93 (first (registers @current-state)))
94 (conj opcodes
95 (aget (memory @current-state)
96 (PC @current-state))))))))
98 (defn print-trace [state n]
99 (let [[program-counters opcodes] (trace state)]
100 (dorun (map (fn [pc op] (println (format "%04X: 0x%02X" pc op)))
101 (take n program-counters)
102 (take n opcodes)))))
104 (defn good-trace []
105 (-> (mid-game) (tick) (IE! 0)
106 (set-inv-mem [0x00 0x00 0X00 0x00])
107 (PC! item-list-start)(print-interrupt)
108 (info) (tick) (info) (tick) (info)))
110 (defn read-down-button []
111 (-> (tick (mid-game))
112 (IE! 0) ; disable interrupts
113 (inject-item-assembly
114 ;; write 00010000 to 0xFF00 to select joypad
115 [0x18 ;D31D ; jump over
116 0x01 ;D31E ; the next 8 bits
117 ;D31F
118 (Integer/parseInt "00100000" 2) ; data section
120 0xFA ;D320 ; load (D31F) into A
121 0x1F ;D321 -->
122 0xD3 ;D322 --> D31F
124 0xEA ;D323 ; load (A), which is
125 0x00 ;D324 --> ; 00010000, into FF00
126 0xFF ;D325 --> FF00
128 0x18 ;D326 ; this is the place where
129 0x01 ;D327 ; we will store whether
130 0x00 ;D328 ; "down" is pressed.
132 0xFA ;D329 ; (FF00) -> A
133 0x00 ;D32A
134 0xFF ;D32B
136 0xCB ;D32C ; Test whether "down"
137 0x5F ;D32D ; is pressed.
139 0x28 ;D32E ; if down is pressed,
140 0x03 ;D32F ; skip the next section
141 ; of code.
142 ;; down-is-not-pressed
143 0xC3 ;D330
144 0x1D ;D331 ; return to beginning
145 0xD3 ;D332
147 ;; down-is-pressed
148 0xEA ;D334 ; write A to D328 if
149 0x28 ;D335 ; "down" was pressed
150 0xD3 ;D336
152 0xC3 ;D330
153 0x1D ;D331 ; return to beginning
154 0xD3 ;D332
155 ])))
157 (defn test-read-down []
158 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)
159 (view-memory (step (step (read-down-button))) 0xD328)))
161 (defn count-frames []
162 (-> (tick (mid-game))
163 (IE! 0) ; disable interrupts
164 (inject-item-assembly
165 [0x18 ;D31D ; jump over
166 0x02 ;D31E ; the next 2 bytes
167 0x00 ;D31F ; frame-count
168 0x00 ;D320 ; v-blank-prev
170 0xFA ;D321
171 0x41 ;D322 ; load (FF41) into A
172 0xFF ;D323 ; this contains mode flags
174 ;; if we're in v-blank, the bit-1 is 0
175 ;; and bit-2 is 1 Otherwise, it is not v-blank.
176 0xCB ;D324 ; test bit-1 of A
177 0x4F ;D325
179 0xC2 ;D326 ; if bit-1 is not 0
180 0x44 ;D327 ; GOTO not-v-blank
181 0xD3 ;D328
183 0xCB ;D329 ; test bit-0 of A
184 0x47 ;D32A
186 0xCA ;D32B ; if bit-0 is not 1
187 0x44 ;D32C ; GOTO not-v-blank
188 0xD3 ;D32D
189 ;;; in v-blank mode
190 ;; if v-blank-prev was 0,
191 ;; increment frame-count
193 0xFA ;D32E ; load v-blank-prev to A
194 0x20 ;D32F
195 0xD3 ;D330
197 0xCB ;D331
198 0x47 ;D332 ; test bit-0 of A
200 0x20 ;D333 ; skip next section
201 0x07 ;D334 ; if v-blank-prev was not zero
203 ;; v-blank was 0, increment frame-count
204 0xFA ;D335 ; load frame-count into A
205 0x1F ;D336
206 0xD3 ;D337
208 0x3C ;D338 ; inc A
210 0xEA ;D339 ; load A into frame-count
211 0x1F ;D33A
212 0xD3 ;D33B
214 ;; set v-blank-prev to 1
215 0x3E ;D33C ; load 1 into A
216 0x01 ;D33D
218 0xEA ;D33E ; load A into v-blank-prev
219 0x20 ;D33F
220 0xD3 ;D340
222 0xC3 ;D341 ; return to beginning
223 0x1D ;D342
224 0xD3 ;D343
226 ;;; not in v-blank mode
227 ;; set v-blank-prev to 0
228 0x3E ;D344 ; load 0 into A
229 0x00 ;D345
231 0xEA ;D346 ; load A into v-blank-prev
232 0x20 ;D347
233 0xD3 ;D348
235 0xC3 ;D349 ; return to beginning
236 0x1D ;D34A
237 0xD3 ;D34B
238 ])))
240 (defn step-count-frames []
241 (-> (read-down-button)
242 (info)
243 (tick) ;; skip over data section
244 (info)
245 (view-register "Register A" A)
246 (tick) ;; load-data into A
247 (view-register "Register A" A)
248 (info)
249 (view-memory 0xFF00)
250 (tick) ;; load A into 0xFF00
251 (view-memory 0xFF00)
252 (info)
253 (tick)
254 (info)
255 (tick)
256 (info)
257 (tick)
258 (info)
259 (tick)
260 (info)
261 (tick)
262 (info)
263 (tick)
264 (print-inventory)))
266 (defn test-count-frames []
267 (= 255 (aget (memory ((apply comp (repeat 255 step))
268 (count-frames)))
269 0xD31F)))
271 ;; specs for main bootstrap program
272 ;; starts in "mode-select" mode
273 ;; Each button press takes place in a single frame.
274 ;; mode-select-mode takes one of the main buttons
275 ;; which selects one of up to eight modes
276 ;; mode 1 activated by the "A" button
277 ;; the next two button presses indicates the start
278 ;; memory location which to which the bootstrap
279 ;; program will write.
280 ;; This is done by using each of the eight buttons to
281 ;; spell out an 8 bit number. The order of buttons is
282 ;; [:d :u :l :r :start :select :b :a]
283 ;; [:a :start :l] --> 00101001
285 ;; the next button press determines how many bytes are to be
286 ;; written, starting at the start position.
288 ;; then, the actual bytes are entered and are written to the
289 ;; start address in sequence.
291 (defn input-number-assembly []
292 [0x18 ;D31D ; jump over
293 0x02 ;D31E ; the next 2 bytes
294 0x00 ;D31F ; frame-count
295 0x00 ;D320 ; v-blank-prev
297 0xFA ;D321
298 0x41 ;D322 ; load (FF41) into A
299 0xFF ;D323 ; this contains mode flags
301 ;; if we're in v-blank, the bit-1 is 0
302 ;; and bit-2 is 1 Otherwise, it is not v-blank.
303 0xCB ;D324 ; test bit-1 of A
304 0x4F ;D325
306 0xC2 ;D326 ; if bit-1 is not 0
307 0x44 ;D327 ; GOTO not-v-blank
308 0xD3 ;D328
310 0xCB ;D329 ; test bit-0 of A
311 0x47 ;D32A
313 0xCA ;D32B ; if bit-0 is not 1
314 0x44 ;D32C ; GOTO not-v-blank
315 0xD3 ;D32D
317 ;;; in v-blank mode
319 ;; if v-blank-prev was 0,
320 ;; increment frame-count
322 0xFA ;D32E ; load v-blank-prev to A
323 0x20 ;D32F
324 0xD3 ;D330
326 0xCB ;D331
327 0x47 ;D332 ; test bit-0 of A
329 0x20 ;D333 ; skip next section
330 0x07 ;D334 ; if v-blank-prev was not zero
332 ;; v-blank was 0, increment frame-count
333 0xFA ;D335 ; load frame-count into A
334 0x1F ;D336
335 0xD3 ;D337
337 0x3C ;D338 ; inc A
339 0xEA ;D339 ; load A into frame-count
340 0x1F ;D33A
341 0xD3 ;D33B
343 ;; set v-blank-prev to 1
344 0x3E ;D33C ; load 1 into A
345 0x01 ;D33D
347 0xEA ;D33E ; load A into v-blank-prev
348 0x20 ;D33F
349 0xD3 ;D340
351 0xC3 ;D341 ; GOTO input handling code
352 0x4E ;D342
353 0xD3 ;D343
355 ;;; not in v-blank mode
356 ;; set v-blank-prev to 0
357 0x3E ;D344 ; load 0 into A
358 0x00 ;D345
360 0xEA ;D346 ; load A into v-blank-prev
361 0x20 ;D347
362 0xD3 ;D348
364 0xC3 ;D349 ; return to beginning
365 0x1D ;D34A
366 0xD3 ;D34B
368 0x00 ;D34C ; these are here
369 0x00 ;D34D ; for glue
372 ;;; calculate input number based on button presses
373 0x18 ;D34E ; skip next 3 bytes
374 0x03 ;D34F
375 ;D350
376 (Integer/parseInt "00100000" 2) ; select directional pad
377 ;D351
378 (Integer/parseInt "00010000" 2) ; select buttons
379 0x00 ;D352 ; input-number
381 ;; select directional pad, store low bits in B
383 0xFA ;D353 ; load (D350) into A
384 0x50 ;D354 -->
385 0xD3 ;D355 --> D31F
387 0xEA ;D356 ; load (A), which is
388 0x00 ;D357 --> ; 00010000, into FF00
389 0xFF ;D358 --> FF00
391 0x06 ;D359
392 ;D35A
393 (Integer/parseInt "11110000" 2) ; "11110000" -> B
394 0xFA ;D35B ; (FF00) -> A
395 0x00 ;D35C
396 0xFF ;D35D
398 0xCB ;D35E ; swap nybbles on A
399 0x37 ;D35F
400 0xA0 ;D360 ; (AND A B) -> A
401 0x47 ;D361 ; A -> B
403 ;; select buttons store bottom bits in C
405 0xFA ; ; load (D351) into A
406 0x51 ; -->
407 0xD3 ; --> D31F
409 0xEA ; ; load (A), which is
410 0x00 ; --> ; 00001000, into FF00
411 0xFF ; --> FF00
413 0x0E ;
414 (Integer/parseInt "00001111" 2) ; "00001111" -> C
416 0xFA ; ; (FF00) -> A
417 0x00 ;
418 0xFF ;
420 0xA1 ; ; (AND A C) -> A
421 0x4F ; ; A -> C
423 ;; combine the B and C registers into the input number
424 0x79 ; ; C -> A
425 0xB0 ; ; (OR A B) -> A
426 0x2F ; ; negate A
428 0xEA ; ; store A into input-number
429 0x52 ;
430 0xD3 ;
432 0xC3 ; ; return to beginning
433 0x1D ;
434 0xD3 ;
435 ])
437 (defn input-number []
438 (-> (tick (mid-game))
439 (IE! 0) ; disable interrupts
440 (inject-item-assembly (input-number-assembly))))
442 (defn test-input-number
443 "Input freestyle buttons and observe the effects at the repl."
444 []
445 (set-state! (input-number))
446 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
448 (defn write-memory-assembly []
450 [
451 ;; Main Timing Loop
452 ;; Constantly check for v-blank and Trigger main state machine on
453 ;; every transtion from v-blank to non-v-blank.
455 0x18 ; D31D ; Variable declaration
456 0x02 ; D31E
457 0x00 ; D31F ; frame-count
458 0x00 ; D320 ; v-blank-prev
460 0xFA ; D321 ; load v-blank mode flags into A
461 0x41 ; D322
462 0xFF ; D323
464 ;; Branch dependent on v-blank. v-blank happens when the last two
465 ;; bits in A are "01"
466 0xCB ; D324
467 0x4F ; D325
469 0xC2 ; D326 ; if bit-1 is not 0, then
470 0x3E ; D327 ; GOTO non-v-blank.
471 0xD3 ; D328
473 0xCB ; D329
474 0x47 ; D32A
476 0xCA ; D32B ; if bit-0 is not 1, then
477 0x3E ; D32C ; GOTO non-v-blank.
478 0xD3 ; D32D
480 ;; V-Blank
481 ;; Activate state-machine if this is a transition event.
483 0xFA ; D32E ; load v-bank-prev into A
484 0x20 ; D32F
485 0xD3 ; D330
487 0xFE ; D331 ; compare A to 0. >--------\
488 0x00 ; D332 \
489 ; |
490 ;; set v-blank-prev to 1. |
491 0x3E ; D333 ; load 1 into A. |
492 0x01 ; D334 |
493 ; |
494 0xEA ; D335 ; load A into v-blank-prev |
495 0x20 ; D336 |
496 0xD3 ; D337 |
497 ; /
498 ;; if v-blank-prev was 0, activate state-machine <------/
499 0xCA ; D338 ; if v-blank-prev
500 0x46 ; D339 ; was 0,
501 0xD3 ; D33A ; GOTO state-machine
503 0xC3 ; D33B
504 0x1D ; D33C
505 0xD3 ; D33D ; GOTO beginning
506 ;; END V-blank
508 ;; Non-V-Blank
509 ;; Set v-blank-prev to 0
510 0x3E ; D33E ; load 0 into A
511 0x00 ; D33F
513 0xEA ; D340 ; load A into v-blank-prev
514 0x20 ; D341
515 0xD3 ; D342
517 0xC3 ; D343
518 0x1D ; D344
519 0xD3 ; D345 ; GOTO beginning
520 ;; END Not-V-Blank
523 ;; Main State Machine -- Input Section
524 ;; This is called once every frame.
525 ;; It collects input and uses it to drive the
526 ;; state transitions.
528 ;; Increment frame-count
529 0xFA ; D346 ; load frame-count into A
530 0x1F ; D347
531 0xD3 ; D348
533 0x3C ; D349 ; inc A
535 0xEA ; D34A
536 0x1F ; D34B ; load A into frame-count
537 0xD3 ; D34C
539 0x00 ; D34D ; glue :)
542 ;;;;;;;;; BEGIN RLM DEBUG
543 ;;0xC3 ;; jump to beginning
544 ;;0x1D
545 ;;0xD3
546 ;;;;;;;;; END RLM DEBUG
549 0x18 ;D34E ; skip next 3 bytes
550 0x03 ;D34F
551 ;D350
552 (Integer/parseInt "00100000" 2) ; select directional pad
553 ;D351
554 (Integer/parseInt "00010000" 2) ; select buttons
555 0x00 ;D352 ; input-number
557 ;; select directional pad; store low bits in B
559 0xFA ;D353 ; load (D350) into A
560 0x50 ;D354 -->
561 0xD3 ;D355 --> D350
563 0xEA ;D356 ; load (A), which is
564 0x00 ;D357 --> ; 00010000, into FF00
565 0xFF ;D358 --> FF00
567 0x06 ;D359
568 ;D35A
569 (Integer/parseInt "11110000" 2) ; "11110000" -> B
570 0xFA ;D35B ; (FF00) -> A
571 0x00 ;D35C
572 0xFF ;D35D
574 0xCB ;D35E ; swap nybbles on A
575 0x37 ;D35F
576 0xA0 ;D360 ; (AND A B) -> A
577 0x47 ;D361 ; A -> B
579 ;; select buttons; store bottom bits in C
581 0xFA ;D362 ; load (D351) into A
582 0x51 ;D363 -->
583 0xD3 ;D364 --> D351
585 0xEA ;D365 ; load (A), which is
586 0x00 ;D366 --> ; 00001000, into FF00
587 0xFF ;D367 --> FF00
589 0x0E ;D368
590 ;D369
591 (Integer/parseInt "00001111" 2) ; "00001111" -> C
593 0xFA ;D36A ; (FF00) -> A
594 0x00 ;D36B
595 0xFF ;D36C
597 0xA1 ;D36D ; (AND A C) -> A
598 0x4F ;D36E ; A -> C
600 ;; combine the B and C registers into the input number
601 0x79 ;D36F ; C -> A
602 0xB0 ;D370 ; (OR A B) -> A
603 0x2F ;D371 ; negate A
605 0xEA ;D372 ; store A into input-number
606 0x52 ;D373
607 0xD3 ;D374
609 0x00 ;D375
610 0x00 ;D376
611 0x00 ;D377
612 0x00 ;D378
613 0x00 ;D379
614 0x00 ;D37A
615 0x00 ;D37B ; these are here because
616 0x00 ;D37C ; I messed up :(
617 0x00 ;D37D
618 0x00 ;D37E
619 0x00 ;D37F
621 ;; beginning of main state machine
622 0x18 ;D380 ; Declaration of variables
623 0x05 ;D381 ; 5 variables:
624 0x00 ;D382 ; current-mode
625 0x00 ;D383 ; bytes-to-write
626 0x00 ;D384 ; bytes-written
627 0x00 ;D385 ; start-point-high
628 0x00 ;D386 ; start-point-low
631 ;; banch on current mode
632 0xFA ;D387 ; load current-mode (0xD382)
633 0x82 ;D388 ; into A
634 0xD3 ;D389
635 0x00 ;D38A
638 ;; GOTO Mode 0 (input-mode) if current-mode is 0
639 0xFE ;D38B
640 0x00 ;D38C ; compare A with 0x00
642 0xCA ;D38D ; goto Mode 0 if A == 0
643 0xA8 ;D38E
644 0xD3 ;D38F
646 ;; GOTO Mode 1 (set-length) if current-mode is 1
647 0xFE ;D390
648 0x01 ;D391 ; compare A with 0x01
650 0xCA ;D392
651 0xB1 ;D393
652 0xD3 ;D394 ; goto Mode 1 if A == 1
654 ;; GOTO Mode 2 (set-start-point-high) if current mode is 2
655 0xFE ;D395
656 0x02 ;D396 ; compare A with 0x02
658 0xCA ;D397
659 0xBF ;D398
660 0xD3 ;D399 ; goto Mode 2 if A == 2
662 ;; GOTO Mode 3 (set-start-point-low) if current mode is 3
663 0xFE ;D39A
664 0x03 ;D39B
666 0xCA ;D39C
667 0xCD ;D39D
668 0xD3 ;D39E ; goto Mode 3 if A == 3
670 ;; GOTO Mode 4 (write-memory) if current mode is 4
671 0xFE ;D39F
672 0x04 ;D3A0
674 0xCA ;D3A1
675 0xDB ;D3A2
676 0xD3 ;D3A3
678 0x00 ;D3A4
679 ;; End of Mode checking, goto beginning
680 0xC3 ;D3A5
681 0x1D ;D3A6
682 0xD3 ;D3A7
685 ;; Mode 0 -- input-mode mode
686 ;; means that we are waiting for a mode, so set the mode to
687 ;; whatever is currently in input-number. If nothing is
688 ;; entered, then the program stays in input-mode mode
690 ;; set current-mode to input-number
691 0xFA ;D3A8 ; load input-number (0xD352)
692 0x52 ;D3A9 ; into A
693 0xD3 ;D3AA
695 0xEA ;D3AB ; load A into current-mode
696 0x82 ;D3AC ; (0xD382)
697 0xD3 ;D3AD
699 0xC3 ;D3AE ; go back to beginning
700 0x1D ;D3AF
701 0xD3 ;D3B0
702 ;; End Mode 0
705 ;; Mode 1 -- set-length mode
706 ;; This is the header for writing things to memory.
707 ;; User specifies the number of bytes to write.
708 ;; Mode is auto advanced to Mode 2 after this mode
709 ;; completes.
711 ;; Set bytes left to write to input-number;
712 ;; set current-mode to 0x02.
713 0xFA ;D3B1 ; load input-number (0xD352)
714 0x52 ;D3B2 ; into A
715 0xD3 ;D3B3
717 0xEA ;D3B4 ; load A into bytes-left-to-write
718 0x83 ;D3B5 ; (0xD383)
719 0xD3 ;D3B6
721 0x3E ;D3B7 ; load 0x02 into A.
722 0x02 ;D3B8
724 0xEA ;D3B9 ; load A to current-mode
725 0x82 ;D3BA ; advancing from Mode 1 to
726 0xD3 ;D3BB ; Mode 2
728 0xC3 ;D3BC ; go back to beginning
729 0x1D ;D3BD
730 0xD3 ;D3BE
731 ;; End Mode 1
734 ;; Mode 2 -- set start-point-high mode
735 ;; Middle part of the header for writing things to memory.
736 ;; User specifies the start location in RAM to which
737 ;; data will be written.
738 ;; Mode is auto advanced to Mode 3 after this mode completes.
740 ;; Set start-point-high to input-number;
741 ;; set current mode to 0x03.
742 0xFA ;D3BF ; load input-number (0xD352)
743 0x52 ;D3C0 ; into A
744 0xD3 ;D3C1
746 0xEA ;D3C2 ; load A into start-point-high
747 0x85 ;D3C3 ; (0xD385)
748 0xD3 ;D3C4
750 0x3E ;D3C5 ; load 0x03 into A.
751 0x03 ;D3C6
753 0xEA ;D3C7 ; load A to current-mode,
754 0x82 ;D3C8 ; advancing from Mode 2 to
755 0xD3 ;D3C9 ; Mode 3.
757 0xC3 ;D3CA ; go back to beginning
758 0x1D ;D3CB
759 0xD3 ;D3CC
760 ;;End Mode 2
763 ;; Mode 3 -- set-start-point-low mode
764 ;; Final part of header for writing things to memory.
765 ;; User specifies the low bytes of 16 bit start-point.
767 ;; Set start-point-low to input-number;
768 ;; set current mode to 0x04
769 0xFA ;D3CD ; load input-number into A
770 0x52 ;D3CE
771 0xD3 ;D3CF
773 0xEA ;D3D0 ; load A into start-point-low
774 0x86 ;D3D1
775 0xD3 ;D3D2
777 0x3E ;D3D3 ; load 0x04 into A.
778 0x04 ;D3D4
780 0xEA ;D3D5 ; load A to current-mode,
781 0x82 ;D3D6 ; advancing from Mode 3 to
782 0xD3 ;D3D7 ; Mode 4.
784 0xC3 ;D3D8 ; go back to beginning
785 0x1D ;D3D9
786 0xD3 ;D3DA
788 ;; Mode 4 -- write bytes mode
790 ;; This is where RAM manipulation happens. User supplies
791 ;; bytes every frame, which are written sequentially to
792 ;; start-point until bytes-to-write have been written. Once
793 ;; bytes-to-write have been written, the mode is reset to 0.
795 ;; compare bytes-written with bytes-to-write.
796 ;; if they are the same, then reset mode to 0
798 0xFA ;D3DB ; load bytes-to-write into A
799 0x83 ;D3DC
800 0xD3 ;D3DD
802 0x47 ;D3DE ; load A into B
804 0xFA ;D3DF ; load bytes-written into A
805 0x84 ;D3E0
806 0xD3 ;D3E1
808 0xB8 ;D3E2 ; compare A with B
810 0xCA ;D3E3 ; if they are equal, go to cleanup
811 0x07 ;D3E4
812 0xD4 ;D3E5
814 ;; Write Memory Section
815 ;; Write the input-number, interpreted as an 8-bit number,
816 ;; into the current target register, determined by
817 ;; (+ start-point bytes-written).
818 ;; Then, increment bytes-written by 1.
820 0xFA ;D3E6 ; load start-point-high into A
821 0x85 ;D3E7
822 0xD3 ;D3E8
824 0x67 ;D3E9 ; load A into H
826 0xFA ;D3EA ; load start-point-low into A
827 0x86 ;D3EB
828 0xD3 ;D3EC
830 0x6F ;D3ED ; load A into L
832 0xFA ;D3EE ; load bytes-written into A
833 0x84 ;D3EF
834 0xD3 ;D3F0
836 0x00 ;D3F1 ; These are here because
837 0x00 ;D3F2 ; I screwed up again.
838 0x00 ;D3F3
840 0x85 ;D3F4 ; add L to A; store A in L.
841 0x6F ;D3F5
843 0x30 ;D3F6 ; If the addition overflowed,
844 0x01 ;D3F7
845 0x24 ;D3F8 ; increment H.
847 ;; Now, HL points to the correct place in memory
849 0xFA ;D3F9 ; load input-number into A
850 0x52 ;D3FA
851 0xD3 ;D3FB
853 0x77 ;D3FC ; load A into (HL)
855 0xFA ;D3FD ; load bytes-written into A
856 0x84 ;D3FE
857 0xD3 ;D3FF
859 0x3C ;D400 ; increment A
861 0xEA ;D401 ; load A into bytes-written
862 0x84 ;D402
863 0xD3 ;D403
865 0xC3 ;D404 ; go back to beginning.
866 0x1D ;D405
867 0xD3 ;D406
868 ;; End Write Memory Section
870 ;; Mode 4 Cleanup Section
871 ;; reset bytes-written to 0
872 ;; set mode to 0
873 0x3E ;D407 ; load 0 into A
874 0x00 ;D408
876 0xEA ;D409 ; load A into bytes-written
877 0x84 ;D40A
878 0xD3 ;D40B
880 0xEA ;D40C ; load A into current-mode
881 0x82 ;D40D
882 0xD3 ;D40E
884 0xC3 ;D40F ; go back to beginning
885 0x1D ;D410
886 0xD3 ;D411
888 ;; End Mode 4
890 ])
894 (def frame-count 0xD31F)
895 (def input 0xD352)
896 (def current-mode 0xD382)
897 (def bytes-to-write 0xD383)
898 (def bytes-written 0xD384)
899 (def start-point-high 0xD385)
900 (def start-point-low 0xD386)
904 (defn write-memory []
905 (-> (tick (mid-game))
906 (IE! 0) ; disable interrupts
907 (inject-item-assembly (write-memory-assembly))))
909 (defn test-write-memory []
910 (set-state! (write-memory))
911 (dorun
912 (dotimes [_ 5000]
913 (view-memory (step @current-state) current-mode))))
915 (def bytes-to-write 0xD383)
916 (def start-point 0xD384)
918 (defn print-blank-assembly
919 [start end]
920 (dorun
921 (map
922 #(println (format "0x00 ;%04X " %))
923 (range start end))))
925 (defn test-mode-2 []
926 (->
927 (write-memory)
928 (view-memory frame-count)
929 (step)
930 (step [:a])
931 (step [:b])
932 (step [:start])
933 (step [])
934 (view-memory frame-count)))
936 (defn test-mode-4 []
937 (->
938 (write-memory)
939 (#(do (println "memory from 0xC00F to 0xC01F:"
940 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
941 (view-memory current-mode)
942 (step [])
943 (step [])
944 (step [])
945 (#(do (println "after three steps") %))
946 (view-memory current-mode)
948 ;; Activate memory writing mode
950 (#(do (println "step with [:a]") %))
951 (step [:a])
952 (view-memory current-mode)
953 (view-memory bytes-to-write)
954 (view-memory start-point-high)
955 (view-memory start-point-low)
957 ;; Specify four bytes to be written
959 (#(do (println "step with [:select]")%))
960 (step [:select])
961 (view-memory current-mode)
962 (view-memory bytes-to-write)
963 (view-memory start-point-high)
964 (view-memory start-point-low)
966 ;; Specify target memory address as 0xC00F
968 (#(do (println "step with [:u :d]")%))
969 (step [:u :d])
970 (view-memory current-mode)
971 (view-memory bytes-to-write)
972 (view-memory start-point-high)
973 (view-memory start-point-low)
975 (#(do (println "step with [:a :b :start :select]")%))
976 (step [:a :b :start :select])
977 (view-memory current-mode)
978 (view-memory bytes-to-write)
979 (view-memory start-point-high)
980 (view-memory start-point-low)
982 ;; Start reprogramming memory
984 (#(do (println "step with [:a]")%))
985 (step [:a])
986 (view-memory current-mode)
987 (view-memory bytes-written)
989 (#(do (println "step with [:b]")%))
990 (step [:b])
991 (view-memory current-mode)
992 (view-memory bytes-written)
994 (#(do (println "step with [:a :b]")%))
995 (step [:a :b])
996 (view-memory current-mode)
997 (view-memory bytes-written)
999 (#(do (println "step with [:select]")%))
1000 (step [:select])
1001 (view-memory current-mode)
1002 (view-memory bytes-written)
1004 ;; Reprogramming done, program ready for more commands.
1006 (#(do (println "step with []")%))
1007 (step [])
1008 (view-memory current-mode)
1009 (view-memory bytes-written)
1011 (#(do (println "memory from 0xC00F to 0xC01F:"
1012 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))))