Mercurial > vba-clojure
view clojure/com/aurellem/assembly.clj @ 114:a454730d92dd
added test for count-frames
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 16 Mar 2012 13:51:17 -0500 |
parents | 0831da75d2c5 |
children | 39fb0cbab25e |
line wrap: on
line source
1 (ns com.aurellem.assembly2 (:use (com.aurellem gb-driver vbm title items))3 (:import [com.aurellem.gb_driver SaveState]))5 (defn mid-game []6 (read-state "mid-game"))8 (defn inject-assembly9 ([^SaveState state10 program-counter registers11 assembly-code]12 (let [scratch-memory (memory state)]13 ;; inject assembly code14 (dorun (map (fn [index val]15 (aset scratch-memory index val))16 (range program-counter17 (+ program-counter (count assembly-code)))18 assembly-code))19 (-> state20 (write-memory! scratch-memory)21 (write-registers! registers)22 (PC! program-counter)))))24 (defn inject-item-assembly25 ([^SaveState state assembly-code]26 (inject-assembly state (inc item-list-start)27 (registers state)28 assembly-code))29 ([assembly-code]30 (inject-item-assembly @current-state assembly-code)))32 (defn info33 ([^SaveState state]34 (println (format "PC: 0x%04X" (PC state)))35 (println "Instruction:"36 (format "0x%02X" (aget (memory state) (PC state))))37 state))39 (defn print-interrupt40 [^SaveState state]41 (println (format "IE: %d" (IE state)))42 state)44 (defn run-assembly45 ([info-fn assembly n]46 (let [final-state47 (reduce (fn [state _]48 (tick (info-fn state)))49 (inject-item-assembly50 (mid-game) assembly)51 (range n))]52 final-state))53 ([assembly n]54 (run-assembly info assembly n)))56 (def buttons-port 0xFF00)58 (defn A [state]59 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))61 (defn binary-str [num]62 (format "%08d"63 (Integer/parseInt64 (Integer/toBinaryString num) 10)))66 (defn view-register [state name reg-fn]67 (println (format "%s: %s" name68 (binary-str (reg-fn state))))69 state)72 (defn view-memory [state mem]73 (println (format "mem 0x%04X = %s" mem74 (binary-str (aget (memory state) mem))))75 state)77 (defn read-down-button []78 (-> (tick (mid-game))79 (IE! 0) ; disable interrupts80 (inject-item-assembly81 (concat82 ;; write 00010000 to 0xFF00 to select joypad83 [0x18 ;D31D ; jump over84 0x01 ;D31E ; the next 8 bits85 ;D31F86 (Integer/parseInt "00100000" 2) ; data section88 0xFA ;D320 ; load (D31F) into A89 0x1F ;D321 -->90 0xD3 ;D322 --> D31F92 0xEA ;D323 ; load (A), which is93 0x00 ;D324 --> ; 00010000, into FF0094 0xFF ;D325 --> FF0096 0x18 ;D326 ; this is the place where97 0x01 ;D327 ; we will store whether98 0x00 ;D328 ; "down" is pressed.100 0xFA ;D329 ; (FF00) -> A101 0x00 ;D32A102 0xFF ;D32B104 0xCB ;D32C ; Test whether "down"105 0x5F ;D32D ; is pressed.107 0x28 ;D32E ; if down is pressed,108 0x03 ;D32F ; skip the next section109 ; of code.110 ;; down-is-not-pressed111 0xC3 ;D330112 0x1D ;D331 ; return to beginning113 0xD3 ;D332115 ;; down-is-pressed116 0xEA ;D334 ; write A to D328 if117 0x28 ;D335 ; "down" was pressed118 0xD3 ;D336120 0xC3 ;D330121 0x1D ;D331 ; return to beginning122 0xD3 ;D332123 ]125 []))))128 ;; specs for main bootstrap program129 ;; starts in "mode-select" mode130 ;; Each button press takes place in a single frame.131 ;; mode-select-mode takes one of the main buttons132 ;; which selects one of up to eight modes133 ;; mode 1 activated by the "A" button134 ;; the next two button presses indicates the start135 ;; memory location which to which the bootstrap136 ;; program will write.137 ;; This is done by using each of the eight buttons to138 ;; spell out an 8 bit number. The order of buttons is139 ;; ["A" "B" "start" "select" "up" "right" "down" "left"],140 ;; [:a :start :l] --> 10100001142 ;; the next button press determines how many bytes are to be143 ;; written, starting at the start position.145 ;; then, the actual bytes are entered and are written to the146 ;; start address in sequence.149 (defn count-frames []150 (-> (tick (mid-game))151 (IE! 0) ; disable interrupts152 (inject-item-assembly153 ;; write 00010000 to 0xFF00 to select joypad154 [0x18 ;D31D ; jump over155 0x02 ;D31E ; the next 2 bytes156 0x00 ;D31F ; frame-count157 0x00 ;D320 ; v-blank-prev159 0xFA ;D321160 0x41 ;D322 ; load (FF41) into A161 0xFF ;D323 ; this contains mode flags165 ;; if we're in v-blank, the bit-1 is 0166 ;; and bit-2 is 1 Otherwise, it is not v-blank.167 0xCB ;D324 ; test bit-1 of A168 0x4F ;D325170 0xC2 ;D326 ; if bit-1 is not 0171 0x43 ;D327 ; GOTO not-v-blank172 0xD3 ;D328174 0xCB ;D329 ; test bit-0 of A175 0x47 ;D32A177 0xCA ;D32B ; if bit-0 is not 1178 0x43 ;D32C ; GOTO not-v-blank179 0xD3 ;D32D181 ;;; in v-blank mode183 ;; if v-blank-prev was 0,184 ;; increment frame-count186 0xFA ;D32E ; load v-blank-prev to A187 0x20 ;D32F188 0xD3 ;D330190 0xCB ;D331191 0x47 ;D332 ; test bit-0 of A193 0x20 ;D333 ; skip next section194 0x07 ;D334 ; if v-blank-prev was not zero196 ;; v-blank was 0, increment frame-count197 0xFA ;D335 ; load frame-count into A198 0x1F ;D336199 0xD3 ;D337201 0x3C ;D338 ; inc A203 0xEA ;D339 ; load A into frame-count204 0x1F ;D33A205 0xD3 ;D33B207 ;; set v-blank-prev to 1208 0x3E ;D33C ; load 1 into A209 0x01 ;D33D211 0xEA ;D33E ; load A into v-blank-prev212 0x20 ;D33F213 0xD3 ;D340215 0x18 ;D341 ; skip not-in-v-blank section216 0x05 ;D342218 ;;; not in v-blank mode219 ;; set v-blank-prev to 0220 0x3E ;D343 ; load 0 into A221 0x00 ;D344223 0xEA ;D345 ; load A into v-blank-prev224 0x20 ;D346225 0xD3 ;D347228 0xC3 ;D348 ; return to beginning229 0x1D ;D349230 0xD3 ;D34A231 ])))233 (defn step-count-frames []234 (-> (read-down-button)235 (info)236 (tick) ;; skip over data section237 (info)238 (view-register "Register A" A)239 (tick) ;; load-data into A240 (view-register "Register A" A)241 (info)242 (view-memory 0xFF00)243 (tick) ;; load A into 0xFF00244 (view-memory 0xFF00)245 (info)246 (tick)247 (info)248 (tick)249 (info)250 (tick)251 (info)252 (tick)253 (info)254 (tick)255 (info)256 (tick)257 (print-inventory)))259 (defn test-read-down []260 (= (view-memory (step (step (read-buttons) [:d])) 0xD328)261 (view-memory (step (step (read-buttons))) 0xD328)))263 (defn trace [state]264 (loop [program-counters []265 opcodes []]266 (let [frame-boundary?267 (com.aurellem.gb.Gb/tick)]268 (println (count opcodes))269 (if frame-boundary?270 [program-counters opcodes]271 (recur272 (conj program-counters273 (first (registers @current-state)))274 (conj opcodes275 (aget (memory @current-state)276 (PC @current-state))))))))278 (defn good-trace []279 (-> (mid-game) (tick) (IE! 0)280 (set-inv-mem [0x00 0x00 0X00 0x00])281 (PC! item-list-start)(print-interrupt)282 (info) (tick) (info) (tick) (info)))