rlm@105
|
1 (ns com.aurellem.assembly
|
rlm@105
|
2 (:use (com.aurellem gb-driver vbm title items))
|
rlm@105
|
3 (:import [com.aurellem.gb_driver SaveState]))
|
rlm@105
|
4
|
rlm@106
|
5 (defn mid-game []
|
rlm@106
|
6 (read-state "mid-game"))
|
rlm@105
|
7
|
rlm@105
|
8 (defn inject-assembly
|
rlm@107
|
9 ([^SaveState state
|
rlm@105
|
10 program-counter registers
|
rlm@105
|
11 assembly-code]
|
rlm@105
|
12 (let [scratch-memory (memory state)]
|
rlm@105
|
13 ;; inject assembly code
|
rlm@105
|
14 (dorun (map (fn [index val]
|
rlm@105
|
15 (aset scratch-memory index val))
|
rlm@105
|
16 (range program-counter
|
rlm@105
|
17 (+ program-counter (count assembly-code)))
|
rlm@105
|
18 assembly-code))
|
rlm@106
|
19 (-> state
|
rlm@106
|
20 (write-memory! scratch-memory)
|
rlm@106
|
21 (write-registers! registers)
|
rlm@106
|
22 (PC! program-counter))))
|
rlm@107
|
23 ;;([program-counter]
|
rlm@107
|
24 ;; (fn [^SaveState state registers assembly-code]
|
rlm@107
|
25 ;; (inject-assembly state program-counter registers assembly-code)))
|
rlm@107
|
26 )
|
rlm@107
|
27
|
rlm@107
|
28
|
rlm@107
|
29 ;;(def inject-assembly-item
|
rlm@107
|
30 ;; (inject-assembly (inc item-list-start)))
|
rlm@105
|
31
|
rlm@105
|
32 (defn inject-item-assembly
|
rlm@105
|
33 ([^SaveState state assembly-code]
|
rlm@105
|
34 (inject-assembly state (inc item-list-start)
|
rlm@105
|
35 (registers state)
|
rlm@105
|
36 assembly-code))
|
rlm@105
|
37 ([assembly-code]
|
rlm@105
|
38 (inject-item-assembly @current-state assembly-code)))
|
rlm@105
|
39
|
rlm@105
|
40 (defn info
|
rlm@105
|
41 ([^SaveState state]
|
rlm@107
|
42 (println (format "PC: 0x%04X" (PC state)))
|
rlm@106
|
43 (println "Instruction:"
|
rlm@106
|
44 (format "0x%02X" (aget (memory state) (PC state))))
|
rlm@105
|
45 state))
|
rlm@105
|
46
|
rlm@107
|
47 (defn print-interrupt
|
rlm@107
|
48 [^SaveState state]
|
rlm@107
|
49 (println (format "IE: %d" (IE state)))
|
rlm@107
|
50 state)
|
rlm@107
|
51
|
rlm@105
|
52 (defn run-assembly
|
rlm@105
|
53 ([info-fn assembly n]
|
rlm@105
|
54 (let [final-state
|
rlm@105
|
55 (reduce (fn [state _]
|
rlm@105
|
56 (tick (info-fn state)))
|
rlm@107
|
57 (inject-item-assembly
|
rlm@107
|
58 (mid-game) assembly)
|
rlm@105
|
59 (range n))]
|
rlm@105
|
60 final-state))
|
rlm@105
|
61 ([assembly n]
|
rlm@105
|
62 (run-assembly info assembly n)))
|
rlm@107
|
63
|
rlm@107
|
64
|
rlm@107
|
65 (def buttons-port 0xFF00)
|
rlm@107
|
66
|
rlm@107
|
67 (defn view-register [state name reg-fn]
|
rlm@107
|
68 (println (format "%s : 0x%02X" name (reg-fn state)))
|
rlm@107
|
69 state)
|
rlm@107
|
70
|
rlm@107
|
71 (defn A [state]
|
rlm@107
|
72 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
|
rlm@107
|
73
|
rlm@107
|
74 (defn view-memory [state mem]
|
rlm@107
|
75 (println (format "mem 0x%04X = 0x%02X" mem (aget (memory state) mem)))
|
rlm@107
|
76 state)
|
rlm@107
|
77
|
rlm@107
|
78 (defn read-buttons []
|
rlm@107
|
79 (let [button-pressed (tick (step (mid-game) [:d]))]
|
rlm@107
|
80 (-> button-pressed
|
rlm@109
|
81 (IE! 0) ; disable interrupts
|
rlm@107
|
82 (inject-item-assembly
|
rlm@107
|
83 (concat
|
rlm@107
|
84 ;; write 00010000 to 0xFF00 to select joypad
|
rlm@107
|
85 [0x18 ;D31D ; jump over
|
rlm@107
|
86 0x01 ;D31E ; the next 8 bits
|
rlm@107
|
87 (Integer/parseInt "00010000" 2) ;D31F data section
|
rlm@107
|
88 0x00 ;D320 ; take a break
|
rlm@107
|
89 0xFA ;D321 ; put DC15 into A
|
rlm@107
|
90 0x1F ;D322 --]
|
rlm@107
|
91 0xD3 ;D323 --] DC1F ; data section
|
rlm@107
|
92 0x01 ;D324 ; load 0xFF00 into BC
|
rlm@107
|
93 0x00 ;D325
|
rlm@107
|
94 0xFF ;D326
|
rlm@109
|
95 0x02 ;D327 (BC)->A ; load (00010000) into FF00
|
rlm@107
|
96 ; to select directional
|
rlm@107
|
97 ; buttons
|
rlm@107
|
98
|
rlm@107
|
99 ]
|
rlm@107
|
100
|
rlm@107
|
101 []))
|
rlm@107
|
102 (info)
|
rlm@107
|
103 (tick) ;; skip over data section
|
rlm@107
|
104 (info)
|
rlm@107
|
105 (tick) ;; no-op
|
rlm@107
|
106 (info)
|
rlm@107
|
107 (view-register "Register A" A)
|
rlm@107
|
108 (tick) ;; load-data into A
|
rlm@107
|
109 (view-register "Register A" A)
|
rlm@107
|
110 (info)
|
rlm@107
|
111 (view-register "Register BC" BC)
|
rlm@108
|
112 (tick) ;; store 0xFFOO into BC
|
rlm@108
|
113 (view-register "Register BC" BC)
|
rlm@108
|
114 (info)
|
rlm@108
|
115 (view-memory 0xFF00)
|
rlm@107
|
116 (tick) ;; load A into 0xFF00
|
rlm@108
|
117 (view-memory 0xFF00)
|
rlm@107
|
118 )))
|
rlm@107
|
119
|
rlm@107
|
120
|
rlm@107
|
121 (defn trace [state]
|
rlm@107
|
122 (loop [program-counters []
|
rlm@107
|
123 opcodes []]
|
rlm@107
|
124 (let [frame-boundary?
|
rlm@107
|
125 (com.aurellem.gb.Gb/tick)]
|
rlm@107
|
126 (println (count opcodes))
|
rlm@107
|
127 (if frame-boundary?
|
rlm@107
|
128 [program-counters opcodes]
|
rlm@107
|
129 (recur
|
rlm@107
|
130 (conj program-counters
|
rlm@107
|
131 (first (registers @current-state)))
|
rlm@107
|
132 (conj opcodes
|
rlm@107
|
133 (aget (memory @current-state)
|
rlm@107
|
134 (PC @current-state))))))))
|
rlm@107
|
135
|
rlm@107
|
136
|
rlm@107
|
137 (defn good-trace []
|
rlm@107
|
138 (-> (mid-game) (tick) (IE! 0)
|
rlm@107
|
139 (set-inv-mem [0x00 0x00 0X00 0x00])
|
rlm@107
|
140 (PC! item-list-start)(print-interrupt)
|
rlm@107
|
141 (info) (tick) (info) (tick) (info))) |