# HG changeset patch
# User Robert McIntyre <rlm@mit.edu>
# Date 1331194203 21600
# Node ID 86093f2ce7d1da92d9fbda3579bceed8ba40e238
# Parent  8cb500493ec2571c4922d4a08c9d80346578999b
got the speedrun to play

diff -r 8cb500493ec2 -r 86093f2ce7d1 clojure/com/aurellem/test_vba.clj
--- a/clojure/com/aurellem/test_vba.clj	Thu Mar 08 01:58:36 2012 -0600
+++ b/clojure/com/aurellem/test_vba.clj	Thu Mar 08 02:10:03 2012 -0600
@@ -1,6 +1,6 @@
 (ns com.aurellem.test-vba
   (:import java.io.File)
-  (:use (com.aurellem vba gb-driver)))
+  (:use (com.aurellem vbm gb-driver)))
 
 (def test-file (File."/home/r/proj/pokemon-escape/speedruns/rlm.vbm"))
 (def speedrun-2942
@@ -106,7 +106,7 @@
 
               ;; rlm additions
 
-              0
+              0 0 
               ;; end additions
               
               0 0 0 0 0 0 0 0 0 0 0 0 0 0
diff -r 8cb500493ec2 -r 86093f2ce7d1 clojure/com/aurellem/vba.clj
--- a/clojure/com/aurellem/vba.clj	Thu Mar 08 01:58:36 2012 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-(ns com.aurellem.vba
-  (:import java.io.File)
-  (:import org.apache.commons.io.FileUtils))
-
-(defn vba-bytes [#^File vba]
-  (let [bytes (FileUtils/readFileToByteArray vba)
-        ints (int-array (count bytes))]
-    (areduce bytes idx _ nil
-             (aset ints idx
-                   (bit-and 0xFF (aget bytes idx))))
-    ints))
-
-(def button-mask
-  {;; main buttons
-   :a         0x0001
-   :b         0x0002
-
-   ;; directional pad
-   :r         0x0010
-   :l         0x0020
-   :u         0x0040
-   :d         0x0080
-
-   ;; meta buttons
-   :select    0x0004
-   :start     0x0008
-
-   ;; hard reset -- not really a button
-   :reset   0x0800})
-
-(defn button-code [buttons]
-  (reduce bit-or 0x0000 (map button-mask buttons)))
-
-(defn buttons [mask]
-  (loop [buttons []
-         masks (seq button-mask)]
-    (if (empty? masks) buttons
-        (let [[button value] (first masks)]
-          (if (not= 0x0 (bit-and value mask))
-            (recur (conj buttons button) (rest masks))
-            (recur buttons (rest masks)))))))
-
-(def vba-header-length 257)
-
-(defn vba-masks [#^File vba]
-  (map (fn [[a b]]
-         (+ (bit-shift-left a 8) b))
-       (partition
-        2 (drop vba-header-length (vba-bytes vba)))))
-
-(defn vba-buttons [#^File vba]
-  (map buttons (vba-masks vba)))
-  
-
-
-         
-    
-  
\ No newline at end of file
diff -r 8cb500493ec2 -r 86093f2ce7d1 clojure/com/aurellem/vbm.clj
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clojure/com/aurellem/vbm.clj	Thu Mar 08 02:10:03 2012 -0600
@@ -0,0 +1,71 @@
+(ns com.aurellem.vbm
+  (:import java.io.File)
+  (:import org.apache.commons.io.FileUtils))
+
+(defn vbm-bytes [#^File vbm]
+  (let [bytes (FileUtils/readFileToByteArray vbm)
+        ints (int-array (count bytes))]
+    (areduce bytes idx _ nil
+             (aset ints idx
+                   (bit-and 0xFF (aget bytes idx))))
+    ints))
+
+(def button-mask
+  {;; main buttons
+   :a         0x0001
+   :b         0x0002
+
+   ;; directional pad
+   :r         0x0010
+   :l         0x0020
+   :u         0x0040
+   :d         0x0080
+
+   ;; meta buttons
+   :select    0x0004
+   :start     0x0008
+
+   ;; hard reset -- not really a button
+   :reset   0x0800})
+
+(defn button-code [buttons]
+  (reduce bit-or 0x0000 (map button-mask buttons)))
+
+(defn buttons [mask]
+  (loop [buttons []
+         masks (seq button-mask)]
+    (if (empty? masks) buttons
+        (let [[button value] (first masks)]
+          (if (not= 0x0000 (bit-and value mask))
+            (recur (conj buttons button) (rest masks))
+            (recur buttons (rest masks)))))))
+
+(def vbm-header-length 255)
+
+(defn repair-vbm
+  "Two 0's must be inserted after every reset, and the first
+   button must be dropped"
+  [vbm-seq]
+  (loop [fixed []
+         pending (next vbm-seq)]
+    (if (empty? pending) fixed
+        (let [mask (first pending)]
+          (if (not= 0x0000 (bit-and mask (button-mask :reset)))
+            (recur (conj fixed mask 0x0000 0x0000) (next pending))
+            (recur (conj fixed mask) (next pending)))))))
+
+(defn vbm-masks [#^File vbm]
+  (repair-vbm
+   (map (fn [[a b]]
+          (+ (bit-shift-left a 8) b))
+        (partition
+         2 (drop vbm-header-length (vbm-bytes vbm))))))
+
+(defn vbm-buttons [#^File vbm]
+  (map buttons (vbm-masks vbm)))
+  
+
+
+         
+    
+  
\ No newline at end of file