annotate clojure/com/aurellem/gb_driver.clj @ 71:39928bf4622d

refactored
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 02:47:09 -0600
parents ff6f1acae59e
children 8a895ed4c0f9
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@62 14 (defn vba-init []
rlm@67 15 (.delete yellow-save-file)
rlm@70 16 (Gb/startEmulator (.getCanonicalPath yellow-rom-image)))
rlm@60 17
rlm@71 18 (defn shutdown [] (Gb/shutdown))
rlm@71 19
rlm@71 20 (defn reset [] (shutdown) (vba-init))
rlm@71 21
rlm@64 22 (defn cpu-data [size arr-fn]
rlm@64 23 (let [store (int-array size)]
rlm@64 24 (fn []
rlm@64 25 (arr-fn store)
rlm@64 26 store)))
rlm@60 27
rlm@64 28 (def ram
rlm@64 29 (cpu-data (Gb/getRAMSize) #(Gb/getRAM %)))
rlm@61 30
rlm@64 31 (def rom
rlm@64 32 (cpu-data (Gb/getROMSize) #(Gb/getROM %)))
rlm@61 33
rlm@64 34 (def working-ram
rlm@64 35 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
rlm@62 36
rlm@64 37 (def video-ram
rlm@64 38 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
rlm@64 39
rlm@64 40 (def registers
rlm@64 41 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
rlm@64 42
rlm@71 43 (def button-code
rlm@71 44 {;; main buttons
rlm@71 45 :a 0x0001
rlm@71 46 :b 0x0002
rlm@71 47
rlm@71 48 ;; directional pad
rlm@71 49 :r 0x0010
rlm@71 50 :l 0x0020
rlm@71 51 :u 0x0040
rlm@71 52 :d 0x0080
rlm@71 53
rlm@71 54 ;; meta buttons
rlm@71 55 :select 0x0004
rlm@71 56 :start 0x0008
rlm@71 57
rlm@71 58 ;; hard reset -- not really a button
rlm@71 59 :reset 0x0800})
rlm@71 60
rlm@71 61 (defn button-mask [buttons]
rlm@71 62 (reduce bit-or 0x0000 (map button-code buttons)))
rlm@71 63
rlm@71 64 (defn buttons [mask]
rlm@71 65 (loop [buttons []
rlm@71 66 masks (seq button-code)]
rlm@71 67 (if (empty? masks) buttons
rlm@71 68 (let [[button value] (first masks)]
rlm@71 69 (if (not= 0x0000 (bit-and value mask))
rlm@71 70 (recur (conj buttons button) (rest masks))
rlm@71 71 (recur buttons (rest masks)))))))
rlm@71 72
rlm@65 73 (defn step
rlm@65 74 ([] (Gb/step))
rlm@71 75 ([mask-or-buttons]
rlm@71 76 (if (number? mask-or-buttons)
rlm@71 77 (Gb/step mask-or-buttons)
rlm@71 78 (Gb/step (button-mask mask-or-buttons)))))