annotate clojure/com/aurellem/gb_driver.clj @ 141:d125a294f9d8

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