Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 387:47d44bb54d32
saving progress...
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 12 Apr 2012 09:21:40 -0500 |
parents | d8cbbf2a3133 |
children | a0d0e1a46b1d |
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 0x57 ;; A->D149 0xF1 ;; pop AF151 0xE6152 0x0F ;; select bottom bits of A154 0xCB155 0x37 ;; swap A nybbles157 0x5A ;; D->E \ necessary to158 0xB3 ;; (or A D) -> A / prevent repeats.160 0x2F ;; (NOT A) -> A161 ])163 (defn test-read-user-input []164 (let [program165 (concat166 (frame-metronome) (read-user-input)167 [0x47 ;; A->B168 0x18169 (->signed-8-bit170 (+ (- (count (frame-metronome)))171 (- (count (read-user-input)))172 (- 3)))])173 read-input174 (-> (tick (mid-game))175 (IE! 0)176 (set-memory-range pokemon-list-start program)177 (PC! pokemon-list-start))]178 (dorun179 (for [i (range 0x100)]180 (assert (= (B (step read-input (buttons i))) i))))181 (println "Tested all inputs.")182 read-input))184 (def mode-select-mode (Integer/parseInt "00000000" 2))185 (def write-mode (Integer/parseInt "00000001" 2))186 (def jump-mode (Integer/parseInt "10000000" 2))188 (def input-high-addr-jump (Integer/parseInt "10000010" 2))189 (def input-high-addr-write (Integer/parseInt "00000010" 2))190 (def input-low-addr-jump (Integer/parseInt "10000100" 2))191 (def input-low-addr-write (Integer/parseInt "00000100" 2))193 (def input-write-num-mode (Integer/parseInt "00001000" 2))194 (def do-write-mode (Integer/parseInt "00010000" 2))202 (defn main-bootstrap-program [start-address]203 (let [[start-high start-low] (disect-bytes-2 start-address)204 jump-distance (+ (count (frame-metronome)205 (read-user-input)))207 init208 [0xAF 0x4F] ;; 0->A; 0->C;210 ;; HL = here211 ;; add C to HL212 ;; jp HL214 prepare-HL215 [0x21] ;; load HL from literal nn216 ;; nn == here defined below218 mode-dispatch219 [0x06 ;\220 0x01 ; | 0->B without repeats221 0x05 ;/222 0x09 ;; add BC to HL223 0xE9] ;; jp225 here226 (disect-bytes-2 (+ 2 start-address (count init)227 jump-distance prepare-HL228 (count mode-dispatch)))232 ;;(here) jr end234 ;;stuff235 ;;modify E236 ;;jr metronome238 ;;stuff239 ;;jr metronome241 input-number243 [0x47 ;; A->B244 0x1E ;;245 input-high-write-jump246 0x18247 (jump-distance ??)]251 input-high-write252 [0x260 (concat init (frame-metronome) (read-user-input)261 prepare-HL here mode-dispatch)270 ))281 ;;;;;; TESTS ;;;;;;283 (defn bootstrap-base []284 (let [program (main-bootstrap-program pokemon-list-start)]285 ;; make sure program is valid output for item-writer286 (bootstrap-pattern program)287 (-> (tick (mid-game))288 (set-memory-range pokemon-list-start program)289 (PC! pokemon-list-start))))291 (defn test-write-bytes-mode []292 (let [target-address 0xC00F293 [target-high target-low] (disect-bytes-2 target-address)294 assembly [0xF3 0x18 0xFE 0x12]295 get-mem-region #(subvec (vec (memory %))296 target-address (+ target-address 20))297 before (bootstrap-base)298 after299 (-> before300 (step []) ; make sure it can handle blanks301 (step []) ; at the beginning.302 (step [])303 (step [:start]) ; select WRITE-BYTES mode304 (step (buttons 4)) ; write 4 bytes305 (step (buttons target-high))306 (step (buttons target-low))307 (step (buttons (nth assembly 0)))308 (step (buttons (nth assembly 1)))309 (step (buttons (nth assembly 2)))310 (step (buttons (nth assembly 3)))311 (step [])312 (step [])313 (step []))]314 (println "before :" (get-mem-region before))315 (println "after :" (get-mem-region after))316 (assert (= assembly (take 4 (get-mem-region after))))317 after))319 (defn test-jump-mode []320 (let [target-address 0xC00F321 [target-high target-low] (disect-bytes-2 target-address)322 post-jump323 (-> (test-write-bytes-mode)324 (step [])325 (step [])326 (step [])327 (step (buttons 0xFF)) ; Select JUMP mode.328 (step (buttons target-high))329 (step (buttons target-low)))330 program-counters331 (capture-program-counter332 post-jump333 10000)]334 (println program-counters)335 (assert (contains? (set program-counters) target-address))336 post-jump))