Mercurial > vba-clojure
view clojure/com/aurellem/gb_driver.clj @ 112:6fe33bb5ea22
saving progress
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 15 Mar 2012 17:29:49 -0500 |
parents | ad96e9464d6a |
children | bcb5c41626b4 |
line wrap: on
line source
1 (ns com.aurellem.gb-driver2 (:import com.aurellem.gb.Gb)3 (:import java.io.File)4 (:import org.apache.commons.io.FileUtils)5 (:import (java.nio IntBuffer ByteOrder)))7 ;; Savestates8 (defrecord SaveState [data])10 (def ^:dynamic *save-state-cache*11 (File. "/home/r/proj/pokemon-escape/save-states/"))13 (def current-state (atom nil))15 (defn state-cache-file [name]16 (File. *save-state-cache* (str name ".sav")))18 (defn write-state!19 ([^SaveState name]20 (write-state! @current-state name))21 ([^SaveState save ^String name]22 (let [buffer (:data save)23 bytes (byte-array (.limit buffer))24 dest (state-cache-file name)]25 (.get buffer bytes)26 (FileUtils/writeByteArrayToFile dest bytes)27 (.rewind buffer)28 dest)))30 (defn read-state [name]31 (let [save (state-cache-file name)]32 (if (.exists save)33 (let [buffer (Gb/saveBuffer)34 bytes (FileUtils/readFileToByteArray save)]35 (.put buffer bytes)36 (.flip buffer)37 (SaveState. buffer)))))38 ;;;;;;;;;;;;;;;;40 ;; Gameboy management41 (Gb/loadVBA)43 (def yellow-rom-image44 (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc"))46 (def yellow-save-file47 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))49 (def on? (atom nil))51 (defn shutdown! [] (Gb/shutdown) (reset! on? false))53 (defn restart! []54 (shutdown!)55 (.delete yellow-save-file)56 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))57 (reset! on? true))59 ;;; The first state!60 (defn gen-root! []61 (restart!)62 (let [state (SaveState. (Gb/saveState))]63 (write-state! state "root" ) state))65 (defn root []66 (if (.exists (state-cache-file "root"))67 (read-state "root")68 (gen-root!)))70 ;;;; Press Buttons72 (def button-code73 {;; main buttons74 :a 0x000175 :b 0x000277 ;; directional pad78 :r 0x001079 :l 0x002080 :u 0x004081 :d 0x008083 ;; meta buttons84 :select 0x000485 :start 0x000887 ;; pseudo-buttons88 :restart 0x0800 ; hard reset89 :listen -1 ; listen for user input90 })92 (defn button-mask [buttons]93 (reduce bit-or 0x0000 (map button-code buttons)))95 (defn set-state! [^SaveState state]96 (assert (:data state) "Not a valid state!")97 (if (not @on?) (restart!))98 (if (not= state @current-state)99 (do100 (Gb/loadState (:data state))101 (reset! current-state state))))103 (defn update-state []104 (reset! current-state105 (SaveState. (Gb/saveState))))107 (defn step108 ([^SaveState state buttons]109 (set-state! state)110 (Gb/step (button-mask buttons))111 (reset! current-state112 (SaveState. (Gb/saveState))))113 ([^SaveState state]114 (step state [:listen]))115 ([] (step (if @current-state @current-state (root)))))117 (defn tick118 ([] (tick @current-state))119 ([^SaveState state]120 (set-state! state)121 (Gb/tick)122 (update-state)))124 (defn play125 ([^SaveState state n]126 (try127 (set-state! state)128 (dorun (dotimes [_ n]129 (Thread/sleep 1)130 (Gb/step)))132 (finally133 (update-state))))134 ([n]135 (play @current-state n)))137 (defn continue!138 ([state]139 (play state Integer/MAX_VALUE))140 ([]141 (continue! @current-state)))143 (defn play-moves144 ([moves [prev state]]145 (set-state! state)146 (dorun (map (fn [move] (step @current-state move)) moves))147 [(concat prev moves) @current-state]))149 ;;;;;;;;;;;152 ;;;;;;;;;;;;;;; CPU data154 (defn cpu-data [size arr-fn]155 (let [store (int-array size)]156 (fn get-data157 ([] (get-data @current-state))158 ([state]159 (set-state! state) (arr-fn store) store))))161 (defn write-cpu-data [size store-fn]162 (fn store-data163 ([state new-data]164 (set-state! state)165 (let [store (int-array new-data)]166 (assert (= size (count new-data)))167 (store-fn store)168 (update-state)))169 ([new-data]170 (store-data @current-state new-data))))173 (def memory174 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))176 (def ram177 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))179 (def rom180 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))182 (def working-ram183 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))185 (def video-ram186 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))188 (def registers189 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))191 (def write-memory!192 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))194 (def write-registers!195 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))197 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;199 (defmacro gen-get-set-register [name index]200 (let [name-bang (symbol (str name "!"))]201 `(do202 (defn ~name203 ~(str "Retrieve the " name " register from state, or "204 "from @current-state if state is absent.")205 ([state#]206 (nth (registers state#) ~index))207 ([]208 (~name @current-state)))209 (defn ~name-bang210 ~(str "Set the " name " register for state, or "211 "for @current-state if state is absent.")212 ([state# new-register#]213 (set-state! state#)214 (let [registers# (registers state#)]215 (aset registers# ~index new-register#)216 (Gb/writeRegisters registers#)217 (update-state)))218 ([new-register#]219 (~name-bang @current-state new-register#))))))221 ;; 16 bit registers222 (gen-get-set-register PC 0)223 (gen-get-set-register SP 1)224 (gen-get-set-register AF 2)225 (gen-get-set-register BC 3)226 (gen-get-set-register DE 4)227 (gen-get-set-register HL 5)228 (gen-get-set-register IFF 6)230 ;; 8 bit registers231 (gen-get-set-register DIV 7)232 (gen-get-set-register TIMA 8)233 (gen-get-set-register TMA 9)234 (gen-get-set-register IF 11)235 (gen-get-set-register LCDC 12)236 (gen-get-set-register STAT 13)237 (gen-get-set-register SCY 14)238 (gen-get-set-register SCX 15)239 (gen-get-set-register LY 16)240 (gen-get-set-register DMA 18)241 (gen-get-set-register WY 19)242 (gen-get-set-register WX 20)243 (gen-get-set-register VBK 21)244 (gen-get-set-register HDMA1 22)245 (gen-get-set-register HDMA2 23)246 (gen-get-set-register HDMA3 24)247 (gen-get-set-register HDMA4 25)248 (gen-get-set-register HDMA5 26)249 (gen-get-set-register SVBK 27)250 (gen-get-set-register IE 28)252 ;;;;;;;;;;;;;;;254 (defmacro defn-memo255 [& forms]256 (let [fun-name (first forms)]257 `(do258 (defn ~@forms)259 (alter-var-root (var ~fun-name) memoize))))