annotate clojure/com/aurellem/assembly.clj @ 111:8be7ce890184

now I have an assembly program that can read button presses!
author Robert McIntyre <rlm@mit.edu>
date Thu, 15 Mar 2012 17:20:55 -0500
parents f9dee79b2215
children 6fe33bb5ea22
rev   line source
rlm@105 1 (ns com.aurellem.assembly
rlm@105 2 (:use (com.aurellem gb-driver vbm title items))
rlm@105 3 (:import [com.aurellem.gb_driver SaveState]))
rlm@105 4
rlm@106 5 (defn mid-game []
rlm@106 6 (read-state "mid-game"))
rlm@105 7
rlm@105 8 (defn inject-assembly
rlm@107 9 ([^SaveState state
rlm@105 10 program-counter registers
rlm@105 11 assembly-code]
rlm@105 12 (let [scratch-memory (memory state)]
rlm@105 13 ;; inject assembly code
rlm@105 14 (dorun (map (fn [index val]
rlm@105 15 (aset scratch-memory index val))
rlm@105 16 (range program-counter
rlm@105 17 (+ program-counter (count assembly-code)))
rlm@105 18 assembly-code))
rlm@106 19 (-> state
rlm@106 20 (write-memory! scratch-memory)
rlm@106 21 (write-registers! registers)
rlm@106 22 (PC! program-counter))))
rlm@107 23 ;;([program-counter]
rlm@107 24 ;; (fn [^SaveState state registers assembly-code]
rlm@107 25 ;; (inject-assembly state program-counter registers assembly-code)))
rlm@107 26 )
rlm@107 27
rlm@107 28
rlm@107 29 ;;(def inject-assembly-item
rlm@107 30 ;; (inject-assembly (inc item-list-start)))
rlm@105 31
rlm@105 32 (defn inject-item-assembly
rlm@105 33 ([^SaveState state assembly-code]
rlm@105 34 (inject-assembly state (inc item-list-start)
rlm@105 35 (registers state)
rlm@105 36 assembly-code))
rlm@105 37 ([assembly-code]
rlm@105 38 (inject-item-assembly @current-state assembly-code)))
rlm@105 39
rlm@105 40 (defn info
rlm@105 41 ([^SaveState state]
rlm@107 42 (println (format "PC: 0x%04X" (PC state)))
rlm@106 43 (println "Instruction:"
rlm@106 44 (format "0x%02X" (aget (memory state) (PC state))))
rlm@105 45 state))
rlm@105 46
rlm@107 47 (defn print-interrupt
rlm@107 48 [^SaveState state]
rlm@107 49 (println (format "IE: %d" (IE state)))
rlm@107 50 state)
rlm@107 51
rlm@105 52 (defn run-assembly
rlm@105 53 ([info-fn assembly n]
rlm@105 54 (let [final-state
rlm@105 55 (reduce (fn [state _]
rlm@105 56 (tick (info-fn state)))
rlm@107 57 (inject-item-assembly
rlm@107 58 (mid-game) assembly)
rlm@105 59 (range n))]
rlm@105 60 final-state))
rlm@105 61 ([assembly n]
rlm@105 62 (run-assembly info assembly n)))
rlm@107 63
rlm@107 64
rlm@107 65 (def buttons-port 0xFF00)
rlm@107 66
rlm@107 67
rlm@107 68 (defn A [state]
rlm@107 69 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
rlm@107 70
rlm@110 71 (defn binary-str [num]
rlm@110 72 (format "%08d"
rlm@110 73 (Integer/parseInt
rlm@110 74 (Integer/toBinaryString num) 10)))
rlm@110 75
rlm@110 76 (defn view-register [state name reg-fn]
rlm@110 77 (println (format "%s: %s" name
rlm@110 78 (binary-str (reg-fn state))))
rlm@110 79 state)
rlm@110 80
rlm@110 81
rlm@107 82 (defn view-memory [state mem]
rlm@110 83 (println (format "mem 0x%04X = %s" mem
rlm@110 84 (binary-str (aget (memory state) mem))))
rlm@107 85 state)
rlm@107 86
rlm@107 87 (defn read-buttons []
rlm@110 88 (-> (tick (mid-game))
rlm@110 89 (IE! 0) ; disable interrupts
rlm@110 90 (inject-item-assembly
rlm@110 91 (concat
rlm@110 92 ;; write 00010000 to 0xFF00 to select joypad
rlm@110 93 [0x18 ;D31D ; jump over
rlm@110 94 0x01 ;D31E ; the next 8 bits
rlm@111 95 (Integer/parseInt "00100000" 2) ;D31F data section
rlm@111 96
rlm@111 97 0xFA ;D320 ; load (D31F) into A
rlm@111 98 0x1F ;D321 -->
rlm@111 99 0xD3 ;D322 --> D31F
rlm@107 100
rlm@111 101 0xEA ;D323 ; load (A), which is
rlm@111 102 0x00 ;D324 --> ; 00010000, into FF00
rlm@111 103 0xFF ;D325 --> FF00
rlm@111 104
rlm@111 105 0x18 ;D326 ; this is the place where
rlm@111 106 0x01 ;D327 ; we will store whether
rlm@111 107 0x00 ;D328 ; "down" is pressed.
rlm@107 108
rlm@111 109 0xFA ;D329 ; (FF00) -> A
rlm@111 110 0x00 ;D32A
rlm@111 111 0xFF ;D32B
rlm@110 112
rlm@111 113 0xCB ;D32C ; Test whether "down"
rlm@111 114 0x5F ;D32D ; is pressed.
rlm@111 115
rlm@111 116 0x28 ;D32E ; if down is pressed,
rlm@111 117 0x03 ;D32F ; skip the next section
rlm@111 118 ; of code.
rlm@111 119 ;; down-is-not-pressed
rlm@111 120 0xC3 ;D330
rlm@111 121 0x1D ;D331 ; return to beginning
rlm@111 122 0xD3 ;D332
rlm@111 123
rlm@111 124 ;; down-is-pressed
rlm@111 125 0xEA ;D334 ; write A to D328 if
rlm@111 126 0x28 ;D335 ; "down" was pressed
rlm@111 127 0xD3 ;D336
rlm@111 128
rlm@111 129 0xC3 ;D330
rlm@111 130 0x1D ;D331 ; return to beginning
rlm@111 131 0xD3 ;D332
rlm@110 132 ]
rlm@110 133
rlm@111 134 []))))
rlm@111 135
rlm@111 136
rlm@111 137 (defn run-a-bit []
rlm@111 138 (-> (read-buttons)
rlm@110 139 (info)
rlm@110 140 (tick) ;; skip over data section
rlm@110 141 (info)
rlm@110 142 (view-register "Register A" A)
rlm@110 143 (tick) ;; load-data into A
rlm@110 144 (view-register "Register A" A)
rlm@110 145 (info)
rlm@110 146 (view-memory 0xFF00)
rlm@110 147 (tick) ;; load A into 0xFF00
rlm@110 148 (view-memory 0xFF00)
rlm@111 149 (info)
rlm@111 150 (tick)
rlm@111 151 (info)
rlm@111 152 (tick)
rlm@111 153 (info)
rlm@111 154 (tick)
rlm@111 155 (info)
rlm@111 156 (tick)
rlm@111 157 (info)
rlm@111 158 (tick)
rlm@111 159 (info)
rlm@111 160 (tick)
rlm@111 161 (print-inventory)))
rlm@111 162
rlm@111 163
rlm@111 164 (comment
rlm@111 165 (view-memory (step (step (read-buttons) [:d])) 0xD328)
rlm@111 166 ;;mem 0xD328 = 11100111
rlm@111 167 (view-memory (step (step (read-buttons))) 0xD328)
rlm@111 168 ;;mem 0xD328 = 00000000
rlm@111 169 )
rlm@111 170
rlm@107 171 (defn trace [state]
rlm@107 172 (loop [program-counters []
rlm@107 173 opcodes []]
rlm@107 174 (let [frame-boundary?
rlm@107 175 (com.aurellem.gb.Gb/tick)]
rlm@107 176 (println (count opcodes))
rlm@107 177 (if frame-boundary?
rlm@107 178 [program-counters opcodes]
rlm@107 179 (recur
rlm@107 180 (conj program-counters
rlm@107 181 (first (registers @current-state)))
rlm@107 182 (conj opcodes
rlm@107 183 (aget (memory @current-state)
rlm@107 184 (PC @current-state))))))))
rlm@107 185
rlm@107 186
rlm@107 187 (defn good-trace []
rlm@107 188 (-> (mid-game) (tick) (IE! 0)
rlm@107 189 (set-inv-mem [0x00 0x00 0X00 0x00])
rlm@107 190 (PC! item-list-start)(print-interrupt)
rlm@107 191 (info) (tick) (info) (tick) (info)))