Mercurial > vba-clojure
view clojure/com/aurellem/gb_driver.clj @ 124:f8dadd9478a5
fixed problem with mode transitions
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 17 Mar 2012 01:23:47 -0500 |
parents | bcb5c41626b4 |
children | 718abf3bec8a |
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)))131 (finally132 (update-state))))133 ([n]134 (play @current-state n)))136 (defn continue!137 ([state]138 (play state Integer/MAX_VALUE))139 ([]140 (continue! @current-state)))142 (defn play-moves143 ([moves [prev state]]144 (set-state! state)145 (dorun (map (fn [move] (step @current-state move)) moves))146 [(concat prev moves) @current-state]))148 ;;;;;;;;;;;151 ;;;;;;;;;;;;;;; CPU data153 (defn cpu-data [size arr-fn]154 (let [store (int-array size)]155 (fn get-data156 ([] (get-data @current-state))157 ([state]158 (set-state! state) (arr-fn store) store))))160 (defn write-cpu-data [size store-fn]161 (fn store-data162 ([state new-data]163 (set-state! state)164 (let [store (int-array new-data)]165 (assert (= size (count new-data)))166 (store-fn store)167 (update-state)))168 ([new-data]169 (store-data @current-state new-data))))172 (def memory173 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))175 (def ram176 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))178 (def rom179 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))181 (def working-ram182 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))184 (def video-ram185 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))187 (def registers188 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))190 (def write-memory!191 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))193 (def write-registers!194 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))196 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;198 (defmacro gen-get-set-register [name index]199 (let [name-bang (symbol (str name "!"))]200 `(do201 (defn ~name202 ~(str "Retrieve the " name " register from state, or "203 "from @current-state if state is absent.")204 ([state#]205 (nth (registers state#) ~index))206 ([]207 (~name @current-state)))208 (defn ~name-bang209 ~(str "Set the " name " register for state, or "210 "for @current-state if state is absent.")211 ([state# new-register#]212 (set-state! state#)213 (let [registers# (registers state#)]214 (aset registers# ~index new-register#)215 (Gb/writeRegisters registers#)216 (update-state)))217 ([new-register#]218 (~name-bang @current-state new-register#))))))220 ;; 16 bit registers221 (gen-get-set-register PC 0)222 (gen-get-set-register SP 1)223 (gen-get-set-register AF 2)224 (gen-get-set-register BC 3)225 (gen-get-set-register DE 4)226 (gen-get-set-register HL 5)227 (gen-get-set-register IFF 6)229 ;; 8 bit registers230 (gen-get-set-register DIV 7)231 (gen-get-set-register TIMA 8)232 (gen-get-set-register TMA 9)233 (gen-get-set-register IF 11)234 (gen-get-set-register LCDC 12)235 (gen-get-set-register STAT 13)236 (gen-get-set-register SCY 14)237 (gen-get-set-register SCX 15)238 (gen-get-set-register LY 16)239 (gen-get-set-register DMA 18)240 (gen-get-set-register WY 19)241 (gen-get-set-register WX 20)242 (gen-get-set-register VBK 21)243 (gen-get-set-register HDMA1 22)244 (gen-get-set-register HDMA2 23)245 (gen-get-set-register HDMA3 24)246 (gen-get-set-register HDMA4 25)247 (gen-get-set-register HDMA5 26)248 (gen-get-set-register SVBK 27)249 (gen-get-set-register IE 28)251 ;;;;;;;;;;;;;;;253 (defmacro defn-memo254 [& forms]255 (let [fun-name (first forms)]256 `(do257 (defn ~@forms)258 (alter-var-root (var ~fun-name) memoize))))