annotate clojure/com/aurellem/cruft/gb_driver.clj @ 141:d125a294f9d8

added save states, since many functions now depend on them.
author Robert McIntyre <rlm@mit.edu>
date Mon, 19 Mar 2012 19:45:32 -0500
parents e8855121f413
children
rev   line source
rlm@87 1 (ns com.aurellem.gb-driver
rlm@87 2 (:import com.aurellem.gb.Gb)
rlm@87 3 (:import java.io.File)
rlm@87 4 (:import org.apache.commons.io.FileUtils)
rlm@87 5 (:import (java.nio IntBuffer ByteOrder)))
rlm@87 6
rlm@87 7 (Gb/loadVBA)
rlm@87 8
rlm@87 9 (def ^:dynamic *max-history* 2e4)
rlm@87 10
rlm@87 11 (def ^:dynamic *backup-saves-to-disk* true)
rlm@87 12
rlm@87 13 (def ^:dynamic *save-history* true)
rlm@87 14
rlm@87 15 (def ^:dynamic *save-state-cache*
rlm@87 16 (File. "/home/r/proj/pokemon-escape/save-states/"))
rlm@87 17
rlm@87 18 (def yellow-rom-image
rlm@87 19 (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc"))
rlm@87 20
rlm@87 21 (def yellow-save-file
rlm@87 22 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))
rlm@87 23
rlm@87 24 (def current-frame (atom 0))
rlm@87 25
rlm@87 26 (defn vba-init []
rlm@87 27 (reset! current-frame 0)
rlm@87 28 (.delete yellow-save-file)
rlm@87 29 (Gb/startEmulator (.getCanonicalPath yellow-rom-image)))
rlm@87 30
rlm@87 31 (defn shutdown [] (Gb/shutdown))
rlm@87 32
rlm@87 33 (defn reset [] (shutdown) (vba-init))
rlm@87 34
rlm@87 35 (defn cpu-data [size arr-fn]
rlm@87 36 (let [store (int-array size)]
rlm@87 37 (fn [] (arr-fn store) store)))
rlm@87 38
rlm@87 39 (def ram
rlm@87 40 (cpu-data (Gb/getRAMSize) #(Gb/getRAM %)))
rlm@87 41
rlm@87 42 (def rom
rlm@87 43 (cpu-data (Gb/getROMSize) #(Gb/getROM %)))
rlm@87 44
rlm@87 45 (def working-ram
rlm@87 46 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
rlm@87 47
rlm@87 48 (def video-ram
rlm@87 49 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
rlm@87 50
rlm@87 51 (def registers
rlm@87 52 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
rlm@87 53
rlm@87 54 (def button-code
rlm@87 55 {;; main buttons
rlm@87 56 :a 0x0001
rlm@87 57 :b 0x0002
rlm@87 58
rlm@87 59 ;; directional pad
rlm@87 60 :r 0x0010
rlm@87 61 :l 0x0020
rlm@87 62 :u 0x0040
rlm@87 63 :d 0x0080
rlm@87 64
rlm@87 65 ;; meta buttons
rlm@87 66 :select 0x0004
rlm@87 67 :start 0x0008
rlm@87 68
rlm@87 69 ;; hard reset -- not really a button
rlm@87 70 :reset 0x0800})
rlm@87 71
rlm@87 72 (defn button-mask [buttons]
rlm@87 73 (reduce bit-or 0x0000 (map button-code buttons)))
rlm@87 74
rlm@87 75 (defn buttons [mask]
rlm@87 76 (loop [buttons []
rlm@87 77 masks (seq button-code)]
rlm@87 78 (if (empty? masks) buttons
rlm@87 79 (let [[button value] (first masks)]
rlm@87 80 (if (not= 0x0000 (bit-and value mask))
rlm@87 81 (recur (conj buttons button) (rest masks))
rlm@87 82 (recur buttons (rest masks)))))))
rlm@87 83
rlm@87 84 (defrecord SaveState [frame save-data])
rlm@87 85
rlm@87 86 (defn frame [] @current-frame)
rlm@87 87
rlm@87 88 (defn save-state []
rlm@87 89 (SaveState. (frame) (Gb/saveState)))
rlm@87 90
rlm@87 91 (defn load-state [#^SaveState save]
rlm@87 92 (reset! current-frame (:frame save))
rlm@87 93 (Gb/loadState (:save-data save)))
rlm@87 94
rlm@87 95 (def empty-history (sorted-map))
rlm@87 96
rlm@87 97 (def history (atom empty-history))
rlm@87 98
rlm@87 99 (defn frame->disk-save [frame]
rlm@87 100 (File. *save-state-cache*
rlm@87 101 (format "%07d.sav" frame)))
rlm@87 102
rlm@87 103 (defn get-save-from-disk [frame]
rlm@87 104 (let [save (frame->disk-save frame)]
rlm@87 105 (if (.exists save)
rlm@87 106 (let [buf (Gb/saveBuffer)
rlm@87 107 bytes (FileUtils/readFileToByteArray save)]
rlm@87 108 (.put buf bytes)
rlm@87 109 (.flip buf)
rlm@87 110 (SaveState. frame buf)))))
rlm@87 111
rlm@87 112 (defn store-save-to-disk [^SaveState save]
rlm@87 113 (let [buf (:save-data save)
rlm@87 114 bytes (byte-array (.limit buf))
rlm@87 115 dest (frame->disk-save (:frame save))]
rlm@87 116 (.get buf bytes)
rlm@87 117 (FileUtils/writeByteArrayToFile dest bytes)
rlm@87 118 (.rewind buf) dest))
rlm@87 119
rlm@87 120 (defn find-save-state [frame]
rlm@87 121 (let [save (@history frame)]
rlm@87 122 (if (not (nil? save)) save
rlm@87 123 (get-save-from-disk frame))))
rlm@87 124
rlm@87 125 (defn goto [frame]
rlm@87 126 (let [save (find-save-state frame)]
rlm@87 127 (if (nil? save)
rlm@87 128 (println frame "is not in history")
rlm@87 129 (do
rlm@87 130 (reset! current-frame frame)
rlm@87 131 (load-state save)))))
rlm@87 132
rlm@87 133 (defn clear-history [] (reset! history empty-history))
rlm@87 134
rlm@87 135 (defn rewind
rlm@87 136 ([] (rewind 1))
rlm@87 137 ([n] (goto (- @current-frame n))))
rlm@87 138
rlm@87 139 (defn backup-state
rlm@87 140 ([] (backup-state (frame)))
rlm@87 141 ([frame]
rlm@87 142 (let [save (save-state)]
rlm@87 143 (swap! history #(assoc % frame save))
rlm@87 144 ;;(store-save-to-disk save)
rlm@87 145 (if (> (count @history) *max-history*)
rlm@87 146 (swap! history #(dissoc % (first (first %))))))))
rlm@87 147
rlm@87 148 (defn advance []
rlm@87 149 (if *save-history*
rlm@87 150 (backup-state @current-frame))
rlm@87 151 (swap! current-frame inc))
rlm@87 152
rlm@87 153 (defn step
rlm@87 154 ([] (advance) (Gb/step))
rlm@87 155 ([mask-or-buttons]
rlm@87 156 (advance)
rlm@87 157 (if (number? mask-or-buttons)
rlm@87 158 (Gb/step mask-or-buttons)
rlm@87 159 (Gb/step (button-mask mask-or-buttons)))))
rlm@87 160
rlm@87 161 (defn play-moves
rlm@87 162 ([start moves]
rlm@87 163 (goto start)
rlm@87 164 (dorun (map step moves))
rlm@87 165 (backup-state)
rlm@87 166 (frame))
rlm@87 167 ([moves]
rlm@87 168 (dorun (map step moves))
rlm@87 169 (backup-state)
rlm@87 170 (frame)))
rlm@87 171
rlm@87 172 (defn play
rlm@87 173 ([] (play Integer/MAX_VALUE))
rlm@87 174 ([n] (dorun (dotimes [_ n] (step)))))
rlm@87 175
rlm@87 176 (defmacro without-saves [& forms]
rlm@87 177 `(binding [*save-history* false]
rlm@87 178 ~@forms))
rlm@87 179
rlm@87 180
rlm@87 181 (require '(clojure [zip :as zip]))
rlm@87 182
rlm@87 183
rlm@87 184
rlm@87 185
rlm@87 186 (defn tree->str [original]
rlm@87 187 (loop [s ".\n" loc (zip/down (zip/seq-zip (seq original)))]
rlm@87 188 (if (zip/end? loc) s
rlm@87 189 (let [d (count (zip/path loc))
rlm@87 190 rep
rlm@87 191 (str
rlm@87 192 s
rlm@87 193 (if (and (zip/up loc)
rlm@87 194 (> (count (-> loc zip/up zip/rights)) 0))
rlm@87 195 "|" "")
rlm@87 196 (apply str (repeat (dec d) " "))
rlm@87 197 (if (= (count (zip/rights loc)) 0)
rlm@87 198 "`-- "
rlm@87 199 "|-- ")
rlm@87 200 (zip/node loc)
rlm@87 201 "\n")]
rlm@87 202 (recur rep (zip/next loc))))))
rlm@87 203
rlm@87 204
rlm@87 205
rlm@87 206