view clojure/com/aurellem/gb/util.clj @ 226:21641c1c3f87

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