comparison 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
comparison
equal deleted inserted replaced
70:ff6f1acae59e 71:39928bf4622d
12 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav")) 12 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))
13 13
14 (defn vba-init [] 14 (defn vba-init []
15 (.delete yellow-save-file) 15 (.delete yellow-save-file)
16 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))) 16 (Gb/startEmulator (.getCanonicalPath yellow-rom-image)))
17
18 (defn shutdown [] (Gb/shutdown))
19
20 (defn reset [] (shutdown) (vba-init))
17 21
18 (defn cpu-data [size arr-fn] 22 (defn cpu-data [size arr-fn]
19 (let [store (int-array size)] 23 (let [store (int-array size)]
20 (fn [] 24 (fn []
21 (arr-fn store) 25 (arr-fn store)
34 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %))) 38 (cpu-data Gb/VRAM_SIZE #(Gb/getVRAM %)))
35 39
36 (def registers 40 (def registers
37 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %))) 41 (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
38 42
43 (def button-code
44 {;; main buttons
45 :a 0x0001
46 :b 0x0002
47
48 ;; directional pad
49 :r 0x0010
50 :l 0x0020
51 :u 0x0040
52 :d 0x0080
53
54 ;; meta buttons
55 :select 0x0004
56 :start 0x0008
57
58 ;; hard reset -- not really a button
59 :reset 0x0800})
60
61 (defn button-mask [buttons]
62 (reduce bit-or 0x0000 (map button-code buttons)))
63
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)))))))
72
39 (defn step 73 (defn step
40 ([] (Gb/step)) 74 ([] (Gb/step))
41 ([mask] (Gb/step mask))) 75 ([mask-or-buttons]
42 76 (if (number? mask-or-buttons)
43 (defn shutdown [] (Gb/shutdown)) 77 (Gb/step mask-or-buttons)
44 78 (Gb/step (button-mask mask-or-buttons)))))
45
46
47