Mercurial > vba-clojure
view clojure/com/aurellem/gb_driver.clj @ 87:e8855121f413
collect cruft, rename other files
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 10 Mar 2012 14:48:17 -0600 |
parents | 95cb2152d7cd |
children | 65c2854c5875 |
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-save! [^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 save))25 (defn read-save [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 (write-save! (SaveState. 0 (Gb/saveState))))59 (defn root []60 (if (.exists (frame->filename 0))61 (read-save 0)62 (gen-root!)))64 ;;;; Press Buttons66 (def button-code67 {;; main buttons68 :a 0x000169 :b 0x000271 ;; directional pad72 :r 0x001073 :l 0x002074 :u 0x004075 :d 0x008077 ;; meta buttons78 :select 0x000479 :start 0x000881 ;; pseudo-buttons82 :restart 0x0800 ; hard reset83 :listen -1 ; listen for user input84 })86 (defn button-mask [buttons]87 (reduce bit-or 0x0000 (map button-code buttons)))89 (def current-state (atom nil))92 (defn set-state! [^SaveState state]93 (assert (:data state) "Not a valid state!")94 (if (not @on?) (restart!))95 (if (not= @current-state state)96 (Gb/loadState (:data state)))97 (reset! current-state state))99 (defrecord Move [keys state])101 (defn step102 ([^SaveState state buttons]103 (set-state! state)104 (Gb/step (button-mask buttons))105 (reset! current-state106 (SaveState. (inc (:frame state))(Gb/saveState))))107 ([^SaveState state]108 (step state [:listen]))109 ([] (step (if @current-state @current-state (root)))))111 (defn move112 [^Move move buttons]113 (Move. (step (:state move) buttons) buttons))116 (defn play117 ([^SaveState state n]118 (reduce (fn [s _] (step s)) state (range n)))119 ([n]120 (play @current-state n)))122 (defn play-moves123 ([moves [prev state]]124 (set-state! state)125 (dorun (map (fn [move] (step @current-state move)) moves))126 [(concat prev moves) @current-state]))128 ;;;;;;;;;;;131 ;;;;;;;;;;;;;;; CPU data135 (defn cpu-data [size arr-fn]136 (let [store (int-array size)]137 (fn [state] (set-state! state) (arr-fn store) store)))139 (def ram140 (cpu-data (Gb/getRAMSize) #(Gb/getRAM %)))142 (def rom143 (cpu-data (Gb/getROMSize) #(Gb/getROM %)))145 (def working-ram146 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))148 (def video-ram149 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))151 (def registers152 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))154 ;; TODO add register names156 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;158 (defn AF [state]159 (nth (registers state) 2))161 (defn BC [state]162 (nth (registers state) 3))164 (defn DE [state]165 (nth (registers state) 4))167 ;;;;;;;;;;;;;;;169 (defmacro defn-memo170 [& forms]171 (let [fun-name (first forms)]172 `(do173 (defn ~@forms)174 (alter-var-root (var ~fun-name) memoize))))