annotate clojure/com/aurellem/gb/util.clj @ 206:32ac21c9a30d

augmented print-pokemon to report inconsistent types; fixed error in give-type
author Robert McIntyre <rlm@mit.edu>
date Fri, 23 Mar 2012 04:36:16 -0500
parents fd549c8f42ae
children 8523faa122b0
rev   line source
rlm@145 1 (ns com.aurellem.gb.util
rlm@145 2 (:use (com.aurellem.gb gb-driver vbm))
rlm@145 3 (:import [com.aurellem.gb.gb_driver SaveState]))
rlm@145 4
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
rlm@145 32 (defn view-register [state name reg-fn]
rlm@145 33 (println (format "%s: %s" name
rlm@145 34 (binary-str (reg-fn state))))
rlm@145 35 state)
rlm@145 36
rlm@174 37 (defn view-memory
rlm@174 38 ([^SaveState state mem]
rlm@174 39 (println (format "mem 0x%04X = %s" mem
rlm@174 40 (binary-str (aget (memory state) mem))))
rlm@174 41 state)
rlm@174 42 ([mem]
rlm@174 43 (view-memory @current-state mem)))
rlm@145 44
rlm@176 45 (defn print-listing
rlm@176 46 ([^SaveState state begin end]
rlm@176 47 (dorun (map
rlm@176 48 (fn [opcode line]
rlm@176 49 (println (format "0x%04X: 0x%02X" line opcode)))
rlm@176 50 (subvec (vec (memory state)) begin end)
rlm@176 51 (range begin end)))
rlm@176 52 state)
rlm@176 53 ([begin end]
rlm@176 54 (print-listing @current-state begin end)))
rlm@145 55
rlm@174 56 (defn print-pc
rlm@174 57 ([^SaveState state]
rlm@174 58 (println (format "PC: 0x%04X" (PC state)))
rlm@174 59 state)
rlm@174 60 ([] (print-pc @current-state)))
rlm@145 61
rlm@174 62 (defn print-op
rlm@174 63 ([^SaveState state]
rlm@174 64 (println (format "OP: 0x%02X" (aget (memory state) (PC state))))
rlm@174 65 state)
rlm@174 66 ([] (print-op @current-state)))
rlm@145 67
rlm@145 68 (defn d-tick
rlm@145 69 ([state]
rlm@145 70 (-> state print-pc print-op tick)))
rlm@145 71
rlm@145 72 (defn print-interrupt
rlm@145 73 [^SaveState state]
rlm@145 74 (println (format "IE: %d" (IE state)))
rlm@145 75 state)
rlm@145 76
rlm@145 77 (defn set-memory
rlm@145 78 ([state location value]
rlm@145 79 (set-state! state)
rlm@145 80 (let [mem (memory state)]
rlm@145 81 (aset mem location value)
rlm@145 82 (write-memory! mem)
rlm@145 83 (update-state)))
rlm@145 84 ([location value]
rlm@145 85 (set-memory @current-state location value)))
rlm@145 86
rlm@145 87 (defn set-memory-range
rlm@145 88 ([state start values]
rlm@145 89 (set-state! state)
rlm@145 90 (let [mem (memory state)]
rlm@145 91
rlm@145 92 (dorun (map (fn [index val]
rlm@145 93 (aset mem index val))
rlm@145 94 (range start
rlm@145 95 (+ start (count values))) values))
rlm@145 96 (write-memory! mem)
rlm@145 97 (update-state)))
rlm@145 98 ([start values]
rlm@145 99 (set-memory-range
rlm@145 100 @current-state start values)))
rlm@145 101
rlm@145 102 (defn common-differences [& seqs]
rlm@145 103 (let [backbone (range (count (first seqs)))]
rlm@145 104 (filter
rlm@145 105 (comp (partial apply distinct?) second)
rlm@145 106 (zipmap backbone
rlm@145 107 (apply (partial map list) seqs)))))
rlm@145 108
rlm@145 109 (defn mid-game []
rlm@145 110 (read-state "mid-game"))
rlm@154 111
rlm@154 112
rlm@192 113
rlm@192 114 (defn disect-bytes-2
rlm@192 115 "return a vector consiting of the last 16 bytes of the
rlm@192 116 integer expressed as two 8 bit nimbers (inside an integer)
rlm@192 117 in the form [high-bits low-bits."
rlm@192 118 [num]
rlm@192 119 [(bit-shift-right
rlm@192 120 (bit-and num 0xFF00) 8)
rlm@192 121 (bit-and num 0xFF)])
rlm@192 122
rlm@192 123 (defn disect-bytes-3
rlm@192 124 "same as disect-bytes-2 except that it assumes the input is a
rlm@192 125 24 bit number and returns [high-bits medium-bits low-bits]"
rlm@192 126 [num]
rlm@192 127 (vec
rlm@192 128 (concat
rlm@192 129 [(bit-shift-right (bit-and num 0xFF0000) 16)]
rlm@192 130 (disect-bytes-2 num))))
rlm@192 131
rlm@192 132 (defn glue-bytes
rlm@192 133 "Given two or three 8-bit numbers inside 32-bit integers,
rlm@192 134 combine them into the integer number that they together
rlm@192 135 represent."
rlm@192 136 ([h l]
rlm@192 137 (+ l (bit-shift-left h 8)))
rlm@192 138
rlm@192 139 ([h m l]
rlm@192 140 (+ (glue-bytes m l)
rlm@192 141 (bit-shift-left h 16))))
rlm@192 142
rlm@192 143