view clojure/com/aurellem/gb/assembly.clj @ 234:00e4efa7c104

saving progress...
author Dylan Holmes <ocsenave@gmail.com>
date Sat, 24 Mar 2012 18:21:16 -0500
parents d585b91de06c
children 19fd38fe376e
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))))
414 (defn write-memory-assembly*
415 "A program for altering in-game memory by pressing buttons."
416 []
417 [
418 0xF3 ; stop interrupts
419 ;; --------- CLEANUP
420 0xAF ; zero A [D31E]
421 0x57 ; A->D; makes D=0.
423 ;; --------- FRAME METRONOME
424 0xF1 ;; pop AF (vblank prev) [D320]
426 0x2F ;; invert A
427 0x4F ;; A -> C
429 0xF0 ;; copy STAT into A
430 0x41
432 0x47 ;; A->B
433 0x1F ;; rotate A right
434 0x2F ;; complement A
435 0xA0 ;; A & B --> A
437 0xF5 ;; push AF (vbprev)
439 0xA1 ;; A & C --> A. Now A_0 contains "increment?"
441 0xCB ;; test A_0. this result will be used twice.
442 0x47
444 0x28 ;; end frame (JUMP) if A_0 = 0.
445 0xF3 ;; TODO: set jump length
447 ;; -------- GET BUTTON INPUT
449 ;; btw, Z bit is now 1
450 ;; prepare to select bits
452 0x3E ;; load 0x20 into A, to measure dpad
453 0x20
455 0x06 ;; load 0x00 into B
456 0x00 ;; to initialize for "OR" loop
458 0xE0 ;; load A into [FF00] ;; start of OR loop [D33C]
459 0x00
461 0xF0 ;; load A from [FF00]
462 0x00
464 0xE6 ;; bitmask 00001111
465 0x0F
467 0xB0 ;; A or B --> A
469 0x28 ;; JUMP forward if Z=0
470 0x08
472 0x47 ;; A -> B
473 0xCB ;; swap B nybbles
474 0x30
476 0x3E ;; load 0x10 into A, to measure btns
477 0x10
479 0xBF ;; compare(A,A) sets Z=0
481 0x18 ;; JUMP back to "load A into [FF00]"
482 0xEF
485 ;; ------ TAKE ACTION BASED ON USER INPUT
487 ;; "input mode"
488 ;; mode 0x00 : select mode
489 ;; mode 0x08 : select bytes-to-write
490 ;; mode 0x10 : select hi-bit
491 ;; mode 0x18 : select lo-bit
493 ;; "output mode"
494 ;; mode 0x20 : write bytes
495 ;; mode 0xFF : jump PC
498 ;; registers
499 ;; D : mode select
500 ;; E : count of bytes to write
501 ;; H : address-high
502 ;; L : address-low
504 ;; now A contains the pressed keys
505 0x2F ; complement A, by request. [D34F]
507 0x47 ; A->B ;; now B contains the pressed keys
509 0xCB ; test bit 5 of D (are we in o/p mode?)
510 0x6A
511 0x28 ; if test == 0, skip this o/p section
512 0x13 ; JUMP
514 0xCB ; else, test bit 0 of D (fragile; are we in pc mode?)
515 0x42
516 0x28 ; if test == 0, skip the following command
517 0x01
519 ;; output mode I: moving the program counter
520 0xE9 ; ** move PC to (HL)
522 ;; output mode II: writing bytes
523 0xAF ; zero A
524 0xBB ; compare count to zero. finished writing?
525 0x28 ; if we are finished, jump back to cleanup
526 0x00 ; TODO: set jump length backwards.
528 ;; continue writing bytes
529 0x78 ;; B->A
530 0x22 ;; copy A to (HL) and increment HL.
531 0x18 ;; end frame. [goto D31F]
532 0xB6 ;; JUMP
535 ;; ---- end of o/p section
537 ;; i/p mode
538 ;; adhere to the mode discipline:
539 ;; D must be one of 0x00 0x08 0x10 0x18.
541 0x3E ;; load the constant 57 into A. [D369]
542 0x57
543 0x82 ;; add the mode to A
544 0xEA ;; store A into "thing to execute"
545 0x74
546 0xD3
548 0x3E ;; load the constant 8 into A
549 0x08
550 0x82 ;; add the mode to A
552 0x57 ;; store the incremented mode into D
553 0x78 ;; B->A; now A contains the pressed keys
555 0x00 ;; var: thing to execute [D374]
557 0x18 ;; end frame
558 0xA8 ;; JUMP
559 ]
560 )
562 (defn write-mem-dyl []
563 (-> (tick (mid-game))
564 (IE! 0)
565 (inject-item-assembly (write-memory-assembly*))))
567 (defn ntick [s n]
568 (if (zero? n) s
569 (do
570 (set-state! s)
571 (dorun (dotimes [_ n]
572 (com.aurellem.gb.Gb/tick)))
573 (update-state))))
576 (defn find-frame-shift [state]
577 ;;(restart!)
578 (set-state! state)
579 (loop [n 0]
580 (if (>= (first (registers)) 0xD32D)
581 (do (println n)
582 (update-state))
583 (do
584 (com.aurellem.gb.Gb/tick)
585 (recur (inc n) )))))
587 (defn demo-assembly [n]
588 (repeat n 0x00))
591 (defn find-frame-shift* [state]
592 (set-state! state)
593 (loop []
594 (com.aurellem.gb.Gb/tick)
595 ;;(println (first (registers)))
596 (if (>= (first (registers)) 0xD32D)
597 (update-state)
598 (recur))))
601 (defn dylan**
602 ([k]
603 (->
604 (tick (mid-game))
605 (inject-item-assembly(write-memory-assembly*))
606 ;;(find-frame-shift)
607 (ntick k)
608 (d-tick)
609 (view-register "A" A)
610 (view-register "B" B)
611 (view-register "D" D)
612 (view-register "E" E)
613 (view-register "F" F)
614 (#(do (println) %))
615 ))
616 ([] (dylan** 0)))
621 (defn dylan* []
622 (->
623 (write-mem-dyl)
625 (tick)
626 (tick)
627 (tick)
628 (tick)
629 (tick)
630 (tick)
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)
646 (tick)
647 (tick)
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)
662 ;;(view-memory 0xD374)
663 (tick)
664 (tick)
665 (tick)
666 (tick)
667 (tick)
668 (tick)
669 (tick)
670 (tick)
671 (tick)
672 (tick)
673 (tick)
674 (tick)
675 (tick)
676 (tick)
677 (tick)
678 ;;(view-memory 0xD374)
679 (d-tick)
681 (view-register "A" A)
682 (view-register "B" B)
683 (view-register "C" C))
685 )
688 (defn dylan []
689 (->
690 (write-mem-dyl)
691 (tick)
692 (tick)
693 (tick)
694 (tick)
695 (tick)
696 (tick)
697 (tick)
698 (tick)
699 (tick)
700 (tick)
701 (tick)
702 (tick)
703 (tick)
704 (tick)
705 (tick) ;; first loop
708 (tick)
709 (tick)
710 (tick)
711 (tick)
712 (tick)
713 (tick)
714 (tick)
715 (tick)
716 (tick)
717 (tick)
718 (tick)
719 (tick)
720 (tick) ;; dpad bits
722 (tick)
723 (tick)
724 (tick)
725 (tick)
726 (tick)
727 (tick)
728 (tick)
729 (tick)
730 (d-tick)
734 (view-register "A" A)
735 (view-register "B" B)
736 (view-register "C" C)
738 ))
743 (defn d2 []
744 (->
745 (write-mem-dyl)
746 (view-memory 0xD31F)
747 step step step step step
748 (view-memory 0xD31F)))
769 (defn write-memory-assembly []
770 [
771 ;; Main Timing Loop
772 ;; Constantly check for v-blank and Trigger main state machine on
773 ;; every transtion from v-blank to non-v-blank.
775 0x18 ; D31D ; Variable declaration
776 0x02 ; D31E
777 0x00 ; D31F ; frame-count
778 0x00 ; D320 ; v-blank-prev
780 0xF0 ; D321 ; load v-blank mode flags into A
781 0x41
782 0x00
785 ;; Branch dependent on v-blank. v-blank happens when the last two
786 ;; bits in A are "01"
787 0xCB ; D324
788 0x4F ; D325
790 0xC2 ; D326 ; if bit-1 is not 0, then
791 0x3E ; D327 ; GOTO non-v-blank.
792 0xD3 ; D328
794 0xCB ; D329
795 0x47 ; D32A
797 0xCA ; D32B ; if bit-0 is not 1, then
798 0x3E ; D32C ; GOTO non-v-blank.
799 0xD3 ; D32D
801 ;; V-Blank
802 ;; Activate state-machine if this is a transition event.
804 0xFA ; D32E ; load v-bank-prev into A
805 0x20 ; D32F
806 0xD3 ; D330
808 0xFE ; D331 ; compare A to 0. >--------\
809 0x00 ; D332 \
810 ; |
811 ;; set v-blank-prev to 1. |
812 0x3E ; D333 ; load 1 into A. |
813 0x01 ; D334 |
814 ; |
815 0xEA ; D335 ; load A into v-blank-prev |
816 0x20 ; D336 |
817 0xD3 ; D337 |
818 ; /
819 ;; if v-blank-prev was 0, activate state-machine <------/
820 0xCA ; D338 ; if v-blank-prev
821 0x46 ; D339 ; was 0,
822 0xD3 ; D33A ; GOTO state-machine
824 0xC3 ; D33B
825 0x1D ; D33C
826 0xD3 ; D33D ; GOTO beginning
827 ;; END V-blank
829 ;; Non-V-Blank
830 ;; Set v-blank-prev to 0
831 0x3E ; D33E ; load 0 into A
832 0x00 ; D33F
834 0xEA ; D340 ; load A into v-blank-prev
835 0x20 ; D341
836 0xD3 ; D342
838 0xC3 ; D343
839 0x1D ; D344
840 0xD3 ; D345 ; GOTO beginning
841 ;; END Not-V-Blank
844 ;; Main State Machine -- Input Section
845 ;; This is called once every frame.
846 ;; It collects input and uses it to drive the
847 ;; state transitions.
849 ;; Increment frame-count
850 0xFA ; D346 ; load frame-count into A
851 0x1F ; D347
852 0xD3 ; D348
854 0x3C ; D349 ; inc A
856 0xEA ; D34A
857 0x1F ; D34B ; load A into frame-count
858 0xD3 ; D34C
860 0x00 ; D34D ; glue :)
862 0x18 ;D34E ; skip next 3 bytes
863 0x03 ;D34F
864 ;D350
865 (Integer/parseInt "00100000" 2) ; select directional pad
866 ;D351
867 (Integer/parseInt "00010000" 2) ; select buttons
868 0x00 ;D352 ; input-number
870 ;; select directional pad; store low bits in B
872 0xFA ;D353 ; load (D350) into A
873 0x50 ;D354 -->
874 0xD3 ;D355 --> D350
876 0xE0 ;D356 ; load (A), which is
877 0x00 ;D357 --> ; 00010000, into FF00
878 0x00 ;D358 --> FF00 ;; NO-OP
880 0x06 ;D359
881 ;D35A
882 (Integer/parseInt "11110000" 2) ; "11110000" -> B
883 0xF0 ;D35B ; (FF00) -> A
884 0x00 ;D35C
885 0x00 ;D35D ;; NO-OP
887 0xCB ;D35E ; swap nybbles on A
888 0x37 ;D35F
889 0xA0 ;D360 ; (AND A B) -> A
890 0x47 ;D361 ; A -> B
892 ;; select buttons; store bottom bits in C
894 0xFA ;D362 ; load (D351) into A
895 0x51 ;D363 -->
896 0xD3 ;D364 --> D351
898 0xE0 ;D365 ; load (A), which is
899 0x00 ;D366 --> ; 00001000, into FF00
900 0x00 ;D367 --> FF00 ;; NO-OP
902 0x0E ;D368
903 ;D369
904 (Integer/parseInt "00001111" 2) ; "00001111" -> C
906 0xF0 ;D36A ; (FF00) -> A
907 0x00 ;D36B
908 0x00 ;D36C
910 0xA1 ;D36D ; (AND A C) -> A
911 0x4F ;D36E ; A -> C
913 ;; combine the B and C registers into the input number
914 0x79 ;D36F ; C -> A
915 0xB0 ;D370 ; (OR A B) -> A
916 0x2F ;D371 ; negate A
918 0xEA ;D372 ; store A into input-number
919 0x52 ;D373
920 0xD3 ;D374
922 0x00 ;D375
923 0x00 ;D376
924 0x00 ;D377
925 0x00 ;D378
926 0x00 ;D379
927 0x00 ;D37A
928 0x00 ;D37B ; these are here because
929 0x00 ;D37C ; I messed up :(
930 0x00 ;D37D
931 0x00 ;D37E
932 0x00 ;D37F
934 ;; beginning of main state machine
935 0x18 ;D380 ; Declaration of variables
936 0x05 ;D381 ; 5 variables:
937 0x00 ;D382 ; current-mode
938 0x00 ;D383 ; bytes-to-write
939 0x00 ;D384 ; bytes-written
940 0x00 ;D385 ; start-point-high
941 0x00 ;D386 ; start-point-low
944 ;; banch on current mode
945 0xFA ;D387 ; load current-mode (0xD382)
946 0x82 ;D388 ; into A
947 0xD3 ;D389
948 0x00 ;D38A
951 ;; GOTO Mode 0 (input-mode) if current-mode is 0
952 0xFE ;D38B
953 0x00 ;D38C ; compare A with 0x00
955 0xCA ;D38D ; goto Mode 0 if A == 0
956 0xA8 ;D38E
957 0xD3 ;D38F
959 ;; GOTO Mode 1 (set-length) if current-mode is 1
960 0xFE ;D390
961 0x01 ;D391 ; compare A with 0x01
963 0xCA ;D392
964 0xB1 ;D393
965 0xD3 ;D394 ; goto Mode 1 if A == 1
967 ;; GOTO Mode 2 (set-start-point-high) if current mode is 2
968 0xFE ;D395
969 0x02 ;D396 ; compare A with 0x02
971 0xCA ;D397
972 0xBF ;D398
973 0xD3 ;D399 ; goto Mode 2 if A == 2
975 ;; GOTO Mode 3 (set-start-point-low) if current mode is 3
976 0xFE ;D39A
977 0x03 ;D39B
979 0xCA ;D39C
980 0xCD ;D39D
981 0xD3 ;D39E ; goto Mode 3 if A == 3
983 ;; GOTO Mode 4 (write-memory) if current mode is 4
984 0xFE ;D39F
985 0x04 ;D3A0
987 0xCA ;D3A1
988 0xDB ;D3A2
989 0xD3 ;D3A3
991 0x00 ;D3A4
992 ;; End of Mode checking, goto beginning
993 0xC3 ;D3A5
994 0x1D ;D3A6
995 0xD3 ;D3A7
998 ;; Mode 0 -- input-mode mode
999 ;; means that we are waiting for a mode, so set the mode to
1000 ;; whatever is currently in input-number. If nothing is
1001 ;; entered, then the program stays in input-mode mode
1003 ;; set current-mode to input-number
1004 0xFA ;D3A8 ; load input-number (0xD352)
1005 0x52 ;D3A9 ; into A
1006 0xD3 ;D3AA
1008 0xEA ;D3AB ; load A into current-mode
1009 0x82 ;D3AC ; (0xD382)
1010 0xD3 ;D3AD
1012 0xC3 ;D3AE ; go back to beginning
1013 0x1D ;D3AF
1014 0xD3 ;D3B0
1015 ;; End Mode 0
1018 ;; Mode 1 -- set-length mode
1019 ;; This is the header for writing things to memory.
1020 ;; User specifies the number of bytes to write.
1021 ;; Mode is auto advanced to Mode 2 after this mode
1022 ;; completes.
1024 ;; Set bytes left to write to input-number;
1025 ;; set current-mode to 0x02.
1026 0xFA ;D3B1 ; load input-number (0xD352)
1027 0x52 ;D3B2 ; into A
1028 0xD3 ;D3B3
1030 0xEA ;D3B4 ; load A into bytes-left-to-write
1031 0x83 ;D3B5 ; (0xD383)
1032 0xD3 ;D3B6
1034 0x3E ;D3B7 ; load 0x02 into A.
1035 0x02 ;D3B8
1037 0xEA ;D3B9 ; load A to current-mode
1038 0x82 ;D3BA ; advancing from Mode 1 to
1039 0xD3 ;D3BB ; Mode 2
1041 0xC3 ;D3BC ; go back to beginning
1042 0x1D ;D3BD
1043 0xD3 ;D3BE
1044 ;; End Mode 1
1047 ;; Mode 2 -- set start-point-high mode
1048 ;; Middle part of the header for writing things to memory.
1049 ;; User specifies the start location in RAM to which
1050 ;; data will be written.
1051 ;; Mode is auto advanced to Mode 3 after this mode completes.
1053 ;; Set start-point-high to input-number;
1054 ;; set current mode to 0x03.
1055 0xFA ;D3BF ; load input-number (0xD352)
1056 0x52 ;D3C0 ; into A
1057 0xD3 ;D3C1
1059 0xEA ;D3C2 ; load A into start-point-high
1060 0x85 ;D3C3 ; (0xD385)
1061 0xD3 ;D3C4
1063 0x3E ;D3C5 ; load 0x03 into A.
1064 0x03 ;D3C6
1066 0xEA ;D3C7 ; load A to current-mode,
1067 0x82 ;D3C8 ; advancing from Mode 2 to
1068 0xD3 ;D3C9 ; Mode 3.
1070 0xC3 ;D3CA ; go back to beginning
1071 0x1D ;D3CB
1072 0xD3 ;D3CC
1073 ;;End Mode 2
1076 ;; Mode 3 -- set-start-point-low mode
1077 ;; Final part of header for writing things to memory.
1078 ;; User specifies the low bytes of 16 bit start-point.
1080 ;; Set start-point-low to input-number;
1081 ;; set current mode to 0x04
1082 0xFA ;D3CD ; load input-number into A
1083 0x52 ;D3CE
1084 0xD3 ;D3CF
1086 0xEA ;D3D0 ; load A into start-point-low
1087 0x86 ;D3D1
1088 0xD3 ;D3D2
1090 0x3E ;D3D3 ; load 0x04 into A.
1091 0x04 ;D3D4
1093 0xEA ;D3D5 ; load A to current-mode,
1094 0x82 ;D3D6 ; advancing from Mode 3 to
1095 0xD3 ;D3D7 ; Mode 4.
1097 0xC3 ;D3D8 ; go back to beginning
1098 0x1D ;D3D9
1099 0xD3 ;D3DA
1101 ;; Mode 4 -- write bytes mode
1103 ;; This is where RAM manipulation happens. User supplies
1104 ;; bytes every frame, which are written sequentially to
1105 ;; start-point until bytes-to-write have been written. Once
1106 ;; bytes-to-write have been written, the mode is reset to 0.
1108 ;; compare bytes-written with bytes-to-write.
1109 ;; if they are the same, then reset mode to 0
1111 0xFA ;D3DB ; load bytes-to-write into A
1112 0x83 ;D3DC
1113 0xD3 ;D3DD
1115 0x47 ;D3DE ; load A into B
1117 0xFA ;D3DF ; load bytes-written into A
1118 0x84 ;D3E0
1119 0xD3 ;D3E1
1121 0xB8 ;D3E2 ; compare A with B
1123 0xCA ;D3E3 ; if they are equal, go to cleanup
1124 0x07 ;D3E4
1125 0xD4 ;D3E5
1127 ;; Write Memory Section
1128 ;; Write the input-number, interpreted as an 8-bit number,
1129 ;; into the current target register, determined by
1130 ;; (+ start-point bytes-written).
1131 ;; Then, increment bytes-written by 1.
1133 0xFA ;D3E6 ; load start-point-high into A
1134 0x85 ;D3E7
1135 0xD3 ;D3E8
1137 0x67 ;D3E9 ; load A into H
1139 0xFA ;D3EA ; load start-point-low into A
1140 0x86 ;D3EB
1141 0xD3 ;D3EC
1143 0x6F ;D3ED ; load A into L
1145 0xFA ;D3EE ; load bytes-written into A
1146 0x84 ;D3EF
1147 0xD3 ;D3F0
1149 0x00 ;D3F1 ; These are here because
1150 0x00 ;D3F2 ; I screwed up again.
1151 0x00 ;D3F3
1153 0x85 ;D3F4 ; add L to A; store A in L.
1154 0x6F ;D3F5
1156 0x30 ;D3F6 ; If the addition overflowed,
1157 0x01 ;D3F7
1158 0x24 ;D3F8 ; increment H.
1160 ;; Now, HL points to the correct place in memory
1162 0xFA ;D3F9 ; load input-number into A
1163 0x52 ;D3FA
1164 0xD3 ;D3FB
1166 0x77 ;D3FC ; load A into (HL)
1168 0xFA ;D3FD ; load bytes-written into A
1169 0x84 ;D3FE
1170 0xD3 ;D3FF
1172 0x3C ;D400 ; increment A
1174 0xEA ;D401 ; load A into bytes-written
1175 0x84 ;D402
1176 0xD3 ;D403
1178 0xC3 ;D404 ; go back to beginning.
1179 0x1D ;D405
1180 0xD3 ;D406
1181 ;; End Write Memory Section
1183 ;; Mode 4 Cleanup Section
1184 ;; reset bytes-written to 0
1185 ;; set mode to 0
1186 0x3E ;D407 ; load 0 into A
1187 0x00 ;D408
1189 0xEA ;D409 ; load A into bytes-written
1190 0x84 ;D40A
1191 0xD3 ;D40B
1193 0xEA ;D40C ; load A into current-mode
1194 0x82 ;D40D
1195 0xD3 ;D40E
1197 0xC3 ;D40F ; go back to beginning
1198 0x1D ;D410
1199 0xD3 ;D411
1201 ;; End Mode 4
1203 ])
1207 (def frame-count 0xD31F)
1208 (def input 0xD352)
1209 (def current-mode 0xD382)
1210 (def bytes-to-write 0xD383)
1211 (def bytes-written 0xD384)
1212 (def start-point-high 0xD385)
1213 (def start-point-low 0xD386)
1217 (defn write-memory []
1218 (-> (tick (mid-game))
1219 (IE! 0) ; disable interrupts
1220 (inject-item-assembly (write-memory-assembly))))
1222 (defn test-write-memory []
1223 (set-state! (write-memory))
1224 (dorun
1225 (dotimes [_ 5000]
1226 (view-memory (step @current-state) current-mode))))
1228 (def bytes-to-write 0xD383)
1229 (def start-point 0xD384)
1231 (defn print-blank-assembly
1232 [start end]
1233 (dorun
1234 (map
1235 #(println (format "0x00 ;%04X " %))
1236 (range start end))))
1238 (defn test-mode-2 []
1239 (->
1240 (write-memory)
1241 (view-memory frame-count)
1242 (step)
1243 (step [:a])
1244 (step [:b])
1245 (step [:start])
1246 (step [])
1247 (view-memory frame-count)))
1251 (defn dylan-test-mode
1252 ([] (dylan-test-mode (write-mem-dyl)))
1253 ([target-state]
1254 (let [
1255 v-blank-prev 54046
1256 btn-register 65280
1257 eggs 0xD374
1260 (->
1261 target-state
1263 (tick)
1264 (tick)
1265 (tick)
1266 (tick);; jumps back to beginning
1268 (tick)
1269 (tick)
1270 (tick)
1271 (tick)
1272 (tick)
1273 (tick)
1274 (tick)
1275 (tick)
1276 (tick)
1277 (tick)
1278 (tick)
1279 (tick)
1282 (tick)
1283 (tick)
1284 (tick)
1285 (tick)
1286 (tick)
1287 (tick)
1288 (tick)
1289 (tick)
1290 (tick)
1291 (tick)
1292 (tick)
1293 (tick)
1294 (tick)
1295 (tick)
1296 (tick)
1297 (tick)
1298 (tick)
1299 (tick)
1300 (tick)
1301 (tick)
1302 (tick) ;; just complemented A
1304 (tick)
1305 (DE! 0x1800)
1306 (AF! 0x7700) ;; change inputs @ A
1307 (tick)
1308 (tick)
1309 (tick)
1310 (tick)
1311 (tick)
1313 ;;(view-memory eggs)
1314 (tick)
1315 (tick)
1316 ;;(view-memory eggs)
1317 (tick)
1318 (tick)
1319 (tick)
1320 (tick)
1321 (tick)
1322 (tick)
1323 (d-tick)
1326 ;;(view-memory btn-register)
1327 (view-register "A" A)
1328 (view-register "B" B)
1330 ;;(view-register "C" C)
1331 (view-register "D" D)
1332 (view-register "E" E)
1333 (view-register "H" H)
1334 (view-register "L" L)
1335 ))))
1339 (defn drive-dylan []
1340 (-> (write-mem-dyl)
1341 (#(do (println "memory from 0xC00F to 0xC01F:"
1342 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
1343 (step [])
1344 (step [])
1345 (step [])
1346 (step [:start])
1347 (step [:select])
1348 (step [:u :d])
1349 (step [:a :b :start :select])
1350 (step [:a])
1351 (step [:b])
1352 (step [:a :b])
1353 (step [:select])
1354 (step [])
1355 (step [])
1356 (step [])
1357 (#(do (println "memory from 0xC00F to 0xC01F:"
1358 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
1359 ))
1361 (defn test-mode-4
1362 ([] (test-mode-4 (write-memory)))
1363 ([target-state]
1364 (->
1365 target-state
1366 (#(do (println "memory from 0xC00F to 0xC01F:"
1367 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
1368 (view-memory current-mode)
1369 (step [])
1370 (step [])
1371 (step [])
1372 (#(do (println "after three steps") %))
1373 (view-memory current-mode)
1375 ;; Activate memory writing mode
1377 (#(do (println "step with [:a]") %))
1378 (step [:a])
1379 (view-memory current-mode)
1380 (view-memory bytes-to-write)
1381 (view-memory start-point-high)
1382 (view-memory start-point-low)
1384 ;; Specify four bytes to be written
1386 (#(do (println "step with [:select]")%))
1387 (step [:select])
1388 (view-memory current-mode)
1389 (view-memory bytes-to-write)
1390 (view-memory start-point-high)
1391 (view-memory start-point-low)
1393 ;; Specify target memory address as 0xC00F
1395 (#(do (println "step with [:u :d]")%))
1396 (step [:u :d])
1397 (view-memory current-mode)
1398 (view-memory bytes-to-write)
1399 (view-memory start-point-high)
1400 (view-memory start-point-low)
1402 (#(do (println "step with [:a :b :start :select]")%))
1403 (step [:a :b :start :select])
1404 (view-memory current-mode)
1405 (view-memory bytes-to-write)
1406 (view-memory start-point-high)
1407 (view-memory start-point-low)
1409 ;; Start reprogramming memory
1411 (#(do (println "step with [:a]")%))
1412 (step [:a])
1413 (view-memory current-mode)
1414 (view-memory bytes-written)
1416 (#(do (println "step with [:b]")%))
1417 (step [:b])
1418 (view-memory current-mode)
1419 (view-memory bytes-written)
1421 (#(do (println "step with [:a :b]")%))
1422 (step [:a :b])
1423 (view-memory current-mode)
1424 (view-memory bytes-written)
1426 (#(do (println "step with [:select]")%))
1427 (step [:select])
1428 (view-memory current-mode)
1429 (view-memory bytes-written)
1431 ;; Reprogramming done, program ready for more commands.
1433 (#(do (println "step with []")%))
1434 (step [])
1435 (view-memory current-mode)
1436 (view-memory bytes-written)
1438 (#(do (println "memory from 0xC00F to 0xC01F:"
1439 (subvec (vec (memory %)) 0xC00F 0xC01F)) %)))))
1446 ;;; ASSEMBLY-READING UTILITIES
1448 (def opcodes
1450 "NOP"
1451 "LD BC,nn"
1452 "LD (BC),A"
1453 "INC BC"
1454 "INC B"
1455 "DEC B"
1456 "LD B,n"
1457 "RLC A"
1458 "LD (nn),SP"
1459 "ADD HL,BC"
1460 "LD A,(BC)"
1461 "DEC BC"
1462 "INC C"
1463 "DEC C"
1464 "LD C,n"
1465 "RRC A"
1467 "STOP"
1468 "LD DE,nn"
1469 "LD (DE),A"
1470 "INC DE"
1471 "INC D"
1472 "DEC D"
1473 "LD D,n"
1474 "RL A"
1475 "JR n"
1476 "ADD HL,DE"
1477 "LD A,(DE)"
1478 "DEC DE"
1479 "INC E"
1480 "DEC E"
1481 "LD E,n"
1482 "RR A"
1484 "JR NZ,n"
1485 "LD HL,nn"
1486 "LDI (HL),A"
1487 "INC HL"
1488 "INC H"
1489 "DEC H"
1490 "LD H,n"
1491 "DAA"
1492 "JR Z,n"
1493 "ADD HL,HL"
1494 "LDI A,(HL)"
1495 "DEC HL"
1496 "INC L"
1497 "DEC L"
1498 "LD L,n"
1499 "CPL"
1501 "JR NC,n"
1502 "LD SP,nn"
1503 "LDD (HL),A"
1504 "INC SP"
1505 "INC (HL)"
1506 "DEC (HL)"
1507 "LD (HL),n"
1508 "SCF"
1509 "JR C,n"
1510 "ADD HL,SP"
1511 "LDD A,(HL)"
1512 "DEC SP"
1513 "INC A"
1514 "DEC A"
1515 "LD A,n"
1516 "CCF"
1518 "LD B,B"
1519 "LD B,C"
1520 "LD B,D"
1521 "LD B,E"
1522 "LD B,H"
1523 "LD B,L"
1524 "LD B,(HL)"
1525 "LD B,A"
1526 "LD C,B"
1527 "LD C,C"
1528 "LD C,D"
1529 "LD C,E"
1530 "LD C,H"
1531 "LD C,L"
1532 "LD C,(HL)"
1533 "LD C,A"
1535 "LD D,B"
1536 "LD D,C"
1537 "LD D,D"
1538 "LD D,E"
1539 "LD D,H"
1540 "LD D,L"
1541 "LD D,(HL)"
1542 "LD D,A"
1543 "LD E,B"
1544 "LD E,C"
1545 "LD E,D"
1546 "LD E,E"
1547 "LD E,H"
1548 "LD E,L"
1549 "LD E,(HL)"
1550 "LD E,A"
1552 "LD H,B"
1553 "LD H,C"
1554 "LD H,D"
1555 "LD H,E"
1556 "LD H,H"
1557 "LD H,L"
1558 "LD H,(HL)"
1559 "LD H,A"
1560 "LD L,B"
1561 "LD L,C"
1562 "LD L,D"
1563 "LD L,E"
1564 "LD L,H"
1565 "LD L,L"
1566 "LD L,(HL)"
1567 "LD L,A"
1569 "LD (HL),B"
1570 "LD (HL),C"
1571 "LD (HL),D"
1572 "LD (HL),E"
1573 "LD (HL),H"
1574 "LD (HL),L"
1575 "HALT"
1576 "LD (HL),A"
1577 "LD A,B"
1578 "LD A,C"
1579 "LD A,D"
1580 "LD A,E"
1581 "LD A,H"
1582 "LD A,L"
1583 "LD A,(HL)"
1584 "LD A,A"
1586 "ADD A,B"
1587 "ADD A,C"
1588 "ADD A,D"
1589 "ADD A,E"
1590 "ADD A,H"
1591 "ADD A,L"
1592 "ADD A,(HL)"
1593 "ADD A,A"
1594 "ADC A,B"
1595 "ADC A,C"
1596 "ADC A,D"
1597 "ADC A,E"
1598 "ADC A,H"
1599 "ADC A,L"
1600 "ADC A,(HL)"
1601 "ADC A,A"
1603 "SUB A,B"
1604 "SUB A,C"
1605 "SUB A,D"
1606 "SUB A,E"
1607 "SUB A,H"
1608 "SUB A,L"
1609 "SUB A,(HL)"
1610 "SUB A,A"
1611 "SBC A,B"
1612 "SBC A,C"
1613 "SBC A,D"
1614 "SBC A,E"
1615 "SBC A,H"
1616 "SBC A,L"
1617 "SBC A,(HL)"
1618 "SBC A,A"
1620 "AND B"
1621 "AND C"
1622 "AND D"
1623 "AND E"
1624 "AND H"
1625 "AND L"
1626 "AND (HL)"
1627 "AND A"
1628 "XOR B"
1629 "XOR C"
1630 "XOR D"
1631 "XOR E"
1632 "XOR H"
1633 "XOR L"
1634 "XOR (HL)"
1635 "XOR A"
1637 "OR B"
1638 "OR C"
1639 "OR D"
1640 "OR E"
1641 "OR H"
1642 "OR L"
1643 "OR (HL)"
1644 "OR A"
1645 "CP B"
1646 "CP C"
1647 "CP D"
1648 "CP E"
1649 "CP H"
1650 "CP L"
1651 "CP (HL)"
1652 "CP A"
1654 "RET NZ"
1655 "POP BC"
1656 "JP NZ,nn"
1657 "JP nn"
1658 "CALL NZ,nn"
1659 "PUSH BC"
1660 "ADD A,n"
1661 "RST 0"
1662 "RET Z"
1663 "RET"
1664 "JP Z,nn"
1665 "Ext ops"
1666 "CALL Z,nn"
1667 "CALL nn"
1668 "ADC A,n"
1669 "RST 8"
1671 "RET NC"
1672 "POP DE"
1673 "JP NC,nn"
1674 "XX"
1675 "CALL NC,nn"
1676 "PUSH DE"
1677 "SUB A,n"
1678 "RST 10"
1679 "RET C"
1680 "RETI"
1681 "JP C,nn"
1682 "XX"
1683 "CALL C,nn"
1684 "XX"
1685 "SBC A,n"
1686 "RST 18"
1688 "LDH (n),A"
1689 "POP HL"
1690 "LDH (C),A"
1691 "XX"
1692 "XX"
1693 "PUSH HL"
1694 "AND n"
1695 "RST 20"
1696 "ADD SP,d"
1697 "JP (HL)"
1698 "LD (nn),A"
1699 "XX"
1700 "XX"
1701 "XX"
1702 "XOR n"
1703 "RST 28"
1705 "LDH A,(n)"
1706 "POP AF"
1707 "XX"
1708 "DI"
1709 "XX"
1710 "PUSH AF"
1711 "OR n"
1712 "RST 30"
1713 "LDHL SP,d"
1714 "LD SP,HL"
1715 "LD A,(nn)"
1716 "EI"
1717 "XX"
1718 "XX"
1719 "CP n"
1720 "RST 38"])
1723 (defn hex
1724 "Converts the number into a hexadecimal-formatted symbol."
1725 [n]
1726 (symbol (str "0x" (.toUpperCase (Integer/toHexString n)))))
1730 (defn arity
1731 "Returns the arity of the given opcode (hex numeral)."
1732 [op]
1733 (cond
1734 (#{0x06 0x0E 0x16 0x1E
1735 0x20 0x26 0x28 0x2E
1736 0x30 0x36 0x38 0x3E
1737 0xC6 0xD6 0xCE 0xDE
1738 0xE0 0xF0 0xE6 0xF6
1739 0xEE 0xFE} op)
1741 (#{0x01 0x08 0x11 0x21
1742 0x31 0xC2 0xC3 0xC4
1743 0xCA 0xDA 0xCC 0xDC
1744 0xCD 0xEA 0xFA} op)
1746 :else
1747 0))