changeset 71:39928bf4622d

refactored
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 02:47:09 -0600
parents ff6f1acae59e
children c88ad4f6d9b4
files clojure/com/aurellem/gb_driver.clj clojure/com/aurellem/test_vba.clj clojure/com/aurellem/test_vbm.clj clojure/com/aurellem/vbm.clj
diffstat 4 files changed, 62 insertions(+), 56 deletions(-) [+]
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)))))
     2.1 --- a/clojure/com/aurellem/test_vba.clj	Thu Mar 08 02:25:20 2012 -0600
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,17 +0,0 @@
     2.4 -(ns com.aurellem.test-vba
     2.5 -  (:import java.io.File)
     2.6 -  (:use (com.aurellem vbm gb-driver)))
     2.7 -
     2.8 -(def test-file (File."/home/r/proj/pokemon-escape/speedruns/rlm.vbm"))
     2.9 -(def speedrun-2942
    2.10 -  (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2942.vbm"))
    2.11 -
    2.12 -(defn test-speedrun []
    2.13 -  (dorun
    2.14 -   (map step (vbm-masks speedrun-2942))))
    2.15 -
    2.16 -(defn play-vbm [#^File vbm]
    2.17 -  (shutdown)
    2.18 -  (vba-init)
    2.19 -  (dorun (map step (vbm-masks vbm))))
    2.20 -
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/clojure/com/aurellem/test_vbm.clj	Thu Mar 08 02:47:09 2012 -0600
     3.3 @@ -0,0 +1,18 @@
     3.4 +(ns com.aurellem.test-vbm
     3.5 +  (:import java.io.File)
     3.6 +  (:use (com.aurellem vbm gb-driver)))
     3.7 +
     3.8 +(def test-file (File."/home/r/proj/pokemon-escape/speedruns/rlm.vbm"))
     3.9 +
    3.10 +(def speedrun-2942
    3.11 +  (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2942.vbm"))
    3.12 +
    3.13 +(def speedrun-2913
    3.14 +  (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2913.vbm"))
    3.15 +
    3.16 +(def speedrun-2771
    3.17 +  (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2771.vbm"))
    3.18 +
    3.19 +(defn test-speedrun []
    3.20 +  (dorun
    3.21 +   (map step (vbm-masks speedrun-2942))))
     4.1 --- a/clojure/com/aurellem/vbm.clj	Thu Mar 08 02:25:20 2012 -0600
     4.2 +++ b/clojure/com/aurellem/vbm.clj	Thu Mar 08 02:47:09 2012 -0600
     4.3 @@ -1,6 +1,7 @@
     4.4  (ns com.aurellem.vbm
     4.5    (:import java.io.File)
     4.6 -  (:import org.apache.commons.io.FileUtils))
     4.7 +  (:import org.apache.commons.io.FileUtils)
     4.8 +  (:use com.aurellem.gb-driver))
     4.9  
    4.10  (defn vbm-bytes [#^File vbm]
    4.11    (let [bytes (FileUtils/readFileToByteArray vbm)
    4.12 @@ -10,36 +11,6 @@
    4.13                     (bit-and 0xFF (aget bytes idx))))
    4.14      ints))
    4.15  
    4.16 -(def button-mask
    4.17 -  {;; main buttons
    4.18 -   :a         0x0001
    4.19 -   :b         0x0002
    4.20 -
    4.21 -   ;; directional pad
    4.22 -   :r         0x0010
    4.23 -   :l         0x0020
    4.24 -   :u         0x0040
    4.25 -   :d         0x0080
    4.26 -
    4.27 -   ;; meta buttons
    4.28 -   :select    0x0004
    4.29 -   :start     0x0008
    4.30 -
    4.31 -   ;; hard reset -- not really a button
    4.32 -   :reset   0x0800})
    4.33 -
    4.34 -(defn button-code [buttons]
    4.35 -  (reduce bit-or 0x0000 (map button-mask buttons)))
    4.36 -
    4.37 -(defn buttons [mask]
    4.38 -  (loop [buttons []
    4.39 -         masks (seq button-mask)]
    4.40 -    (if (empty? masks) buttons
    4.41 -        (let [[button value] (first masks)]
    4.42 -          (if (not= 0x0000 (bit-and value mask))
    4.43 -            (recur (conj buttons button) (rest masks))
    4.44 -            (recur buttons (rest masks)))))))
    4.45 -
    4.46  (def vbm-header-length 255)
    4.47  
    4.48  (defn repair-vbm
    4.49 @@ -49,7 +20,7 @@
    4.50           pending vbm-seq]
    4.51      (if (empty? pending) fixed
    4.52          (let [mask (first pending)]
    4.53 -          (if (not= 0x0000 (bit-and mask (button-mask :reset)))
    4.54 +          (if (not= 0x0000 (bit-and mask (button-code :reset)))
    4.55              (recur (conj fixed mask 0x0000 0x0000) (next pending))
    4.56              (recur (conj fixed mask) (next pending)))))))
    4.57  
    4.58 @@ -63,6 +34,9 @@
    4.59  (defn vbm-buttons [#^File vbm]
    4.60    (map buttons (vbm-masks vbm)))
    4.61    
    4.62 +(defn play-vbm [#^File vbm]
    4.63 +  (reset)
    4.64 +  (dorun (map step (vbm-masks vbm))))
    4.65  
    4.66  
    4.67