Mercurial > vba-clojure
view clojure/com/aurellem/assembly.clj @ 113:0831da75d2c5
completed frame-counting machine language program with dylan's help
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 16 Mar 2012 00:43:28 -0500 |
parents | 6fe33bb5ea22 |
children | a454730d92dd |
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? 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 b-blank? 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 ;D34A234 ])))239 (defn run-a-bit []240 (-> (read-down-button)241 (info)242 (tick) ;; skip over data section243 (info)244 (view-register "Register A" A)245 (tick) ;; load-data into A246 (view-register "Register A" A)247 (info)248 (view-memory 0xFF00)249 (tick) ;; load A into 0xFF00250 (view-memory 0xFF00)251 (info)252 (tick)253 (info)254 (tick)255 (info)256 (tick)257 (info)258 (tick)259 (info)260 (tick)261 (info)262 (tick)263 (print-inventory)))265 (defn test-read-pad []266 (= (view-memory (step (step (read-buttons) [:d])) 0xD328)267 (view-memory (step (step (read-buttons))) 0xD328)))269 (defn trace [state]270 (loop [program-counters []271 opcodes []]272 (let [frame-boundary?273 (com.aurellem.gb.Gb/tick)]274 (println (count opcodes))275 (if frame-boundary?276 [program-counters opcodes]277 (recur278 (conj program-counters279 (first (registers @current-state)))280 (conj opcodes281 (aget (memory @current-state)282 (PC @current-state))))))))284 (defn good-trace []285 (-> (mid-game) (tick) (IE! 0)286 (set-inv-mem [0x00 0x00 0X00 0x00])287 (PC! item-list-start)(print-interrupt)288 (info) (tick) (info) (tick) (info)))