view 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
line wrap: on
line source
1 (ns com.aurellem.gb-driver
2 (:import com.aurellem.gb.Gb)
3 (:import java.io.File)
4 (:import (java.nio IntBuffer ByteOrder)))
6 (Gb/loadVBA)
8 (def yellow-rom-image
9 (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc"))
11 (def yellow-save-file
12 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))
14 (defn vba-init []
15 (.delete yellow-save-file)
16 (Gb/startEmulator (.getCanonicalPath yellow-rom-image)))
18 (defn shutdown [] (Gb/shutdown))
20 (defn reset [] (shutdown) (vba-init))
22 (defn cpu-data [size arr-fn]
23 (let [store (int-array size)]
24 (fn []
25 (arr-fn store)
26 store)))
28 (def ram
29 (cpu-data (Gb/getRAMSize) #(Gb/getRAM %)))
31 (def rom
32 (cpu-data (Gb/getROMSize) #(Gb/getROM %)))
34 (def working-ram
35 (cpu-data Gb/WRAM_SIZE #(Gb/getWRAM %)))
37 (def video-ram
38 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
40 (def registers
41 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
43 (def button-code
44 {;; main buttons
45 :a 0x0001
46 :b 0x0002
48 ;; directional pad
49 :r 0x0010
50 :l 0x0020
51 :u 0x0040
52 :d 0x0080
54 ;; meta buttons
55 :select 0x0004
56 :start 0x0008
58 ;; hard reset -- not really a button
59 :reset 0x0800})
61 (defn button-mask [buttons]
62 (reduce bit-or 0x0000 (map button-code buttons)))
64 (defn buttons [mask]
65 (loop [buttons []
66 masks (seq button-code)]
67 (if (empty? masks) buttons
68 (let [[button value] (first masks)]
69 (if (not= 0x0000 (bit-and value mask))
70 (recur (conj buttons button) (rest masks))
71 (recur buttons (rest masks)))))))
73 (defn step
74 ([] (Gb/step))
75 ([mask-or-buttons]
76 (if (number? mask-or-buttons)
77 (Gb/step mask-or-buttons)
78 (Gb/step (button-mask mask-or-buttons)))))