Mercurial > vba-clojure
view clojure/com/aurellem/gb_driver.clj @ 73:8a895ed4c0f9
added history facilities
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 08 Mar 2012 04:37:10 -0600 |
parents | 39928bf4622d |
children | aaddd7b72a0e |
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 rewind [] (goto (dec @current-frame)))93 (defn backup-state [frame]94 (swap! history #(assoc % frame (save-state))))96 (def ^:dynamic *save-history* true)98 (defn advance []99 (let [save (save-state)]100 (backup-state @current-frame)101 (swap! current-frame inc)))103 (defn step104 ([] (advance) (Gb/step))105 ([mask-or-buttons]106 (advance)107 (if (number? mask-or-buttons)108 (Gb/step mask-or-buttons)109 (Gb/step (button-mask mask-or-buttons)))))111 (defn frame [] @current-frame)