Mercurial > vba-clojure
view clojure/com/aurellem/gb/rlm_assembly.clj @ 409:55a45f67e4a4
brought documentation up to date.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 14 Apr 2012 01:32:22 -0500 |
parents | bca0abd39db5 |
children | 0162dd315814 |
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]))10 ;; Specs for Main Bootstrap Program12 ;; Number-Input13 ;; Number input works using all eight buttons to14 ;; spell out an 8 bit number. The order of buttons is15 ;; [:d :u :l :r :start :select :b :a] --> 1111111116 ;; [ :l :start :a] --> 0010100118 ;;; MODES19 ;; There are five modes in total:20 ;; MODE-SELECT21 ;; SET-H22 ;; SET-L23 ;; WRITE24 ;; JUMP26 ;;; MODE-SELECT27 ;; The bootstrap program starts in MODE-SELECT mode.28 ;; MODE-SELECT transitions to one of three modes depending29 ;; on which buttons are pressed:30 ;; 0 : MODE-SELECT31 ;; 0x67 : SET-H32 ;; 0x6F : SET-L33 ;; 0x47 : WRITE34 ;; 0xE9 : JUMP36 ;;; SET-H37 ;; SET-H sets the high 8 bits of the target address to which38 ;; data will be written / the program will jump. It expects39 ;; the following:40 ;;41 ;; Byte 0 : New Value of H42 ;; Byte 1 : 0x0044 ;;; SET-L45 ;; This mode sets the low 8 bits of the target address and has46 ;; the same semantics as SET-H.48 ;;; WRITE-BYTES49 ;; WRITE-BYTES mode writes sequences of arbitray values to50 ;; arbitray memory locations. It expects you to enter a51 ;; header of one byte describing how many bytes to write.53 ;; Byte 0 : Number of Bytes to Write55 ;; Then, you enter the number of bytes specified in Byte 056 ;; and they are written to the start address in sequence.57 ;; After the last byte is written control returns to58 ;; MODE-SELECT mode. The Target address will be incremented by59 ;; Number of Bytes to Write once you are done writing.61 ;; Example: to write the sequence [1 2 3 4] starting at62 ;; the target address enter:63 ;; Byte 0 : 4 (will write four bytes)64 ;; Byte 3 : 1 (write 1 to 0xC01F)65 ;; Byte 4 : 2 (write 2 to 0xC020)66 ;; Byte 5 : 3 (write 3 to 0xC021)67 ;; Byte 6 : 4 (write 4 to 0xC022)69 ;;; JUMP70 ;; JUMP mode jumps program control to the target address.72 ;;; EXAMPLE73 ;; To write the infinite loop program [0x18 0xFE] to address74 ;; 0xC00F and then jump to said program, enter the following75 ;; starting from MODE-SELECT mode.77 ;; Byte 0 : 0x67 [:a :b :l :u :select] ;; SET-H mode78 ;; Byte 1 : 0xC0 [:d :u] ;; 0xC0 -> H79 ;; Byte 2 : 0x00 [] ;; trailer81 ;; Byte 3 : 0x6F [:a :start :b :l :u :select] ;; SET-L mode82 ;; Byte 4 : 0x0F [:a :start :b :select] ;; 0x0F -> L83 ;; Byte 5 : 0x00 [] ;; trailer85 ;; Byte 6 : 0x47 [:a :b :u :select] ;; WRITE-MODE86 ;; Byte 7 : 0x02 [:b] ;; write 2 bytes87 ;; Byte 8 : 0x18 [:r :start] ;; assembly88 ;; Byte 9 : 0xFE [:r :start :b :d :l :u :select] ;; assembly90 ;; target address is now 0xC011 since we wrote 2 bytes.91 ;; set it back to 0xC00F.93 ;; Byte 10 : 0x6F [:a :start :b :l :u :select] ;; SET-L mode94 ;; Byte 12 : 0x0F [:a :start :b :select] ;; 0x0F -> L95 ;; Byte 13 : 0x00 [] ;; trailer97 ;; Byte 14 : 0xE9 ;; JUMP-MODE99 (defn ->signed-8-bit [n]100 (if (< n 0)101 (+ 256 n) n))103 (defn frame-metronome** []104 (let [init [0xC5] ;; save value of BC105 timing-loop106 [0x01 ; \107 0x43 ; |108 0xFE ; | load 0xFF44 into BC without repeats109 0x0C ; |110 0x04 ; /111 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)112 continue-if-144113 [0xFE114 144 ;; compare LY (in A) with 144115 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)116 (->signed-8-bit117 (+ -4 (- (count timing-loop))))]118 spin-loop119 [0x05 ;; dec B, which is 0xFF120 0x20 ;; spin until B==0121 0xFD]]122 (concat init timing-loop continue-if-144 spin-loop)))124 (defn frame-metronome* []125 [0x3E ;; smallest version, but uses repeated nybbles126 0x01127 0xE0128 0xFF])131 (defn frame-metronome []132 [0x06 ;; load 0xFE into B133 0xFE134 0x04 ;; inc B, now B == FF135 0x3E136 0x01 ;; 1->A138 0x48 ;; B->C139 0x02]) ;; A->(BC) set exclusive v-blank interrupt141 (defn test-frame-metronome142 "Ensure that frame-metronome ticks exactly once every frame."143 ([] (test-frame-metronome 151))144 ([steps]145 (let [inc-E [0x1C 0x76 0x18146 (->signed-8-bit -4)]148 program (concat (frame-metronome) inc-E)149 count-frames150 (-> (tick (mid-game))151 (IE! 0)152 (DE! 0)153 (set-memory-range pokemon-list-start program)154 (PC! pokemon-list-start))155 E-after-moves156 (E (run-moves count-frames (repeat steps [])))]157 ;;(println "E:" E-after-moves)158 (assert (= steps E-after-moves))159 (println "frame-count test passed.")160 count-frames)))162 (defn read-user-input []163 [0xAF 0x4F 0x47 ;; 0->A; 0->C; 0->B164 0xC5 ;; save value of BC166 0x3E167 0x20 ; prepare to measure d-pad169 0x3F ; clear carry flag no-op to prevent repeated nybbles171 0x01 ;\172 0x01 ; |173 0xFE ; | load 0xFF00 into BC without repeats174 0x04 ; |175 0x0D ;/177 0x02178 0x0A ;; get D-pad info180 0xF5 ;; push AF182 0x3E183 0x10 ; prepare to measure buttons185 0x3F ;; clear carry flag no-op to prevent repeated nybbbles187 0x02188 0x0A ;; get button info190 0xE6 ;; select bottom bits of A191 0x0F193 0x47 ;; A->B195 0xF1 ;; pop AF197 0xE6198 0x0F ;; select bottom bits of A200 0xCB201 0x37 ;; swap A nybbles203 0xB0 ;; (or A B) -> A205 0x2F ;; (NOT A) -> A206 ])208 (defn test-read-user-input []209 (let [program210 (concat211 (frame-metronome) (read-user-input)212 [0x5F ;; A-> E213 0x76214 0x18215 (->signed-8-bit216 (+ (- (count (read-user-input)))217 (- 4)))])218 read-input219 (-> (tick (mid-game))220 (IE! 0)221 (set-memory-range pokemon-list-start program)222 (PC! pokemon-list-start))]223 (dorun224 (for [i (range 0x100)]225 (assert (= (E (step read-input (buttons i))) i))))226 (println "tested all inputs.")227 read-input))229 (def symbol-index230 (fn [symbol sequence]231 (count (take-while232 (partial not= symbol)233 sequence))))235 (defn main-bootstrap-program236 ([] (main-bootstrap-program pokemon-list-start))237 ([start-address]238 ;; Register Use:240 ;; ED non-volitale scratch242 ;; A user-input243 ;; HL target-address244 ;; B bytes-to-write245 ;; C non-volatile scratch247 ;; Modes (with codes) are:249 ;; single-action-modes:250 ;; SET-TARGET-HIGH 0x67 ;; A->H251 ;; SET-TARGET-LOW 0x6F ;; A->L252 ;; JUMP 0xE9 ;; jump to (HL)254 ;; multi-action-modes255 ;; WRITE 0x47 ;; A->B257 (let [header (concat (frame-metronome) (read-user-input))259 input260 [0xC1 ;; pop BC so it's not volatile262 0x5F ;; A->E263 0xAF ;; test for output-mode (bytes-to-write > 0)264 0xB8 ;; (cp A B)265 0x7B ;; E->A266 0x20 ;; skip to output section if267 :to-output ;; we're not in input mode269 :to-be-executed271 ;; write mode to instruction-to-be-executed (pun)272 0xEA273 :to-be-executed-address275 ;; protection region -- do not queue this op for276 ;; execution if the last one was non-zero277 0x79 ;; C->A278 0xA7 ;; test A==0279 0x28280 0x04281 0xAF ;; put a no op (0x00) in to-be-executed282 0xEA ;;283 :to-be-executed-address285 0x7B ;; E->A286 0x4F ;; A->C now C stores previous instruction287 0x18 ;; return288 :to-halt]290 output291 [:output-start ;; just a label292 0x3F ;; ;; prevent repeated nybbles293 0x54 ;;294 0x5D ;; HL->DE \295 ;; | This mess is here to do296 0x12 ;; A->(DE) | 0x22 (LDI (HL), A) without297 ;; / any repeating nybbles298 0x05 ;; DEC bytes-to-write (B)300 0x23 ;; inc HL302 0x76 ;; HALT, peasant!303 0x18304 :to-beginning]306 symbols307 {:to-be-executed-address308 (reverse309 (disect-bytes-2310 (+ start-address311 (count header)312 (symbol-index :to-be-executed input))))313 :to-be-executed 0x3F} ;; clear carry flag no-op315 program** (flatten316 (replace symbols (concat header input output)))318 resolve-internal-jumps319 {:output-start []320 :to-output321 (->signed-8-bit322 (dec323 (- (symbol-index :output-start program**)324 (symbol-index :to-output program**))))}326 program*327 (flatten (replace resolve-internal-jumps program**))329 resolve-external-jumps330 {:to-halt331 (- (- (symbol-index :to-beginning program*)332 (symbol-index :to-halt program*)) 3)334 :to-beginning335 (->signed-8-bit336 (+ 2 (count (frame-metronome))337 (- (symbol-index :to-beginning program*))))}339 program340 (replace resolve-external-jumps program*)]341 program)))344 ;;;;;; TESTS ;;;;;;346 (def set-H-mode 0x67)347 (def set-L-mode 0x6F)348 (def jump-mode 0xE9)349 (def write-mode 0x47)352 (defn bootstrap-base []353 (let [program (main-bootstrap-program pokemon-list-start)]354 ;; make sure program is valid output for item-writer355 (-> (tick (mid-game))356 (set-memory-range pokemon-list-start program)357 (PC! pokemon-list-start)358 (step [])359 (step []))))361 (defn test-set-H []362 (letfn [(test-H [state n]363 (let [after364 (-> state365 (step (buttons set-H-mode))366 (step (buttons n))367 (step []))]368 ;;(println "desired H =" n "actual =" (H after))369 (assert (= n (H after)))370 after))]371 (let [result (reduce test-H (bootstrap-base) (range 0x100))]372 (println "set H test passed.")373 result)))375 (defn test-write-bytes []376 (let [target-address 0xC00F377 [target-high target-low] (disect-bytes-2 target-address)378 assembly [0xF3 0x18 0xFE 0x12]379 get-mem-region #(subvec (vec (memory %))380 target-address (+ target-address 20))381 before (bootstrap-base)382 after383 (-> before384 (step []) ; make sure it can handle blanks385 (step []) ; at the beginning.386 (step [])387 (step (buttons set-H-mode)) ; select set-H388 (step (buttons target-high))389 (step [])390 (step (buttons set-L-mode))391 (step (buttons target-low))392 (step [])393 (step (buttons write-mode))394 (step (buttons 4)) ; write 4 bytes395 (step (buttons (nth assembly 0)))396 (step (buttons (nth assembly 1)))397 (step (buttons (nth assembly 2)))398 (step (buttons (nth assembly 3))))]399 ;;(println "before :" (get-mem-region before))400 ;;(println "after :" (get-mem-region after))401 ;;(assert (= assembly (take 4 (get-mem-region after))))402 (println "write-test-passed.")403 after))405 (defn test-jump []406 (let [target-address 0xC00F407 [target-high target-low] (disect-bytes-2 target-address)408 post-jump409 (-> (test-write-bytes)410 (step (buttons set-H-mode)) ; select set-H411 (step (buttons target-high))412 (step [])413 (step (buttons set-L-mode))414 (step (buttons target-low))415 (step [])416 (step (buttons jump-mode))) ; Select JUMP mode.417 program-counters418 (capture-program-counter419 post-jump420 10000)]421 (assert (contains? (set program-counters) target-address))422 (println "jump test passed.")423 post-jump))425 (defn test-no-repeated-nybbles []426 (bootstrap-pattern (main-bootstrap-program))427 (println "no-repeated-nybbles"))429 (defn run-all-tests []430 (test-frame-metronome)431 (test-read-user-input)432 (test-set-H)433 (test-write-bytes)434 (test-jump)435 (test-no-repeated-nybbles)436 (println "\n all tests passed."))