Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 412:543a78679971
saving progress...
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Fri, 13 Apr 2012 09:07:09 -0500 |
parents | bca0abd39db5 |
children | 55a45f67e4a4 |
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))129 (println "frame-count test passed.")130 count-frames)))132 (defn read-user-input []133 [0xAF 0x4F 0x47 ;; 0->A; 0->C; 0->B134 0xC5 ;; save value of BC136 0x3E137 0x20 ; prepare to measure d-pad139 0x3F ; clear carry flag no-op to prevent repeated nybbles141 0x01 ;\142 0x01 ; |143 0xFE ; | load 0xFF00 into BC without repeats144 0x04 ; |145 0x0D ;/147 0x02148 0x0A ;; get D-pad info150 0xF5 ;; push AF152 0x3E153 0x10 ; prepare to measure buttons155 0x3F ;; clear carry flag no-op to prevent repeated nybbbles157 0x02158 0x0A ;; get button info160 0xE6 ;; select bottom bits of A161 0x0F163 0x47 ;; A->B165 0xF1 ;; pop AF167 0xE6168 0x0F ;; select bottom bits of A170 0xCB171 0x37 ;; swap A nybbles173 0xB0 ;; (or A B) -> A175 0x2F ;; (NOT A) -> A176 ])178 (defn test-read-user-input []179 (let [program180 (concat181 (frame-metronome) (read-user-input)182 [0x5F ;; A-> E183 0x76184 0x18185 (->signed-8-bit186 (+ (- (count (read-user-input)))187 (- 4)))])188 read-input189 (-> (tick (mid-game))190 (IE! 0)191 (set-memory-range pokemon-list-start program)192 (PC! pokemon-list-start))]193 (dorun194 (for [i (range 0x100)]195 (assert (= (E (step read-input (buttons i))) i))))196 (println "tested all inputs.")197 read-input))199 (def symbol-index200 (fn [symbol sequence]201 (count (take-while202 (partial not= symbol)203 sequence))))205 (defn main-bootstrap-program206 ([] (main-bootstrap-program pokemon-list-start))207 ([start-address]208 ;; Register Use:210 ;; ED non-volitale scratch212 ;; A user-input213 ;; HL target-address214 ;; B bytes-to-write215 ;; C non-volatile scratch217 ;; Modes (with codes) are:219 ;; single-action-modes:220 ;; SET-TARGET-HIGH 0x67 ;; A->H221 ;; SET-TARGET-LOW 0x6F ;; A->L222 ;; JUMP 0xE9 ;; jump to (HL)224 ;; multi-action-modes225 ;; WRITE 0x47 ;; A->B227 (let [header (concat (frame-metronome) (read-user-input))229 input230 [0xC1 ;; pop BC so it's not volatile232 0x5F ;; A->E233 0xAF ;; test for output-mode (bytes-to-write > 0)234 0xB8 ;; (cp A B)235 0x7B ;; E->A236 0x20 ;; skip to output section if237 :to-output ;; we're not in input mode239 :to-be-executed241 ;; write mode to instruction-to-be-executed (pun)242 0xEA243 :to-be-executed-address245 ;; protection region -- do not queue this op for246 ;; execution if the last one was non-zero247 0x79 ;; C->A248 0xA7 ;; test A==0249 0x28250 0x04251 0xAF ;; put a no op (0x00) in to-be-executed252 0xEA ;;253 :to-be-executed-address255 0x7B ;; E->A256 0x4F ;; A->C now C stores previous instruction257 0x18 ;; return258 :to-halt]260 output261 [:output-start ;; just a label262 0x3F ;; ;; prevent repeated nybbles263 0x54 ;;264 0x5D ;; HL->DE \265 ;; | This mess is here to do266 0x12 ;; A->(DE) | 0x22 (LDI (HL), A) without267 ;; / any repeating nybbles268 0x05 ;; DEC bytes-to-write (B)270 0x23 ;; inc HL272 0x76 ;; HALT, peasant!273 0x18274 :to-beginning]276 symbols277 {:to-be-executed-address278 (reverse279 (disect-bytes-2280 (+ start-address281 (count header)282 (symbol-index :to-be-executed input))))283 :to-be-executed 0x3F} ;; clear carry flag no-op285 program** (flatten286 (replace symbols (concat header input output)))288 resolve-internal-jumps289 {:output-start []290 :to-output291 (->signed-8-bit292 (dec293 (- (symbol-index :output-start program**)294 (symbol-index :to-output program**))))}296 program*297 (flatten (replace resolve-internal-jumps program**))299 resolve-external-jumps300 {:to-halt301 (- (- (symbol-index :to-beginning program*)302 (symbol-index :to-halt program*)) 3)304 :to-beginning305 (->signed-8-bit306 (+ 2 (count (frame-metronome))307 (- (symbol-index :to-beginning program*))))}309 program310 (replace resolve-external-jumps program*)]311 program)))314 ;;;;;; TESTS ;;;;;;316 (def set-H-mode 0x67)317 (def set-L-mode 0x6F)318 (def jump-mode 0xE9)319 (def write-mode 0x47)322 (defn bootstrap-base []323 (let [program (main-bootstrap-program pokemon-list-start)]324 ;; make sure program is valid output for item-writer325 (-> (tick (mid-game))326 (set-memory-range pokemon-list-start program)327 (PC! pokemon-list-start)328 (step [])329 (step []))))331 (defn test-set-H []332 (letfn [(test-H [state n]333 (let [after334 (-> state335 (step (buttons set-H-mode))336 (step (buttons n))337 (step []))]338 ;;(println "desired H =" n "actual =" (H after))339 (assert (= n (H after)))340 after))]341 (let [result (reduce test-H (bootstrap-base) (range 0x100))]342 (println "set H test passed.")343 result)))345 (defn test-write-bytes []346 (let [target-address 0xC00F347 [target-high target-low] (disect-bytes-2 target-address)348 assembly [0xF3 0x18 0xFE 0x12]349 get-mem-region #(subvec (vec (memory %))350 target-address (+ target-address 20))351 before (bootstrap-base)352 after353 (-> before354 (step []) ; make sure it can handle blanks355 (step []) ; at the beginning.356 (step [])357 (step (buttons set-H-mode)) ; select set-H358 (step (buttons target-high))359 (step [])360 (step (buttons set-L-mode))361 (step (buttons target-low))362 (step [])363 (step (buttons write-mode))364 (step (buttons 4)) ; write 4 bytes365 (step (buttons (nth assembly 0)))366 (step (buttons (nth assembly 1)))367 (step (buttons (nth assembly 2)))368 (step (buttons (nth assembly 3)))369 (step [])370 (step [])371 (step []))]372 ;;(println "before :" (get-mem-region before))373 ;;(println "after :" (get-mem-region after))374 ;;(assert (= assembly (take 4 (get-mem-region after))))375 (println "write-test-passed.")376 after))378 (defn test-jump []379 (let [target-address 0xC00F380 [target-high target-low] (disect-bytes-2 target-address)381 post-jump382 (-> (test-write-bytes)383 (step (buttons set-H-mode)) ; select set-H384 (step (buttons target-high))385 (step [])386 (step (buttons set-L-mode))387 (step (buttons target-low))388 (step [])389 (step (buttons jump-mode))) ; Select JUMP mode.390 program-counters391 (capture-program-counter392 post-jump393 10000)]394 (assert (contains? (set program-counters) target-address))395 (println "jump test passed.")396 post-jump))398 (defn test-no-repeated-nybbles []399 (bootstrap-pattern (main-bootstrap-program))400 (println "no-repeated-nybbles"))402 (defn run-all-tests []403 (test-frame-metronome)404 (test-read-user-input)405 (test-set-H)406 (test-write-bytes)407 (test-jump)408 (test-no-repeated-nybbles)409 (println "\n all tests passed."))