annotate clojure/com/aurellem/gb/gb_driver.clj @ 274:210b465e4720

refactored walking code from buy-initial-objects to its own function.
author Robert McIntyre <rlm@mit.edu>
date Tue, 27 Mar 2012 11:30:58 -0500
parents d5dddf33543c
children d263df762c59
rev   line source
rlm@145 1 (ns com.aurellem.gb.gb-driver
rlm@145 2 (:import com.aurellem.gb.Gb)
rlm@145 3 (:import java.io.File)
rlm@145 4 (:import org.apache.commons.io.FileUtils)
rlm@145 5 (:import (java.nio IntBuffer ByteOrder)))
rlm@145 6
rlm@145 7 ;; Savestates
ocsenave@227 8
rlm@145 9 (defrecord SaveState [data])
rlm@145 10
rlm@145 11 (def user-home (File. (System/getProperty "user.home")))
rlm@145 12
rlm@145 13 (def ^:dynamic *save-state-cache*
rlm@145 14 (File. user-home "proj/vba-clojure/save-states/"))
rlm@145 15
rlm@145 16 (def current-state (atom nil))
rlm@145 17
rlm@145 18 (defn state-cache-file [name]
rlm@145 19 (File. *save-state-cache* (str name ".sav")))
rlm@145 20
rlm@145 21 (defn write-state!
rlm@145 22 ([^SaveState name]
rlm@145 23 (write-state! @current-state name))
rlm@145 24 ([^SaveState save ^String name]
rlm@145 25 (let [buffer (:data save)
rlm@145 26 bytes (byte-array (.limit buffer))
rlm@145 27 dest (state-cache-file name)]
rlm@145 28 (.get buffer bytes)
rlm@145 29 (FileUtils/writeByteArrayToFile dest bytes)
rlm@145 30 (.rewind buffer)
rlm@145 31 dest)))
rlm@145 32
rlm@145 33 (defn read-state [name]
rlm@145 34 (let [save (state-cache-file name)]
rlm@154 35 (assert (.exists save))
rlm@154 36 (let [buffer (Gb/saveBuffer)
rlm@154 37 bytes (FileUtils/readFileToByteArray save)]
rlm@154 38 (.put buffer bytes)
rlm@154 39 (.flip buffer)
rlm@154 40 (SaveState. buffer))))
rlm@145 41 ;;;;;;;;;;;;;;;;
rlm@145 42
rlm@145 43 ;; Gameboy management
rlm@145 44 (Gb/loadVBA)
rlm@145 45
rlm@145 46 (def yellow-rom-image
rlm@145 47 (File. user-home "proj/pokemon-escape/roms/yellow.gbc"))
rlm@145 48
rlm@145 49 (def yellow-save-file
rlm@145 50 (File. user-home "proj/pokemon-escape/roms/yellow.sav"))
rlm@145 51
rlm@145 52 (def on? (atom nil))
rlm@145 53
rlm@145 54 (defn shutdown! [] (Gb/shutdown) (reset! on? false))
rlm@145 55
rlm@145 56 (defn restart! []
rlm@145 57 (shutdown!)
rlm@145 58 (.delete yellow-save-file)
rlm@145 59 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))
rlm@145 60 (reset! on? true))
rlm@145 61
rlm@145 62 ;;; The first state!
rlm@145 63 (defn gen-root! []
rlm@145 64 (restart!)
rlm@145 65 (let [state (SaveState. (Gb/saveState))]
rlm@145 66 (write-state! state "root" ) state))
rlm@145 67
rlm@145 68 (defn root []
rlm@145 69 (if (.exists (state-cache-file "root"))
rlm@145 70 (read-state "root")
rlm@145 71 (gen-root!)))
rlm@145 72
rlm@145 73 ;;;; Press Buttons
rlm@145 74
rlm@145 75 (def button-code
rlm@145 76 {;; main buttons
rlm@145 77 :a 0x0001
rlm@145 78 :b 0x0002
rlm@145 79
rlm@145 80 ;; directional pad
rlm@145 81 :r 0x0010
rlm@145 82 :l 0x0020
rlm@145 83 :u 0x0040
rlm@145 84 :d 0x0080
rlm@145 85
rlm@145 86 ;; meta buttons
rlm@145 87 :select 0x0004
rlm@145 88 :start 0x0008
rlm@145 89
rlm@145 90 ;; pseudo-buttons
rlm@145 91 :restart 0x0800 ; hard reset
rlm@145 92 :listen -1 ; listen for user input
rlm@145 93 })
rlm@145 94
rlm@145 95 (defn button-mask [buttons]
rlm@145 96 (reduce bit-or 0x0000 (map button-code buttons)))
rlm@145 97
rlm@145 98 (defn set-state! [^SaveState state]
rlm@145 99 (assert (:data state) "Not a valid state!")
rlm@145 100 (if (not @on?) (restart!))
rlm@145 101 (if (not= state @current-state)
rlm@145 102 (do
rlm@145 103 (Gb/loadState (:data state))
rlm@145 104 (reset! current-state state))))
rlm@145 105
rlm@145 106 (defn update-state []
rlm@145 107 (reset! current-state
rlm@145 108 (SaveState. (Gb/saveState))))
rlm@145 109
rlm@145 110 (defn step
rlm@145 111 ([^SaveState state buttons]
rlm@145 112 (set-state! state)
rlm@145 113 (Gb/step (button-mask buttons))
rlm@145 114 (reset! current-state
rlm@145 115 (SaveState. (Gb/saveState))))
rlm@145 116 ([^SaveState state]
rlm@145 117 (step state [:listen]))
rlm@145 118 ([] (step (if @current-state @current-state (root)))))
rlm@145 119
rlm@145 120 (defn tick
rlm@145 121 ([] (tick @current-state))
rlm@145 122 ([^SaveState state]
rlm@145 123 (set-state! state)
rlm@145 124 (Gb/tick)
rlm@145 125 (update-state)))
rlm@145 126
rlm@145 127 (defn play
rlm@145 128 ([^SaveState state n]
rlm@145 129 (try
rlm@145 130 (set-state! state)
rlm@145 131 (dorun (dotimes [_ n]
rlm@145 132 (Thread/sleep 1)
rlm@145 133 (Gb/step)))
rlm@145 134 (finally
rlm@145 135 (update-state))))
rlm@145 136 ([n]
rlm@145 137 (play @current-state n)))
rlm@145 138
rlm@145 139 (defn continue!
rlm@145 140 ([state]
rlm@145 141 (play state Integer/MAX_VALUE))
rlm@145 142 ([]
rlm@145 143 (continue! @current-state)))
rlm@145 144
rlm@154 145 (defn run-moves [state moves]
rlm@154 146 (set-state! state)
rlm@154 147 (dorun (map #(Gb/step (button-mask %))
rlm@154 148 moves))
rlm@154 149 (update-state))
rlm@154 150
rlm@145 151 (defn play-moves
rlm@145 152 ([moves [prev state]]
rlm@145 153 (set-state! state)
rlm@145 154 (dorun (map (fn [move] (step @current-state move)) moves))
rlm@145 155 [(concat prev moves) @current-state]))
rlm@145 156
rlm@145 157 ;;;;;;;;;;;
rlm@145 158
rlm@145 159
rlm@145 160 ;;;;;;;;;;;;;;; CPU data
rlm@145 161
rlm@145 162 (defn cpu-data [size arr-fn]
rlm@145 163 (let [store (int-array size)]
rlm@145 164 (fn get-data
rlm@145 165 ([] (get-data @current-state))
rlm@145 166 ([state]
rlm@145 167 (set-state! state) (arr-fn store) store))))
rlm@145 168
rlm@145 169 (defn write-cpu-data [size store-fn]
rlm@145 170 (fn store-data
rlm@145 171 ([state new-data]
rlm@145 172 (set-state! state)
rlm@145 173 (let [store (int-array new-data)]
rlm@145 174 (assert (= size (count new-data)))
rlm@145 175 (store-fn store)
rlm@145 176 (update-state)))
rlm@145 177 ([new-data]
rlm@145 178 (store-data @current-state new-data))))
rlm@145 179
rlm@145 180
rlm@145 181 (def memory
rlm@145 182 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
rlm@145 183
rlm@145 184 (def ram
rlm@145 185 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))
rlm@145 186
rlm@145 187 (def rom
rlm@145 188 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))
rlm@145 189
rlm@145 190 (def working-ram
rlm@145 191 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
rlm@145 192
rlm@145 193 (def video-ram
rlm@145 194 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
rlm@145 195
rlm@145 196 (def registers
rlm@145 197 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
rlm@145 198
rlm@145 199 (def write-memory!
rlm@145 200 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))
rlm@145 201
rlm@145 202 (def write-registers!
rlm@145 203 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))
rlm@145 204
rlm@191 205 (def write-rom!
rlm@191 206 (write-cpu-data Gb/ROM_SIZE #(Gb/writeROM %)))
rlm@191 207
rlm@145 208 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@145 209
rlm@145 210 (defmacro gen-get-set-register [name index]
rlm@145 211 (let [name-bang (symbol (str name "!"))]
rlm@145 212 `(do
rlm@145 213 (defn ~name
rlm@145 214 ~(str "Retrieve the " name " register from state, or "
rlm@145 215 "from @current-state if state is absent.")
rlm@145 216 ([state#]
rlm@145 217 (nth (registers state#) ~index))
rlm@145 218 ([]
rlm@145 219 (~name @current-state)))
rlm@145 220 (defn ~name-bang
rlm@145 221 ~(str "Set the " name " register for state, or "
rlm@145 222 "for @current-state if state is absent.")
rlm@145 223 ([state# new-register#]
rlm@145 224 (set-state! state#)
rlm@145 225 (let [registers# (registers state#)]
rlm@145 226 (aset registers# ~index new-register#)
rlm@145 227 (Gb/writeRegisters registers#)
rlm@145 228 (update-state)))
rlm@145 229 ([new-register#]
rlm@145 230 (~name-bang @current-state new-register#))))))
rlm@145 231
rlm@145 232 ;; 16 bit registers
rlm@145 233 (gen-get-set-register PC 0)
rlm@145 234 (gen-get-set-register SP 1)
rlm@145 235 (gen-get-set-register AF 2)
rlm@145 236 (gen-get-set-register BC 3)
rlm@145 237 (gen-get-set-register DE 4)
rlm@145 238 (gen-get-set-register HL 5)
rlm@145 239 (gen-get-set-register IFF 6)
rlm@145 240
rlm@145 241 ;; 8 bit registers
rlm@145 242 (gen-get-set-register DIV 7)
rlm@145 243 (gen-get-set-register TIMA 8)
rlm@145 244 (gen-get-set-register TMA 9)
rlm@145 245 (gen-get-set-register IF 11)
rlm@145 246 (gen-get-set-register LCDC 12)
rlm@145 247 (gen-get-set-register STAT 13)
rlm@145 248 (gen-get-set-register SCY 14)
rlm@145 249 (gen-get-set-register SCX 15)
rlm@145 250 (gen-get-set-register LY 16)
rlm@145 251 (gen-get-set-register DMA 18)
rlm@145 252 (gen-get-set-register WY 19)
rlm@145 253 (gen-get-set-register WX 20)
rlm@145 254 (gen-get-set-register VBK 21)
rlm@145 255 (gen-get-set-register HDMA1 22)
rlm@145 256 (gen-get-set-register HDMA2 23)
rlm@145 257 (gen-get-set-register HDMA3 24)
rlm@145 258 (gen-get-set-register HDMA4 25)
rlm@145 259 (gen-get-set-register HDMA5 26)
rlm@145 260 (gen-get-set-register SVBK 27)
rlm@145 261 (gen-get-set-register IE 28)
rlm@145 262
rlm@145 263 ;;;;;;;;;;;;;;;
rlm@145 264
rlm@145 265 (defmacro defn-memo
rlm@145 266 [& forms]
rlm@145 267 (let [fun-name (first forms)]
rlm@145 268 `(do
rlm@145 269 (defn ~@forms)
rlm@145 270 (alter-var-root (var ~fun-name) memoize))))
rlm@145 271
ocsenave@227 272
ocsenave@227 273
ocsenave@227 274 (def original-rom (rom (root)))