Mercurial > vba-clojure
view clojure/com/aurellem/assembly.clj @ 137:1c58fa3cfc68
Checkpoint: about to include the state machine in the assembly* code.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Sun, 18 Mar 2012 20:49:40 -0500 |
parents | ffeeabae7dcd |
children | 2b69cbe8a5b9 |
line wrap: on
line source
1 (ns com.aurellem.assembly2 (: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-assembly9 ([^SaveState state10 program-counter registers11 assembly-code]12 (let [scratch-memory (memory state)]13 ;; inject assembly code14 (dorun (map (fn [index val]15 (aset scratch-memory index val))16 (range program-counter17 (+ program-counter (count assembly-code)))18 assembly-code))19 (-> state20 (write-memory! scratch-memory)21 (write-registers! registers)22 (PC! program-counter)))))24 (defn inject-item-assembly25 ([^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 info33 ([^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-interrupt40 [^SaveState state]41 (println (format "IE: %d" (IE state)))42 state)44 (defn print-listing [state begin end]45 (dorun (map46 (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-assembly53 ([info-fn assembly n]54 (let [final-state55 (reduce (fn [state _]56 (tick (info-fn state)))57 (inject-item-assembly58 (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 B [state]70 (bit-shift-right (bit-and 0x0000FF00 (BC state)) 8))72 (defn C [state]73 (bit-and 0xFF (BC state)))75 (defn binary-str [num]76 (format "%08d"77 (Integer/parseInt78 (Integer/toBinaryString num) 10)))80 (defn view-register [state name reg-fn]81 (println (format "%s: %s" name82 (binary-str (reg-fn state))))83 state)85 (defn view-memory [state mem]86 (println (format "mem 0x%04X = %s" mem87 (binary-str (aget (memory state) mem))))88 state)90 (defn trace [state]91 (loop [program-counters [(first (registers @current-state)) ]92 opcodes [(aget (memory @current-state) (PC @current-state))]]93 (let [frame-boundary?94 (com.aurellem.gb.Gb/tick)]95 (if frame-boundary?96 [program-counters opcodes]97 (recur98 (conj program-counters99 (first (registers @current-state)))100 (conj opcodes101 (aget (memory @current-state)102 (PC @current-state))))))))104 (defn print-trace [state n]105 (let [[program-counters opcodes] (trace state)]106 (dorun (map (fn [pc op] (println (format "%04X: 0x%02X" pc op)))107 (take n program-counters)108 (take n opcodes)))))110 (defn good-trace []111 (-> (mid-game) (tick) (IE! 0)112 (set-inv-mem [0x00 0x00 0X00 0x00])113 (PC! item-list-start)(print-interrupt)114 (info) (tick) (info) (tick) (info)))116 (defn read-down-button []117 (-> (tick (mid-game))118 (IE! 0) ; disable interrupts119 (inject-item-assembly120 ;; write 00010000 to 0xFF00 to select joypad121 [0x18 ;D31D ; jump over122 0x01 ;D31E ; the next 8 bits123 ;D31F124 (Integer/parseInt "00100000" 2) ; data section126 0xFA ;D320 ; load (D31F) into A127 0x1F ;D321 -->128 0xD3 ;D322 --> D31F130 0xEA ;D323 ; load (A), which is131 0x00 ;D324 --> ; 00010000, into FF00132 0xFF ;D325 --> FF00134 0x18 ;D326 ; this is the place where135 0x01 ;D327 ; we will store whether136 0x00 ;D328 ; "down" is pressed.138 0xFA ;D329 ; (FF00) -> A139 0x00 ;D32A140 0xFF ;D32B142 0xCB ;D32C ; Test whether "down"143 0x5F ;D32D ; is pressed.145 0x28 ;D32E ; if down is pressed,146 0x03 ;D32F ; skip the next section147 ; of code.148 ;; down-is-not-pressed149 0xC3 ;D330150 0x1D ;D331 ; return to beginning151 0xD3 ;D332153 ;; down-is-pressed154 0xEA ;D334 ; write A to D328 if155 0x28 ;D335 ; "down" was pressed156 0xD3 ;D336158 0xC3 ;D330159 0x1D ;D331 ; return to beginning160 0xD3 ;D332161 ])))163 (defn test-read-down []164 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)165 (view-memory (step (step (read-down-button))) 0xD328)))167 (defn count-frames []168 (-> (tick (mid-game))169 (IE! 0) ; disable interrupts170 (inject-item-assembly171 [0x18 ;D31D ; jump over172 0x02 ;D31E ; the next 2 bytes173 0x00 ;D31F ; frame-count174 0x00 ;D320 ; v-blank-prev176 0xFA ;D321177 0x41 ;D322 ; load (FF41) into A178 0xFF ;D323 ; this contains mode flags180 ;; if we're in v-blank, the bit-1 is 0181 ;; and bit-2 is 1 Otherwise, it is not v-blank.182 0xCB ;D324 ; test bit-1 of A183 0x4F ;D325185 0xC2 ;D326 ; if bit-1 is not 0186 0x44 ;D327 ; GOTO not-v-blank187 0xD3 ;D328189 0xCB ;D329 ; test bit-0 of A190 0x47 ;D32A192 0xCA ;D32B ; if bit-0 is not 1193 0x44 ;D32C ; GOTO not-v-blank194 0xD3 ;D32D195 ;;; in v-blank mode196 ;; if v-blank-prev was 0,197 ;; increment frame-count199 0xFA ;D32E ; load v-blank-prev to A200 0x20 ;D32F201 0xD3 ;D330203 0xCB ;D331204 0x47 ;D332 ; test bit-0 of A206 0x20 ;D333 ; skip next section207 0x07 ;D334 ; if v-blank-prev was not zero209 ;; v-blank was 0, increment frame-count210 0xFA ;D335 ; load frame-count into A211 0x1F ;D336212 0xD3 ;D337214 0x3C ;D338 ; inc A216 0xEA ;D339 ; load A into frame-count217 0x1F ;D33A218 0xD3 ;D33B220 ;; set v-blank-prev to 1221 0x3E ;D33C ; load 1 into A222 0x01 ;D33D224 0xEA ;D33E ; load A into v-blank-prev225 0x20 ;D33F226 0xD3 ;D340228 0xC3 ;D341 ; return to beginning229 0x1D ;D342230 0xD3 ;D343232 ;;; not in v-blank mode233 ;; set v-blank-prev to 0234 0x3E ;D344 ; load 0 into A235 0x00 ;D345237 0xEA ;D346 ; load A into v-blank-prev238 0x20 ;D347239 0xD3 ;D348241 0xC3 ;D349 ; return to beginning242 0x1D ;D34A243 0xD3 ;D34B244 ])))246 (defn step-count-frames []247 (-> (read-down-button)248 (info)249 (tick) ;; skip over data section250 (info)251 (view-register "Register A" A)252 (tick) ;; load-data into A253 (view-register "Register A" A)254 (info)255 (view-memory 0xFF00)256 (tick) ;; load A into 0xFF00257 (view-memory 0xFF00)258 (info)259 (tick)260 (info)261 (tick)262 (info)263 (tick)264 (info)265 (tick)266 (info)267 (tick)268 (info)269 (tick)270 (print-inventory)))272 (defn test-count-frames []273 (= 255 (aget (memory ((apply comp (repeat 255 step))274 (count-frames)))275 0xD31F)))277 ;; specs for main bootstrap program278 ;; starts in "mode-select" mode279 ;; Each button press takes place in a single frame.280 ;; mode-select-mode takes one of the main buttons281 ;; which selects one of up to eight modes282 ;; mode 1 activated by the "A" button283 ;; the next two button presses indicates the start284 ;; memory location which to which the bootstrap285 ;; program will write.286 ;; This is done by using each of the eight buttons to287 ;; spell out an 8 bit number. The order of buttons is288 ;; [:d :u :l :r :start :select :b :a]289 ;; [:a :start :l] --> 00101001291 ;; the next button press determines how many bytes are to be292 ;; written, starting at the start position.294 ;; then, the actual bytes are entered and are written to the295 ;; start address in sequence.297 (defn input-number-assembly []298 [0x18 ;D31D ; jump over299 0x02 ;D31E ; the next 2 bytes300 0x00 ;D31F ; frame-count301 0x00 ;D320 ; v-blank-prev303 0xFA ;D321304 0x41 ;D322 ; load (FF41) into A305 0xFF ;D323 ; this contains mode flags307 ;; if we're in v-blank, the bit-1 is 0308 ;; and bit-2 is 1 Otherwise, it is not v-blank.309 0xCB ;D324 ; test bit-1 of A310 0x4F ;D325312 0xC2 ;D326 ; if bit-1 is not 0313 0x44 ;D327 ; GOTO not-v-blank314 0xD3 ;D328316 0xCB ;D329 ; test bit-0 of A317 0x47 ;D32A319 0xCA ;D32B ; if bit-0 is not 1320 0x44 ;D32C ; GOTO not-v-blank321 0xD3 ;D32D323 ;;; in v-blank mode325 ;; if v-blank-prev was 0,326 ;; increment frame-count328 0xFA ;D32E ; load v-blank-prev to A329 0x20 ;D32F330 0xD3 ;D330332 0xCB ;D331333 0x47 ;D332 ; test bit-0 of A335 0x20 ;D333 ; skip next section336 0x07 ;D334 ; if v-blank-prev was not zero338 ;; v-blank was 0, increment frame-count339 0xFA ;D335 ; load frame-count into A340 0x1F ;D336341 0xD3 ;D337343 0x3C ;D338 ; inc A345 0xEA ;D339 ; load A into frame-count346 0x1F ;D33A347 0xD3 ;D33B349 ;; set v-blank-prev to 1350 0x3E ;D33C ; load 1 into A351 0x01 ;D33D353 0xEA ;D33E ; load A into v-blank-prev354 0x20 ;D33F355 0xD3 ;D340357 0xC3 ;D341 ; GOTO input handling code358 0x4E ;D342359 0xD3 ;D343361 ;;; not in v-blank mode362 ;; set v-blank-prev to 0363 0x3E ;D344 ; load 0 into A364 0x00 ;D345366 0xEA ;D346 ; load A into v-blank-prev367 0x20 ;D347368 0xD3 ;D348370 0xC3 ;D349 ; return to beginning371 0x1D ;D34A372 0xD3 ;D34B374 0x00 ;D34C ; these are here375 0x00 ;D34D ; for glue378 ;;; calculate input number based on button presses379 0x18 ;D34E ; skip next 3 bytes380 0x03 ;D34F381 ;D350382 (Integer/parseInt "00100000" 2) ; select directional pad383 ;D351384 (Integer/parseInt "00010000" 2) ; select buttons385 0x00 ;D352 ; input-number387 ;; select directional pad, store low bits in B389 0xFA ;D353 ; load (D350) into A390 0x50 ;D354 -->391 0xD3 ;D355 --> D31F393 0xEA ;D356 ; load A, which is394 0x00 ;D357 --> ; 00010000, into FF00395 0xFF ;D358 --> FF00397 0x06 ;D359398 ;D35A399 (Integer/parseInt "11110000" 2) ; "11110000" -> B400 0xFA ;D35B ; (FF00) -> A401 0x00 ;D35C402 0xFF ;D35D404 0xCB ;D35E ; swap nybbles on A405 0x37 ;D35F406 0xA0 ;D360 ; (AND A B) -> A407 0x47 ;D361 ; A -> B409 ;; select buttons store bottom bits in C411 0xFA ; ; load (D351) into A412 0x51 ; -->413 0xD3 ; --> D31F415 0xEA ; ; load (A), which is416 0x00 ; --> ; 00001000, into FF00417 0xFF ; --> FF00419 0x0E ;420 (Integer/parseInt "00001111" 2) ; "00001111" -> C422 0xFA ; ; (FF00) -> A423 0x00 ;424 0xFF ;426 0xA1 ; ; (AND A C) -> A427 0x4F ; ; A -> C429 ;; combine the B and C registers into the input number430 0x79 ; ; C -> A431 0xB0 ; ; (OR A B) -> A432 0x2F ; ; negate A434 0xEA ; ; store A into input-number435 0x52 ;436 0xD3 ;438 0xC3 ; ; return to beginning439 0x1D ;440 0xD3 ;441 ])444 (defn print-pc [state]445 (println (format "PC: 0x%04X" (PC state)))446 state)448 (defn print-op [state]449 (println (format "OP: 0x%02X" (aget (memory state) (PC state))))450 state)452 (defn d-tick453 ([state]454 (-> state print-pc print-op tick)))456 (defn input-number []457 (-> (tick (mid-game))458 (IE! 0) ; disable interrupts459 (inject-item-assembly (input-number-assembly))))461 (defn test-input-number462 "Input freestyle buttons and observe the effects at the repl."463 []464 (set-state! (input-number))465 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))495 (defn write-memory-assembly*496 "Currently, grabs input from the user each frame."497 []498 [499 ;; --------- FRAME METRONOME500 0x00 ;; v-blank-prev D31D502 0xFA ;; load modes into A503 0x41504 0xFF506 0x47 ;; A -> B507 0xCB ;; rotate A508 0x2F509 0x2F ;; invert A511 0xA0512 0x47 ;; now B_0 contains (VB==1)514 0xFA ;; load v-blank-prev515 0x1D516 0xD3518 0x2F ;; complement v-blank-prev520 0xA0 ;; A & B --> A521 0x4F ;; now C_0 contains increment?524 0x78 ;; B->A526 0xEA ;; spit A --> vbprev527 0x1D528 0xD3530 0xCB ;test C_0531 0x41532 0x20 ; JUMP ahead to button input if nonzero533 0x03534 0x18 ; JUMP back to frame metronome (D31E)535 0xE7537 ;; -------- GET BUTTON INPUT539 ;; btw, C_0 is now 1540 0x00 ;; var: which-input D337541 ;; prepare to select bits543 0x06 ;; load 0x00 into B544 0x00546 0x3E ;; load 0x20 into A, to measure dpad547 0x20550 0xE0 ;; load A into [FF00] ;; D33C551 0x00553 0xF0 ;; load A from [FF00]554 0x00556 0xE6 ;; bitmask 00001111557 0x0F559 0xB0 ;; A or B --> A560 0xCB561 0x41 ;; test bit 0 of C562 0x28 ;; JUMP forward if 0563 0x08565 0x47 ;; A -> B566 0xCB ;; swap B nybbles567 0x30568 0x0C ;; increment C569 0x3E ;; load 0x10 into A, to measure btns570 0x10571 0x18 ;; JUMP back to "load A into [FF00]" [20 steps?]572 0xED574 ;; now A contains the pressed keys583 0xC3 ;; todo replace with relative jump584 0x1E585 0xD3586 ;;0x18 ;;JUMP back to "metronome" in one hop [D31E]587 ;;0xCA ;; E1589 ]590 )592 (defn write-mem-dyl []593 (-> (tick (mid-game))594 (IE! 0)595 (inject-item-assembly (write-memory-assembly*))))598 (defn dylan []599 (->600 (write-mem-dyl)601 (tick)602 (tick)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) ;; first loop618 (tick)619 (tick)620 (tick)621 (tick)622 (tick)623 (tick)624 (tick)625 (tick)626 (tick)627 (tick)628 (tick)629 (tick)630 (tick) ;; dpad bits632 (tick)633 (tick)634 (tick)635 (tick)636 (tick)637 (tick)638 (tick)639 (tick)640 (d-tick)644 (view-register "A" A)645 (view-register "B" B)646 (view-register "C" C)648 ))653 (defn d2 []654 (->655 (write-mem-dyl)656 (view-memory 0xD31F)657 step step step step step658 (view-memory 0xD31F)))679 (defn write-memory-assembly []680 [681 ;; Main Timing Loop682 ;; Constantly check for v-blank and Trigger main state machine on683 ;; every transtion from v-blank to non-v-blank.685 0x18 ; D31D ; Variable declaration686 0x02 ; D31E687 0x00 ; D31F ; frame-count688 0x00 ; D320 ; v-blank-prev690 0xF0 ; D321 ; load v-blank mode flags into A691 0x41692 0x00695 ;; Branch dependent on v-blank. v-blank happens when the last two696 ;; bits in A are "01"697 0xCB ; D324698 0x4F ; D325700 0xC2 ; D326 ; if bit-1 is not 0, then701 0x3E ; D327 ; GOTO non-v-blank.702 0xD3 ; D328704 0xCB ; D329705 0x47 ; D32A707 0xCA ; D32B ; if bit-0 is not 1, then708 0x3E ; D32C ; GOTO non-v-blank.709 0xD3 ; D32D711 ;; V-Blank712 ;; Activate state-machine if this is a transition event.714 0xFA ; D32E ; load v-bank-prev into A715 0x20 ; D32F716 0xD3 ; D330718 0xFE ; D331 ; compare A to 0. >--------\719 0x00 ; D332 \720 ; |721 ;; set v-blank-prev to 1. |722 0x3E ; D333 ; load 1 into A. |723 0x01 ; D334 |724 ; |725 0xEA ; D335 ; load A into v-blank-prev |726 0x20 ; D336 |727 0xD3 ; D337 |728 ; /729 ;; if v-blank-prev was 0, activate state-machine <------/730 0xCA ; D338 ; if v-blank-prev731 0x46 ; D339 ; was 0,732 0xD3 ; D33A ; GOTO state-machine734 0xC3 ; D33B735 0x1D ; D33C736 0xD3 ; D33D ; GOTO beginning737 ;; END V-blank739 ;; Non-V-Blank740 ;; Set v-blank-prev to 0741 0x3E ; D33E ; load 0 into A742 0x00 ; D33F744 0xEA ; D340 ; load A into v-blank-prev745 0x20 ; D341746 0xD3 ; D342748 0xC3 ; D343749 0x1D ; D344750 0xD3 ; D345 ; GOTO beginning751 ;; END Not-V-Blank754 ;; Main State Machine -- Input Section755 ;; This is called once every frame.756 ;; It collects input and uses it to drive the757 ;; state transitions.759 ;; Increment frame-count760 0xFA ; D346 ; load frame-count into A761 0x1F ; D347762 0xD3 ; D348764 0x3C ; D349 ; inc A766 0xEA ; D34A767 0x1F ; D34B ; load A into frame-count768 0xD3 ; D34C770 0x00 ; D34D ; glue :)772 0x18 ;D34E ; skip next 3 bytes773 0x03 ;D34F774 ;D350775 (Integer/parseInt "00100000" 2) ; select directional pad776 ;D351777 (Integer/parseInt "00010000" 2) ; select buttons778 0x00 ;D352 ; input-number780 ;; select directional pad; store low bits in B782 0xFA ;D353 ; load (D350) into A783 0x50 ;D354 -->784 0xD3 ;D355 --> D350786 0xE0 ;D356 ; load (A), which is787 0x00 ;D357 --> ; 00010000, into FF00788 0x00 ;D358 --> FF00 ;; NO-OP790 0x06 ;D359791 ;D35A792 (Integer/parseInt "11110000" 2) ; "11110000" -> B793 0xF0 ;D35B ; (FF00) -> A794 0x00 ;D35C795 0x00 ;D35D ;; NO-OP797 0xCB ;D35E ; swap nybbles on A798 0x37 ;D35F799 0xA0 ;D360 ; (AND A B) -> A800 0x47 ;D361 ; A -> B802 ;; select buttons; store bottom bits in C804 0xFA ;D362 ; load (D351) into A805 0x51 ;D363 -->806 0xD3 ;D364 --> D351808 0xE0 ;D365 ; load (A), which is809 0x00 ;D366 --> ; 00001000, into FF00810 0x00 ;D367 --> FF00 ;; NO-OP812 0x0E ;D368813 ;D369814 (Integer/parseInt "00001111" 2) ; "00001111" -> C816 0xF0 ;D36A ; (FF00) -> A817 0x00 ;D36B818 0x00 ;D36C820 0xA1 ;D36D ; (AND A C) -> A821 0x4F ;D36E ; A -> C823 ;; combine the B and C registers into the input number824 0x79 ;D36F ; C -> A825 0xB0 ;D370 ; (OR A B) -> A826 0x2F ;D371 ; negate A828 0xEA ;D372 ; store A into input-number829 0x52 ;D373830 0xD3 ;D374832 0x00 ;D375833 0x00 ;D376834 0x00 ;D377835 0x00 ;D378836 0x00 ;D379837 0x00 ;D37A838 0x00 ;D37B ; these are here because839 0x00 ;D37C ; I messed up :(840 0x00 ;D37D841 0x00 ;D37E842 0x00 ;D37F844 ;; beginning of main state machine845 0x18 ;D380 ; Declaration of variables846 0x05 ;D381 ; 5 variables:847 0x00 ;D382 ; current-mode848 0x00 ;D383 ; bytes-to-write849 0x00 ;D384 ; bytes-written850 0x00 ;D385 ; start-point-high851 0x00 ;D386 ; start-point-low854 ;; banch on current mode855 0xFA ;D387 ; load current-mode (0xD382)856 0x82 ;D388 ; into A857 0xD3 ;D389858 0x00 ;D38A861 ;; GOTO Mode 0 (input-mode) if current-mode is 0862 0xFE ;D38B863 0x00 ;D38C ; compare A with 0x00865 0xCA ;D38D ; goto Mode 0 if A == 0866 0xA8 ;D38E867 0xD3 ;D38F869 ;; GOTO Mode 1 (set-length) if current-mode is 1870 0xFE ;D390871 0x01 ;D391 ; compare A with 0x01873 0xCA ;D392874 0xB1 ;D393875 0xD3 ;D394 ; goto Mode 1 if A == 1877 ;; GOTO Mode 2 (set-start-point-high) if current mode is 2878 0xFE ;D395879 0x02 ;D396 ; compare A with 0x02881 0xCA ;D397882 0xBF ;D398883 0xD3 ;D399 ; goto Mode 2 if A == 2885 ;; GOTO Mode 3 (set-start-point-low) if current mode is 3886 0xFE ;D39A887 0x03 ;D39B889 0xCA ;D39C890 0xCD ;D39D891 0xD3 ;D39E ; goto Mode 3 if A == 3893 ;; GOTO Mode 4 (write-memory) if current mode is 4894 0xFE ;D39F895 0x04 ;D3A0897 0xCA ;D3A1898 0xDB ;D3A2899 0xD3 ;D3A3901 0x00 ;D3A4902 ;; End of Mode checking, goto beginning903 0xC3 ;D3A5904 0x1D ;D3A6905 0xD3 ;D3A7908 ;; Mode 0 -- input-mode mode909 ;; means that we are waiting for a mode, so set the mode to910 ;; whatever is currently in input-number. If nothing is911 ;; entered, then the program stays in input-mode mode913 ;; set current-mode to input-number914 0xFA ;D3A8 ; load input-number (0xD352)915 0x52 ;D3A9 ; into A916 0xD3 ;D3AA918 0xEA ;D3AB ; load A into current-mode919 0x82 ;D3AC ; (0xD382)920 0xD3 ;D3AD922 0xC3 ;D3AE ; go back to beginning923 0x1D ;D3AF924 0xD3 ;D3B0925 ;; End Mode 0928 ;; Mode 1 -- set-length mode929 ;; This is the header for writing things to memory.930 ;; User specifies the number of bytes to write.931 ;; Mode is auto advanced to Mode 2 after this mode932 ;; completes.934 ;; Set bytes left to write to input-number;935 ;; set current-mode to 0x02.936 0xFA ;D3B1 ; load input-number (0xD352)937 0x52 ;D3B2 ; into A938 0xD3 ;D3B3940 0xEA ;D3B4 ; load A into bytes-left-to-write941 0x83 ;D3B5 ; (0xD383)942 0xD3 ;D3B6944 0x3E ;D3B7 ; load 0x02 into A.945 0x02 ;D3B8947 0xEA ;D3B9 ; load A to current-mode948 0x82 ;D3BA ; advancing from Mode 1 to949 0xD3 ;D3BB ; Mode 2951 0xC3 ;D3BC ; go back to beginning952 0x1D ;D3BD953 0xD3 ;D3BE954 ;; End Mode 1957 ;; Mode 2 -- set start-point-high mode958 ;; Middle part of the header for writing things to memory.959 ;; User specifies the start location in RAM to which960 ;; data will be written.961 ;; Mode is auto advanced to Mode 3 after this mode completes.963 ;; Set start-point-high to input-number;964 ;; set current mode to 0x03.965 0xFA ;D3BF ; load input-number (0xD352)966 0x52 ;D3C0 ; into A967 0xD3 ;D3C1969 0xEA ;D3C2 ; load A into start-point-high970 0x85 ;D3C3 ; (0xD385)971 0xD3 ;D3C4973 0x3E ;D3C5 ; load 0x03 into A.974 0x03 ;D3C6976 0xEA ;D3C7 ; load A to current-mode,977 0x82 ;D3C8 ; advancing from Mode 2 to978 0xD3 ;D3C9 ; Mode 3.980 0xC3 ;D3CA ; go back to beginning981 0x1D ;D3CB982 0xD3 ;D3CC983 ;;End Mode 2986 ;; Mode 3 -- set-start-point-low mode987 ;; Final part of header for writing things to memory.988 ;; User specifies the low bytes of 16 bit start-point.990 ;; Set start-point-low to input-number;991 ;; set current mode to 0x04992 0xFA ;D3CD ; load input-number into A993 0x52 ;D3CE994 0xD3 ;D3CF996 0xEA ;D3D0 ; load A into start-point-low997 0x86 ;D3D1998 0xD3 ;D3D21000 0x3E ;D3D3 ; load 0x04 into A.1001 0x04 ;D3D41003 0xEA ;D3D5 ; load A to current-mode,1004 0x82 ;D3D6 ; advancing from Mode 3 to1005 0xD3 ;D3D7 ; Mode 4.1007 0xC3 ;D3D8 ; go back to beginning1008 0x1D ;D3D91009 0xD3 ;D3DA1011 ;; Mode 4 -- write bytes mode1013 ;; This is where RAM manipulation happens. User supplies1014 ;; bytes every frame, which are written sequentially to1015 ;; start-point until bytes-to-write have been written. Once1016 ;; bytes-to-write have been written, the mode is reset to 0.1018 ;; compare bytes-written with bytes-to-write.1019 ;; if they are the same, then reset mode to 01021 0xFA ;D3DB ; load bytes-to-write into A1022 0x83 ;D3DC1023 0xD3 ;D3DD1025 0x47 ;D3DE ; load A into B1027 0xFA ;D3DF ; load bytes-written into A1028 0x84 ;D3E01029 0xD3 ;D3E11031 0xB8 ;D3E2 ; compare A with B1033 0xCA ;D3E3 ; if they are equal, go to cleanup1034 0x07 ;D3E41035 0xD4 ;D3E51037 ;; Write Memory Section1038 ;; Write the input-number, interpreted as an 8-bit number,1039 ;; into the current target register, determined by1040 ;; (+ start-point bytes-written).1041 ;; Then, increment bytes-written by 1.1043 0xFA ;D3E6 ; load start-point-high into A1044 0x85 ;D3E71045 0xD3 ;D3E81047 0x67 ;D3E9 ; load A into H1049 0xFA ;D3EA ; load start-point-low into A1050 0x86 ;D3EB1051 0xD3 ;D3EC1053 0x6F ;D3ED ; load A into L1055 0xFA ;D3EE ; load bytes-written into A1056 0x84 ;D3EF1057 0xD3 ;D3F01059 0x00 ;D3F1 ; These are here because1060 0x00 ;D3F2 ; I screwed up again.1061 0x00 ;D3F31063 0x85 ;D3F4 ; add L to A; store A in L.1064 0x6F ;D3F51066 0x30 ;D3F6 ; If the addition overflowed,1067 0x01 ;D3F71068 0x24 ;D3F8 ; increment H.1070 ;; Now, HL points to the correct place in memory1072 0xFA ;D3F9 ; load input-number into A1073 0x52 ;D3FA1074 0xD3 ;D3FB1076 0x77 ;D3FC ; load A into (HL)1078 0xFA ;D3FD ; load bytes-written into A1079 0x84 ;D3FE1080 0xD3 ;D3FF1082 0x3C ;D400 ; increment A1084 0xEA ;D401 ; load A into bytes-written1085 0x84 ;D4021086 0xD3 ;D4031088 0xC3 ;D404 ; go back to beginning.1089 0x1D ;D4051090 0xD3 ;D4061091 ;; End Write Memory Section1093 ;; Mode 4 Cleanup Section1094 ;; reset bytes-written to 01095 ;; set mode to 01096 0x3E ;D407 ; load 0 into A1097 0x00 ;D4081099 0xEA ;D409 ; load A into bytes-written1100 0x84 ;D40A1101 0xD3 ;D40B1103 0xEA ;D40C ; load A into current-mode1104 0x82 ;D40D1105 0xD3 ;D40E1107 0xC3 ;D40F ; go back to beginning1108 0x1D ;D4101109 0xD3 ;D4111111 ;; End Mode 41113 ])1117 (def frame-count 0xD31F)1118 (def input 0xD352)1119 (def current-mode 0xD382)1120 (def bytes-to-write 0xD383)1121 (def bytes-written 0xD384)1122 (def start-point-high 0xD385)1123 (def start-point-low 0xD386)1127 (defn write-memory []1128 (-> (tick (mid-game))1129 (IE! 0) ; disable interrupts1130 (inject-item-assembly (write-memory-assembly))))1132 (defn test-write-memory []1133 (set-state! (write-memory))1134 (dorun1135 (dotimes [_ 5000]1136 (view-memory (step @current-state) current-mode))))1138 (def bytes-to-write 0xD383)1139 (def start-point 0xD384)1141 (defn print-blank-assembly1142 [start end]1143 (dorun1144 (map1145 #(println (format "0x00 ;%04X " %))1146 (range start end))))1148 (defn test-mode-2 []1149 (->1150 (write-memory)1151 (view-memory frame-count)1152 (step)1153 (step [:a])1154 (step [:b])1155 (step [:start])1156 (step [])1157 (view-memory frame-count)))1159 (defn test-mode-41160 ([] (test-mode-4 (write-memory)))1161 ([target-state]1162 (->1163 target-state1164 (#(do (println "memory from 0xC00F to 0xC01F:"1165 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))1166 (view-memory current-mode)1167 (step [])1168 (step [])1169 (step [])1170 (#(do (println "after three steps") %))1171 (view-memory current-mode)1173 ;; Activate memory writing mode1175 (#(do (println "step with [:a]") %))1176 (step [:a])1177 (view-memory current-mode)1178 (view-memory bytes-to-write)1179 (view-memory start-point-high)1180 (view-memory start-point-low)1182 ;; Specify four bytes to be written1184 (#(do (println "step with [:select]")%))1185 (step [:select])1186 (view-memory current-mode)1187 (view-memory bytes-to-write)1188 (view-memory start-point-high)1189 (view-memory start-point-low)1191 ;; Specify target memory address as 0xC00F1193 (#(do (println "step with [:u :d]")%))1194 (step [:u :d])1195 (view-memory current-mode)1196 (view-memory bytes-to-write)1197 (view-memory start-point-high)1198 (view-memory start-point-low)1200 (#(do (println "step with [:a :b :start :select]")%))1201 (step [:a :b :start :select])1202 (view-memory current-mode)1203 (view-memory bytes-to-write)1204 (view-memory start-point-high)1205 (view-memory start-point-low)1207 ;; Start reprogramming memory1209 (#(do (println "step with [:a]")%))1210 (step [:a])1211 (view-memory current-mode)1212 (view-memory bytes-written)1214 (#(do (println "step with [:b]")%))1215 (step [:b])1216 (view-memory current-mode)1217 (view-memory bytes-written)1219 (#(do (println "step with [:a :b]")%))1220 (step [:a :b])1221 (view-memory current-mode)1222 (view-memory bytes-written)1224 (#(do (println "step with [:select]")%))1225 (step [:select])1226 (view-memory current-mode)1227 (view-memory bytes-written)1229 ;; Reprogramming done, program ready for more commands.1231 (#(do (println "step with []")%))1232 (step [])1233 (view-memory current-mode)1234 (view-memory bytes-written)1236 (#(do (println "memory from 0xC00F to 0xC01F:"1237 (subvec (vec (memory %)) 0xC00F 0xC01F)) %)))))