view clojure/com/aurellem/assembly.clj @ 114:a454730d92dd

added test for count-frames
author Robert McIntyre <rlm@mit.edu>
date Fri, 16 Mar 2012 13:51:17 -0500
parents 0831da75d2c5
children 39fb0cbab25e
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)))))
24 (defn inject-item-assembly
25 ([^SaveState state assembly-code]
26 (inject-assembly state (inc item-list-start)
27 (registers state)
28 assembly-code))
29 ([assembly-code]
30 (inject-item-assembly @current-state assembly-code)))
32 (defn info
33 ([^SaveState state]
34 (println (format "PC: 0x%04X" (PC state)))
35 (println "Instruction:"
36 (format "0x%02X" (aget (memory state) (PC state))))
37 state))
39 (defn print-interrupt
40 [^SaveState state]
41 (println (format "IE: %d" (IE state)))
42 state)
44 (defn run-assembly
45 ([info-fn assembly n]
46 (let [final-state
47 (reduce (fn [state _]
48 (tick (info-fn state)))
49 (inject-item-assembly
50 (mid-game) assembly)
51 (range n))]
52 final-state))
53 ([assembly n]
54 (run-assembly info assembly n)))
56 (def buttons-port 0xFF00)
58 (defn A [state]
59 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
61 (defn binary-str [num]
62 (format "%08d"
63 (Integer/parseInt
64 (Integer/toBinaryString num) 10)))
66 (defn view-register [state name reg-fn]
67 (println (format "%s: %s" name
68 (binary-str (reg-fn state))))
69 state)
72 (defn view-memory [state mem]
73 (println (format "mem 0x%04X = %s" mem
74 (binary-str (aget (memory state) mem))))
75 state)
77 (defn read-down-button []
78 (-> (tick (mid-game))
79 (IE! 0) ; disable interrupts
80 (inject-item-assembly
81 (concat
82 ;; write 00010000 to 0xFF00 to select joypad
83 [0x18 ;D31D ; jump over
84 0x01 ;D31E ; the next 8 bits
85 ;D31F
86 (Integer/parseInt "00100000" 2) ; data section
88 0xFA ;D320 ; load (D31F) into A
89 0x1F ;D321 -->
90 0xD3 ;D322 --> D31F
92 0xEA ;D323 ; load (A), which is
93 0x00 ;D324 --> ; 00010000, into FF00
94 0xFF ;D325 --> FF00
96 0x18 ;D326 ; this is the place where
97 0x01 ;D327 ; we will store whether
98 0x00 ;D328 ; "down" is pressed.
100 0xFA ;D329 ; (FF00) -> A
101 0x00 ;D32A
102 0xFF ;D32B
104 0xCB ;D32C ; Test whether "down"
105 0x5F ;D32D ; is pressed.
107 0x28 ;D32E ; if down is pressed,
108 0x03 ;D32F ; skip the next section
109 ; of code.
110 ;; down-is-not-pressed
111 0xC3 ;D330
112 0x1D ;D331 ; return to beginning
113 0xD3 ;D332
115 ;; down-is-pressed
116 0xEA ;D334 ; write A to D328 if
117 0x28 ;D335 ; "down" was pressed
118 0xD3 ;D336
120 0xC3 ;D330
121 0x1D ;D331 ; return to beginning
122 0xD3 ;D332
123 ]
125 []))))
128 ;; specs for main bootstrap program
129 ;; starts in "mode-select" mode
130 ;; Each button press takes place in a single frame.
131 ;; mode-select-mode takes one of the main buttons
132 ;; which selects one of up to eight modes
133 ;; mode 1 activated by the "A" button
134 ;; the next two button presses indicates the start
135 ;; memory location which to which the bootstrap
136 ;; program will write.
137 ;; This is done by using each of the eight buttons to
138 ;; spell out an 8 bit number. The order of buttons is
139 ;; ["A" "B" "start" "select" "up" "right" "down" "left"],
140 ;; [:a :start :l] --> 10100001
142 ;; the next button press determines how many bytes are to be
143 ;; written, starting at the start position.
145 ;; then, the actual bytes are entered and are written to the
146 ;; start address in sequence.
149 (defn count-frames []
150 (-> (tick (mid-game))
151 (IE! 0) ; disable interrupts
152 (inject-item-assembly
153 ;; write 00010000 to 0xFF00 to select joypad
154 [0x18 ;D31D ; jump over
155 0x02 ;D31E ; the next 2 bytes
156 0x00 ;D31F ; frame-count
157 0x00 ;D320 ; v-blank-prev
159 0xFA ;D321
160 0x41 ;D322 ; load (FF41) into A
161 0xFF ;D323 ; this contains mode flags
165 ;; if we're in v-blank, the bit-1 is 0
166 ;; and bit-2 is 1 Otherwise, it is not v-blank.
167 0xCB ;D324 ; test bit-1 of A
168 0x4F ;D325
170 0xC2 ;D326 ; if bit-1 is not 0
171 0x43 ;D327 ; GOTO not-v-blank
172 0xD3 ;D328
174 0xCB ;D329 ; test bit-0 of A
175 0x47 ;D32A
177 0xCA ;D32B ; if bit-0 is not 1
178 0x43 ;D32C ; GOTO not-v-blank
179 0xD3 ;D32D
181 ;;; in v-blank mode
183 ;; if v-blank-prev was 0,
184 ;; increment frame-count
186 0xFA ;D32E ; load v-blank-prev to A
187 0x20 ;D32F
188 0xD3 ;D330
190 0xCB ;D331
191 0x47 ;D332 ; test bit-0 of A
193 0x20 ;D333 ; skip next section
194 0x07 ;D334 ; if v-blank-prev was not zero
196 ;; v-blank was 0, increment frame-count
197 0xFA ;D335 ; load frame-count into A
198 0x1F ;D336
199 0xD3 ;D337
201 0x3C ;D338 ; inc A
203 0xEA ;D339 ; load A into frame-count
204 0x1F ;D33A
205 0xD3 ;D33B
207 ;; set v-blank-prev to 1
208 0x3E ;D33C ; load 1 into A
209 0x01 ;D33D
211 0xEA ;D33E ; load A into v-blank-prev
212 0x20 ;D33F
213 0xD3 ;D340
215 0x18 ;D341 ; skip not-in-v-blank section
216 0x05 ;D342
218 ;;; not in v-blank mode
219 ;; set v-blank-prev to 0
220 0x3E ;D343 ; load 0 into A
221 0x00 ;D344
223 0xEA ;D345 ; load A into v-blank-prev
224 0x20 ;D346
225 0xD3 ;D347
228 0xC3 ;D348 ; return to beginning
229 0x1D ;D349
230 0xD3 ;D34A
231 ])))
233 (defn step-count-frames []
234 (-> (read-down-button)
235 (info)
236 (tick) ;; skip over data section
237 (info)
238 (view-register "Register A" A)
239 (tick) ;; load-data into A
240 (view-register "Register A" A)
241 (info)
242 (view-memory 0xFF00)
243 (tick) ;; load A into 0xFF00
244 (view-memory 0xFF00)
245 (info)
246 (tick)
247 (info)
248 (tick)
249 (info)
250 (tick)
251 (info)
252 (tick)
253 (info)
254 (tick)
255 (info)
256 (tick)
257 (print-inventory)))
259 (defn test-read-down []
260 (= (view-memory (step (step (read-buttons) [:d])) 0xD328)
261 (view-memory (step (step (read-buttons))) 0xD328)))
263 (defn trace [state]
264 (loop [program-counters []
265 opcodes []]
266 (let [frame-boundary?
267 (com.aurellem.gb.Gb/tick)]
268 (println (count opcodes))
269 (if frame-boundary?
270 [program-counters opcodes]
271 (recur
272 (conj program-counters
273 (first (registers @current-state)))
274 (conj opcodes
275 (aget (memory @current-state)
276 (PC @current-state))))))))
278 (defn good-trace []
279 (-> (mid-game) (tick) (IE! 0)
280 (set-inv-mem [0x00 0x00 0X00 0x00])
281 (PC! item-list-start)(print-interrupt)
282 (info) (tick) (info) (tick) (info)))