Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 404:41647cb85901
got main-bootstrap-program down to 67 opcodes.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 13 Apr 2012 11:18:08 -0500 |
parents | ea37e98e188e |
children | bca0abd39db5 |
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 frame-metronome* []95 [0x3E ;; smallest version, but uses repeated nybbles96 0x0197 0xE098 0xFF])101 (defn frame-metronome []102 [0x06 ;; load 0xFE into B103 0xFE104 0x04 ;; inc B, now B == FF105 0x3E106 0x01 ;; 1->A108 0x48 ;; B->C109 0x02]) ;; A->(BC) set exclusive v-blank interrupt111 (defn test-frame-metronome112 "Ensure that frame-metronome ticks exactly once every frame."113 ([] (test-frame-metronome 151))114 ([steps]115 (let [inc-E [0x1C 0x76 0x18116 (->signed-8-bit -4)]118 program (concat (frame-metronome) inc-E)119 count-frames120 (-> (tick (mid-game))121 (IE! 0)122 (DE! 0)123 (set-memory-range pokemon-list-start program)124 (PC! pokemon-list-start))125 E-after-moves126 (E (run-moves count-frames (repeat steps [])))]127 (println "E:" E-after-moves)128 (assert (= steps E-after-moves))130 (println "E =" E-after-moves "after" steps "steps")131 count-frames)))133 (defn read-user-input []134 [0xAF 0x4F 0x47 ;; 0->A; 0->C; 0->B135 0xC5 ;; save value of BC137 0x3E138 0x20 ; prepare to measure d-pad140 0x01 ;\141 0x01 ; |142 0xFE ; | load 0xFF00 into BC without repeats143 0x04 ; |144 0x0D ;/146 0x02147 0x0A ;; get D-pad info149 0xF5 ;; push AF151 0x3E152 0x10 ; prepare to measure buttons154 0x3F ;; clear carry flag no-op to prevent repeated nybbbles156 0x02157 0x0A ;; get button info159 0xE6 ;; select bottom bits of A160 0x0F162 0x47 ;; A->B164 0xF1 ;; pop AF166 0xE6167 0x0F ;; select bottom bits of A169 0xCB170 0x37 ;; swap A nybbles172 0xB0 ;; (or A B) -> A174 0x2F ;; (NOT A) -> A175 ])177 (defn test-read-user-input []178 (let [program179 (concat180 (frame-metronome) (read-user-input)181 [0x5F ;; A-> E182 0x76183 0x18184 (->signed-8-bit185 (+ (- (count (read-user-input)))186 (- 4)))])187 read-input188 (-> (tick (mid-game))189 (IE! 0)190 (set-memory-range pokemon-list-start program)191 (PC! pokemon-list-start))]192 (dorun193 (for [i (range 0x100)]194 (assert (= (E (step read-input (buttons i))) i))))195 (println "Tested all inputs.")196 read-input))198 (def symbol-index199 (fn [symbol sequence]200 (count (take-while201 (partial not= symbol)202 sequence))))204 (defn main-bootstrap-program205 ([] (main-bootstrap-program pokemon-list-start))206 ([start-address]207 ;; Register Use:209 ;; ED non-volitale scratch211 ;; A user-input212 ;; HL target-address213 ;; B bytes-to-write214 ;; C non-volatile scratch216 ;; Modes (with codes) are:218 ;; single-action-modes:219 ;; SET-TARGET-HIGH 0x67 ;; A->H220 ;; SET-TARGET-LOW 0x6F ;; A->L221 ;; JUMP 0xE9 ;; jump to (HL)223 ;; multi-action-modes224 ;; WRITE 0x47 ;; A->B226 (let [header (concat (frame-metronome) (read-user-input))228 input229 [0xC1 ;; pop BC so it's not volatile231 0x5F ;; A->E232 0xAF ;; test for output-mode (bytes-to-write > 0)233 0xB8 ;; (cp A B)234 0x7B ;; E->A235 0x20 ;; skip to output section if236 :to-output ;; we're not in input mode238 :to-be-executed240 ;; write mode to instruction-to-be-executed (pun)241 0xEA242 :to-be-executed-address244 ;; protection region -- do not queue this op for245 ;; execution if the last one was non-zero246 0x79 ;; C->A247 0xA7 ;; test A==0248 0x28249 0x04250 0xAF ;; put a no op (0x00) in to-be-executed251 0xEA ;;252 :to-be-executed-address254 0x7B ;; E->A255 0x4F ;; A->C now C stores previous instruction256 0x18 ;; return257 :to-halt]259 output260 [:output-start ;; just a label261 0x54 ;;262 0x5D ;; HL->DE \263 ;; | This mess is here to do264 0x12 ;; A->(DE) | 0x22 (LDI (HL), A) without265 ;; | any repeating nybbles266 0x23 ;; inc HL /268 0x05 ;; DEC bytes-to-write (B)270 0x76 ;; HALT, peasant!271 0x18272 :to-beginning]274 symbols275 {:to-be-executed-address276 (reverse277 (disect-bytes-2278 (+ start-address279 (count header)280 (symbol-index :to-be-executed input))))281 :to-be-executed 0x00} ;; clear carry flag no-op283 program** (flatten284 (replace symbols (concat header input output)))286 resolve-internal-jumps287 {:output-start []288 :to-output289 (->signed-8-bit290 (dec291 (- (symbol-index :output-start program**)292 (symbol-index :to-output program**))))}294 program*295 (flatten (replace resolve-internal-jumps program**))297 resolve-external-jumps298 {:to-halt299 (- (- (symbol-index :to-beginning program*)300 (symbol-index :to-halt program*)) 3)302 :to-beginning303 (->signed-8-bit304 (+ 2 (count (frame-metronome))305 (- (symbol-index :to-beginning program*))))}307 program308 (replace resolve-external-jumps program*)]309 program)))312 ;;;;;; TESTS ;;;;;;314 (def set-H-mode 0x67)315 (def set-L-mode 0x6F)316 (def jump-mode 0xE9)317 (def write-mode 0x47)320 (defn bootstrap-base []321 (let [program (main-bootstrap-program pokemon-list-start)]322 ;; make sure program is valid output for item-writer323 ;;(bootstrap-pattern program)324 (-> (tick (mid-game))325 (set-memory-range pokemon-list-start program)326 (PC! pokemon-list-start)327 (step [])328 (step []))))330 (defn test-set-H []331 (letfn [(test-H [state n]332 (let [after333 (-> state334 (step (buttons set-H-mode))335 (step (buttons n))336 (step []))]337 ;;(println "desired H =" n "actual =" (H after))338 (assert (= n (H after)))339 after))]340 (let [result (reduce test-H (bootstrap-base) (range 0x100))]341 (println "tested all H values")342 result)))344 (defn test-write-bytes []345 (let [target-address 0xC00F346 [target-high target-low] (disect-bytes-2 target-address)347 assembly [0xF3 0x18 0xFE 0x12]348 get-mem-region #(subvec (vec (memory %))349 target-address (+ target-address 20))350 before (bootstrap-base)351 after352 (-> before353 (step []) ; make sure it can handle blanks354 (step []) ; at the beginning.355 (step [])356 (step (buttons set-H-mode)) ; select set-H357 (step (buttons target-high))358 (step [])359 (step (buttons set-L-mode))360 (step (buttons target-low))361 (step [])362 (step (buttons write-mode))363 (step (buttons 4)) ; write 4 bytes364 (step (buttons (nth assembly 0)))365 (step (buttons (nth assembly 1)))366 (step (buttons (nth assembly 2)))367 (step (buttons (nth assembly 3)))368 (step [])369 (step [])370 (step []))]371 (println "before :" (get-mem-region before))372 (println "after :" (get-mem-region after))373 (assert (= assembly (take 4 (get-mem-region after))))374 after))376 (defn test-jump []377 (let [target-address 0xC00F378 [target-high target-low] (disect-bytes-2 target-address)379 post-jump380 (-> (test-write-bytes)381 (step (buttons set-H-mode)) ; select set-H382 (step (buttons target-high))383 (step [])384 (step (buttons set-L-mode))385 (step (buttons target-low))386 (step [])387 (step (buttons jump-mode))) ; Select JUMP mode.388 program-counters389 (capture-program-counter390 post-jump391 10000)]392 (assert (contains? (set program-counters) target-address))393 (println "jump test passed")394 post-jump))397 (defn run-all-tests []398 (test-frame-metronome)399 (test-read-user-input)400 (test-set-H)401 (test-write-bytes)402 (test-jump))