Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 600:31128a10ee28
use png to save memory.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sun, 02 Sep 2012 09:20:09 -0500 |
parents | 747d47d96d2f |
children |
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 (:import [com.aurellem.gb.gb_driver SaveState]))7 (defn pc-item-writer-program8 []9 (let [;;limit 7510 limit 201 ;; (item-hack 201 is the smallest I could make this.)11 [target-high target-low] (disect-bytes-2 pokemon-list-start)]12 (flatten13 [[0x00 ;; (item-hack) no-op (can't buy repel (1E) at celadon)14 0x1E ;; load limit into E15 limit16 0x3F ;; (item-hack) set carry flag no-op18 ;; load 2 into C.19 0x0E ;; C == 1 means input-first nybble20 0x04 ;; C == 0 means input-second nybble22 0x21 ;; load target into HL23 target-low24 target-high25 0x37 ;; (item-hack) set carry flag no-op27 0x00 ;; (item-hack) no-op28 0x37 ;; (item-hack) set carry flag no-op30 0x00 ;; (item-hack) no-op31 0xF3 ;; disable interrupts32 ;; Input Section34 0x3E ;; load 0x20 into A, to measure buttons35 0x1037 0x00 ;; (item-hack) no-op38 0xE0 ;; load A into [FF00]39 0x0041 0xF0 ;; load 0xFF00 into A to get42 0x00 ;; button presses44 0xE645 0x0F ;; select bottom four bits of A46 0x37 ;; (item-hack) set carry flag no-op48 0x00 ;; (item-hack) no-op49 0xB8 ;; see if input is different (CP A B)51 0x00 ;; (item-hack) (INC SP)52 0x28 ;; repeat above steps if input is not different53 ;; (jump relative backwards if B != A)54 0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)56 0x47 ;; load A into B58 0x0D ;; dec C59 0x37 ;; (item-hack) set-carry flag60 ;; branch based on C:61 0x20 ;; JR NZ62 23 ;; skip "input second nybble" and "jump to target" below64 ;; input second nybble66 0x0C ;; inc C67 0x0C ;; inc C69 0x00 ;; (item-hack) no-op70 0xE6 ;; select bottom bits71 0x0F72 0x37 ;; (item-hack) set-carry flag no-op74 0x00 ;; (item-hack) no-op75 0xB2 ;; (OR A D) -> A77 0x22 ;; (do (A -> (HL)) (INC HL))79 0x1D ;; (DEC E)81 0x00 ;; (item-hack)82 0x20 ;; jump back to input section if not done83 0xDA ;; literal -36 == TM 18 (counter)84 0x01 ;; (item-hack) set BC to literal (no-op)86 ;; jump to target87 0x00 ;; (item-hack) these two bytes can be anything.88 0x0190 0x00 ;; (item-hack) no-op91 0xBF ;; (CP A A) ensures Z93 0xCA ;; (item-hack) jump if Z94 target-low95 target-high96 0x01 ;; (item-hack) will never be reached.98 ;; input first nybble99 0x00100 0xCB101 0x37 ;; swap nybbles on A103 0x57 ;; A -> D105 0x37 ;; (item-hack) set carry flag no-op106 0x18 ;; relative jump backwards107 0xCD ;; literal -51 == TM05; go back to input section108 0x01 ;; (item-hack) will never reach this instruction110 ]111 (repeat 8 [0x00 0x01]);; these can be anything113 [;; jump to actual program114 0x00115 0x37 ;; (item-hack) set carry flag no-op117 0x2E ;; 0x3A -> L118 0x3A121 0x00 ;; (item-hack) no-op122 0x26 ;; 0xD5 -> L123 0xD5124 0x01 ;; (item-hack) set-carry BC126 0x00 ;; (item-hack) these can be anything127 0x01129 0x00130 0xE9 ;; jump to (HL)131 ]])))135 ;; Specs for Main Bootstrap Program137 ;; Number-Input138 ;; Number input works using all eight buttons to139 ;; spell out an 8 bit number. The order of buttons is140 ;; [:d :u :l :r :start :select :b :a] --> 11111111141 ;; [ :l :start :a] --> 00101001143 ;;; MODES144 ;; There are five modes in total:145 ;; MODE-SELECT146 ;; SET-H147 ;; SET-L148 ;; WRITE149 ;; JUMP151 ;;; MODE-SELECT152 ;; The bootstrap program starts in MODE-SELECT mode.153 ;; MODE-SELECT transitions to one of three modes depending154 ;; on which buttons are pressed:155 ;; 0 : MODE-SELECT156 ;; 0x67 : SET-H157 ;; 0x6F : SET-L158 ;; 0x47 : WRITE159 ;; 0xE9 : JUMP161 ;;; SET-H162 ;; SET-H sets the high 8 bits of the target address to which163 ;; data will be written / the program will jump. It expects164 ;; the following:165 ;;166 ;; Byte 0 : New Value of H167 ;; Byte 1 : 0x00169 ;;; SET-L170 ;; This mode sets the low 8 bits of the target address and has171 ;; the same semantics as SET-H.173 ;;; WRITE-BYTES174 ;; WRITE-BYTES mode writes sequences of arbitray values to175 ;; arbitray memory locations. It expects you to enter a176 ;; header of one byte describing how many bytes to write.178 ;; Byte 0 : Number of Bytes to Write180 ;; Then, you enter the number of bytes specified in Byte 0181 ;; and they are written to the start address in sequence.182 ;; After the last byte is written control returns to183 ;; MODE-SELECT mode. The Target address will be incremented by184 ;; Number of Bytes to Write once you are done writing.186 ;; Example: to write the sequence [1 2 3 4] starting at187 ;; the target address enter:188 ;; Byte 0 : 4 (will write four bytes)189 ;; Byte 3 : 1 (write 1 to 0xC01F)190 ;; Byte 4 : 2 (write 2 to 0xC020)191 ;; Byte 5 : 3 (write 3 to 0xC021)192 ;; Byte 6 : 4 (write 4 to 0xC022)194 ;;; JUMP195 ;; JUMP mode jumps program control to the target address.197 ;;; EXAMPLE198 ;; To write the infinite loop program [0x18 0xFE] to address199 ;; 0xC00F and then jump to said program, enter the following200 ;; starting from MODE-SELECT mode.202 ;; Byte 0 : 0x67 [:a :b :l :u :select] ;; SET-H mode203 ;; Byte 1 : 0xC0 [:d :u] ;; 0xC0 -> H204 ;; Byte 2 : 0x00 [] ;; trailer206 ;; Byte 3 : 0x6F [:a :start :b :l :u :select] ;; SET-L mode207 ;; Byte 4 : 0x0F [:a :start :b :select] ;; 0x0F -> L208 ;; Byte 5 : 0x00 [] ;; trailer210 ;; Byte 6 : 0x47 [:a :b :u :select] ;; WRITE-MODE211 ;; Byte 7 : 0x02 [:b] ;; write 2 bytes212 ;; Byte 8 : 0x18 [:r :start] ;; assembly213 ;; Byte 9 : 0xFE [:r :start :b :d :l :u :select] ;; assembly215 ;; target address is now 0xC011 since we wrote 2 bytes.216 ;; set it back to 0xC00F.218 ;; Byte 10 : 0x6F [:a :start :b :l :u :select] ;; SET-L mode219 ;; Byte 12 : 0x0F [:a :start :b :select] ;; 0x0F -> L220 ;; Byte 13 : 0x00 [] ;; trailer222 ;; Byte 14 : 0xE9 ;; JUMP-MODE224 (defn ->signed-8-bit [n]225 (if (< n 0)226 (+ 256 n) n))228 (defn frame-metronome []229 (let [init [0xC5] ;; save value of BC230 timing-loop231 [0x01 ; \232 0x43 ; |233 0xFE ; | load 0xFF44 into BC without repeats234 0x0C ; |235 0x04 ; /236 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)237 continue-if-144238 [0xFE239 144 ;; compare LY (in A) with 144240 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)241 (->signed-8-bit242 (+ -4 (- (count timing-loop))))]243 spin-loop244 [0x05 ;; dec B, which is 0xFF245 0x20 ;; spin until B==0246 0xFD]]247 (concat init timing-loop continue-if-144 spin-loop)))249 (defn frame-metronome* []250 [0x3E ;; smallest version, but uses repeated nybbles251 0x01252 0xE0253 0xFF])255 (defn frame-metronome** []256 [0x06 ;; load 0xFE into B257 0xFE258 0x04 ;; inc B, now B == FF260 0x3E ;; RLM-debug261 0x01 ;; 1->A263 0x48 ;; B->C264 0x02]) ;; A->(BC) set exclusive v-blank interrupt266 (defn test-frame-metronome267 "Ensure that frame-metronome ticks exactly once every frame."268 ([] (test-frame-metronome 151))269 ([steps]270 (let [inc-E [0x1C 0x18271 (->signed-8-bit272 (+ -3273 (-(count (frame-metronome)))))]275 program (concat (frame-metronome) inc-E)276 count-frames277 (-> (tick (mid-game))278 (IE! 0)279 (DE! 0)280 (set-memory-range pokemon-list-start program)281 (PC! pokemon-list-start))282 E-after-moves283 (E (run-moves count-frames (repeat steps [])))]284 ;;(println "E:" E-after-moves)285 (assert (= steps E-after-moves))286 (println "frame-count test passed.")287 count-frames)))289 (defn read-user-input []290 [0x3E291 0x20 ; prepare to measure d-pad293 0x3F ; clear carry flag no-op to prevent repeated nybbles295 0x01 ;\296 0x01 ; |297 0xFE ; | load 0xFF00 into BC without repeats298 0x04 ; |299 0x0D ;/301 0x02302 0x0A ;; get D-pad info304 0xF5 ;; push AF306 0x3E307 0x10 ; prepare to measure buttons309 0x3F ;; clear carry flag no-op to prevent repeated nybbbles311 0x02312 0x0A ;; get button info314 0xE6 ;; select bottom bits of A315 0x0F317 0x47 ;; A->B319 0xF1 ;; pop AF321 0xE6322 0x0F ;; select bottom bits of A324 0xCB325 0x37 ;; swap A nybbles327 0xB0 ;; (or A B) -> A329 0x2F ;; (NOT A) -> A330 ])332 (defn test-read-user-input []333 (let [program334 (concat335 (frame-metronome) (read-user-input)336 [0x5F ;; A-> E337 0x76338 0x18339 (->signed-8-bit340 (+ (- (count (read-user-input)))341 (- 4)))])342 read-input343 (-> (tick (mid-game))344 (IE! 0)345 (set-memory-range pokemon-list-start program)346 (PC! pokemon-list-start))]347 (dorun348 (for [i (range 0x100)]349 (assert (= (E (step read-input (buttons i))) i))))350 (println "tested all inputs.")351 read-input))353 (def symbol-index354 (fn [symbol sequence]355 (count (take-while356 (partial not= symbol)357 sequence))))359 (defn bootstrap-state-machine360 ([start-address]361 ;; Register Use:363 ;; ED non-volitale scratch365 ;; A user-input (A MUST contain user-input for this to work!)366 ;; HL target-address367 ;; B bytes-to-write368 ;; C non-volatile scratch370 ;; Modes (with codes) are:372 ;; single-action-modes:373 ;; SET-TARGET-HIGH 0x67 ;; A->H374 ;; SET-TARGET-LOW 0x6F ;; A->L375 ;; JUMP 0xE9 ;; jump to (HL)377 ;; multi-action-modes378 ;; WRITE 0x47 ;; A->B379 (let [380 input381 [0xC1 ;; pop BC so it's not volatile383 0x5F ;; A->E384 0xAF ;; test for output-mode (bytes-to-write > 0)385 0xB8 ;; (cp A B)386 0x7B ;; E->A387 0x20 ;; skip to output section if388 :to-output ;; we're not in input mode390 :to-be-executed392 ;; write mode to instruction-to-be-executed (pun)393 0xEA394 :to-be-executed-address396 ;; protection region -- do not queue this op for397 ;; execution if the last one was non-zero398 0x79 ;; C->A399 0xA7 ;; test A==0400 0x28401 0x04402 0xAF ;; put a no op (0x00) in to-be-executed403 0xEA ;;404 :to-be-executed-address406 0x7B ;; E->A407 0x4F ;; A->C now C stores previous instruction408 0x18 ;; return409 :to-jump]411 output412 [:output-start ;; just a label413 0x3F ;; ;; prevent repeated nybbles414 0x54 ;;415 0x5D ;; HL->DE \416 ;; | This mess is here to do417 0x12 ;; A->(DE) | 0x22 (LDI (HL), A) without418 ;; / any repeating nybbles419 0x05 ;; DEC bytes-to-write (B)421 0x23 ;; inc HL422 ]424 symbols425 {:to-be-executed-address426 (reverse427 (disect-bytes-2428 (+ start-address429 (symbol-index :to-be-executed input))))430 :to-be-executed 0x3F} ;; clear carry flag no-op432 program** (flatten433 (replace434 symbols435 (concat input output)))437 resolve-internal-jumps438 {:output-start []439 :to-output440 (->signed-8-bit441 (dec442 (- (symbol-index :output-start program**)443 (symbol-index :to-output program**))))}445 program*446 (flatten (replace resolve-internal-jumps program**))448 resolve-external-jumps449 {:to-jump450 (- (- (count program*)451 (symbol-index :to-jump program*)) 1)}452 program453 (replace resolve-external-jumps program*)]454 program)))457 (defn main-bootstrap-program458 ([] (main-bootstrap-program pokemon-list-start))459 ([start-address]460 (let [init [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B461 header (concat (frame-metronome) (read-user-input))462 state-machine-start-address463 (+ start-address (count init) (count header))464 state-machine465 (bootstrap-state-machine state-machine-start-address)467 return-to-header468 (flatten469 [0x18470 (->signed-8-bit471 (- (count init)472 2 ;; this command length473 3 ;; I have no idea why we need a 3 here474 ;; need to investigate.475 (count header)476 (count state-machine)))])]478 (concat init header state-machine return-to-header))))482 (defn no-consecutive-repeats? [seq]483 (not (contains? (set(map - seq (rest seq))) 0)))485 (defn byte->nybbles [byte]486 [(bit-shift-right byte 4) (bit-and byte 0x0F)])488 (defn bootstrap-pattern489 "Given an assembly sequence, generate the keypresses required to490 create that sequence in memory using the pc-item-writer491 program. The assembly must not have any consecutive repeating492 nybbles."493 [assembly]494 (let [nybbles (flatten (map byte->nybbles assembly))495 moves (map (comp buttons (partial - 15)) nybbles)496 header (map buttons497 (concat (repeat498 23 ;; found by manual binary search499 (- 15 (first nybbles)))500 [(first nybbles)]))]501 (assert (no-consecutive-repeats? nybbles))502 (concat header moves)))504 ;;;;;; TESTS ;;;;;;506 (def set-H-mode 0x67)507 (def set-L-mode 0x6F)508 (def jump-mode 0xE9)509 (def write-mode 0x47)512 (defn bootstrap-base []513 (let [program (main-bootstrap-program pokemon-list-start)]514 ;; make sure program is valid output for item-writer515 (-> (tick (mid-game))516 (set-memory-range pokemon-list-start program)517 (PC! pokemon-list-start)518 (step [])519 (step []))))521 (defn test-set-H []522 (letfn [(test-H [state n]523 (let [after524 (-> state525 (step (buttons set-H-mode))526 (step (buttons n))527 (step []))]528 ;;(println "desired H =" n "actual =" (H after))529 (assert (= n (H after)))530 after))]531 (let [result (reduce test-H (bootstrap-base) (range 0x100))]532 (println "set H test passed.")533 result)))535 (defn test-write-bytes []536 (let [target-address 0xC00F537 [target-high target-low] (disect-bytes-2 target-address)538 assembly [0xF3 0x18 0xFE 0x12]539 get-mem-region #(subvec (vec (memory %))540 target-address (+ target-address 20))541 before (bootstrap-base)542 after543 (-> before544 (step []) ; make sure it can handle blanks545 (step []) ; at the beginning.546 (step [])547 (step (buttons set-H-mode)) ; select set-H548 (step (buttons target-high))549 (step [])550 (step (buttons set-L-mode))551 (step (buttons target-low))552 (step [])553 (step (buttons write-mode))554 (step (buttons 4)) ; write 4 bytes555 (step (buttons (nth assembly 0)))556 (step (buttons (nth assembly 1)))557 (step (buttons (nth assembly 2)))558 (step (buttons (nth assembly 3))))]559 ;;(println "before :" (get-mem-region before))560 ;;(println "after :" (get-mem-region after))561 ;;(assert (= assembly (take 4 (get-mem-region after))))562 (println "write-test-passed.")563 after))565 (defn test-jump []566 (let [target-address 0xC00F567 [target-high target-low] (disect-bytes-2 target-address)568 post-jump569 (-> (test-write-bytes)570 (step (buttons set-H-mode)) ; select set-H571 (step (buttons target-high))572 (step [])573 (step (buttons set-L-mode))574 (step (buttons target-low))575 (step [])576 (step (buttons jump-mode))) ; Select JUMP mode.577 program-counters578 (capture-program-counter579 post-jump580 10000)]581 (assert (contains? (set program-counters) target-address))582 (println "jump test passed.")583 post-jump))585 (defn test-no-repeated-nybbles []586 (bootstrap-pattern (main-bootstrap-program))587 (println "no-repeated-nybbles"))589 (defn run-all-tests []590 (test-frame-metronome)591 (test-read-user-input)592 (test-set-H)593 (test-write-bytes)594 (test-jump)595 (test-no-repeated-nybbles)596 (println "\n all tests passed."))