Mercurial > vba-clojure
view clojure/com/aurellem/gb_driver.clj @ 105:2f8089eacab9
working on some actual assembly programs
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 13 Mar 2012 14:40:01 -0500 |
parents | d2e00ea31508 |
children | 3a60bb14a64a |
line wrap: on
line source
1 (ns com.aurellem.gb-driver2 (:import com.aurellem.gb.Gb)3 (:import java.io.File)4 (:import org.apache.commons.io.FileUtils)5 (:import (java.nio IntBuffer ByteOrder)))7 ;; Savestates8 (defrecord SaveState [frame data])10 (def ^:dynamic *save-state-cache*11 (File. "/home/r/proj/pokemon-escape/save-states/"))13 (defn frame->filename [frame]14 (File. *save-state-cache* (format "%07d.sav" frame)))16 (defn write-state! [^SaveState save]17 (let [buf (:data save)18 bytes (byte-array (.limit buf))19 dest (frame->filename (:frame save))]20 (.get buf bytes)21 (FileUtils/writeByteArrayToFile dest bytes)22 (.rewind buf)23 dest))25 (defn read-state [frame]26 (let [save (frame->filename frame)]27 (if (.exists save)28 (let [buf (Gb/saveBuffer)29 bytes (FileUtils/readFileToByteArray save)]30 (.put buf bytes)31 (.flip buf)32 (SaveState. frame buf)))))33 ;;;;;;;;;;;;;;;;35 ;; Gameboy management36 (Gb/loadVBA)38 (def yellow-rom-image39 (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc"))41 (def yellow-save-file42 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))44 (def on? (atom nil))46 (defn shutdown! [] (Gb/shutdown) (reset! on? false))48 (defn restart! []49 (shutdown!)50 (.delete yellow-save-file)51 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))52 (reset! on? true))54 ;;; The first state!55 (defn gen-root! []56 (restart!)57 (let [state (SaveState. 0 (Gb/saveState))]58 (write-state! state)59 state))61 (defn root []62 (if (.exists (frame->filename 0))63 (read-state 0)64 (gen-root!)))66 ;;;; Press Buttons68 (def button-code69 {;; main buttons70 :a 0x000171 :b 0x000273 ;; directional pad74 :r 0x001075 :l 0x002076 :u 0x004077 :d 0x008079 ;; meta buttons80 :select 0x000481 :start 0x000883 ;; pseudo-buttons84 :restart 0x0800 ; hard reset85 :listen -1 ; listen for user input86 })88 (defn button-mask [buttons]89 (reduce bit-or 0x0000 (map button-code buttons)))91 (def current-state (atom nil))93 (defn set-state! [^SaveState state]94 (assert (:data state) "Not a valid state!")95 (if (not @on?) (restart!))96 (Gb/loadState (:data state))97 (reset! current-state state))99 (defrecord Move [keys state])101 (defn update-state []102 (reset! current-state103 (SaveState. (:frame @current-state)104 (Gb/saveState))))106 (defn step107 ([^SaveState state buttons]108 (set-state! state)109 (Gb/step (button-mask buttons))110 (reset! current-state111 (SaveState. (inc (:frame state))(Gb/saveState))))112 ([^SaveState state]113 (step state [:listen]))114 ([] (step (if @current-state @current-state (root)))))116 (defn tick117 ([] (tick @current-state))118 ([state]119 (set-state! state)120 (Gb/tick)121 (update-state)))123 (defn move124 [^Move move buttons]125 (Move. (step (:state move) buttons) buttons))127 (defn play128 ([^SaveState state n]129 (reduce (fn [s _] (step s)) state (range n)))130 ([n]131 (play @current-state n)))133 (defn continue! []134 (play @current-state Integer/MAX_VALUE))136 (defn play-moves137 ([moves [prev state]]138 (set-state! state)139 (dorun (map (fn [move] (step @current-state move)) moves))140 [(concat prev moves) @current-state]))142 ;;;;;;;;;;;145 ;;;;;;;;;;;;;;; CPU data147 (defn cpu-data [size arr-fn]148 (let [store (int-array size)]149 (fn get-data150 ([] (get-data @current-state))151 ([state]152 (set-state! state) (arr-fn store) store))))154 (defn write-cpu-data [size store-fn]155 (fn [new-data]156 (let [store (int-array new-data)]157 (assert (= size (count new-data)))158 (store-fn store))))160 (def memory161 (cpu-data Gb/GB_MEMORY #(Gb/getMemory %)))163 (def ram164 (cpu-data Gb/RAM_SIZE #(Gb/getRAM %)))166 (def rom167 (cpu-data Gb/ROM_SIZE #(Gb/getROM %)))169 (def working-ram170 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))172 (def video-ram173 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))175 (def registers176 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))178 (def write-memory!179 (write-cpu-data Gb/GB_MEMORY #(Gb/writeMemory %)))181 (def write-registers!182 (write-cpu-data Gb/NUM_REGISTERS #(Gb/writeRegisters %)))184 ;;;;; Registers ;;;;;;;;;;;;;;;;;;;;;;;;;;;186 (defmacro gen-get-set-register [name index]187 (let [name-bang (symbol (str name "!"))]188 `(do189 (defn ~name190 ~(str "Retrieve the " name " register from state, or "191 "from @current-state if state is absent.")192 ([state#]193 (nth (registers state#) ~index))194 ([]195 (~name @current-state)))196 (defn ~name-bang197 ~(str "Set the " name " register for state, or "198 "for @current-state if state is absent.")199 ([state# new-register#]200 (set-state! state#)201 (let [registers# (registers state#)]202 (aset registers# ~index new-register#)203 (Gb/writeRegisters registers#)204 (update-state)))205 ([new-register#]206 (~name-bang @current-state new-register#))))))208 ;; 16 bit registers209 (gen-get-set-register PC 0)210 (gen-get-set-register SP 1)211 (gen-get-set-register AF 2)212 (gen-get-set-register BC 3)213 (gen-get-set-register DE 4)214 (gen-get-set-register HL 5)215 (gen-get-set-register IFF 6)217 ;; 8 bit registers218 (gen-get-set-register DIV 7)219 (gen-get-set-register TIMA 8)220 (gen-get-set-register TMA 9)221 (gen-get-set-register IF 11)222 (gen-get-set-register LCDC 12)223 (gen-get-set-register STAT 13)224 (gen-get-set-register SCY 14)225 (gen-get-set-register SCX 15)226 (gen-get-set-register LY 16)227 (gen-get-set-register DMA 18)228 (gen-get-set-register WY 19)229 (gen-get-set-register WX 20)230 (gen-get-set-register VBK 21)231 (gen-get-set-register HDMA1 22)232 (gen-get-set-register HDMA2 23)233 (gen-get-set-register HDMA3 24)234 (gen-get-set-register HDMA4 25)235 (gen-get-set-register HDMA5 26)236 (gen-get-set-register SVBK 27)237 (gen-get-set-register IE 28)239 ;;;;;;;;;;;;;;;241 (defmacro defn-memo242 [& forms]243 (let [fun-name (first forms)]244 `(do245 (defn ~@forms)246 (alter-var-root (var ~fun-name) memoize))))