view clojure/com/aurellem/gb/util.clj @ 237:ff37bc3004a7

continuing work on map-function-addresses.
author Robert McIntyre <rlm@mit.edu>
date Sat, 24 Mar 2012 23:38:21 -0500
parents fe26776e1a58
children 22f58fa47c3c
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 (let [val (aget (memory state) mem)]
40 (println (format "0x%04X = %s 0x%02X %d" mem
41 (binary-str val) val val)))
42 state)
43 ([mem]
44 (view-memory @current-state mem)))
46 (defn print-listing
47 ([^SaveState state begin end]
48 (dorun (map
49 (fn [opcode line]
50 (println (format "0x%04X: 0x%02X" line opcode)))
51 (subvec (vec (memory state)) begin end)
52 (range begin end)))
53 state)
54 ([begin end]
55 (print-listing @current-state begin end)))
57 (defn print-pc
58 ([^SaveState state]
59 (println (format "PC: 0x%04X" (PC state)))
60 state)
61 ([] (print-pc @current-state)))
63 (defn print-op
64 ([^SaveState state]
65 (println (format "OP: 0x%02X" (aget (memory state) (PC state))))
66 state)
67 ([] (print-op @current-state)))
69 (defn d-tick
70 ([state]
71 (-> state print-pc print-op tick)))
73 (defn print-interrupt
74 [^SaveState state]
75 (println (format "IE: %d" (IE state)))
76 state)
78 (defn set-memory
79 ([state location value]
80 (set-state! state)
81 (let [mem (memory state)]
82 (aset mem location value)
83 (write-memory! mem)
84 (update-state)))
85 ([location value]
86 (set-memory @current-state location value)))
88 (defn set-memory-range
89 ([state start values]
90 (set-state! state)
91 (let [mem (memory state)]
93 (dorun (map (fn [index val]
94 (aset mem index val))
95 (range start
96 (+ start (count values))) values))
97 (write-memory! mem)
98 (update-state)))
99 ([start values]
100 (set-memory-range
101 @current-state start values)))
103 (defn common-differences [& seqs]
104 (let [backbone (range (count (first seqs)))]
105 (filter
106 (comp (partial apply distinct?) second)
107 (zipmap backbone
108 (apply (partial map list) seqs)))))
110 (defn temporal-compare [& states]
111 (apply common-differences
112 (map (comp vec memory)
113 states)))
115 (defn mid-game []
116 (read-state "mid-game"))
120 (defn disect-bytes-2
121 "return a vector consiting of the last 16 bytes of the
122 integer expressed as two 8 bit nimbers (inside an integer)
123 in the form [high-bits low-bits."
124 [num]
125 [(bit-shift-right
126 (bit-and num 0xFF00) 8)
127 (bit-and num 0xFF)])
129 (defn disect-bytes-3
130 "same as disect-bytes-2 except that it assumes the input is a
131 24 bit number and returns [high-bits medium-bits low-bits]"
132 [num]
133 (vec
134 (concat
135 [(bit-shift-right (bit-and num 0xFF0000) 16)]
136 (disect-bytes-2 num))))
138 (defn glue-bytes
139 "Given two or three 8-bit numbers inside 32-bit integers,
140 combine them into the integer number that they together
141 represent."
142 ([h l]
143 (+ l (bit-shift-left h 8)))
145 ([h m l]
146 (+ (glue-bytes m l)
147 (bit-shift-left h 16))))
149 (def cartography
150 (File. user-home
151 "proj/vba-clojure/clojure/com/aurellem/exp/cartography"))
155 (defn print-D-memory
156 ([^SaveState state]
158 (let [descriptions
159 (clojure.string/split-lines
160 (slurp cartography))]
161 (dorun
162 (map
163 (fn [line data desc]
164 (printf "%04X %02X%s\n"
165 line data (apply str
166 (drop 20 desc))))
167 (range pokemon-record-begin
168 (inc D-memory-end))
170 (subvec (vec (memory state))
171 pokemon-record-begin
172 (inc D-memory-end))
173 descriptions))))
174 ([] (print-D-memory @current-state)))