Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 403:ea37e98e188e
removed one opcode
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 13 Apr 2012 09:59:32 -0500 |
parents | eee219d1a259 |
children | 41647cb85901 |
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-metronome74 ([] (frame-metronome true))75 ([spin-loop?]76 (let [init [0xC5] ;; save value of BC77 timing-loop78 [0x01 ; \79 0x43 ; |80 0xFE ; | load 0xFF44 into BC without repeats81 0x0C ; |82 0x04 ; /83 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)84 continue-if-14485 [0xFE86 144 ;; compare LY (in A) with 14487 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)88 (->signed-8-bit89 (+ -4 (- (count timing-loop))))]90 spin-loop91 [0x05 ;; dec B, which is 0xFF92 0x20 ;; spin until B==093 0xFD]]94 (concat init timing-loop continue-if-14495 (if spin-loop?96 spin-loop [])))))98 (defn test-frame-metronome99 "Ensure that frame-metronome ticks exactly once every frame."100 ([] (test-frame-metronome 151))101 ([steps]102 (let [inc-E [0x1C 0x18103 (->signed-8-bit104 (+ -3 (- (count (frame-metronome)))))]105 program (concat (frame-metronome) inc-E)106 count-frames107 (-> (tick (mid-game))108 (IE! 0)109 (DE! 0)110 (set-memory-range pokemon-list-start program)111 (PC! pokemon-list-start))112 E-after-moves113 (E (run-moves count-frames (repeat steps [])))]114 (println "E:" E-after-moves)115 (assert (= steps E-after-moves))117 (println "E =" E-after-moves "after" steps "steps")118 count-frames)))120 (defn read-user-input []121 [0x3E122 0x20 ; prepare to measure d-pad124 0x01 ;\125 0x01 ; |126 0xFE ; | load 0xFF00 into BC without repeats127 0x04 ; |128 0x0D ;/130 0x02131 0x0A ;; get D-pad info133 0xF5 ;; push AF135 0x3E136 0x10 ; prepare to measure buttons138 0x3F ;; clear carry flag no-op to prevent repeated nybbbles140 0x02141 0x0A ;; get button info143 0xE6 ;; select bottom bits of A144 0x0F146 0x47 ;; A->B148 0xF1 ;; pop AF150 0xE6151 0x0F ;; select bottom bits of A153 0xCB154 0x37 ;; swap A nybbles156 0xB0 ;; (or A B) -> A158 0x2F ;; (NOT A) -> A160 ])162 (defn test-read-user-input []163 (let [program164 (concat165 (frame-metronome) (read-user-input)166 [0x5F ;; A-> E167 0x18168 (->signed-8-bit169 (+ (- (count (frame-metronome)))170 (- (count (read-user-input)))171 (- 3)))])172 read-input173 (-> (tick (mid-game))174 (IE! 0)175 (set-memory-range pokemon-list-start program)176 (PC! pokemon-list-start))]177 (dorun178 (for [i (range 0x100)]179 (assert (= (E (step read-input (buttons i))) i))))180 (println "Tested all inputs.")181 read-input))183 (def symbol-index184 (fn [symbol sequence]185 (count (take-while186 (partial not= symbol)187 sequence))))189 (defn main-bootstrap-program190 ([] (main-bootstrap-program pokemon-list-start))191 ([start-address]192 ;; Register Use:194 ;; ED non-volitale scratch196 ;; A user-input197 ;; HL target-address198 ;; B bytes-to-write199 ;; C non-volatile scratch201 ;; Modes (with codes) are:203 ;; single-action-modes:204 ;; SET-TARGET-HIGH 0x67 ;; A->H205 ;; SET-TARGET-LOW 0x6F ;; A->L206 ;; JUMP 0xE9 ;; jump to (HL)208 ;; multi-action-modes209 ;; WRITE 0x47 ;; A->B211 (let [[start-high start-low] (disect-bytes-2 start-address)212 jump-distance (+ (count (frame-metronome))213 (count (read-user-input)))215 init216 [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B218 input219 [0xC1 ;; pop BC so it's not volatile221 0x5F ;; A->E222 0xAF ;; test for output-mode (bytes-to-write > 0)223 0xB8 ;; (cp A B)224 0x7B ;; E->A225 0x20 ;; skip to output section if226 :to-output ;; we're not in input mode228 :to-be-executed230 ;; write mode to instruction-to-be-executed (pun)231 0xEA232 :to-be-executed-address234 ;; protection region -- do not queue this op for235 ;; execution if the last one was non-zero236 0x79 ;; C->A237 0xA7 ;; test A==0238 0x28239 0x04240 0xAF ;; put a no op (0x00) in to-be-executed241 0xEA ;;242 :to-be-executed-address244 0x7B ;; E->A245 0x4F ;; A->C now C stores previous instruction246 0x18 ;; return247 :to-beginning-1]249 output250 [:output-start ;; just a label251 0x54 ;;252 0x5D ;; HL->DE \253 ;; | This mess is here to do254 0x12 ;; A->(DE) | 0x22 (LDI (HL), A) without255 ;; | any repeating nybbles256 0x23 ;; inc HL /258 0x05 ;; DEC bytes-to-write (B)260 0x18261 :to-beginning-2]263 symbols264 {:to-be-executed-address265 (reverse266 (disect-bytes-2267 (+ start-address jump-distance268 (count init)269 (symbol-index :to-be-executed input))))270 :to-be-executed 0x00} ;; clear carry flag no-op272 program** (flatten273 (replace274 symbols275 (concat init (frame-metronome)276 (read-user-input)277 input output)))278 resolve-internal-jumps279 {:output-start []280 :to-output281 (->signed-8-bit282 (dec283 (- (symbol-index :output-start program**)284 (symbol-index :to-output program**))))}286 program*287 (flatten (replace resolve-internal-jumps program**))289 resolve-external-jumps290 {:to-beginning-1291 (->signed-8-bit292 (+ (count init)293 -2 (- (dec (symbol-index :to-beginning-1 program*)))))294 :to-beginning-2295 (->signed-8-bit296 (+ (count init)297 -2 (- (dec (symbol-index :to-beginning-2 program*)))))}299 program300 (replace resolve-external-jumps program*)]301 program)))304 ;;;;;; TESTS ;;;;;;306 (def set-H-mode 0x67)307 (def set-L-mode 0x6F)308 (def jump-mode 0xE9)309 (def write-mode 0x47)312 (defn bootstrap-base []313 (let [program (main-bootstrap-program pokemon-list-start)]314 ;; make sure program is valid output for item-writer315 ;;(bootstrap-pattern program)316 (-> (tick (mid-game))317 (set-memory-range pokemon-list-start program)318 (PC! pokemon-list-start)319 (step [])320 (step []))))322 (defn test-set-H []323 (letfn [(test-H [state n]324 (let [after325 (-> state326 (step (buttons set-H-mode))327 (step (buttons n))328 (step []))]329 ;;(println "desired H =" n "actual =" (H after))330 (assert (= n (H after)))331 after))]332 (let [result (reduce test-H (bootstrap-base) (range 0x100))]333 (println "tested all H values")334 result)))336 (defn test-write-bytes []337 (let [target-address 0xC00F338 [target-high target-low] (disect-bytes-2 target-address)339 assembly [0xF3 0x18 0xFE 0x12]340 get-mem-region #(subvec (vec (memory %))341 target-address (+ target-address 20))342 before (bootstrap-base)343 after344 (-> before345 (step []) ; make sure it can handle blanks346 (step []) ; at the beginning.347 (step [])348 (step (buttons set-H-mode)) ; select set-H349 (step (buttons target-high))350 (step [])351 (step (buttons set-L-mode))352 (step (buttons target-low))353 (step [])354 (step (buttons write-mode))355 (step (buttons 4)) ; write 4 bytes356 (step (buttons (nth assembly 0)))357 (step (buttons (nth assembly 1)))358 (step (buttons (nth assembly 2)))359 (step (buttons (nth assembly 3)))360 (step [])361 (step [])362 (step []))]363 (println "before :" (get-mem-region before))364 (println "after :" (get-mem-region after))365 (assert (= assembly (take 4 (get-mem-region after))))366 after))368 (defn test-jump []369 (let [target-address 0xC00F370 [target-high target-low] (disect-bytes-2 target-address)371 post-jump372 (-> (test-write-bytes)373 (step (buttons set-H-mode)) ; select set-H374 (step (buttons target-high))375 (step [])376 (step (buttons set-L-mode))377 (step (buttons target-low))378 (step [])379 (step (buttons jump-mode))) ; Select JUMP mode.380 program-counters381 (capture-program-counter382 post-jump383 10000)]384 (assert (contains? (set program-counters) target-address))385 (println "jump test passed")386 post-jump))389 (defn run-all-tests []390 (test-frame-metronome)391 (test-read-user-input)392 (test-set-H)393 (test-write-bytes)394 (test-jump))