Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 386:d8cbbf2a3133
changed scratch ragisters from BC to DE.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 12 Apr 2012 06:27:03 -0500 |
parents | 3f3cfc89be91 |
children | 47d44bb54d32 |
line wrap: on
line source
1 (ns com.aurellem.gb.rlm-assembly2 "Version of main bootstrap program that is valid output for the3 item-writer program."4 (:use (com.aurellem.gb gb-driver assembly util vbm constants))5 (:use (com.aurellem.run bootstrap-1))6 (:import [com.aurellem.gb.gb_driver SaveState]))8 ;; Specs for Main Bootstrap Program10 ;; Number-Input11 ;; Number input works using all eight buttons to12 ;; spell out an 8 bit number. The order of buttons is13 ;; [:d :u :l :r :start :select :b :a] --> 1111111114 ;; [ :l :start :a] --> 0010100116 ;;; MODE-SELECT17 ;; The bootstrap program starts in MODE-SELECT mode.18 ;; MODE-SELECT transitions to one of three modes depending19 ;; on which buttons are pressed:20 ;; 0 (no-buttons) : MODE-SELECT21 ;; 8 [:start] : WRITE-BYTES22 ;; 0xFF (all-buttons) : JUMP24 ;;; WRITE-BYTES26 ;; WRITE-BYTES mode writes sequences of arbitray values to27 ;; arbitray memory locations. It expects you to enter a28 ;; header of three bytes describing what to write:30 ;; Byte 0 : Number of Bytes to Write31 ;; Byte 1 : Start Address High Byte32 ;; Byte 1 : Start Address Low Byte34 ;; Then, you enter the number of bytes specified in Byte 035 ;; they are written to the start address in36 ;; sequence. After the last byte is written control37 ;; returns to MODE-SELECT mode.39 ;; Example: to write the sequence [1 2 3 4] starting at40 ;; address 0xC01F enter41 ;; Byte 0 : 4 (will write four bytes)42 ;; Byte 1 : 0xC0 (high byte of 0xC01F)43 ;; Byte 2 : 0x1F (low byte of 0xC01F)44 ;; Byte 3 : 1 (write 1 to 0xC01F)45 ;; Byte 4 : 2 (write 2 to 0xC020)46 ;; Byte 5 : 3 (write 3 to 0xC021)47 ;; Byte 6 : 4 (write 4 to 0xC022)49 ;;; JUMP50 ;; JUMP mode jumps program control to any arbitray51 ;; location. It expects you to enter two bytes which52 ;; correspond to the high and low bytes of the memory53 ;; address to which you want to jump.54 ;; Byte 0 : Jump Address High Byte55 ;; Byte 1 : Jump Address Low Byte57 ;; Example: to jump to address 0x1234 enter58 ;; Byte 0 : 0x12 (high byte of 0x1234)59 ;; Byte 1 : 0x34 (low byte of 0x1234)62 (defn ->signed-8-bit [n]63 (if (< n 0)64 (+ 256 n) n))66 (defn frame-metronome []67 (let [timing-loop68 [;;0x01 ; \69 ;;0x43 ; |70 ;;0xFE ; | load 0xFF44 into BC without repeats71 ;;0x0C ; |72 ;;0x04 ; /73 ;;0x0A ;; (BC) -> A, now A = LY (vertical line coord)75 0x11 ; \76 0x43 ; |77 0xFE ; | load 0xFF44 into DE without repeats78 0x1C ; |79 0x14 ; /80 0x1A ;; (DE) -> A, now A = LY (vertical line coord)81 ]82 continue-if-14483 [0xFE84 144 ;; compare LY (in A) with 14485 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)86 (->signed-8-bit87 (+ -4 (- (count timing-loop))))]88 spin-loop89 [;;0x05 ;; dec B, which is 0xFF90 0x15 ;; dec D, which is 0xFF91 0x20 ;; spin until B==092 0xFD]]93 (concat timing-loop continue-if-144 spin-loop)))95 (defn test-frame-metronome96 "Ensure that frame-metronome ticks exactly once every frame."97 ([] (test-frame-metronome 151))98 ([steps]99 (let [inc-B [0x04 0x18100 (->signed-8-bit101 (+ -3 (- (count (frame-metronome)))))]102 program (concat (frame-metronome) inc-B)103 count-frames104 (-> (tick (mid-game))105 (IE! 0)106 (BC! 0)107 (set-memory-range pokemon-list-start program)108 (PC! pokemon-list-start))109 B-after-moves (B (run-moves count-frames (repeat steps [])))]110 (println "B:" B-after-moves)111 (assert (= steps B-after-moves))113 (println "B =" B-after-moves "after" steps "steps")114 count-frames)))116 (defn read-user-input []117 [;;0x01 ;\118 ;;0x01 ;|119 ;;0xFE ;| load 0xFF00 into BC without repeats120 ;;0x04 ;|121 ;;0x0D ;/123 0x11 ; \124 0x01 ; |125 0xFE ; | load 0xFF44 into DE without repeats126 0x14 ; |127 0x1D ; /129 0x3E130 (Integer/parseInt "00100000" 2) ; prepare to measure d-pad132 0x12133 0x1A ;; get D-pad info135 0xF5 ;; push AF137 0x3E138 (Integer/parseInt "00010000" 2) ; prepare to measure buttons140 0x12141 0x1A ;; get button info144 0xE6 ;; select bottom bits of A145 0x0F147 0x47 ;; A->B149 0xF1 ;; pop AF151 0xE6152 0x0F ;; select bottom bits of A154 0xCB155 0x37 ;; swap A nybbles157 0xB0 ;; (AND A B) -> A159 0x2F ;; (NOT A) -> A160 ])162 (defn test-read-user-input []163 (let [program164 (concat165 (frame-metronome) (read-user-input)166 [0x47 ;; A->B167 0x18168 (->signed-8-bit169 (+ (- (count (frame-metronome)))170 (- (count (read-user-input)))171 (- 3)))])172 read-input173 (-> (tick (mid-game))174 (IE! 0)175 (set-memory-range pokemon-list-start program)176 (PC! pokemon-list-start))]177 (dorun178 (for [i (range 0x100)]179 (assert (= (B (step read-input (buttons i))) i))))180 (println "Tested all inputs.")181 read-input))183 (def mode-select-mode (Integer/parseInt "00000000" 2))184 (def write-mode (Integer/parseInt "00000001" 2))185 (def jump-mode (Integer/parseInt "10000000" 2))187 (def input-high-addr-jump (Integer/parseInt "10000010" 2))188 (def input-high-addr-write (Integer/parseInt "00000010" 2))189 (def input-low-addr-jump (Integer/parseInt "10000100" 2))190 (def input-low-addr-write (Integer/parseInt "00000100" 2))192 (def input-write-num-mode (Integer/parseInt "00001000" 2))193 (def do-write-mode (Integer/parseInt "00010000" 2))201 (defn main-bootstrap-program [start-address]202 (let [[start-high start-low] (disect-bytes-2 start-address)203 jump-distance (+ (count (frame-metronome)204 (read-user-input)))206 init207 [0x1E 0xFF 0x1C] ;; 0-> E without repeats210 ;; HL -> BC211 ;; HL = here212 ;; add E to HL213 ;; jp HL215 mode-dispatch216 [0x44220 ;;(here) jr metronome222 ;;stuff223 ;;modify E224 ;;jr metronome226 ;;stuff227 ;;jr metronome235 mode-dispatch236 [0xCB237 0x43 ;; test bit 0239 0xCB240 0x4B ;; test bit 1242 0xCB243 0x53 ;; test bit 2245 0xCB246 0x5B ;; test bit 3248 0xCB249 0x63 ;; test bit 4251 0xCB252 0x6B ;; test bit 5254 ];;default257 mode-select258 [0x5F] ;; A->E260 ;; delayed inputs261 input-high 0x67 ;; A->H262 input-low 0x6F ;; A->L263 input-num 0x57 ;; A->D265 ;; final-actions266 jump 0xE9 ;; jp (HL)267 write 0x22 ;; A->(HL); inc HL268 input-mode 0x00 ;; no-op272 input-high273 [0x67 ;; A->H274 0x1E ;; change mode275 input-low-mode]280 mode-dispatch288 ))299 ;;;;;; TESTS ;;;;;;301 (defn bootstrap-base []302 (let [program (main-bootstrap-program pokemon-list-start)]303 ;; make sure program is valid output for item-writer304 (bootstrap-pattern program)305 (-> (tick (mid-game))306 (set-memory-range pokemon-list-start program)307 (PC! pokemon-list-start))))309 (defn test-write-bytes-mode []310 (let [target-address 0xC00F311 [target-high target-low] (disect-bytes-2 target-address)312 assembly [0xF3 0x18 0xFE 0x12]313 get-mem-region #(subvec (vec (memory %))314 target-address (+ target-address 20))315 before (bootstrap-base)316 after317 (-> before318 (step []) ; make sure it can handle blanks319 (step []) ; at the beginning.320 (step [])321 (step [:start]) ; select WRITE-BYTES mode322 (step (buttons 4)) ; write 4 bytes323 (step (buttons target-high))324 (step (buttons target-low))325 (step (buttons (nth assembly 0)))326 (step (buttons (nth assembly 1)))327 (step (buttons (nth assembly 2)))328 (step (buttons (nth assembly 3)))329 (step [])330 (step [])331 (step []))]332 (println "before :" (get-mem-region before))333 (println "after :" (get-mem-region after))334 (assert (= assembly (take 4 (get-mem-region after))))335 after))337 (defn test-jump-mode []338 (let [target-address 0xC00F339 [target-high target-low] (disect-bytes-2 target-address)340 post-jump341 (-> (test-write-bytes-mode)342 (step [])343 (step [])344 (step [])345 (step (buttons 0xFF)) ; Select JUMP mode.346 (step (buttons target-high))347 (step (buttons target-low)))348 program-counters349 (capture-program-counter350 post-jump351 10000)]352 (println program-counters)353 (assert (contains? (set program-counters) target-address))354 post-jump))