Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 390:dbf7b5a2e9e7
saving progress.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 11 Apr 2012 23:40:01 -0500 |
parents | bb8978d370d8 |
children | 2e9b2d27f32f |
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]))9 ;; MODE-SELECT10 ;; SET-LENGTH11 ;; SET-TARGET12 ;; WRITE13 ;; JUMP15 ;; Specs for Main Bootstrap Program17 ;; Number-Input18 ;; Number input works using all eight buttons to19 ;; spell out an 8 bit number. The order of buttons is20 ;; [:d :u :l :r :start :select :b :a] --> 1111111121 ;; [ :l :start :a] --> 0010100123 ;;; MODE-SELECT24 ;; The bootstrap program starts in MODE-SELECT mode.25 ;; MODE-SELECT transitions to one of three modes depending26 ;; on which buttons are pressed:27 ;; 0 (no-buttons) : MODE-SELECT28 ;; 8 [:start] : WRITE-BYTES29 ;; 0xFF (all-buttons) : JUMP31 ;;; WRITE-BYTES33 ;; WRITE-BYTES mode writes sequences of arbitray values to34 ;; arbitray memory locations. It expects you to enter a35 ;; header of three bytes describing what to write:37 ;; Byte 0 : Number of Bytes to Write38 ;; Byte 1 : Start Address High Byte39 ;; Byte 1 : Start Address Low Byte41 ;; Then, you enter the number of bytes specified in Byte 042 ;; they are written to the start address in43 ;; sequence. After the last byte is written control44 ;; returns to MODE-SELECT mode.46 ;; Example: to write the sequence [1 2 3 4] starting at47 ;; address 0xC01F enter48 ;; Byte 0 : 4 (will write four bytes)49 ;; Byte 1 : 0xC0 (high byte of 0xC01F)50 ;; Byte 2 : 0x1F (low byte of 0xC01F)51 ;; Byte 3 : 1 (write 1 to 0xC01F)52 ;; Byte 4 : 2 (write 2 to 0xC020)53 ;; Byte 5 : 3 (write 3 to 0xC021)54 ;; Byte 6 : 4 (write 4 to 0xC022)56 ;;; JUMP57 ;; JUMP mode jumps program control to any arbitray58 ;; location. It expects you to enter two bytes which59 ;; correspond to the high and low bytes of the memory60 ;; address to which you want to jump.61 ;; Byte 0 : Jump Address High Byte62 ;; Byte 1 : Jump Address Low Byte64 ;; Example: to jump to address 0x1234 enter65 ;; Byte 0 : 0x12 (high byte of 0x1234)66 ;; Byte 1 : 0x34 (low byte of 0x1234)69 (defn ->signed-8-bit [n]70 (if (< n 0)71 (+ 256 n) n))73 (defn frame-metronome []74 (let [init [0xC5] ;; save value of BC75 timing-loop76 [0x01 ; \77 0x43 ; |78 0xFE ; | load 0xFF44 into BC without repeats79 0x0C ; |80 0x04 ; /81 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)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 0x20 ;; spin until B==091 0xFD]]92 (concat init timing-loop continue-if-144 spin-loop)))94 (defn test-frame-metronome95 "Ensure that frame-metronome ticks exactly once every frame."96 ([] (test-frame-metronome 151))97 ([steps]98 (let [inc-E [0x1C 0x1899 (->signed-8-bit100 (+ -3 (- (count (frame-metronome)))))]101 program (concat (frame-metronome) inc-E)102 count-frames103 (-> (tick (mid-game))104 (IE! 0)105 (DE! 0)106 (set-memory-range pokemon-list-start program)107 (PC! pokemon-list-start))108 E-after-moves109 (E (run-moves count-frames (repeat steps [])))]110 (println "E:" E-after-moves)111 (assert (= steps E-after-moves))113 (println "E =" E-after-moves "after" steps "steps")114 count-frames)))116 (defn read-user-input []117 [0x3E118 0x20 ; prepare to measure d-pad120 0x01 ;\121 0x01 ; |122 0xFE ; | load 0xFF00 into BC without repeats123 0x04 ; |124 0x0D ;/126 0x02127 0x0A ;; get D-pad info129 0xF5 ;; push AF131 0x3E132 0x10 ; prepare to measure buttons134 0x3F ;; clear carry flag no-op to prevent repeated nybbbles136 0x02137 0x0A ;; get button info139 0xE6 ;; select bottom bits of A140 0x0F142 0x47 ;; A->B144 0xF1 ;; pop AF146 0xE6147 0x0F ;; select bottom bits of A149 0xCB150 0x37 ;; swap A nybbles152 0xB0 ;; (or A B) -> A154 0x2F ;; (NOT A) -> A156 ])158 (defn test-read-user-input []159 (let [program160 (concat161 (frame-metronome) (read-user-input)162 [0x5F ;; A-> E163 0x18164 (->signed-8-bit165 (+ (- (count (frame-metronome)))166 (- (count (read-user-input)))167 (- 3)))])168 read-input169 (-> (tick (mid-game))170 (IE! 0)171 (set-memory-range pokemon-list-start program)172 (PC! pokemon-list-start))]173 (dorun174 (for [i (range 0x100)]175 (assert (= (E (step read-input (buttons i))) i))))176 (println "Tested all inputs.")177 read-input))179 (def mode-select-mode (Integer/parseInt "00000000" 2))180 (def write-mode (Integer/parseInt "00000001" 2))181 (def jump-mode (Integer/parseInt "10000000" 2))183 (def input-high-addr-jump (Integer/parseInt "10000010" 2))184 (def input-high-addr-write (Integer/parseInt "00000010" 2))185 (def input-low-addr-jump (Integer/parseInt "10000100" 2))186 (def input-low-addr-write (Integer/parseInt "00000100" 2))188 (def input-write-num-mode (Integer/parseInt "00001000" 2))189 (def do-write-mode (Integer/parseInt "00010000" 2))191 (defn main-bootstrap-program [start-address]192 ;; Register Use:194 ;; BC persistent scratch195 ;;196 ;; A user-input197 ;; HL target-address198 ;; D current-mode199 ;; E bytes-to-write202 ;; Modes are:204 ;; single-action-modes:205 ;; SET-MODE206 ;; SET-TARGET-HIGH207 ;; SET-TARGET-LOW208 ;; SET-WRITE-LENGTH209 ;; JUMP211 ;; multi-action-modes212 ;; WRITE216 (let [[start-high start-low] (disect-bytes-2 start-address)217 jump-distance (+ (count (frame-metronome))218 (read-user-input))220 init221 [0xAF 0x5F 0x57 0x47] ;; 0->A; 0->E; 0->D; 0->B226 input227 [0xC1 ;; pop BC so it's not volatile228 ;; some condition229 ;; to skip if not in input230 ;; mode235 0xE9 ;; jump to (HL)237 0x22 ;; A->(HL) ; inc HL240 0x57 ;; A->D242 0x67 ;; A->H243 0x6F ;; A->L245 0x47 ;; A->B246 0x4F ;; A->C255 ;; HL = here256 ;; add C to HL257 ;; jp HL259 ]]))267 (comment269 ;;;;;; TESTS ;;;;;;271 (defn bootstrap-base []272 (let [program (main-bootstrap-program pokemon-list-start)]273 ;; make sure program is valid output for item-writer274 (bootstrap-pattern program)275 (-> (tick (mid-game))276 (set-memory-range pokemon-list-start program)277 (PC! pokemon-list-start))))279 (defn test-write-bytes-mode []280 (let [target-address 0xC00F281 [target-high target-low] (disect-bytes-2 target-address)282 assembly [0xF3 0x18 0xFE 0x12]283 get-mem-region #(subvec (vec (memory %))284 target-address (+ target-address 20))285 before (bootstrap-base)286 after287 (-> before288 (step []) ; make sure it can handle blanks289 (step []) ; at the beginning.290 (step [])291 (step [:start]) ; select WRITE-BYTES mode292 (step (buttons 4)) ; write 4 bytes293 (step (buttons target-high))294 (step (buttons target-low))295 (step (buttons (nth assembly 0)))296 (step (buttons (nth assembly 1)))297 (step (buttons (nth assembly 2)))298 (step (buttons (nth assembly 3)))299 (step [])300 (step [])301 (step []))]302 (println "before :" (get-mem-region before))303 (println "after :" (get-mem-region after))304 (assert (= assembly (take 4 (get-mem-region after))))305 after))307 (defn test-jump-mode []308 (let [target-address 0xC00F309 [target-high target-low] (disect-bytes-2 target-address)310 post-jump311 (-> (test-write-bytes-mode)312 (step [])313 (step [])314 (step [])315 (step (buttons 0xFF)) ; Select JUMP mode.316 (step (buttons target-high))317 (step (buttons target-low)))318 program-counters319 (capture-program-counter320 post-jump321 10000)]322 (println program-counters)323 (assert (contains? (set program-counters) target-address))324 post-jump))325 )