Mercurial > vba-clojure
view clojure/com/aurellem/gb_driver.clj @ 136:ffeeabae7dcd
minor tweaking; incremental results: 91 -> 58
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Sun, 18 Mar 2012 05:53:25 -0500 |
parents | 718abf3bec8a |
children | d125a294f9d8 |
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 user-home (File. (System/getProperty "user.home")))12 (def ^:dynamic *save-state-cache*13 (File. user-home "proj/pokemon-escape/save-states/"))15 (def current-state (atom nil))17 (defn state-cache-file [name]18 (File. *save-state-cache* (str name ".sav")))20 (defn write-state!21 ([^SaveState name]22 (write-state! @current-state name))23 ([^SaveState save ^String name]24 (let [buffer (:data save)25 bytes (byte-array (.limit buffer))26 dest (state-cache-file name)]27 (.get buffer bytes)28 (FileUtils/writeByteArrayToFile dest bytes)29 (.rewind buffer)30 dest)))32 (defn read-state [name]33 (let [save (state-cache-file name)]34 (if (.exists save)35 (let [buffer (Gb/saveBuffer)36 bytes (FileUtils/readFileToByteArray save)]37 (.put buffer bytes)38 (.flip buffer)39 (SaveState. buffer)))))40 ;;;;;;;;;;;;;;;;42 ;; Gameboy management43 (Gb/loadVBA)45 (def yellow-rom-image46 (File. user-home "proj/pokemon-escape/roms/yellow.gbc"))48 (def yellow-save-file49 (File. user-home "proj/pokemon-escape/roms/yellow.sav"))51 (def on? (atom nil))53 (defn shutdown! [] (Gb/shutdown) (reset! on? false))55 (defn restart! []56 (shutdown!)57 (.delete yellow-save-file)58 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))59 (reset! on? true))61 ;;; The first state!62 (defn gen-root! []63 (restart!)64 (let [state (SaveState. (Gb/saveState))]65 (write-state! state "root" ) state))67 (defn root []68 (if (.exists (state-cache-file "root"))69 (read-state "root")70 (gen-root!)))72 ;;;; Press Buttons74 (def button-code75 {;; main buttons76 :a 0x000177 :b 0x000279 ;; directional pad80 :r 0x001081 :l 0x002082 :u 0x004083 :d 0x008085 ;; meta buttons86 :select 0x000487 :start 0x000889 ;; pseudo-buttons90 :restart 0x0800 ; hard reset91 :listen -1 ; listen for user input92 })94 (defn button-mask [buttons]95 (reduce bit-or 0x0000 (map button-code buttons)))97 (defn set-state! [^SaveState state]98 (assert (:data state) "Not a valid state!")99 (if (not @on?) (restart!))100 (if (not= state @current-state)101 (do102 (Gb/loadState (:data state))103 (reset! current-state state))))105 (defn update-state []106 (reset! current-state107 (SaveState. (Gb/saveState))))109 (defn step110 ([^SaveState state buttons]111 (set-state! state)112 (Gb/step (button-mask buttons))113 (reset! current-state114 (SaveState. (Gb/saveState))))115 ([^SaveState state]116 (step state [:listen]))117 ([] (step (if @current-state @current-state (root)))))119 (defn tick120 ([] (tick @current-state))121 ([^SaveState state]122 (set-state! state)123 (Gb/tick)124 (update-state)))126 (defn play127 ([^SaveState state n]128 (try129 (set-state! state)130 (dorun (dotimes [_ n]131 (Thread/sleep 1)132 (Gb/step)))133 (finally134 (update-state))))135 ([n]136 (play @current-state n)))138 (defn continue!139 ([state]140 (play state Integer/MAX_VALUE))141 ([]142 (continue! @current-state)))144 (defn play-moves145 ([moves [prev state]]146 (set-state! state)147 (dorun (map (fn [move] (step @current-state move)) moves))148 [(concat prev moves) @current-state]))150 ;;;;;;;;;;;153 ;;;;;;;;;;;;;;; CPU data155 (defn cpu-data [size arr-fn]156 (let [store (int-array size)]157 (fn get-data158 ([] (get-data @current-state))159 ([state]160 (set-state! state) (arr-fn store) store))))162 (defn write-cpu-data [size store-fn]163 (fn store-data164 ([state new-data]165 (set-state! state)166 (let [store (int-array new-data)]167 (assert (= size (count new-data)))168 (store-fn store)169 (update-state)))170 ([new-data]171 (store-data @current-state new-data))))174 (def memory175 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))177 (def ram178 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))180 (def rom181 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))183 (def working-ram184 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))186 (def video-ram187 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))189 (def registers190 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))192 (def write-memory!193 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))195 (def write-registers!196 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))198 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;200 (defmacro gen-get-set-register [name index]201 (let [name-bang (symbol (str name "!"))]202 `(do203 (defn ~name204 ~(str "Retrieve the " name " register from state, or "205 "from @current-state if state is absent.")206 ([state#]207 (nth (registers state#) ~index))208 ([]209 (~name @current-state)))210 (defn ~name-bang211 ~(str "Set the " name " register for state, or "212 "for @current-state if state is absent.")213 ([state# new-register#]214 (set-state! state#)215 (let [registers# (registers state#)]216 (aset registers# ~index new-register#)217 (Gb/writeRegisters registers#)218 (update-state)))219 ([new-register#]220 (~name-bang @current-state new-register#))))))222 ;; 16 bit registers223 (gen-get-set-register PC 0)224 (gen-get-set-register SP 1)225 (gen-get-set-register AF 2)226 (gen-get-set-register BC 3)227 (gen-get-set-register DE 4)228 (gen-get-set-register HL 5)229 (gen-get-set-register IFF 6)231 ;; 8 bit registers232 (gen-get-set-register DIV 7)233 (gen-get-set-register TIMA 8)234 (gen-get-set-register TMA 9)235 (gen-get-set-register IF 11)236 (gen-get-set-register LCDC 12)237 (gen-get-set-register STAT 13)238 (gen-get-set-register SCY 14)239 (gen-get-set-register SCX 15)240 (gen-get-set-register LY 16)241 (gen-get-set-register DMA 18)242 (gen-get-set-register WY 19)243 (gen-get-set-register WX 20)244 (gen-get-set-register VBK 21)245 (gen-get-set-register HDMA1 22)246 (gen-get-set-register HDMA2 23)247 (gen-get-set-register HDMA3 24)248 (gen-get-set-register HDMA4 25)249 (gen-get-set-register HDMA5 26)250 (gen-get-set-register SVBK 27)251 (gen-get-set-register IE 28)253 ;;;;;;;;;;;;;;;255 (defmacro defn-memo256 [& forms]257 (let [fun-name (first forms)]258 `(do259 (defn ~@forms)260 (alter-var-root (var ~fun-name) memoize))))