annotate clojure/com/aurellem/gb_driver.clj @ 85:3f4fdd270059

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