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@145
|
37 (defn view-memory [state mem]
|
rlm@145
|
38 (println (format "mem 0x%04X = %s" mem
|
rlm@145
|
39 (binary-str (aget (memory state) mem))))
|
rlm@145
|
40 state)
|
rlm@145
|
41
|
rlm@145
|
42 (defn print-listing [state begin end]
|
rlm@145
|
43 (dorun (map
|
rlm@145
|
44 (fn [opcode line]
|
rlm@145
|
45 (println (format "0x%04X: 0x%02X" line opcode)))
|
rlm@145
|
46 (subvec (vec (memory state)) begin end)
|
rlm@145
|
47 (range begin end)))
|
rlm@145
|
48 state)
|
rlm@145
|
49
|
rlm@145
|
50 (defn print-pc [state]
|
rlm@145
|
51 (println (format "PC: 0x%04X" (PC state)))
|
rlm@145
|
52 state)
|
rlm@145
|
53
|
rlm@145
|
54 (defn print-op [state]
|
rlm@145
|
55 (println (format "OP: 0x%02X" (aget (memory state) (PC state))))
|
rlm@145
|
56 state)
|
rlm@145
|
57
|
rlm@145
|
58 (defn d-tick
|
rlm@145
|
59 ([state]
|
rlm@145
|
60 (-> state print-pc print-op tick)))
|
rlm@145
|
61
|
rlm@145
|
62 (defn print-interrupt
|
rlm@145
|
63 [^SaveState state]
|
rlm@145
|
64 (println (format "IE: %d" (IE state)))
|
rlm@145
|
65 state)
|
rlm@145
|
66
|
rlm@145
|
67 (defn set-memory
|
rlm@145
|
68 ([state location value]
|
rlm@145
|
69 (set-state! state)
|
rlm@145
|
70 (let [mem (memory state)]
|
rlm@145
|
71 (aset mem location value)
|
rlm@145
|
72 (write-memory! mem)
|
rlm@145
|
73 (update-state)))
|
rlm@145
|
74 ([location value]
|
rlm@145
|
75 (set-memory @current-state location value)))
|
rlm@145
|
76
|
rlm@145
|
77 (defn set-memory-range
|
rlm@145
|
78 ([state start values]
|
rlm@145
|
79 (set-state! state)
|
rlm@145
|
80 (let [mem (memory state)]
|
rlm@145
|
81
|
rlm@145
|
82 (dorun (map (fn [index val]
|
rlm@145
|
83 (aset mem index val))
|
rlm@145
|
84 (range start
|
rlm@145
|
85 (+ start (count values))) values))
|
rlm@145
|
86 (write-memory! mem)
|
rlm@145
|
87 (update-state)))
|
rlm@145
|
88 ([start values]
|
rlm@145
|
89 (set-memory-range
|
rlm@145
|
90 @current-state start values)))
|
rlm@145
|
91
|
rlm@145
|
92 (defn common-differences [& seqs]
|
rlm@145
|
93 (let [backbone (range (count (first seqs)))]
|
rlm@145
|
94 (filter
|
rlm@145
|
95 (comp (partial apply distinct?) second)
|
rlm@145
|
96 (zipmap backbone
|
rlm@145
|
97 (apply (partial map list) seqs)))))
|
rlm@145
|
98
|
rlm@145
|
99 (defn mid-game []
|
rlm@145
|
100 (read-state "mid-game"))
|
rlm@154
|
101
|
rlm@154
|
102
|
rlm@154
|
103 |