Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 384:8013915a07b3
completed read-user-input by working off of Dylan's version.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 11 Apr 2012 14:04:21 -0500 |
parents | 9eae7e914bf0 |
children | 3f3cfc89be91 |
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)74 continue-if-14475 [0xFE76 144 ;; compare LY (in A) with 14477 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)78 (->signed-8-bit79 (+ -4 (- (count timing-loop))))]80 spin-loop81 [0x05 ;; dec B, which is 0xFF82 0x20 ;; spin until B==083 0xFD]]84 (concat timing-loop continue-if-144 spin-loop)))86 (defn test-frame-metronome87 "Ensure that frame-metronome ticks exactly once every frame."88 ([] (test-frame-metronome 151))89 ([steps]90 (let [inc-D [0x14 0x1891 (->signed-8-bit92 (+ -3 (- (count (frame-metronome)))))]93 program (concat (frame-metronome) inc-D)94 count-frames95 (-> (tick (mid-game))96 (IE! 0)97 (DE! 0)98 (set-memory-range pokemon-list-start program)99 (PC! pokemon-list-start))100 D-after-moves (D (run-moves count-frames (repeat steps [])))]101 (println "D:" D-after-moves)102 (assert (= steps D-after-moves))104 (println "D =" D-after-moves "after" steps "steps")105 count-frames)))107 (defn read-user-input []108 [0x01 ;\109 0x01 ;|110 0xFE ;| load 0xFF00 into BC without repeats111 0x04 ;|112 0x0D ;/114 0x3E115 (Integer/parseInt "00100000" 2) ; prepare to measure d-pad117 0x02118 0x0A ;; get D-pad info120 0xE6 ;; select bottom bits of A121 0x0F123 0xCB124 0x37 ;; swap A nybbles126 0x57 ;; A->D128 0x3E129 (Integer/parseInt "00010000" 2) ; prepare to measure buttons131 0x02132 0x0A ;; get button info134 0xE6135 0x0F ;; select bottom bits of A137 0xB2 ;; combine together into A138 0x2F ;; (NOT A) -> A139 ])141 (defn test-read-user-input []142 (let [program143 (concat144 (frame-metronome) (read-user-input)145 [0x5F ;; A-> E146 0x18147 (->signed-8-bit148 (+ (- (count (frame-metronome)))149 (- (count (read-user-input)))150 (- 3)))])151 read-input152 (-> (tick (mid-game))153 (IE! 0)154 (set-memory-range pokemon-list-start program)155 (PC! pokemon-list-start))]156 (dorun157 (for [i (range 0x100)]158 (assert (= (E (step read-input (buttons i))) i))))159 (println "Tested all inputs.")160 read-input))164 (defn main-bootstrap-program [start-address]165 (let [[start-high start-low] (disect-bytes-2 start-address)166 ]167 ))178 ;;;;;; TESTS ;;;;;;180 (defn bootstrap-base []181 (let [program (main-bootstrap-program pokemon-list-start)]182 ;; make sure program is valid output for item-writer183 (bootstrap-pattern program)184 (-> (tick (mid-game))185 (set-memory-range pokemon-list-start program)186 (PC! pokemon-list-start))))188 (defn test-write-bytes-mode []189 (let [target-address 0xC00F190 [target-high target-low] (disect-bytes-2 target-address)191 assembly [0xF3 0x18 0xFE 0x12]192 get-mem-region #(subvec (vec (memory %))193 target-address (+ target-address 20))194 before (bootstrap-base)195 after196 (-> before197 (step []) ; make sure it can handle blanks198 (step []) ; at the beginning.199 (step [])200 (step [:start]) ; select WRITE-BYTES mode201 (step (buttons 4)) ; write 4 bytes202 (step (buttons target-high))203 (step (buttons target-low))204 (step (buttons (nth assembly 0)))205 (step (buttons (nth assembly 1)))206 (step (buttons (nth assembly 2)))207 (step (buttons (nth assembly 3)))208 (step [])209 (step [])210 (step []))]211 (println "before :" (get-mem-region before))212 (println "after :" (get-mem-region after))213 (assert (= assembly (take 4 (get-mem-region after))))214 after))216 (defn test-jump-mode []217 (let [target-address 0xC00F218 [target-high target-low] (disect-bytes-2 target-address)219 post-jump220 (-> (test-write-bytes-mode)221 (step [])222 (step [])223 (step [])224 (step (buttons 0xFF)) ; Select JUMP mode.225 (step (buttons target-high))226 (step (buttons target-low)))227 program-counters228 (capture-program-counter229 post-jump230 10000)]231 (println program-counters)232 (assert (contains? (set program-counters) target-address))233 post-jump))