annotate clojure/com/aurellem/gb_driver.clj @ 75:eb7d4efe0f34

added play command
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 06:01:09 -0600
parents aaddd7b72a0e
children d7c38ce83421
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@65 8 (def yellow-rom-image
rlm@65 9 (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc"))
rlm@65 10
rlm@67 11 (def yellow-save-file
rlm@67 12 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))
rlm@67 13
rlm@73 14 (def current-frame (atom 0))
rlm@73 15
rlm@62 16 (defn vba-init []
rlm@73 17 (reset! current-frame 0)
rlm@67 18 (.delete yellow-save-file)
rlm@70 19 (Gb/startEmulator (.getCanonicalPath yellow-rom-image)))
rlm@60 20
rlm@71 21 (defn shutdown [] (Gb/shutdown))
rlm@71 22
rlm@71 23 (defn reset [] (shutdown) (vba-init))
rlm@71 24
rlm@64 25 (defn cpu-data [size arr-fn]
rlm@64 26 (let [store (int-array size)]
rlm@64 27 (fn []
rlm@64 28 (arr-fn store)
rlm@64 29 store)))
rlm@60 30
rlm@64 31 (def ram
rlm@64 32 (cpu-data (Gb/getRAMSize) #(Gb/getRAM %)))
rlm@61 33
rlm@64 34 (def rom
rlm@64 35 (cpu-data (Gb/getROMSize) #(Gb/getROM %)))
rlm@61 36
rlm@64 37 (def working-ram
rlm@64 38 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
rlm@62 39
rlm@64 40 (def video-ram
rlm@64 41 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
rlm@64 42
rlm@64 43 (def registers
rlm@64 44 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
rlm@64 45
rlm@71 46 (def button-code
rlm@71 47 {;; main buttons
rlm@71 48 :a 0x0001
rlm@71 49 :b 0x0002
rlm@71 50
rlm@71 51 ;; directional pad
rlm@71 52 :r 0x0010
rlm@71 53 :l 0x0020
rlm@71 54 :u 0x0040
rlm@71 55 :d 0x0080
rlm@71 56
rlm@71 57 ;; meta buttons
rlm@71 58 :select 0x0004
rlm@71 59 :start 0x0008
rlm@71 60
rlm@71 61 ;; hard reset -- not really a button
rlm@71 62 :reset 0x0800})
rlm@71 63
rlm@71 64 (defn button-mask [buttons]
rlm@71 65 (reduce bit-or 0x0000 (map button-code buttons)))
rlm@71 66
rlm@71 67 (defn buttons [mask]
rlm@71 68 (loop [buttons []
rlm@71 69 masks (seq button-code)]
rlm@71 70 (if (empty? masks) buttons
rlm@71 71 (let [[button value] (first masks)]
rlm@71 72 (if (not= 0x0000 (bit-and value mask))
rlm@71 73 (recur (conj buttons button) (rest masks))
rlm@71 74 (recur buttons (rest masks)))))))
rlm@71 75
rlm@73 76
rlm@73 77 (defn save-state [] (Gb/saveState))
rlm@73 78
rlm@73 79 (def history (atom {}))
rlm@73 80
rlm@73 81 (defn goto [frame]
rlm@73 82 (let [save (@history frame)]
rlm@73 83 (if (not (nil? save))
rlm@73 84 (do
rlm@73 85 (reset! current-frame frame)
rlm@73 86 (Gb/loadState save))
rlm@73 87 (println "no backup state"))))
rlm@73 88
rlm@73 89 (defn clear-history [] (reset! history {}))
rlm@73 90
rlm@74 91 (defn rewind
rlm@74 92 ([n] (goto (- @current-frame n)))
rlm@74 93 ([] (rewind 1)))
rlm@73 94
rlm@73 95 (defn backup-state [frame]
rlm@73 96 (swap! history #(assoc % frame (save-state))))
rlm@73 97
rlm@73 98 (def ^:dynamic *save-history* true)
rlm@73 99
rlm@73 100 (defn advance []
rlm@75 101 (swap! current-frame inc)
rlm@75 102 (if *save-history*
rlm@75 103 (let [save (save-state)]
rlm@75 104 (backup-state @current-frame))))
rlm@73 105
rlm@65 106 (defn step
rlm@73 107 ([] (advance) (Gb/step))
rlm@71 108 ([mask-or-buttons]
rlm@73 109 (advance)
rlm@71 110 (if (number? mask-or-buttons)
rlm@71 111 (Gb/step mask-or-buttons)
rlm@71 112 (Gb/step (button-mask mask-or-buttons)))))
rlm@73 113
rlm@75 114 (defn step! [& args]
rlm@75 115 (binding [*save-history* false]
rlm@75 116 (apply step args)))
rlm@75 117
rlm@73 118 (defn frame [] @current-frame)
rlm@73 119
rlm@73 120