view clojure/com/aurellem/vbm.clj @ 71:39928bf4622d

refactored
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 02:47:09 -0600
parents ff6f1acae59e
children c88ad4f6d9b4
line wrap: on
line source
1 (ns com.aurellem.vbm
2 (:import java.io.File)
3 (:import org.apache.commons.io.FileUtils)
4 (:use com.aurellem.gb-driver))
6 (defn vbm-bytes [#^File vbm]
7 (let [bytes (FileUtils/readFileToByteArray vbm)
8 ints (int-array (count bytes))]
9 (areduce bytes idx _ nil
10 (aset ints idx
11 (bit-and 0xFF (aget bytes idx))))
12 ints))
14 (def vbm-header-length 255)
16 (defn repair-vbm
17 "Two 0's must be inserted after every reset."
18 [vbm-seq]
19 (loop [fixed []
20 pending vbm-seq]
21 (if (empty? pending) fixed
22 (let [mask (first pending)]
23 (if (not= 0x0000 (bit-and mask (button-code :reset)))
24 (recur (conj fixed mask 0x0000 0x0000) (next pending))
25 (recur (conj fixed mask) (next pending)))))))
27 (defn vbm-masks [#^File vbm]
28 (repair-vbm
29 (map (fn [[a b]]
30 (+ (bit-shift-left a 8) b))
31 (partition
32 2 (drop vbm-header-length (vbm-bytes vbm))))))
34 (defn vbm-buttons [#^File vbm]
35 (map buttons (vbm-masks vbm)))
37 (defn play-vbm [#^File vbm]
38 (reset)
39 (dorun (map step (vbm-masks vbm))))