annotate clojure/com/aurellem/gb/gb_driver.clj @ 542:614eddc5b38c

working on generating first major video.
author Robert McIntyre <rlm@mit.edu>
date Mon, 25 Jun 2012 16:08:56 -0500
parents 2620d6318e8d
children
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@526 4 (:import javax.sound.sampled.AudioFormat)
rlm@145 5 (:import org.apache.commons.io.FileUtils)
rlm@145 6 (:import (java.nio IntBuffer ByteOrder)))
rlm@145 7
rlm@145 8 ;; Savestates
ocsenave@227 9
rlm@145 10 (defrecord SaveState [data])
rlm@145 11
rlm@145 12 (def user-home (File. (System/getProperty "user.home")))
rlm@145 13
rlm@145 14 (def ^:dynamic *save-state-cache*
rlm@145 15 (File. user-home "proj/vba-clojure/save-states/"))
rlm@145 16
rlm@145 17 (def current-state (atom nil))
rlm@145 18
rlm@145 19 (defn state-cache-file [name]
rlm@145 20 (File. *save-state-cache* (str name ".sav")))
rlm@145 21
rlm@145 22 (defn write-state!
rlm@145 23 ([^SaveState name]
rlm@145 24 (write-state! @current-state name))
rlm@145 25 ([^SaveState save ^String name]
rlm@145 26 (let [buffer (:data save)
rlm@145 27 bytes (byte-array (.limit buffer))
rlm@145 28 dest (state-cache-file name)]
rlm@145 29 (.get buffer bytes)
rlm@145 30 (FileUtils/writeByteArrayToFile dest bytes)
rlm@145 31 (.rewind buffer)
rlm@145 32 dest)))
rlm@145 33
rlm@145 34 (defn read-state [name]
rlm@145 35 (let [save (state-cache-file name)]
rlm@154 36 (assert (.exists save))
rlm@154 37 (let [buffer (Gb/saveBuffer)
rlm@154 38 bytes (FileUtils/readFileToByteArray save)]
rlm@154 39 (.put buffer bytes)
rlm@154 40 (.flip buffer)
rlm@154 41 (SaveState. buffer))))
rlm@145 42 ;;;;;;;;;;;;;;;;
rlm@145 43
rlm@496 44
rlm@145 45 ;; Gameboy management
rlm@145 46 (Gb/loadVBA)
rlm@145 47
rlm@145 48 (def yellow-rom-image
rlm@145 49 (File. user-home "proj/pokemon-escape/roms/yellow.gbc"))
rlm@145 50
rlm@145 51 (def yellow-save-file
rlm@145 52 (File. user-home "proj/pokemon-escape/roms/yellow.sav"))
rlm@145 53
rlm@145 54 (def on? (atom nil))
rlm@145 55
rlm@145 56 (defn shutdown! [] (Gb/shutdown) (reset! on? false))
rlm@145 57
rlm@145 58 (defn restart! []
rlm@145 59 (shutdown!)
rlm@145 60 (.delete yellow-save-file)
rlm@145 61 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))
rlm@145 62 (reset! on? true))
rlm@145 63
rlm@145 64 ;;; The first state!
rlm@145 65 (defn gen-root! []
rlm@145 66 (restart!)
rlm@145 67 (let [state (SaveState. (Gb/saveState))]
rlm@145 68 (write-state! state "root" ) state))
rlm@145 69
rlm@145 70 (defn root []
rlm@145 71 (if (.exists (state-cache-file "root"))
rlm@145 72 (read-state "root")
rlm@145 73 (gen-root!)))
rlm@145 74
rlm@145 75 ;;;; Press Buttons
rlm@145 76
rlm@145 77 (def button-code
rlm@145 78 {;; main buttons
rlm@145 79 :a 0x0001
rlm@145 80 :b 0x0002
rlm@145 81
rlm@145 82 ;; directional pad
rlm@145 83 :r 0x0010
rlm@145 84 :l 0x0020
rlm@145 85 :u 0x0040
rlm@145 86 :d 0x0080
rlm@145 87
rlm@145 88 ;; meta buttons
rlm@145 89 :select 0x0004
rlm@145 90 :start 0x0008
rlm@145 91
rlm@145 92 ;; pseudo-buttons
rlm@145 93 :restart 0x0800 ; hard reset
rlm@145 94 :listen -1 ; listen for user input
rlm@145 95 })
rlm@145 96
rlm@145 97 (defn button-mask [buttons]
rlm@145 98 (reduce bit-or 0x0000 (map button-code buttons)))
rlm@145 99
rlm@145 100 (defn set-state! [^SaveState state]
rlm@145 101 (assert (:data state) "Not a valid state!")
rlm@145 102 (if (not @on?) (restart!))
rlm@145 103 (if (not= state @current-state)
rlm@145 104 (do
rlm@145 105 (Gb/loadState (:data state))
rlm@145 106 (reset! current-state state))))
rlm@145 107
rlm@145 108 (defn update-state []
rlm@145 109 (reset! current-state
rlm@145 110 (SaveState. (Gb/saveState))))
rlm@145 111
rlm@145 112 (defn step
rlm@145 113 ([^SaveState state buttons]
rlm@145 114 (set-state! state)
rlm@145 115 (Gb/step (button-mask buttons))
rlm@145 116 (reset! current-state
rlm@145 117 (SaveState. (Gb/saveState))))
rlm@145 118 ([^SaveState state]
rlm@145 119 (step state [:listen]))
rlm@145 120 ([] (step (if @current-state @current-state (root)))))
rlm@145 121
rlm@145 122 (defn tick
rlm@145 123 ([] (tick @current-state))
rlm@145 124 ([^SaveState state]
rlm@145 125 (set-state! state)
rlm@145 126 (Gb/tick)
rlm@145 127 (update-state)))
rlm@145 128
rlm@145 129 (defn play
rlm@145 130 ([^SaveState state n]
rlm@145 131 (try
rlm@145 132 (set-state! state)
rlm@145 133 (dorun (dotimes [_ n]
rlm@145 134 (Thread/sleep 1)
rlm@145 135 (Gb/step)))
rlm@145 136 (finally
rlm@145 137 (update-state))))
rlm@145 138 ([n]
rlm@145 139 (play @current-state n)))
rlm@145 140
rlm@145 141 (defn continue!
rlm@145 142 ([state]
rlm@145 143 (play state Integer/MAX_VALUE))
rlm@145 144 ([]
rlm@145 145 (continue! @current-state)))
rlm@145 146
rlm@326 147 (defn run-moves [state moves]
rlm@154 148 (set-state! state)
rlm@154 149 (dorun (map #(Gb/step (button-mask %))
rlm@154 150 moves))
rlm@154 151 (update-state))
rlm@154 152
rlm@145 153 (defn play-moves
rlm@145 154 ([moves [prev state]]
rlm@145 155 (set-state! state)
rlm@145 156 (dorun (map (fn [move] (step @current-state move)) moves))
rlm@145 157 [(concat prev moves) @current-state]))
rlm@522 158
rlm@522 159 (defn accurate-memory
rlm@522 160 ([^SaveState state address]
rlm@522 161 (set-state! state)
rlm@522 162 (Gb/readMemory address))
rlm@522 163 ([address]
rlm@522 164 (accurate-memory @current-state address)))
rlm@145 165
rlm@145 166 ;;;;;;;;;;;
rlm@145 167
rlm@496 168 ;; Get Screen Pixels, Save Screenshot
rlm@496 169
rlm@496 170 (defn write-png!
rlm@496 171 ([^SaveState state ^File target]
rlm@496 172 (set-state! state)
rlm@496 173 (Gb/nwritePNG (.getCanonicalPath target)))
rlm@496 174 ([^File target]
rlm@496 175 (write-png! @current-state target)))
rlm@496 176
rlm@145 177 ;;;;;;;;;;;;;;; CPU data
rlm@145 178
rlm@145 179 (defn cpu-data [size arr-fn]
rlm@145 180 (let [store (int-array size)]
rlm@145 181 (fn get-data
rlm@145 182 ([] (get-data @current-state))
rlm@145 183 ([state]
rlm@145 184 (set-state! state) (arr-fn store) store))))
rlm@145 185
rlm@145 186 (defn write-cpu-data [size store-fn]
rlm@145 187 (fn store-data
rlm@145 188 ([state new-data]
rlm@145 189 (set-state! state)
rlm@145 190 (let [store (int-array new-data)]
rlm@145 191 (assert (= size (count new-data)))
rlm@145 192 (store-fn store)
rlm@145 193 (update-state)))
rlm@145 194 ([new-data]
rlm@145 195 (store-data @current-state new-data))))
rlm@145 196
rlm@526 197 (def gb-sound-format
rlm@526 198 "44100 hertz, linear PCM, 2 channels with 16 bits per sample."
rlm@526 199 (AudioFormat. 44100 16 2 true false))
rlm@526 200
rlm@526 201 (let [store (byte-array Gb/MAX_SOUND_BYTES)]
rlm@526 202 (defn sound-bytes
rlm@526 203 "Returns a byte array containting the sound samples
rlm@526 204 generated this step."
rlm@526 205 ([](sound-bytes @current-state))
rlm@524 206 ([state]
rlm@524 207 (set-state! state)
rlm@524 208 (Gb/getFrameSound store)
rlm@526 209 (let [actual-bytes (* 2 (Gb/getSoundFrameWritten))]
rlm@526 210 (Gb/setSoundFrameWritten 0)
rlm@526 211 (byte-array (take actual-bytes store))))))
rlm@525 212
rlm@145 213 (def memory
rlm@145 214 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))
rlm@145 215
rlm@145 216 (def ram
rlm@145 217 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))
rlm@145 218
rlm@145 219 (def rom
rlm@145 220 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))
rlm@145 221
rlm@145 222 (def working-ram
rlm@145 223 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
rlm@145 224
rlm@145 225 (def video-ram
rlm@145 226 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
rlm@145 227
rlm@145 228 (def registers
rlm@145 229 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
rlm@145 230
rlm@496 231 (def pixels
rlm@496 232 (cpu-data (* Gb/DISPLAY_WIDTH Gb/DISPLAY_HEIGHT)
rlm@496 233 #(Gb/getPixels %)))
rlm@496 234
rlm@145 235 (def write-memory!
rlm@145 236 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))
rlm@145 237
rlm@145 238 (def write-registers!
rlm@145 239 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))
rlm@145 240
rlm@191 241 (def write-rom!
rlm@191 242 (write-cpu-data Gb/ROM_SIZE #(Gb/writeROM %)))
rlm@191 243
rlm@145 244 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;
rlm@145 245
rlm@145 246 (defmacro gen-get-set-register [name index]
rlm@145 247 (let [name-bang (symbol (str name "!"))]
rlm@145 248 `(do
rlm@145 249 (defn ~name
rlm@145 250 ~(str "Retrieve the " name " register from state, or "
rlm@145 251 "from @current-state if state is absent.")
rlm@145 252 ([state#]
rlm@145 253 (nth (registers state#) ~index))
rlm@145 254 ([]
rlm@145 255 (~name @current-state)))
rlm@145 256 (defn ~name-bang
rlm@145 257 ~(str "Set the " name " register for state, or "
rlm@145 258 "for @current-state if state is absent.")
rlm@145 259 ([state# new-register#]
rlm@145 260 (set-state! state#)
rlm@145 261 (let [registers# (registers state#)]
rlm@145 262 (aset registers# ~index new-register#)
rlm@145 263 (Gb/writeRegisters registers#)
rlm@145 264 (update-state)))
rlm@145 265 ([new-register#]
rlm@145 266 (~name-bang @current-state new-register#))))))
rlm@145 267
rlm@145 268 ;; 16 bit registers
rlm@145 269 (gen-get-set-register PC 0)
rlm@145 270 (gen-get-set-register SP 1)
rlm@145 271 (gen-get-set-register AF 2)
rlm@145 272 (gen-get-set-register BC 3)
rlm@145 273 (gen-get-set-register DE 4)
rlm@145 274 (gen-get-set-register HL 5)
rlm@145 275 (gen-get-set-register IFF 6)
rlm@145 276
rlm@145 277 ;; 8 bit registers
rlm@145 278 (gen-get-set-register DIV 7)
rlm@145 279 (gen-get-set-register TIMA 8)
rlm@145 280 (gen-get-set-register TMA 9)
rlm@145 281 (gen-get-set-register IF 11)
rlm@145 282 (gen-get-set-register LCDC 12)
rlm@145 283 (gen-get-set-register STAT 13)
rlm@145 284 (gen-get-set-register SCY 14)
rlm@145 285 (gen-get-set-register SCX 15)
rlm@145 286 (gen-get-set-register LY 16)
rlm@145 287 (gen-get-set-register DMA 18)
rlm@145 288 (gen-get-set-register WY 19)
rlm@145 289 (gen-get-set-register WX 20)
rlm@145 290 (gen-get-set-register VBK 21)
rlm@145 291 (gen-get-set-register HDMA1 22)
rlm@145 292 (gen-get-set-register HDMA2 23)
rlm@145 293 (gen-get-set-register HDMA3 24)
rlm@145 294 (gen-get-set-register HDMA4 25)
rlm@145 295 (gen-get-set-register HDMA5 26)
rlm@145 296 (gen-get-set-register SVBK 27)
rlm@145 297 (gen-get-set-register IE 28)
rlm@145 298
rlm@145 299 ;;;;;;;;;;;;;;;
rlm@145 300
rlm@145 301 (defmacro defn-memo
rlm@145 302 [& forms]
rlm@145 303 (let [fun-name (first forms)]
rlm@145 304 `(do
rlm@145 305 (defn ~@forms)
rlm@145 306 (alter-var-root (var ~fun-name) memoize))))
rlm@145 307
rlm@488 308 (def original-rom (rom (root)))
ocsenave@227 309
rlm@489 310
rlm@489 311 ;; RGB colors in an image are not the same as those in a GameBoy, so I
rlm@489 312 ;; need to convert them. Fortunately, this code is already written
rlm@489 313 ;; for me in this C-code from the public domain hi-color converter by
rlm@489 314 ;; Glen Cook, Jeff Frohwein, and Rob Jones.
rlm@488 315 (defn rgb->gb-rb [[r g b :as color]]
rlm@488 316 (let [store (int-array 3)]
rlm@488 317 (Gb/translateRGB (int-array color) store)
rlm@524 318 (vec store)))
rlm@524 319