view clojure/com/aurellem/assembly.clj @ 111:8be7ce890184

now I have an assembly program that can read button presses!
author Robert McIntyre <rlm@mit.edu>
date Thu, 15 Mar 2012 17:20:55 -0500
parents f9dee79b2215
children 6fe33bb5ea22
line wrap: on
line source
1 (ns com.aurellem.assembly
2 (:use (com.aurellem gb-driver vbm title items))
3 (:import [com.aurellem.gb_driver SaveState]))
5 (defn mid-game []
6 (read-state "mid-game"))
8 (defn inject-assembly
9 ([^SaveState state
10 program-counter registers
11 assembly-code]
12 (let [scratch-memory (memory state)]
13 ;; inject assembly code
14 (dorun (map (fn [index val]
15 (aset scratch-memory index val))
16 (range program-counter
17 (+ program-counter (count assembly-code)))
18 assembly-code))
19 (-> state
20 (write-memory! scratch-memory)
21 (write-registers! registers)
22 (PC! program-counter))))
23 ;;([program-counter]
24 ;; (fn [^SaveState state registers assembly-code]
25 ;; (inject-assembly state program-counter registers assembly-code)))
26 )
29 ;;(def inject-assembly-item
30 ;; (inject-assembly (inc item-list-start)))
32 (defn inject-item-assembly
33 ([^SaveState state assembly-code]
34 (inject-assembly state (inc item-list-start)
35 (registers state)
36 assembly-code))
37 ([assembly-code]
38 (inject-item-assembly @current-state assembly-code)))
40 (defn info
41 ([^SaveState state]
42 (println (format "PC: 0x%04X" (PC state)))
43 (println "Instruction:"
44 (format "0x%02X" (aget (memory state) (PC state))))
45 state))
47 (defn print-interrupt
48 [^SaveState state]
49 (println (format "IE: %d" (IE state)))
50 state)
52 (defn run-assembly
53 ([info-fn assembly n]
54 (let [final-state
55 (reduce (fn [state _]
56 (tick (info-fn state)))
57 (inject-item-assembly
58 (mid-game) assembly)
59 (range n))]
60 final-state))
61 ([assembly n]
62 (run-assembly info assembly n)))
65 (def buttons-port 0xFF00)
68 (defn A [state]
69 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
71 (defn binary-str [num]
72 (format "%08d"
73 (Integer/parseInt
74 (Integer/toBinaryString num) 10)))
76 (defn view-register [state name reg-fn]
77 (println (format "%s: %s" name
78 (binary-str (reg-fn state))))
79 state)
82 (defn view-memory [state mem]
83 (println (format "mem 0x%04X = %s" mem
84 (binary-str (aget (memory state) mem))))
85 state)
87 (defn read-buttons []
88 (-> (tick (mid-game))
89 (IE! 0) ; disable interrupts
90 (inject-item-assembly
91 (concat
92 ;; write 00010000 to 0xFF00 to select joypad
93 [0x18 ;D31D ; jump over
94 0x01 ;D31E ; the next 8 bits
95 (Integer/parseInt "00100000" 2) ;D31F data section
97 0xFA ;D320 ; load (D31F) into A
98 0x1F ;D321 -->
99 0xD3 ;D322 --> D31F
101 0xEA ;D323 ; load (A), which is
102 0x00 ;D324 --> ; 00010000, into FF00
103 0xFF ;D325 --> FF00
105 0x18 ;D326 ; this is the place where
106 0x01 ;D327 ; we will store whether
107 0x00 ;D328 ; "down" is pressed.
109 0xFA ;D329 ; (FF00) -> A
110 0x00 ;D32A
111 0xFF ;D32B
113 0xCB ;D32C ; Test whether "down"
114 0x5F ;D32D ; is pressed.
116 0x28 ;D32E ; if down is pressed,
117 0x03 ;D32F ; skip the next section
118 ; of code.
119 ;; down-is-not-pressed
120 0xC3 ;D330
121 0x1D ;D331 ; return to beginning
122 0xD3 ;D332
124 ;; down-is-pressed
125 0xEA ;D334 ; write A to D328 if
126 0x28 ;D335 ; "down" was pressed
127 0xD3 ;D336
129 0xC3 ;D330
130 0x1D ;D331 ; return to beginning
131 0xD3 ;D332
132 ]
134 []))))
137 (defn run-a-bit []
138 (-> (read-buttons)
139 (info)
140 (tick) ;; skip over data section
141 (info)
142 (view-register "Register A" A)
143 (tick) ;; load-data into A
144 (view-register "Register A" A)
145 (info)
146 (view-memory 0xFF00)
147 (tick) ;; load A into 0xFF00
148 (view-memory 0xFF00)
149 (info)
150 (tick)
151 (info)
152 (tick)
153 (info)
154 (tick)
155 (info)
156 (tick)
157 (info)
158 (tick)
159 (info)
160 (tick)
161 (print-inventory)))
164 (comment
165 (view-memory (step (step (read-buttons) [:d])) 0xD328)
166 ;;mem 0xD328 = 11100111
167 (view-memory (step (step (read-buttons))) 0xD328)
168 ;;mem 0xD328 = 00000000
169 )
171 (defn trace [state]
172 (loop [program-counters []
173 opcodes []]
174 (let [frame-boundary?
175 (com.aurellem.gb.Gb/tick)]
176 (println (count opcodes))
177 (if frame-boundary?
178 [program-counters opcodes]
179 (recur
180 (conj program-counters
181 (first (registers @current-state)))
182 (conj opcodes
183 (aget (memory @current-state)
184 (PC @current-state))))))))
187 (defn good-trace []
188 (-> (mid-game) (tick) (IE! 0)
189 (set-inv-mem [0x00 0x00 0X00 0x00])
190 (PC! item-list-start)(print-interrupt)
191 (info) (tick) (info) (tick) (info)))