diff 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 diff
     1.1 --- a/clojure/com/aurellem/gb_driver.clj	Thu Mar 08 02:25:20 2012 -0600
     1.2 +++ b/clojure/com/aurellem/gb_driver.clj	Thu Mar 08 02:47:09 2012 -0600
     1.3 @@ -15,6 +15,10 @@
     1.4    (.delete yellow-save-file)
     1.5    (Gb/startEmulator (.getCanonicalPath yellow-rom-image)))
     1.6  
     1.7 +(defn shutdown [] (Gb/shutdown))
     1.8 +
     1.9 +(defn reset [] (shutdown) (vba-init))
    1.10 +
    1.11  (defn cpu-data [size arr-fn]
    1.12    (let [store (int-array size)]
    1.13      (fn [] 
    1.14 @@ -36,12 +40,39 @@
    1.15  (def registers
    1.16    (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %)))
    1.17  
    1.18 +(def button-code
    1.19 +  {;; main buttons
    1.20 +   :a         0x0001
    1.21 +   :b         0x0002
    1.22 +
    1.23 +   ;; directional pad
    1.24 +   :r         0x0010
    1.25 +   :l         0x0020
    1.26 +   :u         0x0040
    1.27 +   :d         0x0080
    1.28 +
    1.29 +   ;; meta buttons
    1.30 +   :select    0x0004
    1.31 +   :start     0x0008
    1.32 +
    1.33 +   ;; hard reset -- not really a button
    1.34 +   :reset   0x0800})
    1.35 +
    1.36 +(defn button-mask [buttons]
    1.37 +  (reduce bit-or 0x0000 (map button-code buttons)))
    1.38 +
    1.39 +(defn buttons [mask]
    1.40 +  (loop [buttons []
    1.41 +         masks (seq button-code)]
    1.42 +    (if (empty? masks) buttons
    1.43 +        (let [[button value] (first masks)]
    1.44 +          (if (not= 0x0000 (bit-and value mask))
    1.45 +            (recur (conj buttons button) (rest masks))
    1.46 +            (recur buttons (rest masks)))))))
    1.47 +
    1.48  (defn step
    1.49    ([] (Gb/step))
    1.50 -  ([mask] (Gb/step mask)))
    1.51 -
    1.52 -(defn shutdown [] (Gb/shutdown))
    1.53 -
    1.54 -   
    1.55 -  
    1.56 -
    1.57 +  ([mask-or-buttons]
    1.58 +     (if (number? mask-or-buttons)
    1.59 +       (Gb/step mask-or-buttons)
    1.60 +       (Gb/step (button-mask mask-or-buttons)))))