Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 378:5c4a30521d09
created efficient frame-metronome program
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Wed, 11 Apr 2012 11:43:51 -0500 |
parents | 1f14c1b8af7e |
children | f86bd04bd9fc |
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 [0x47 ;; A->B70 0x2671 0xFE ;; load FF into H without repeats72 0x2474 0x2E75 0x43 ;; load 44 into L without repeats76 0x2C78 0x7E] ;; (HL) -> A, now A = LY (vertical line coord)79 jump-if-not-14480 [0xFE81 144 ;; compare LY (in A) with 14482 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)83 (->signed-8-bit (+ -4 (- (count timing-loop))))]84 continue-if-different85 [0xB8 ;; compare A with B86 0x2887 (->signed-8-bit88 (+ -3 (- (+ (count timing-loop) (count jump-if-not-144)))))]]89 (concat timing-loop jump-if-not-144 continue-if-different)))91 (defn test-frame-metronome []92 (let [inc-C [0x0C 0x1893 (->signed-8-bit94 (+ -3 (- (count (frame-metronome)))))]95 program (concat (frame-metronome) inc-C)96 count-frames97 (-> (tick (mid-game))98 (IE! 0)99 (BC! 0)100 (set-memory-range pokemon-list-start program)101 (PC! pokemon-list-start))102 steps 151]103 (assert104 (= 151105 (C (run-moves count-frames (repeat steps [])))))106 count-frames))109 (defn main-bootstrap-program [start-address]110 (let [[start-high start-low] (disect-bytes-2 start-address)111 ]112 ))123 ;;;;;; TESTS ;;;;;;125 (defn bootstrap-base []126 (let [program (main-bootstrap-program pokemon-list-start)]127 ;; make sure program is valid output for item-writer128 (bootstrap-pattern program)129 (-> (tick (mid-game))130 (set-memory-range pokemon-list-start program)131 (PC! pokemon-list-start))))133 (defn test-write-bytes-mode []134 (let [target-address 0xC00F135 [target-high target-low] (disect-bytes-2 target-address)136 assembly [0xF3 0x18 0xFE 0x12]137 get-mem-region #(subvec (vec (memory %))138 target-address (+ target-address 20))139 before (bootstrap-base)140 after141 (-> before142 (step []) ; make sure it can handle blanks143 (step []) ; at the beginning.144 (step [])145 (step [:start]) ; select WRITE-BYTES mode146 (step (buttons 4)) ; write 4 bytes147 (step (buttons target-high))148 (step (buttons target-low))149 (step (buttons (nth assembly 0)))150 (step (buttons (nth assembly 1)))151 (step (buttons (nth assembly 2)))152 (step (buttons (nth assembly 3)))153 (step [])154 (step [])155 (step []))]156 (println "before :" (get-mem-region before))157 (println "after :" (get-mem-region after))158 (assert (= assembly (take 4 (get-mem-region after))))159 after))161 (defn test-jump-mode []162 (let [target-address 0xC00F163 [target-high target-low] (disect-bytes-2 target-address)164 post-jump165 (-> (test-write-bytes-mode)166 (step [])167 (step [])168 (step [])169 (step (buttons 0xFF)) ; Select JUMP mode.170 (step (buttons target-high))171 (step (buttons target-low)))172 program-counters173 (capture-program-counter174 post-jump175 10000)]176 (println program-counters)177 (assert (contains? (set program-counters) target-address))178 post-jump))