# HG changeset patch # User Robert McIntyre # Date 1331196429 21600 # Node ID 39928bf4622d3d933c540c5d2b79aa311881266a # Parent ff6f1acae59ee1bf03f41febda3d2880865e081f refactored diff -r ff6f1acae59e -r 39928bf4622d clojure/com/aurellem/gb_driver.clj --- a/clojure/com/aurellem/gb_driver.clj Thu Mar 08 02:25:20 2012 -0600 +++ b/clojure/com/aurellem/gb_driver.clj Thu Mar 08 02:47:09 2012 -0600 @@ -15,6 +15,10 @@ (.delete yellow-save-file) (Gb/startEmulator (.getCanonicalPath yellow-rom-image))) +(defn shutdown [] (Gb/shutdown)) + +(defn reset [] (shutdown) (vba-init)) + (defn cpu-data [size arr-fn] (let [store (int-array size)] (fn [] @@ -36,12 +40,39 @@ (def registers (cpu-data Gb/NUM_REGISTERS #(Gb/getRegisters %))) +(def button-code + {;; 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-mask [buttons] + (reduce bit-or 0x0000 (map button-code buttons))) + +(defn buttons [mask] + (loop [buttons [] + masks (seq button-code)] + (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))))))) + (defn step ([] (Gb/step)) - ([mask] (Gb/step mask))) - -(defn shutdown [] (Gb/shutdown)) - - - - + ([mask-or-buttons] + (if (number? mask-or-buttons) + (Gb/step mask-or-buttons) + (Gb/step (button-mask mask-or-buttons))))) diff -r ff6f1acae59e -r 39928bf4622d clojure/com/aurellem/test_vba.clj --- a/clojure/com/aurellem/test_vba.clj Thu Mar 08 02:25:20 2012 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -(ns com.aurellem.test-vba - (:import java.io.File) - (:use (com.aurellem vbm gb-driver))) - -(def test-file (File."/home/r/proj/pokemon-escape/speedruns/rlm.vbm")) -(def speedrun-2942 - (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2942.vbm")) - -(defn test-speedrun [] - (dorun - (map step (vbm-masks speedrun-2942)))) - -(defn play-vbm [#^File vbm] - (shutdown) - (vba-init) - (dorun (map step (vbm-masks vbm)))) - diff -r ff6f1acae59e -r 39928bf4622d clojure/com/aurellem/test_vbm.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clojure/com/aurellem/test_vbm.clj Thu Mar 08 02:47:09 2012 -0600 @@ -0,0 +1,18 @@ +(ns com.aurellem.test-vbm + (:import java.io.File) + (:use (com.aurellem vbm gb-driver))) + +(def test-file (File."/home/r/proj/pokemon-escape/speedruns/rlm.vbm")) + +(def speedrun-2942 + (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2942.vbm")) + +(def speedrun-2913 + (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2913.vbm")) + +(def speedrun-2771 + (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2771.vbm")) + +(defn test-speedrun [] + (dorun + (map step (vbm-masks speedrun-2942)))) diff -r ff6f1acae59e -r 39928bf4622d clojure/com/aurellem/vbm.clj --- a/clojure/com/aurellem/vbm.clj Thu Mar 08 02:25:20 2012 -0600 +++ b/clojure/com/aurellem/vbm.clj Thu Mar 08 02:47:09 2012 -0600 @@ -1,6 +1,7 @@ (ns com.aurellem.vbm (:import java.io.File) - (:import org.apache.commons.io.FileUtils)) + (:import org.apache.commons.io.FileUtils) + (:use com.aurellem.gb-driver)) (defn vbm-bytes [#^File vbm] (let [bytes (FileUtils/readFileToByteArray vbm) @@ -10,36 +11,6 @@ (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 @@ -49,7 +20,7 @@ pending vbm-seq] (if (empty? pending) fixed (let [mask (first pending)] - (if (not= 0x0000 (bit-and mask (button-mask :reset))) + (if (not= 0x0000 (bit-and mask (button-code :reset))) (recur (conj fixed mask 0x0000 0x0000) (next pending)) (recur (conj fixed mask) (next pending))))))) @@ -63,6 +34,9 @@ (defn vbm-buttons [#^File vbm] (map buttons (vbm-masks vbm))) +(defn play-vbm [#^File vbm] + (reset) + (dorun (map step (vbm-masks vbm))))