annotate clojure/com/aurellem/gb_driver.clj @ 77:9ba461a5c60f

disk backup complete
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 21:16:53 -0600
parents d7c38ce83421
children 7ab48d728ee4
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@77 9 (def ^:dynamic *max-history* 10)
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@73 139 (defn backup-state [frame]
rlm@77 140 (let [save (save-state)]
rlm@77 141 (swap! history #(assoc % frame save))
rlm@77 142 (store-save-to-disk save)
rlm@77 143 (if (> (count @history) *max-history*)
rlm@77 144 (swap! history #(dissoc % (first (first %)))))))
rlm@73 145
rlm@73 146 (defn advance []
rlm@75 147 (if *save-history*
rlm@76 148 (backup-state @current-frame))
rlm@76 149 (swap! current-frame inc))
rlm@73 150
rlm@65 151 (defn step
rlm@73 152 ([] (advance) (Gb/step))
rlm@71 153 ([mask-or-buttons]
rlm@73 154 (advance)
rlm@71 155 (if (number? mask-or-buttons)
rlm@71 156 (Gb/step mask-or-buttons)
rlm@71 157 (Gb/step (button-mask mask-or-buttons)))))
rlm@73 158
rlm@76 159 (defn play
rlm@77 160 ([] (play Integer/MAX_VALUE))
rlm@77 161 ([n] (dorun (dotimes [_ n] (step)))))
rlm@75 162
rlm@77 163