Mercurial > vba-clojure
view clojure/com/aurellem/assembly.clj @ 112:6fe33bb5ea22
saving progress
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 15 Mar 2012 17:29:49 -0500 |
parents | 8be7ce890184 |
children | 0831da75d2c5 |
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-buttons []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 (Integer/parseInt "00100000" 2) ;D31F data section87 0xFA ;D320 ; load (D31F) into A88 0x1F ;D321 -->89 0xD3 ;D322 --> D31F91 0xEA ;D323 ; load (A), which is92 0x00 ;D324 --> ; 00010000, into FF0093 0xFF ;D325 --> FF0095 0x18 ;D326 ; this is the place where96 0x01 ;D327 ; we will store whether97 0x00 ;D328 ; "down" is pressed.99 0xFA ;D329 ; (FF00) -> A100 0x00 ;D32A101 0xFF ;D32B103 0xCB ;D32C ; Test whether "down"104 0x5F ;D32D ; is pressed.106 0x28 ;D32E ; if down is pressed,107 0x03 ;D32F ; skip the next section108 ; of code.109 ;; down-is-not-pressed110 0xC3 ;D330111 0x1D ;D331 ; return to beginning112 0xD3 ;D332114 ;; down-is-pressed115 0xEA ;D334 ; write A to D328 if116 0x28 ;D335 ; "down" was pressed117 0xD3 ;D336119 0xC3 ;D330120 0x1D ;D331 ; return to beginning121 0xD3 ;D332122 ]124 []))))126 (defn run-a-bit []127 (-> (read-buttons)128 (info)129 (tick) ;; skip over data section130 (info)131 (view-register "Register A" A)132 (tick) ;; load-data into A133 (view-register "Register A" A)134 (info)135 (view-memory 0xFF00)136 (tick) ;; load A into 0xFF00137 (view-memory 0xFF00)138 (info)139 (tick)140 (info)141 (tick)142 (info)143 (tick)144 (info)145 (tick)146 (info)147 (tick)148 (info)149 (tick)150 (print-inventory)))152 (defn test-read-pad []153 (= (view-memory (step (step (read-buttons) [:d])) 0xD328)154 (view-memory (step (step (read-buttons))) 0xD328)))156 (defn trace [state]157 (loop [program-counters []158 opcodes []]159 (let [frame-boundary?160 (com.aurellem.gb.Gb/tick)]161 (println (count opcodes))162 (if frame-boundary?163 [program-counters opcodes]164 (recur165 (conj program-counters166 (first (registers @current-state)))167 (conj opcodes168 (aget (memory @current-state)169 (PC @current-state))))))))171 (defn good-trace []172 (-> (mid-game) (tick) (IE! 0)173 (set-inv-mem [0x00 0x00 0X00 0x00])174 (PC! item-list-start)(print-interrupt)175 (info) (tick) (info) (tick) (info)))