view clojure/com/aurellem/gb/assembly.clj @ 175:5d9a7a0ca09a

beginning test of latest assembly code. 240->70.
author Dylan Holmes <ocsenave@gmail.com>
date Wed, 21 Mar 2012 18:17:37 -0500
parents 412ca096a9ba
children 95b2758dd517
line wrap: on
line source
1 (ns com.aurellem.gb.assembly
2 (:use (com.aurellem.gb gb-driver vbm util items))
3 (:import [com.aurellem.gb.gb_driver SaveState]))
5 (defn inject-assembly
6 ([^SaveState state
7 program-counter registers
8 assembly-code]
9 (let [scratch-memory (memory state)]
10 ;; inject assembly code
11 (dorun (map (fn [index val]
12 (aset scratch-memory index val))
13 (range program-counter
14 (+ program-counter (count assembly-code)))
15 assembly-code))
16 (-> state
17 (write-memory! scratch-memory)
18 (write-registers! registers)
19 (PC! program-counter)))))
21 (defn inject-item-assembly
22 ([^SaveState state assembly-code]
23 (inject-assembly state (inc item-list-start)
24 (registers state)
25 assembly-code))
26 ([assembly-code]
27 (inject-item-assembly @current-state assembly-code)))
29 (defn run-assembly
30 ([info-fn assembly n]
31 (let [final-state
32 (reduce (fn [state _]
33 (tick (info-fn state)))
34 (inject-item-assembly
35 (mid-game) assembly)
36 (range n))]
37 final-state))
38 ([assembly n]
39 (run-assembly d-tick assembly n)))
41 (def buttons-port 0xFF00)
43 (defn trace [state]
44 (loop [program-counters [(first (registers @current-state)) ]
45 opcodes [(aget (memory @current-state) (PC @current-state))]]
46 (let [frame-boundary?
47 (com.aurellem.gb.Gb/tick)]
48 (if frame-boundary?
49 [program-counters opcodes]
50 (recur
51 (conj program-counters
52 (first (registers @current-state)))
53 (conj opcodes
54 (aget (memory @current-state)
55 (PC @current-state))))))))
57 (defn print-trace [state n]
58 (let [[program-counters opcodes] (trace state)]
59 (dorun (map (fn [pc op] (println (format "%04X: 0x%02X" pc op)))
60 (take n program-counters)
61 (take n opcodes)))))
63 (defn good-trace []
64 (-> (mid-game) (tick) (IE! 0)
65 (set-inv-mem [0x00 0x00 0X00 0x00])
66 (PC! item-list-start)(print-interrupt)
67 (d-tick) (tick) (d-tick) (tick) (d-tick)))
69 (defn read-down-button []
70 (-> (tick (mid-game))
71 (IE! 0) ; disable interrupts
72 (inject-item-assembly
73 ;; write 00010000 to 0xFF00 to select joypad
74 [0x18 ;D31D ; jump over
75 0x01 ;D31E ; the next 8 bits
76 ;D31F
77 (Integer/parseInt "00100000" 2) ; data section
79 0xFA ;D320 ; load (D31F) into A
80 0x1F ;D321 -->
81 0xD3 ;D322 --> D31F
83 0xEA ;D323 ; load (A), which is
84 0x00 ;D324 --> ; 00010000, into FF00
85 0xFF ;D325 --> FF00
87 0x18 ;D326 ; this is the place where
88 0x01 ;D327 ; we will store whether
89 0x00 ;D328 ; "down" is pressed.
91 0xFA ;D329 ; (FF00) -> A
92 0x00 ;D32A
93 0xFF ;D32B
95 0xCB ;D32C ; Test whether "down"
96 0x5F ;D32D ; is pressed.
98 0x28 ;D32E ; if down is pressed,
99 0x03 ;D32F ; skip the next section
100 ; of code.
101 ;; down-is-not-pressed
102 0xC3 ;D330
103 0x1D ;D331 ; return to beginning
104 0xD3 ;D332
106 ;; down-is-pressed
107 0xEA ;D334 ; write A to D328 if
108 0x28 ;D335 ; "down" was pressed
109 0xD3 ;D336
111 0xC3 ;D330
112 0x1D ;D331 ; return to beginning
113 0xD3 ;D332
114 ])))
116 (defn test-read-down []
117 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)
118 (view-memory (step (step (read-down-button))) 0xD328)))
120 (defn count-frames []
121 (-> (tick (mid-game))
122 (IE! 0) ; disable interrupts
123 (inject-item-assembly
124 [0x18 ;D31D ; jump over
125 0x02 ;D31E ; the next 2 bytes
126 0x00 ;D31F ; frame-count
127 0x00 ;D320 ; v-blank-prev
129 0xFA ;D321
130 0x41 ;D322 ; load (FF41) into A
131 0xFF ;D323 ; this contains mode flags
133 ;; if we're in v-blank, the bit-1 is 0
134 ;; and bit-2 is 1 Otherwise, it is not v-blank.
135 0xCB ;D324 ; test bit-1 of A
136 0x4F ;D325
138 0xC2 ;D326 ; if bit-1 is not 0
139 0x44 ;D327 ; GOTO not-v-blank
140 0xD3 ;D328
142 0xCB ;D329 ; test bit-0 of A
143 0x47 ;D32A
145 0xCA ;D32B ; if bit-0 is not 1
146 0x44 ;D32C ; GOTO not-v-blank
147 0xD3 ;D32D
148 ;;; in v-blank mode
149 ;; if v-blank-prev was 0,
150 ;; increment frame-count
152 0xFA ;D32E ; load v-blank-prev to A
153 0x20 ;D32F
154 0xD3 ;D330
156 0xCB ;D331
157 0x47 ;D332 ; test bit-0 of A
159 0x20 ;D333 ; skip next section
160 0x07 ;D334 ; if v-blank-prev was not zero
162 ;; v-blank was 0, increment frame-count
163 0xFA ;D335 ; load frame-count into A
164 0x1F ;D336
165 0xD3 ;D337
167 0x3C ;D338 ; inc A
169 0xEA ;D339 ; load A into frame-count
170 0x1F ;D33A
171 0xD3 ;D33B
173 ;; set v-blank-prev to 1
174 0x3E ;D33C ; load 1 into A
175 0x01 ;D33D
177 0xEA ;D33E ; load A into v-blank-prev
178 0x20 ;D33F
179 0xD3 ;D340
181 0xC3 ;D341 ; return to beginning
182 0x1D ;D342
183 0xD3 ;D343
185 ;;; not in v-blank mode
186 ;; set v-blank-prev to 0
187 0x3E ;D344 ; load 0 into A
188 0x00 ;D345
190 0xEA ;D346 ; load A into v-blank-prev
191 0x20 ;D347
192 0xD3 ;D348
194 0xC3 ;D349 ; return to beginning
195 0x1D ;D34A
196 0xD3 ;D34B
197 ])))
199 (defn step-count-frames []
200 (-> (read-down-button)
201 (d-tick)
202 (tick) ;; skip over data section
203 (d-tick)
204 (view-register "Register A" A)
205 (tick) ;; load-data into A
206 (view-register "Register A" A)
207 (d-tick)
208 (view-memory 0xFF00)
209 (tick) ;; load A into 0xFF00
210 (view-memory 0xFF00)
211 (d-tick)
212 (tick)
213 (d-tick)
214 (tick)
215 (d-tick)
216 (tick)
217 (d-tick)
218 (tick)
219 (d-tick)
220 (tick)
221 (d-tick)
222 (tick)
223 (print-inventory)))
225 (defn test-count-frames []
226 (= 255 (aget (memory ((apply comp (repeat 255 step))
227 (count-frames)))
228 0xD31F)))
230 ;; specs for main bootstrap program
231 ;; starts in "mode-select" mode
232 ;; Each button press takes place in a single frame.
233 ;; mode-select-mode takes one of the main buttons
234 ;; which selects one of up to eight modes
235 ;; mode 1 activated by the "A" button
236 ;; the next two button presses indicates the start
237 ;; memory location which to which the bootstrap
238 ;; program will write.
239 ;; This is done by using each of the eight buttons to
240 ;; spell out an 8 bit number. The order of buttons is
241 ;; [:d :u :l :r :start :select :b :a]
242 ;; [:a :start :l] --> 00101001
244 ;; the next button press determines how many bytes are to be
245 ;; written, starting at the start position.
247 ;; then, the actual bytes are entered and are written to the
248 ;; start address in sequence.
250 (defn input-number-assembly []
251 [0x18 ;D31D ; jump over
252 0x02 ;D31E ; the next 2 bytes
253 0x00 ;D31F ; frame-count
254 0x00 ;D320 ; v-blank-prev
256 0xFA ;D321
257 0x41 ;D322 ; load (FF41) into A
258 0xFF ;D323 ; this contains mode flags
260 ;; if we're in v-blank, the bit-1 is 0
261 ;; and bit-2 is 1 Otherwise, it is not v-blank.
262 0xCB ;D324 ; test bit-1 of A
263 0x4F ;D325
265 0xC2 ;D326 ; if bit-1 is not 0
266 0x44 ;D327 ; GOTO not-v-blank
267 0xD3 ;D328
269 0xCB ;D329 ; test bit-0 of A
270 0x47 ;D32A
272 0xCA ;D32B ; if bit-0 is not 1
273 0x44 ;D32C ; GOTO not-v-blank
274 0xD3 ;D32D
276 ;;; in v-blank mode
278 ;; if v-blank-prev was 0,
279 ;; increment frame-count
281 0xFA ;D32E ; load v-blank-prev to A
282 0x20 ;D32F
283 0xD3 ;D330
285 0xCB ;D331
286 0x47 ;D332 ; test bit-0 of A
288 0x20 ;D333 ; skip next section
289 0x07 ;D334 ; if v-blank-prev was not zero
291 ;; v-blank was 0, increment frame-count
292 0xFA ;D335 ; load frame-count into A
293 0x1F ;D336
294 0xD3 ;D337
296 0x3C ;D338 ; inc A
298 0xEA ;D339 ; load A into frame-count
299 0x1F ;D33A
300 0xD3 ;D33B
302 ;; set v-blank-prev to 1
303 0x3E ;D33C ; load 1 into A
304 0x01 ;D33D
306 0xEA ;D33E ; load A into v-blank-prev
307 0x20 ;D33F
308 0xD3 ;D340
310 0xC3 ;D341 ; GOTO input handling code
311 0x4E ;D342
312 0xD3 ;D343
314 ;;; not in v-blank mode
315 ;; set v-blank-prev to 0
316 0x3E ;D344 ; load 0 into A
317 0x00 ;D345
319 0xEA ;D346 ; load A into v-blank-prev
320 0x20 ;D347
321 0xD3 ;D348
323 0xC3 ;D349 ; return to beginning
324 0x1D ;D34A
325 0xD3 ;D34B
327 0x00 ;D34C ; these are here
328 0x00 ;D34D ; for glue
331 ;;; calculate input number based on button presses
332 0x18 ;D34E ; skip next 3 bytes
333 0x03 ;D34F
334 ;D350
335 (Integer/parseInt "00100000" 2) ; select directional pad
336 ;D351
337 (Integer/parseInt "00010000" 2) ; select buttons
338 0x00 ;D352 ; input-number
340 ;; select directional pad, store low bits in B
342 0xFA ;D353 ; load (D350) into A
343 0x50 ;D354 -->
344 0xD3 ;D355 --> D31F
346 0xEA ;D356 ; load A, which is
347 0x00 ;D357 --> ; 00010000, into FF00
348 0xFF ;D358 --> FF00
350 0x06 ;D359
351 ;D35A
352 (Integer/parseInt "11110000" 2) ; "11110000" -> B
353 0xFA ;D35B ; (FF00) -> A
354 0x00 ;D35C
355 0xFF ;D35D
357 0xCB ;D35E ; swap nybbles on A
358 0x37 ;D35F
359 0xA0 ;D360 ; (AND A B) -> A
360 0x47 ;D361 ; A -> B
362 ;; select buttons store bottom bits in C
364 0xFA ; ; load (D351) into A
365 0x51 ; -->
366 0xD3 ; --> D31F
368 0xEA ; ; load (A), which is
369 0x00 ; --> ; 00001000, into FF00
370 0xFF ; --> FF00
372 0x0E ;
373 (Integer/parseInt "00001111" 2) ; "00001111" -> C
375 0xFA ; ; (FF00) -> A
376 0x00 ;
377 0xFF ;
379 0xA1 ; ; (AND A C) -> A
380 0x4F ; ; A -> C
382 ;; combine the B and C registers into the input number
383 0x79 ; ; C -> A
384 0xB0 ; ; (OR A B) -> A
385 0x2F ; ; negate A
387 0xEA ; ; store A into input-number
388 0x52 ;
389 0xD3 ;
391 0xC3 ; ; return to beginning
392 0x1D ;
393 0xD3 ;
394 ])
398 (defn input-number []
399 (-> (tick (mid-game))
400 (IE! 0) ; disable interrupts
401 (inject-item-assembly (input-number-assembly))))
403 (defn test-input-number
404 "Input freestyle buttons and observe the effects at the repl."
405 []
406 (set-state! (input-number))
407 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
410 (defn write-memory-assembly*
411 "A program for altering in-game memory by pressing buttons."
412 []
413 [
414 0xF3 ; stop interrupts
415 ;; --------- CLEANUP
416 0xAF ; zero A [D31E]
417 0x57 ; A->D; makes D=0.
419 ;; --------- FRAME METRONOME
420 0xF1 ;; pop AF (vblank prev) [D320]
422 0x2F ;; invert A
423 0x47 ;; A -> B
425 0xF0 ;; copy STAT into A
426 0x41
428 0xCB ;; swap A nybbles; now A_0 is (VB==1).
429 0x37
431 0xF5 ;; push AF (vbprev)
433 0xA0 ;; A & B --> A. Now A_0 contains "increment?"
435 0xCB ;; test A_0. this result will be used twice.
436 0x47
437 0x28 ;; end frame (JUMP) if A_0 = 0.
438 0x00 ;; TODO: set jump length
440 ;; -------- GET BUTTON INPUT
442 ;; btw, Z bit is now 1
443 ;; prepare to select bits
445 0x3E ;; load 0x20 into A, to measure dpad
446 0x20
448 0x06 ;; load 0x00 into B
449 0x00 ;; to initialize for "OR" loop
451 0xE0 ;; load A into [FF00] ;; start of OR loop [D33C]
452 0x00
454 0xF0 ;; load A from [FF00]
455 0x00
457 0xE6 ;; bitmask 00001111
458 0x0F
460 0xB0 ;; A or B --> A
462 0x28 ;; JUMP forward if Z=0
463 0x08
465 0x47 ;; A -> B
466 0xCB ;; swap B nybbles
467 0x30
469 0x3E ;; load 0x10 into A, to measure btns
470 0x10
472 0xBF ;; compare(A,A) sets Z=0
474 0x18 ;; JUMP back to "load A into [FF00]" [20 steps?]
475 0xED
478 ;; ------ TAKE ACTION BASED ON USER INPUT
480 ;; "input mode"
481 ;; mode 0x00 : select mode
482 ;; mode 0x08 : select bytes-to-write
483 ;; mode 0x10 : select hi-bit
484 ;; mode 0x18 : select lo-bit
486 ;; "output mode"
487 ;; mode 0x20 : write bytes
488 ;; mode 0xFF : jump PC
491 ;; registers
492 ;; D : mode select
493 ;; E : count of bytes to write
494 ;; H : address-high
495 ;; L : address-low
497 ;; now A contains the pressed keys
498 0x2F ; complement A, by request. [D34F]
500 0x47 ; A->B ;; now B contains the pressed keys
502 0xCB ; test bit 5 of D (are we in o/p mode?)
503 0x6A
504 0x28 ; if test == 0, skip this o/p section
505 0x13 ; JUMP
507 0xCB ; else, test bit 0 of D (fragile; are we in pc mode?)
508 0x42
509 0x28 ; if test == 0, skip the following command
510 0x01
512 ;; output mode I: moving the program counter
513 0xE9 ; ** move PC to (HL)
515 ;; output mode II: writing bytes
516 0xAF ; zero A
517 0xBB ; compare count to zero. finished writing?
518 0x28 ; if we are finished, jump back to cleanup
519 0x00 ; TODO: set jump length backwards.
521 ;; continue writing bytes
522 0x78 ;; B->A
523 0x22 ;; copy A to (HL) and increment HL.
524 0x18 ;; end frame. [goto D31F]
525 0xB6 ;; JUMP
528 ;; ---- end of o/p section
530 ;; i/p mode
531 ;; adhere to the mode discipline:
532 ;; D must be one of 0x00 0x08 0x10 0x18.
534 0x3E ;; load the constant 57 into A. [D369]
535 0x57
536 0x82 ;; add the mode to A
537 0xEA ;; store A into "thing to execute"
538 0x74
539 0xD3
541 0x3E ;; load the constant 8 into A
542 0x08
543 0x82 ;; add the mode to A
545 0x57 ;; store the incremented mode into D
546 0x78 ;; B->A; now A contains the pressed keys
548 0x00 ;; var: thing to execute [D374]
550 0x18 ;; end frame
551 0xA8 ;; JUMP
552 ]
553 )
555 (defn write-mem-dyl []
556 (-> (tick (mid-game))
557 (IE! 0)
558 (inject-item-assembly (write-memory-assembly*))))
561 (defn dylan* []
562 (->
563 (write-mem-dyl)
565 (tick)
566 (tick)
567 (tick)
568 (tick)
569 (tick)
570 (tick)
571 (tick)
572 (tick)
573 (tick)
574 (tick)
575 (tick)
576 (tick)
577 (tick)
578 (tick)
579 (tick)
580 (tick)
581 (tick)
582 (tick)
583 (tick)
584 (tick)
585 (tick)
586 (tick)
587 (tick)
588 (tick)
589 (tick)
590 (tick)
591 (tick)
592 (tick)
593 (tick)
594 (tick)
595 (tick)
596 (tick)
597 (tick)
598 (tick)
599 (tick)
600 (tick)
602 ;;(view-memory 0xD374)
603 (tick)
604 (tick)
605 (tick)
606 (tick)
607 (tick)
608 (tick)
609 (tick)
610 (tick)
611 (tick)
612 (tick)
613 (tick)
614 (tick)
615 (tick)
616 (tick)
617 (tick)
618 ;;(view-memory 0xD374)
619 (d-tick)
621 (view-register "A" A)
622 (view-register "B" B)
623 (view-register "C" C))
625 )
628 (defn dylan []
629 (->
630 (write-mem-dyl)
631 (tick)
632 (tick)
633 (tick)
634 (tick)
635 (tick)
636 (tick)
637 (tick)
638 (tick)
639 (tick)
640 (tick)
641 (tick)
642 (tick)
643 (tick)
644 (tick)
645 (tick) ;; first loop
648 (tick)
649 (tick)
650 (tick)
651 (tick)
652 (tick)
653 (tick)
654 (tick)
655 (tick)
656 (tick)
657 (tick)
658 (tick)
659 (tick)
660 (tick) ;; dpad bits
662 (tick)
663 (tick)
664 (tick)
665 (tick)
666 (tick)
667 (tick)
668 (tick)
669 (tick)
670 (d-tick)
674 (view-register "A" A)
675 (view-register "B" B)
676 (view-register "C" C)
678 ))
683 (defn d2 []
684 (->
685 (write-mem-dyl)
686 (view-memory 0xD31F)
687 step step step step step
688 (view-memory 0xD31F)))
709 (defn write-memory-assembly []
710 [
711 ;; Main Timing Loop
712 ;; Constantly check for v-blank and Trigger main state machine on
713 ;; every transtion from v-blank to non-v-blank.
715 0x18 ; D31D ; Variable declaration
716 0x02 ; D31E
717 0x00 ; D31F ; frame-count
718 0x00 ; D320 ; v-blank-prev
720 0xF0 ; D321 ; load v-blank mode flags into A
721 0x41
722 0x00
725 ;; Branch dependent on v-blank. v-blank happens when the last two
726 ;; bits in A are "01"
727 0xCB ; D324
728 0x4F ; D325
730 0xC2 ; D326 ; if bit-1 is not 0, then
731 0x3E ; D327 ; GOTO non-v-blank.
732 0xD3 ; D328
734 0xCB ; D329
735 0x47 ; D32A
737 0xCA ; D32B ; if bit-0 is not 1, then
738 0x3E ; D32C ; GOTO non-v-blank.
739 0xD3 ; D32D
741 ;; V-Blank
742 ;; Activate state-machine if this is a transition event.
744 0xFA ; D32E ; load v-bank-prev into A
745 0x20 ; D32F
746 0xD3 ; D330
748 0xFE ; D331 ; compare A to 0. >--------\
749 0x00 ; D332 \
750 ; |
751 ;; set v-blank-prev to 1. |
752 0x3E ; D333 ; load 1 into A. |
753 0x01 ; D334 |
754 ; |
755 0xEA ; D335 ; load A into v-blank-prev |
756 0x20 ; D336 |
757 0xD3 ; D337 |
758 ; /
759 ;; if v-blank-prev was 0, activate state-machine <------/
760 0xCA ; D338 ; if v-blank-prev
761 0x46 ; D339 ; was 0,
762 0xD3 ; D33A ; GOTO state-machine
764 0xC3 ; D33B
765 0x1D ; D33C
766 0xD3 ; D33D ; GOTO beginning
767 ;; END V-blank
769 ;; Non-V-Blank
770 ;; Set v-blank-prev to 0
771 0x3E ; D33E ; load 0 into A
772 0x00 ; D33F
774 0xEA ; D340 ; load A into v-blank-prev
775 0x20 ; D341
776 0xD3 ; D342
778 0xC3 ; D343
779 0x1D ; D344
780 0xD3 ; D345 ; GOTO beginning
781 ;; END Not-V-Blank
784 ;; Main State Machine -- Input Section
785 ;; This is called once every frame.
786 ;; It collects input and uses it to drive the
787 ;; state transitions.
789 ;; Increment frame-count
790 0xFA ; D346 ; load frame-count into A
791 0x1F ; D347
792 0xD3 ; D348
794 0x3C ; D349 ; inc A
796 0xEA ; D34A
797 0x1F ; D34B ; load A into frame-count
798 0xD3 ; D34C
800 0x00 ; D34D ; glue :)
802 0x18 ;D34E ; skip next 3 bytes
803 0x03 ;D34F
804 ;D350
805 (Integer/parseInt "00100000" 2) ; select directional pad
806 ;D351
807 (Integer/parseInt "00010000" 2) ; select buttons
808 0x00 ;D352 ; input-number
810 ;; select directional pad; store low bits in B
812 0xFA ;D353 ; load (D350) into A
813 0x50 ;D354 -->
814 0xD3 ;D355 --> D350
816 0xE0 ;D356 ; load (A), which is
817 0x00 ;D357 --> ; 00010000, into FF00
818 0x00 ;D358 --> FF00 ;; NO-OP
820 0x06 ;D359
821 ;D35A
822 (Integer/parseInt "11110000" 2) ; "11110000" -> B
823 0xF0 ;D35B ; (FF00) -> A
824 0x00 ;D35C
825 0x00 ;D35D ;; NO-OP
827 0xCB ;D35E ; swap nybbles on A
828 0x37 ;D35F
829 0xA0 ;D360 ; (AND A B) -> A
830 0x47 ;D361 ; A -> B
832 ;; select buttons; store bottom bits in C
834 0xFA ;D362 ; load (D351) into A
835 0x51 ;D363 -->
836 0xD3 ;D364 --> D351
838 0xE0 ;D365 ; load (A), which is
839 0x00 ;D366 --> ; 00001000, into FF00
840 0x00 ;D367 --> FF00 ;; NO-OP
842 0x0E ;D368
843 ;D369
844 (Integer/parseInt "00001111" 2) ; "00001111" -> C
846 0xF0 ;D36A ; (FF00) -> A
847 0x00 ;D36B
848 0x00 ;D36C
850 0xA1 ;D36D ; (AND A C) -> A
851 0x4F ;D36E ; A -> C
853 ;; combine the B and C registers into the input number
854 0x79 ;D36F ; C -> A
855 0xB0 ;D370 ; (OR A B) -> A
856 0x2F ;D371 ; negate A
858 0xEA ;D372 ; store A into input-number
859 0x52 ;D373
860 0xD3 ;D374
862 0x00 ;D375
863 0x00 ;D376
864 0x00 ;D377
865 0x00 ;D378
866 0x00 ;D379
867 0x00 ;D37A
868 0x00 ;D37B ; these are here because
869 0x00 ;D37C ; I messed up :(
870 0x00 ;D37D
871 0x00 ;D37E
872 0x00 ;D37F
874 ;; beginning of main state machine
875 0x18 ;D380 ; Declaration of variables
876 0x05 ;D381 ; 5 variables:
877 0x00 ;D382 ; current-mode
878 0x00 ;D383 ; bytes-to-write
879 0x00 ;D384 ; bytes-written
880 0x00 ;D385 ; start-point-high
881 0x00 ;D386 ; start-point-low
884 ;; banch on current mode
885 0xFA ;D387 ; load current-mode (0xD382)
886 0x82 ;D388 ; into A
887 0xD3 ;D389
888 0x00 ;D38A
891 ;; GOTO Mode 0 (input-mode) if current-mode is 0
892 0xFE ;D38B
893 0x00 ;D38C ; compare A with 0x00
895 0xCA ;D38D ; goto Mode 0 if A == 0
896 0xA8 ;D38E
897 0xD3 ;D38F
899 ;; GOTO Mode 1 (set-length) if current-mode is 1
900 0xFE ;D390
901 0x01 ;D391 ; compare A with 0x01
903 0xCA ;D392
904 0xB1 ;D393
905 0xD3 ;D394 ; goto Mode 1 if A == 1
907 ;; GOTO Mode 2 (set-start-point-high) if current mode is 2
908 0xFE ;D395
909 0x02 ;D396 ; compare A with 0x02
911 0xCA ;D397
912 0xBF ;D398
913 0xD3 ;D399 ; goto Mode 2 if A == 2
915 ;; GOTO Mode 3 (set-start-point-low) if current mode is 3
916 0xFE ;D39A
917 0x03 ;D39B
919 0xCA ;D39C
920 0xCD ;D39D
921 0xD3 ;D39E ; goto Mode 3 if A == 3
923 ;; GOTO Mode 4 (write-memory) if current mode is 4
924 0xFE ;D39F
925 0x04 ;D3A0
927 0xCA ;D3A1
928 0xDB ;D3A2
929 0xD3 ;D3A3
931 0x00 ;D3A4
932 ;; End of Mode checking, goto beginning
933 0xC3 ;D3A5
934 0x1D ;D3A6
935 0xD3 ;D3A7
938 ;; Mode 0 -- input-mode mode
939 ;; means that we are waiting for a mode, so set the mode to
940 ;; whatever is currently in input-number. If nothing is
941 ;; entered, then the program stays in input-mode mode
943 ;; set current-mode to input-number
944 0xFA ;D3A8 ; load input-number (0xD352)
945 0x52 ;D3A9 ; into A
946 0xD3 ;D3AA
948 0xEA ;D3AB ; load A into current-mode
949 0x82 ;D3AC ; (0xD382)
950 0xD3 ;D3AD
952 0xC3 ;D3AE ; go back to beginning
953 0x1D ;D3AF
954 0xD3 ;D3B0
955 ;; End Mode 0
958 ;; Mode 1 -- set-length mode
959 ;; This is the header for writing things to memory.
960 ;; User specifies the number of bytes to write.
961 ;; Mode is auto advanced to Mode 2 after this mode
962 ;; completes.
964 ;; Set bytes left to write to input-number;
965 ;; set current-mode to 0x02.
966 0xFA ;D3B1 ; load input-number (0xD352)
967 0x52 ;D3B2 ; into A
968 0xD3 ;D3B3
970 0xEA ;D3B4 ; load A into bytes-left-to-write
971 0x83 ;D3B5 ; (0xD383)
972 0xD3 ;D3B6
974 0x3E ;D3B7 ; load 0x02 into A.
975 0x02 ;D3B8
977 0xEA ;D3B9 ; load A to current-mode
978 0x82 ;D3BA ; advancing from Mode 1 to
979 0xD3 ;D3BB ; Mode 2
981 0xC3 ;D3BC ; go back to beginning
982 0x1D ;D3BD
983 0xD3 ;D3BE
984 ;; End Mode 1
987 ;; Mode 2 -- set start-point-high mode
988 ;; Middle part of the header for writing things to memory.
989 ;; User specifies the start location in RAM to which
990 ;; data will be written.
991 ;; Mode is auto advanced to Mode 3 after this mode completes.
993 ;; Set start-point-high to input-number;
994 ;; set current mode to 0x03.
995 0xFA ;D3BF ; load input-number (0xD352)
996 0x52 ;D3C0 ; into A
997 0xD3 ;D3C1
999 0xEA ;D3C2 ; load A into start-point-high
1000 0x85 ;D3C3 ; (0xD385)
1001 0xD3 ;D3C4
1003 0x3E ;D3C5 ; load 0x03 into A.
1004 0x03 ;D3C6
1006 0xEA ;D3C7 ; load A to current-mode,
1007 0x82 ;D3C8 ; advancing from Mode 2 to
1008 0xD3 ;D3C9 ; Mode 3.
1010 0xC3 ;D3CA ; go back to beginning
1011 0x1D ;D3CB
1012 0xD3 ;D3CC
1013 ;;End Mode 2
1016 ;; Mode 3 -- set-start-point-low mode
1017 ;; Final part of header for writing things to memory.
1018 ;; User specifies the low bytes of 16 bit start-point.
1020 ;; Set start-point-low to input-number;
1021 ;; set current mode to 0x04
1022 0xFA ;D3CD ; load input-number into A
1023 0x52 ;D3CE
1024 0xD3 ;D3CF
1026 0xEA ;D3D0 ; load A into start-point-low
1027 0x86 ;D3D1
1028 0xD3 ;D3D2
1030 0x3E ;D3D3 ; load 0x04 into A.
1031 0x04 ;D3D4
1033 0xEA ;D3D5 ; load A to current-mode,
1034 0x82 ;D3D6 ; advancing from Mode 3 to
1035 0xD3 ;D3D7 ; Mode 4.
1037 0xC3 ;D3D8 ; go back to beginning
1038 0x1D ;D3D9
1039 0xD3 ;D3DA
1041 ;; Mode 4 -- write bytes mode
1043 ;; This is where RAM manipulation happens. User supplies
1044 ;; bytes every frame, which are written sequentially to
1045 ;; start-point until bytes-to-write have been written. Once
1046 ;; bytes-to-write have been written, the mode is reset to 0.
1048 ;; compare bytes-written with bytes-to-write.
1049 ;; if they are the same, then reset mode to 0
1051 0xFA ;D3DB ; load bytes-to-write into A
1052 0x83 ;D3DC
1053 0xD3 ;D3DD
1055 0x47 ;D3DE ; load A into B
1057 0xFA ;D3DF ; load bytes-written into A
1058 0x84 ;D3E0
1059 0xD3 ;D3E1
1061 0xB8 ;D3E2 ; compare A with B
1063 0xCA ;D3E3 ; if they are equal, go to cleanup
1064 0x07 ;D3E4
1065 0xD4 ;D3E5
1067 ;; Write Memory Section
1068 ;; Write the input-number, interpreted as an 8-bit number,
1069 ;; into the current target register, determined by
1070 ;; (+ start-point bytes-written).
1071 ;; Then, increment bytes-written by 1.
1073 0xFA ;D3E6 ; load start-point-high into A
1074 0x85 ;D3E7
1075 0xD3 ;D3E8
1077 0x67 ;D3E9 ; load A into H
1079 0xFA ;D3EA ; load start-point-low into A
1080 0x86 ;D3EB
1081 0xD3 ;D3EC
1083 0x6F ;D3ED ; load A into L
1085 0xFA ;D3EE ; load bytes-written into A
1086 0x84 ;D3EF
1087 0xD3 ;D3F0
1089 0x00 ;D3F1 ; These are here because
1090 0x00 ;D3F2 ; I screwed up again.
1091 0x00 ;D3F3
1093 0x85 ;D3F4 ; add L to A; store A in L.
1094 0x6F ;D3F5
1096 0x30 ;D3F6 ; If the addition overflowed,
1097 0x01 ;D3F7
1098 0x24 ;D3F8 ; increment H.
1100 ;; Now, HL points to the correct place in memory
1102 0xFA ;D3F9 ; load input-number into A
1103 0x52 ;D3FA
1104 0xD3 ;D3FB
1106 0x77 ;D3FC ; load A into (HL)
1108 0xFA ;D3FD ; load bytes-written into A
1109 0x84 ;D3FE
1110 0xD3 ;D3FF
1112 0x3C ;D400 ; increment A
1114 0xEA ;D401 ; load A into bytes-written
1115 0x84 ;D402
1116 0xD3 ;D403
1118 0xC3 ;D404 ; go back to beginning.
1119 0x1D ;D405
1120 0xD3 ;D406
1121 ;; End Write Memory Section
1123 ;; Mode 4 Cleanup Section
1124 ;; reset bytes-written to 0
1125 ;; set mode to 0
1126 0x3E ;D407 ; load 0 into A
1127 0x00 ;D408
1129 0xEA ;D409 ; load A into bytes-written
1130 0x84 ;D40A
1131 0xD3 ;D40B
1133 0xEA ;D40C ; load A into current-mode
1134 0x82 ;D40D
1135 0xD3 ;D40E
1137 0xC3 ;D40F ; go back to beginning
1138 0x1D ;D410
1139 0xD3 ;D411
1141 ;; End Mode 4
1143 ])
1147 (def frame-count 0xD31F)
1148 (def input 0xD352)
1149 (def current-mode 0xD382)
1150 (def bytes-to-write 0xD383)
1151 (def bytes-written 0xD384)
1152 (def start-point-high 0xD385)
1153 (def start-point-low 0xD386)
1157 (defn write-memory []
1158 (-> (tick (mid-game))
1159 (IE! 0) ; disable interrupts
1160 (inject-item-assembly (write-memory-assembly))))
1162 (defn test-write-memory []
1163 (set-state! (write-memory))
1164 (dorun
1165 (dotimes [_ 5000]
1166 (view-memory (step @current-state) current-mode))))
1168 (def bytes-to-write 0xD383)
1169 (def start-point 0xD384)
1171 (defn print-blank-assembly
1172 [start end]
1173 (dorun
1174 (map
1175 #(println (format "0x00 ;%04X " %))
1176 (range start end))))
1178 (defn test-mode-2 []
1179 (->
1180 (write-memory)
1181 (view-memory frame-count)
1182 (step)
1183 (step [:a])
1184 (step [:b])
1185 (step [:start])
1186 (step [])
1187 (view-memory frame-count)))
1191 (defn dylan-test-mode
1192 ([] (dylan-test-mode (write-mem-dyl)))
1193 ([target-state]
1194 (let [
1195 v-blank-prev 54046
1196 btn-register 65280
1197 eggs 0xD374
1200 (->
1201 target-state
1203 (tick)
1204 (tick)
1205 (tick)
1206 (tick);; jumps back to beginning
1208 (tick)
1209 (tick)
1210 (tick)
1211 (tick)
1212 (tick)
1213 (tick)
1214 (tick)
1215 (tick)
1216 (tick)
1217 (tick)
1218 (tick)
1219 (tick)
1222 (tick)
1223 (tick)
1224 (tick)
1225 (tick)
1226 (tick)
1227 (tick)
1228 (tick)
1229 (tick)
1230 (tick)
1231 (tick)
1232 (tick)
1233 (tick)
1234 (tick)
1235 (tick)
1236 (tick)
1237 (tick)
1238 (tick)
1239 (tick)
1240 (tick)
1241 (tick)
1242 (tick) ;; just complemented A
1244 (tick)
1245 (DE! 0x1800)
1246 (AF! 0x7700) ;; change inputs @ A
1247 (tick)
1248 (tick)
1249 (tick)
1250 (tick)
1251 (tick)
1253 ;;(view-memory eggs)
1254 (tick)
1255 (tick)
1256 ;;(view-memory eggs)
1257 (tick)
1258 (tick)
1259 (tick)
1260 (tick)
1261 (tick)
1262 (tick)
1263 (d-tick)
1266 ;;(view-memory btn-register)
1267 (view-register "A" A)
1268 (view-register "B" B)
1270 ;;(view-register "C" C)
1271 (view-register "D" D)
1272 (view-register "E" E)
1273 (view-register "H" H)
1274 (view-register "L" L)
1275 ))))
1279 (defn drive-dylan []
1280 (-> (write-mem-dyl)
1281 (#(do (println "memory from 0xC00F to 0xC01F:"
1282 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
1283 (step [])
1284 (step [])
1285 (step [])
1286 (step [:start])
1287 (step [:select])
1288 (step [:u :d])
1289 (step [:a :b :start :select])
1290 (step [:a])
1291 (step [:b])
1292 (step [:a :b])
1293 (step [:select])
1294 (step [])
1295 (step [])
1296 (step [])
1297 (#(do (println "memory from 0xC00F to 0xC01F:"
1298 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
1299 ))
1301 (defn test-mode-4
1302 ([] (test-mode-4 (write-memory)))
1303 ([target-state]
1304 (->
1305 target-state
1306 (#(do (println "memory from 0xC00F to 0xC01F:"
1307 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
1308 (view-memory current-mode)
1309 (step [])
1310 (step [])
1311 (step [])
1312 (#(do (println "after three steps") %))
1313 (view-memory current-mode)
1315 ;; Activate memory writing mode
1317 (#(do (println "step with [:a]") %))
1318 (step [:a])
1319 (view-memory current-mode)
1320 (view-memory bytes-to-write)
1321 (view-memory start-point-high)
1322 (view-memory start-point-low)
1324 ;; Specify four bytes to be written
1326 (#(do (println "step with [:select]")%))
1327 (step [:select])
1328 (view-memory current-mode)
1329 (view-memory bytes-to-write)
1330 (view-memory start-point-high)
1331 (view-memory start-point-low)
1333 ;; Specify target memory address as 0xC00F
1335 (#(do (println "step with [:u :d]")%))
1336 (step [:u :d])
1337 (view-memory current-mode)
1338 (view-memory bytes-to-write)
1339 (view-memory start-point-high)
1340 (view-memory start-point-low)
1342 (#(do (println "step with [:a :b :start :select]")%))
1343 (step [:a :b :start :select])
1344 (view-memory current-mode)
1345 (view-memory bytes-to-write)
1346 (view-memory start-point-high)
1347 (view-memory start-point-low)
1349 ;; Start reprogramming memory
1351 (#(do (println "step with [:a]")%))
1352 (step [:a])
1353 (view-memory current-mode)
1354 (view-memory bytes-written)
1356 (#(do (println "step with [:b]")%))
1357 (step [:b])
1358 (view-memory current-mode)
1359 (view-memory bytes-written)
1361 (#(do (println "step with [:a :b]")%))
1362 (step [:a :b])
1363 (view-memory current-mode)
1364 (view-memory bytes-written)
1366 (#(do (println "step with [:select]")%))
1367 (step [:select])
1368 (view-memory current-mode)
1369 (view-memory bytes-written)
1371 ;; Reprogramming done, program ready for more commands.
1373 (#(do (println "step with []")%))
1374 (step [])
1375 (view-memory current-mode)
1376 (view-memory bytes-written)
1378 (#(do (println "memory from 0xC00F to 0xC01F:"
1379 (subvec (vec (memory %)) 0xC00F 0xC01F)) %)))))