Mercurial > vba-clojure
view clojure/com/aurellem/vbm.clj @ 87:e8855121f413
collect cruft, rename other files
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 10 Mar 2012 14:48:17 -0600 |
parents | 8a895ed4c0f9 |
children | 65c2854c5875 |
line wrap: on
line source
1 (ns com.aurellem.vbm2 (:import java.io.File)3 (:import org.apache.commons.io.FileUtils)4 (:use com.aurellem.gb-driver))6 (defn buttons [mask]7 (loop [buttons []8 masks (seq (dissoc button-code :listen))]9 (if (empty? masks) buttons10 (let [[button value] (first masks)]11 (if (not= 0x0000 (bit-and value mask))12 (recur (conj buttons button) (rest masks))13 (recur buttons (rest masks)))))))15 (defn vbm-bytes [#^File vbm]16 (let [bytes (FileUtils/readFileToByteArray vbm)17 ints (int-array (count bytes))]18 (areduce bytes idx _ nil19 (aset ints idx20 (bit-and 0xFF (aget bytes idx))))21 ints))23 (def vbm-header-length 255)25 (defn repair-vbm26 "Two 0's must be inserted after every reset."27 [vbm-masks]28 (loop [fixed []29 pending vbm-masks]30 (if (empty? pending) fixed31 (let [mask (first pending)]32 (if (not= 0x0000 (bit-and mask (button-code :reset)))33 (recur (conj fixed mask 0x0000 0x0000) (next pending))34 (recur (conj fixed mask) (next pending)))))))36 (defn vbm-masks [#^File vbm]37 (repair-vbm38 (map (fn [[a b]]39 (+ (bit-shift-left a 8) b))40 (partition41 2 (drop vbm-header-length (vbm-bytes vbm))))))43 (defn vbm-buttons [#^File vbm]44 (map buttons (vbm-masks vbm)))46 (defn play-vbm [#^File vbm]47 (restart!)48 (dorun (map step (vbm-masks vbm))))50 (defn convert-buttons51 "To write a vbm file, we must remove the last two buttons after any52 reset event."53 [buttons]54 (loop [fixed []55 pending buttons]56 (if (empty? pending) fixed57 (let [mask (first pending)]58 (if (contains? (set (first pending)) :reset)59 (recur (conj fixed mask) (drop 3 pending))60 (recur (conj fixed mask) (next pending)))))))62 (def vbm-header63 (byte-array64 (map65 byte66 [86 66 77 26 1 0 0 0 105 74 88 79 89 1 0 0 0 0 0 0 0 1 2 112 0 0 067 0 0 0 0 0 1 0 0 0 80 79 75 69 77 79 78 32 89 69 76 76 1 -105 124 468 3 0 0 0 0 0 0 0 0 1 0 0 95 95 95 95 95 95 95 95 95 95 95 95 95 9569 95 95 82 111 98 101 114 116 32 32 77 99 73 110 116 121 114 101 9570 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 9571 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 9572 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 9573 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 9574 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 9575 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 9576 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 9577 95 95 95 95])))79 (def vbm-trailer80 (byte-array81 (map byte [0])))83 (defn buttons->vbm-bytes [buttons]84 (let [bytes-in-ints85 (map button-mask (convert-buttons buttons))86 high-bits (map #(bit-shift-right (bit-and 0xFF00 %) 8)87 bytes-in-ints)88 low-bits (map #(bit-and 0xFF %) bytes-in-ints)89 convert-byte (fn [i] (byte (if (>= i 128) (- i 256) i)))90 contents91 (byte-array92 (concat93 vbm-header94 (map convert-byte (interleave high-bits low-bits))95 vbm-trailer))]96 contents))98 (defn write-vbm [buttons #^File out]99 (clojure.java.io/copy (buttons->vbm-bytes buttons) out))