annotate clojure/com/aurellem/gb/util.clj @ 273:69184558fcf3

further improvements on hxc-pokemon-base.
author Dylan Holmes <ocsenave@gmail.com>
date Tue, 27 Mar 2012 02:05:16 -0500
parents 22f58fa47c3c
children eec3e69500d9
rev   line source
rlm@145 1 (ns com.aurellem.gb.util
rlm@222 2 (:use (com.aurellem.gb gb-driver vbm constants))
rlm@222 3 (:import java.io.File)
rlm@145 4 (:import [com.aurellem.gb.gb_driver SaveState]))
rlm@145 5
rlm@145 6 (defn A [state]
rlm@145 7 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
rlm@145 8
rlm@145 9 (defn B [state]
rlm@145 10 (bit-shift-right (bit-and 0x0000FF00 (BC state)) 8))
rlm@145 11
rlm@145 12 (defn D [state]
rlm@145 13 (bit-shift-right (bit-and 0x0000FF00 (DE state)) 8))
rlm@145 14
rlm@145 15 (defn H [state]
rlm@145 16 (bit-shift-right (bit-and 0x0000FF00 (HL state)) 8))
rlm@145 17
rlm@145 18 (defn C [state]
rlm@145 19 (bit-and 0xFF (BC state)))
rlm@145 20 (defn F [state]
rlm@145 21 (bit-and 0xFF (AF state)))
rlm@145 22 (defn E [state]
rlm@145 23 (bit-and 0xFF (DE state)))
rlm@145 24 (defn L [state]
rlm@145 25 (bit-and 0xFF (HL state)))
rlm@145 26
rlm@145 27 (defn binary-str [num]
rlm@145 28 (format "%08d"
rlm@145 29 (Integer/parseInt
rlm@145 30 (Integer/toBinaryString num) 10)))
rlm@145 31
ocsenave@273 32
rlm@145 33 (defn view-register [state name reg-fn]
rlm@145 34 (println (format "%s: %s" name
rlm@145 35 (binary-str (reg-fn state))))
rlm@145 36 state)
rlm@145 37
rlm@174 38 (defn view-memory
rlm@174 39 ([^SaveState state mem]
rlm@230 40 (let [val (aget (memory state) mem)]
rlm@230 41 (println (format "0x%04X = %s 0x%02X %d" mem
rlm@230 42 (binary-str val) val val)))
rlm@174 43 state)
rlm@174 44 ([mem]
rlm@174 45 (view-memory @current-state mem)))
rlm@145 46
rlm@176 47 (defn print-listing
rlm@176 48 ([^SaveState state begin end]
rlm@176 49 (dorun (map
rlm@176 50 (fn [opcode line]
rlm@247 51 (println (format "0x%04X: 0x%02X %s %d"
rlm@247 52 line
rlm@247 53 opcode (binary-str opcode)
rlm@247 54 opcode)))
rlm@176 55 (subvec (vec (memory state)) begin end)
rlm@176 56 (range begin end)))
rlm@176 57 state)
rlm@176 58 ([begin end]
rlm@176 59 (print-listing @current-state begin end)))
rlm@145 60
rlm@174 61 (defn print-pc
rlm@174 62 ([^SaveState state]
rlm@174 63 (println (format "PC: 0x%04X" (PC state)))
rlm@174 64 state)
rlm@174 65 ([] (print-pc @current-state)))
rlm@145 66
rlm@174 67 (defn print-op
rlm@174 68 ([^SaveState state]
rlm@174 69 (println (format "OP: 0x%02X" (aget (memory state) (PC state))))
rlm@174 70 state)
rlm@174 71 ([] (print-op @current-state)))
rlm@145 72
rlm@145 73 (defn d-tick
rlm@145 74 ([state]
rlm@145 75 (-> state print-pc print-op tick)))
rlm@145 76
rlm@145 77 (defn print-interrupt
rlm@145 78 [^SaveState state]
rlm@145 79 (println (format "IE: %d" (IE state)))
rlm@145 80 state)
rlm@145 81
rlm@145 82 (defn set-memory
rlm@145 83 ([state location value]
rlm@145 84 (set-state! state)
rlm@145 85 (let [mem (memory state)]
rlm@145 86 (aset mem location value)
rlm@145 87 (write-memory! mem)
rlm@145 88 (update-state)))
rlm@145 89 ([location value]
rlm@145 90 (set-memory @current-state location value)))
rlm@145 91
rlm@145 92 (defn set-memory-range
rlm@145 93 ([state start values]
rlm@145 94 (set-state! state)
rlm@145 95 (let [mem (memory state)]
rlm@145 96
rlm@145 97 (dorun (map (fn [index val]
rlm@145 98 (aset mem index val))
rlm@145 99 (range start
rlm@145 100 (+ start (count values))) values))
rlm@145 101 (write-memory! mem)
rlm@145 102 (update-state)))
rlm@145 103 ([start values]
rlm@145 104 (set-memory-range
rlm@145 105 @current-state start values)))
rlm@145 106
rlm@145 107 (defn common-differences [& seqs]
rlm@145 108 (let [backbone (range (count (first seqs)))]
rlm@145 109 (filter
rlm@145 110 (comp (partial apply distinct?) second)
rlm@145 111 (zipmap backbone
rlm@145 112 (apply (partial map list) seqs)))))
rlm@145 113
rlm@212 114 (defn temporal-compare [& states]
rlm@212 115 (apply common-differences
rlm@212 116 (map (comp vec memory)
rlm@212 117 states)))
rlm@212 118
rlm@145 119 (defn mid-game []
rlm@145 120 (read-state "mid-game"))
rlm@154 121
rlm@154 122
rlm@192 123
rlm@192 124 (defn disect-bytes-2
rlm@192 125 "return a vector consiting of the last 16 bytes of the
ocsenave@273 126 integer expressed as two 8 bit numbers (inside an integer)
ocsenave@273 127 in the form [high-bits low-bits]."
rlm@192 128 [num]
rlm@192 129 [(bit-shift-right
rlm@192 130 (bit-and num 0xFF00) 8)
rlm@192 131 (bit-and num 0xFF)])
rlm@192 132
rlm@192 133 (defn disect-bytes-3
rlm@192 134 "same as disect-bytes-2 except that it assumes the input is a
rlm@192 135 24 bit number and returns [high-bits medium-bits low-bits]"
rlm@192 136 [num]
rlm@192 137 (vec
rlm@192 138 (concat
rlm@192 139 [(bit-shift-right (bit-and num 0xFF0000) 16)]
rlm@192 140 (disect-bytes-2 num))))
rlm@192 141
rlm@192 142 (defn glue-bytes
rlm@192 143 "Given two or three 8-bit numbers inside 32-bit integers,
rlm@192 144 combine them into the integer number that they together
rlm@192 145 represent."
rlm@192 146 ([h l]
rlm@192 147 (+ l (bit-shift-left h 8)))
rlm@192 148
rlm@192 149 ([h m l]
rlm@192 150 (+ (glue-bytes m l)
rlm@192 151 (bit-shift-left h 16))))
rlm@192 152
rlm@222 153 (def cartography
rlm@222 154 (File. user-home
rlm@222 155 "proj/vba-clojure/clojure/com/aurellem/exp/cartography"))
rlm@192 156
rlm@222 157
rlm@222 158
rlm@222 159 (defn print-D-memory
rlm@222 160 ([^SaveState state]
rlm@222 161
rlm@222 162 (let [descriptions
rlm@222 163 (clojure.string/split-lines
rlm@222 164 (slurp cartography))]
rlm@222 165 (dorun
rlm@222 166 (map
rlm@222 167 (fn [line data desc]
rlm@222 168 (printf "%04X %02X%s\n"
rlm@222 169 line data (apply str
rlm@222 170 (drop 20 desc))))
rlm@222 171 (range pokemon-record-begin
rlm@222 172 (inc D-memory-end))
rlm@222 173
rlm@222 174 (subvec (vec (memory state))
rlm@222 175 pokemon-record-begin
rlm@222 176 (inc D-memory-end))
rlm@222 177 descriptions))))
rlm@222 178 ([] (print-D-memory @current-state)))
rlm@222 179