Mercurial > vba-clojure
view clojure/com/aurellem/gb_driver.clj @ 100:2090bcb78f44
created functions to get and set register values
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 12 Mar 2012 12:37:04 -0500 |
parents | 9fad96094950 |
children | 37711ff4a7bc |
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 [frame data])10 (def ^:dynamic *save-state-cache*11 (File. "/home/r/proj/pokemon-escape/save-states/"))13 (defn frame->filename [frame]14 (File. *save-state-cache* (format "%07d.sav" frame)))16 (defn write-state! [^SaveState save]17 (let [buf (:data save)18 bytes (byte-array (.limit buf))19 dest (frame->filename (:frame save))]20 (.get buf bytes)21 (FileUtils/writeByteArrayToFile dest bytes)22 (.rewind buf)23 dest))25 (defn read-state [frame]26 (let [save (frame->filename frame)]27 (if (.exists save)28 (let [buf (Gb/saveBuffer)29 bytes (FileUtils/readFileToByteArray save)]30 (.put buf bytes)31 (.flip buf)32 (SaveState. frame buf)))))33 ;;;;;;;;;;;;;;;;35 ;; Gameboy management36 (Gb/loadVBA)38 (def yellow-rom-image39 (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc"))41 (def yellow-save-file42 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))44 (def on? (atom nil))46 (defn shutdown! [] (Gb/shutdown) (reset! on? false))48 (defn restart! []49 (shutdown!)50 (.delete yellow-save-file)51 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))52 (reset! on? true))54 ;;; The first state!55 (defn gen-root! []56 (restart!)57 (let [state (SaveState. 0 (Gb/saveState))]58 (write-state! state)59 state))61 (defn root []62 (if (.exists (frame->filename 0))63 (read-state 0)64 (gen-root!)))66 ;;;; Press Buttons68 (def button-code69 {;; main buttons70 :a 0x000171 :b 0x000273 ;; directional pad74 :r 0x001075 :l 0x002076 :u 0x004077 :d 0x008079 ;; meta buttons80 :select 0x000481 :start 0x000883 ;; pseudo-buttons84 :restart 0x0800 ; hard reset85 :listen -1 ; listen for user input86 })88 (defn button-mask [buttons]89 (reduce bit-or 0x0000 (map button-code buttons)))91 (def current-state (atom nil))93 (defn set-state! [^SaveState state]94 (assert (:data state) "Not a valid state!")95 (if (not @on?) (restart!))96 (if (not= @current-state state)97 (Gb/loadState (:data state)))98 (reset! current-state state))100 (defrecord Move [keys state])102 (defn update-state []103 (reset! current-state104 (SaveState. (:frame @current-state)105 (Gb/saveState))))107 (defn step108 ([^SaveState state buttons]109 (set-state! state)110 (Gb/step (button-mask buttons))111 (reset! current-state112 (SaveState. (inc (:frame state))(Gb/saveState))))113 ([^SaveState state]114 (step state [:listen]))115 ([] (step (if @current-state @current-state (root)))))117 (defn tick118 ([] (tick @current-state))119 ([state]120 (set-state! state)121 (Gb/tick)122 (update-state)))124 (defn move125 [^Move move buttons]126 (Move. (step (:state move) buttons) buttons))128 (defn play129 ([^SaveState state n]130 (reduce (fn [s _] (step s)) state (range n)))131 ([n]132 (play @current-state n)))134 (defn play-moves135 ([moves [prev state]]136 (set-state! state)137 (dorun (map (fn [move] (step @current-state move)) moves))138 [(concat prev moves) @current-state]))140 ;;;;;;;;;;;143 ;;;;;;;;;;;;;;; CPU data145 (defn cpu-data [size arr-fn]146 (let [store (int-array size)]147 (fn get-data148 ([] (get-data @current-state))149 ([state]150 (set-state! state) (arr-fn store) store))))152 (defn write-cpu-data [size store-fn]153 (fn [new-data]154 (let [store (int-array new-data)]155 (assert (= size (count new-data)))156 (store-fn store))))158 (def memory159 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))161 (def ram162 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))164 (def rom165 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))167 (def working-ram168 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))170 (def video-ram171 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))173 (def registers174 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))176 (def write-memory!177 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))179 (def write-registers!180 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))182 ;; (defn write-memory! [new-memory]183 ;; (let [store (int-array new-memory)]184 ;; (assert (= Gb/GB_MEMORY) (count new-memory))185 ;; (Gb/writeMemory new-memory)))188 ;; TODO add register names190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;192 (defmacro gen-get-set-register [name index]193 (let [name-bang (symbol (str name "!"))]194 `(do195 (defn ~name196 ~(str "Retrieve the " name " register from state, or "197 "from @current-state if state is absent.")198 ([state#]199 (nth (registers state#) ~index))200 ([]201 (~name @current-state)))202 (defn ~name-bang203 ~(str "Set the " name " register for state, or "204 "for @current-state if state is absent.")205 ([state# new-register#]206 (set-state! state#)207 (let [registers# (registers state#)]208 (aset registers# ~index new-register#)209 (Gb/writeRegisters registers#)210 (update-state)))211 ([new-register#]212 (~name-bang @current-state new-register#))))))216 (gen-get-set-register PC 0)217 (gen-get-set-register SP 1)218 (gen-get-set-register AF 2)219 (gen-get-set-register BC 3)220 (gen-get-set-register DE 4)221 (gen-get-set-register HL 5)222 (gen-get-set-register IFF 6)226 ;;;;;;;;;;;;;;;228 (defmacro defn-memo229 [& forms]230 (let [fun-name (first forms)]231 `(do232 (defn ~@forms)233 (alter-var-root (var ~fun-name) memoize))))