annotate clojure/com/aurellem/gb_driver.clj @ 76:d7c38ce83421

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