Mercurial > vba-clojure
view 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 |
line wrap: on
line source
1 (ns com.aurellem.gb-driver2 (:import com.aurellem.gb.Gb)3 (:import java.io.File)4 (:import (java.nio IntBuffer ByteOrder)))6 (Gb/loadVBA)8 (def yellow-rom-image9 (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc"))11 (def yellow-save-file12 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))14 (def current-frame (atom 0))16 (defn vba-init []17 (reset! current-frame 0)18 (.delete yellow-save-file)19 (Gb/startEmulator (.getCanonicalPath yellow-rom-image)))21 (defn shutdown [] (Gb/shutdown))23 (defn reset [] (shutdown) (vba-init))25 (defn cpu-data [size arr-fn]26 (let [store (int-array size)]27 (fn []28 (arr-fn store)29 store)))31 (def ram32 (cpu-data (Gb/getRAMSize) #(Gb/getRAM %)))34 (def rom35 (cpu-data (Gb/getROMSize) #(Gb/getROM %)))37 (def working-ram38 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))40 (def video-ram41 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))43 (def registers44 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))46 (def button-code47 {;; main buttons48 :a 0x000149 :b 0x000251 ;; directional pad52 :r 0x001053 :l 0x002054 :u 0x004055 :d 0x008057 ;; meta buttons58 :select 0x000459 :start 0x000861 ;; hard reset -- not really a button62 :reset 0x0800})64 (defn button-mask [buttons]65 (reduce bit-or 0x0000 (map button-code buttons)))67 (defn buttons [mask]68 (loop [buttons []69 masks (seq button-code)]70 (if (empty? masks) buttons71 (let [[button value] (first masks)]72 (if (not= 0x0000 (bit-and value mask))73 (recur (conj buttons button) (rest masks))74 (recur buttons (rest masks)))))))77 (defn save-state [] (Gb/saveState))79 (def history (atom {}))81 (defn goto [frame]82 (let [save (@history frame)]83 (if (not (nil? save))84 (do85 (reset! current-frame frame)86 (Gb/loadState save))87 (println "no backup state"))))89 (defn clear-history [] (reset! history {}))91 (defn rewind92 ([n] (goto (- @current-frame n)))93 ([] (rewind 1)))95 (defn backup-state [frame]96 (swap! history #(assoc % frame (save-state))))98 (def ^:dynamic *save-history* true)100 (defn advance []101 (swap! current-frame inc)102 (if *save-history*103 (let [save (save-state)]104 (backup-state @current-frame))))106 (defn step107 ([] (advance) (Gb/step))108 ([mask-or-buttons]109 (advance)110 (if (number? mask-or-buttons)111 (Gb/step mask-or-buttons)112 (Gb/step (button-mask mask-or-buttons)))))114 (defn step! [& args]115 (binding [*save-history* false]116 (apply step args)))118 (defn frame [] @current-frame)