Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 399:ddb3c6299619
allowed non-even initial blank inputs.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 13 Apr 2012 05:35:55 -0500 |
parents | 0591dcddf831 |
children | 1b9137ef7380 |
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 symbol-index180 (fn [symbol sequence]181 (count (take-while182 (partial not= symbol)183 sequence))))186 (defn main-bootstrap-program [start-address]187 ;; Register Use:189 ;; ED non-volitale scratch191 ;; A user-input192 ;; HL target-address193 ;; B bytes-to-write194 ;; C non-volatile scratch196 ;; Modes (with codes) are:198 ;; single-action-modes:199 ;; SET-TARGET-HIGH 0x67 ;; A->H200 ;; SET-TARGET-LOW 0x6F ;; A->L201 ;; JUMP 0xE9 ;; jump to (HL)203 ;; multi-action-modes204 ;; WRITE 0x47 ;; A->B206 (let [[start-high start-low] (disect-bytes-2 start-address)207 jump-distance (+ (count (frame-metronome))208 (count (read-user-input)))210 init211 [0xAF 0x4F 0x57 0x47] ;; 0->A; 0->C; 0->D; 0->B213 input214 [0xC1 ;; pop BC so it's not volatile216 0x5F ;; A->E217 0xAF ;; test for output-mode (bytes-to-write > 0)218 0x00 ;; (cp A B)219 0x7B ;; E->A220 0x20 ;; skip input section if221 :to-output ;; we're not in input mode223 :to-be-executed225 ;; write mode to instruction-to-be-executed (pun)226 0xEA227 :to-be-executed-address229 ;; if A is zero, skip the protection section230 0xA7231 0x28232 0x09234 0x0C ;; inc C235 0xCB236 0x41 ;; test bit 0 of C237 0x20 ;; if (0 == (C % 2))238 0x04239 0xAF ;; put a no op (0x00) in to-be-executed240 0xEA ;; every other time.241 :to-be-executed-address243 0x18 ;; return244 :to-beginning-1]246 ;; output247 ;; [:output-start ;; just a label248 ;; 0x54 ;;249 ;; 0x5D ;; HL->DE \250 ;; ;; |251 ;; 0x79 ;; C->A | this mess is all to do252 ;; 0x12 ;; A->(DE) | 0x22 (LDI (HL), A) without253 ;; ;; | any repeating nybbles254 ;; 0x23 ;; inc HL /257 ;; 0x05 ;; DEC bytes-to-write (B)258 ;; 0x20 ;; if there are no more bytes to write,259 ;; 0x04260 ;;262 ;; 0x18263 ;; :to-beginning-2]265 output266 [:output-start ;; just a label267 0x00 ;;268 0x00 ;; HL->DE \269 ;; |270 0x00 ;; C->A | this mess is all to do271 0x00 ;; A->(DE) | 0x22 (LDI (HL), A) without272 ;; | any repeating nybbles273 0x00 ;; inc HL /276 0x00 ;; DEC bytes-to-write (B)277 0x00 ;; if there are no more bytes to write,278 0x00279 0x00 ;; put a no op (0x00) in to-be-executed280 0x00281 0x00282 0x00284 0x00285 0x00]289 symbols290 {:to-be-executed-address291 (reverse292 (disect-bytes-2293 (+ start-address jump-distance294 (count init)295 (symbol-index :to-be-executed input))))296 :to-be-executed 0x00} ;; clear carry flag no-op298 program** (flatten299 (replace300 symbols301 (concat init (frame-metronome)302 (read-user-input)303 input output)))304 resolve-internal-jumps305 {:output-start []306 :to-output307 (->signed-8-bit308 (- (symbol-index :output-start program**)309 (symbol-index :to-output program**)))}311 program*312 (flatten (replace resolve-internal-jumps program**))314 resolve-external-jumps315 {:to-beginning-1316 (->signed-8-bit317 (+ (count init)318 -2 (- (dec (symbol-index :to-beginning-1 program*)))))319 :to-beginning-2320 (->signed-8-bit321 (+ (count init)322 -2 (- (dec (symbol-index :to-beginning-2 program*)))))}324 program325 (replace resolve-external-jumps program*)]326 program))329 ;;;;;; TESTS ;;;;;;331 (defn bootstrap-base []332 (let [program (main-bootstrap-program pokemon-list-start)]333 ;; make sure program is valid output for item-writer334 ;;(bootstrap-pattern program)335 (-> (tick (mid-game))336 (set-memory-range pokemon-list-start program)337 (PC! pokemon-list-start))))339 (defn test-set-H [n]340 (let [after341 (-> (bootstrap-base)342 (step [])343 (step (buttons 0x67))344 (step (buttons n))345 (step [])346 (step [])347 )]348 (hex (H after))))350 (defn test-write-bytes-mode []351 (let [target-address 0xC00F352 [target-high target-low] (disect-bytes-2 target-address)353 assembly [0xF3 0x18 0xFE 0x12]354 get-mem-region #(subvec (vec (memory %))355 target-address (+ target-address 20))356 before (bootstrap-base)357 after358 (-> before359 (step []) ; make sure it can handle blanks360 (step []) ; at the beginning.361 (step [])362 (step [:start]) ; select WRITE-BYTES mode363 (step (buttons 4)) ; write 4 bytes364 (step (buttons target-high))365 (step (buttons target-low))366 (step (buttons (nth assembly 0)))367 (step (buttons (nth assembly 1)))368 (step (buttons (nth assembly 2)))369 (step (buttons (nth assembly 3)))370 (step [])371 (step [])372 (step []))]373 (println "before :" (get-mem-region before))374 (println "after :" (get-mem-region after))375 (assert (= assembly (take 4 (get-mem-region after))))376 after))378 (defn test-jump-mode []379 (let [target-address 0xC00F380 [target-high target-low] (disect-bytes-2 target-address)381 post-jump382 (-> (test-write-bytes-mode)383 (step [])384 (step [])385 (step [])386 (step (buttons 0xFF)) ; Select JUMP mode.387 (step (buttons target-high))388 (step (buttons target-low)))389 program-counters390 (capture-program-counter391 post-jump392 10000)]393 (println program-counters)394 (assert (contains? (set program-counters) target-address))395 post-jump))