view clojure/com/aurellem/assembly.clj @ 122:e85b53994fac

working on proper timing for modes
author Robert McIntyre <rlm@mit.edu>
date Fri, 16 Mar 2012 20:55:28 -0500
parents 744de3427c05
children c9a280b8bd1c
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 print-listing [state begin end]
45 (dorun (map
46 (fn [opcode line]
47 (println (format "0x%04X: 0x%02X" line opcode)))
48 (subvec (vec (memory state)) begin end)
49 (range begin end)))
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)))
64 (def buttons-port 0xFF00)
66 (defn A [state]
67 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
69 (defn binary-str [num]
70 (format "%08d"
71 (Integer/parseInt
72 (Integer/toBinaryString num) 10)))
74 (defn view-register [state name reg-fn]
75 (println (format "%s: %s" name
76 (binary-str (reg-fn state))))
77 state)
79 (defn view-memory [state mem]
80 (println (format "mem 0x%04X = %s" mem
81 (binary-str (aget (memory state) mem))))
82 state)
84 (defn trace [state]
85 (loop [program-counters []
86 opcodes []]
87 (let [frame-boundary?
88 (com.aurellem.gb.Gb/tick)]
89 (println (count opcodes))
90 (if frame-boundary?
91 [program-counters opcodes]
92 (recur
93 (conj program-counters
94 (first (registers @current-state)))
95 (conj opcodes
96 (aget (memory @current-state)
97 (PC @current-state))))))))
99 (defn good-trace []
100 (-> (mid-game) (tick) (IE! 0)
101 (set-inv-mem [0x00 0x00 0X00 0x00])
102 (PC! item-list-start)(print-interrupt)
103 (info) (tick) (info) (tick) (info)))
105 (defn read-down-button []
106 (-> (tick (mid-game))
107 (IE! 0) ; disable interrupts
108 (inject-item-assembly
109 ;; write 00010000 to 0xFF00 to select joypad
110 [0x18 ;D31D ; jump over
111 0x01 ;D31E ; the next 8 bits
112 ;D31F
113 (Integer/parseInt "00100000" 2) ; data section
115 0xFA ;D320 ; load (D31F) into A
116 0x1F ;D321 -->
117 0xD3 ;D322 --> D31F
119 0xEA ;D323 ; load (A), which is
120 0x00 ;D324 --> ; 00010000, into FF00
121 0xFF ;D325 --> FF00
123 0x18 ;D326 ; this is the place where
124 0x01 ;D327 ; we will store whether
125 0x00 ;D328 ; "down" is pressed.
127 0xFA ;D329 ; (FF00) -> A
128 0x00 ;D32A
129 0xFF ;D32B
131 0xCB ;D32C ; Test whether "down"
132 0x5F ;D32D ; is pressed.
134 0x28 ;D32E ; if down is pressed,
135 0x03 ;D32F ; skip the next section
136 ; of code.
137 ;; down-is-not-pressed
138 0xC3 ;D330
139 0x1D ;D331 ; return to beginning
140 0xD3 ;D332
142 ;; down-is-pressed
143 0xEA ;D334 ; write A to D328 if
144 0x28 ;D335 ; "down" was pressed
145 0xD3 ;D336
147 0xC3 ;D330
148 0x1D ;D331 ; return to beginning
149 0xD3 ;D332
150 ])))
152 (defn test-read-down []
153 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)
154 (view-memory (step (step (read-down-button))) 0xD328)))
156 (defn count-frames []
157 (-> (tick (mid-game))
158 (IE! 0) ; disable interrupts
159 (inject-item-assembly
160 [0x18 ;D31D ; jump over
161 0x02 ;D31E ; the next 2 bytes
162 0x00 ;D31F ; frame-count
163 0x00 ;D320 ; v-blank-prev
165 0xFA ;D321
166 0x41 ;D322 ; load (FF41) into A
167 0xFF ;D323 ; this contains mode flags
169 ;; if we're in v-blank, the bit-1 is 0
170 ;; and bit-2 is 1 Otherwise, it is not v-blank.
171 0xCB ;D324 ; test bit-1 of A
172 0x4F ;D325
174 0xC2 ;D326 ; if bit-1 is not 0
175 0x44 ;D327 ; GOTO not-v-blank
176 0xD3 ;D328
178 0xCB ;D329 ; test bit-0 of A
179 0x47 ;D32A
181 0xCA ;D32B ; if bit-0 is not 1
182 0x44 ;D32C ; GOTO not-v-blank
183 0xD3 ;D32D
185 ;;; in v-blank mode
187 ;; if v-blank-prev was 0,
188 ;; increment frame-count
190 0xFA ;D32E ; load v-blank-prev to A
191 0x20 ;D32F
192 0xD3 ;D330
194 0xCB ;D331
195 0x47 ;D332 ; test bit-0 of A
197 0x20 ;D333 ; skip next section
198 0x07 ;D334 ; if v-blank-prev was not zero
200 ;; v-blank was 0, increment frame-count
201 0xFA ;D335 ; load frame-count into A
202 0x1F ;D336
203 0xD3 ;D337
205 0x3C ;D338 ; inc A
207 0xEA ;D339 ; load A into frame-count
208 0x1F ;D33A
209 0xD3 ;D33B
211 ;; set v-blank-prev to 1
212 0x3E ;D33C ; load 1 into A
213 0x01 ;D33D
215 0xEA ;D33E ; load A into v-blank-prev
216 0x20 ;D33F
217 0xD3 ;D340
219 0xC3 ;D341 ; return to beginning
220 0x1D ;D342
221 0xD3 ;D343
223 ;;; not in v-blank mode
224 ;; set v-blank-prev to 0
225 0x3E ;D344 ; load 0 into A
226 0x00 ;D345
228 0xEA ;D346 ; load A into v-blank-prev
229 0x20 ;D347
230 0xD3 ;D348
232 0xC3 ;D349 ; return to beginning
233 0x1D ;D34A
234 0xD3 ;D34B
235 ])))
237 (defn step-count-frames []
238 (-> (read-down-button)
239 (info)
240 (tick) ;; skip over data section
241 (info)
242 (view-register "Register A" A)
243 (tick) ;; load-data into A
244 (view-register "Register A" A)
245 (info)
246 (view-memory 0xFF00)
247 (tick) ;; load A into 0xFF00
248 (view-memory 0xFF00)
249 (info)
250 (tick)
251 (info)
252 (tick)
253 (info)
254 (tick)
255 (info)
256 (tick)
257 (info)
258 (tick)
259 (info)
260 (tick)
261 (print-inventory)))
263 (defn test-count-frames []
264 (= 255 (aget (memory ((apply comp (repeat 255 step))
265 (count-frames)))
266 0xD31F)))
268 ;; specs for main bootstrap program
269 ;; starts in "mode-select" mode
270 ;; Each button press takes place in a single frame.
271 ;; mode-select-mode takes one of the main buttons
272 ;; which selects one of up to eight modes
273 ;; mode 1 activated by the "A" button
274 ;; the next two button presses indicates the start
275 ;; memory location which to which the bootstrap
276 ;; program will write.
277 ;; This is done by using each of the eight buttons to
278 ;; spell out an 8 bit number. The order of buttons is
279 ;; [:d :u :l :r :start :select :b :a]
280 ;; [:a :start :l] --> 00101001
282 ;; the next button press determines how many bytes are to be
283 ;; written, starting at the start position.
285 ;; then, the actual bytes are entered and are written to the
286 ;; start address in sequence.
288 (defn input-number-assembly []
289 [0x18 ;D31D ; jump over
290 0x02 ;D31E ; the next 2 bytes
291 0x00 ;D31F ; frame-count
292 0x00 ;D320 ; v-blank-prev
294 0xFA ;D321
295 0x41 ;D322 ; load (FF41) into A
296 0xFF ;D323 ; this contains mode flags
298 ;; if we're in v-blank, the bit-1 is 0
299 ;; and bit-2 is 1 Otherwise, it is not v-blank.
300 0xCB ;D324 ; test bit-1 of A
301 0x4F ;D325
303 0xC2 ;D326 ; if bit-1 is not 0
304 0x44 ;D327 ; GOTO not-v-blank
305 0xD3 ;D328
307 0xCB ;D329 ; test bit-0 of A
308 0x47 ;D32A
310 0xCA ;D32B ; if bit-0 is not 1
311 0x44 ;D32C ; GOTO not-v-blank
312 0xD3 ;D32D
314 ;;; in v-blank mode
316 ;; if v-blank-prev was 0,
317 ;; increment frame-count
319 0xFA ;D32E ; load v-blank-prev to A
320 0x20 ;D32F
321 0xD3 ;D330
323 0xCB ;D331
324 0x47 ;D332 ; test bit-0 of A
326 0x20 ;D333 ; skip next section
327 0x07 ;D334 ; if v-blank-prev was not zero
329 ;; v-blank was 0, increment frame-count
330 0xFA ;D335 ; load frame-count into A
331 0x1F ;D336
332 0xD3 ;D337
334 0x3C ;D338 ; inc A
336 0xEA ;D339 ; load A into frame-count
337 0x1F ;D33A
338 0xD3 ;D33B
340 ;; set v-blank-prev to 1
341 0x3E ;D33C ; load 1 into A
342 0x01 ;D33D
344 0xEA ;D33E ; load A into v-blank-prev
345 0x20 ;D33F
346 0xD3 ;D340
348 0xC3 ;D341 ; GOTO input handling code
349 0x4E ;D342
350 0xD3 ;D343
352 ;;; not in v-blank mode
353 ;; set v-blank-prev to 0
354 0x3E ;D344 ; load 0 into A
355 0x00 ;D345
357 0xEA ;D346 ; load A into v-blank-prev
358 0x20 ;D347
359 0xD3 ;D348
361 0xC3 ;D349 ; return to beginning
362 0x1D ;D34A
363 0xD3 ;D34B
365 0x00 ;D34C ; these are here
366 0x00 ;D34D ; for glue
369 ;;; calculate input number based on button presses
370 0x18 ;D34E ; skip next 3 bytes
371 0x03 ;D34F
372 ;D350
373 (Integer/parseInt "00100000" 2) ; select directional pad
374 ;D351
375 (Integer/parseInt "00010000" 2) ; select buttons
376 0x00 ;D352 ; input-number
378 ;; select directional pad, store low bits in B
380 0xFA ;D353 ; load (D350) into A
381 0x50 ;D354 -->
382 0xD3 ;D355 --> D31F
384 0xEA ;D356 ; load (A), which is
385 0x00 ;D357 --> ; 00010000, into FF00
386 0xFF ;D358 --> FF00
388 0x06 ;D359
389 ;D35A
390 (Integer/parseInt "11110000" 2) ; "11110000" -> B
391 0xFA ;D35B ; (FF00) -> A
392 0x00 ;D35C
393 0xFF ;D35D
395 0xCB ;D35E ; swap nybbles on A
396 0x37 ;D35F
397 0xA0 ;D360 ; (AND A B) -> A
398 0x47 ;D361 ; A -> B
400 ;; select buttons store bottom bits in C
402 0xFA ; ; load (D351) into A
403 0x51 ; -->
404 0xD3 ; --> D31F
406 0xEA ; ; load (A), which is
407 0x00 ; --> ; 00001000, into FF00
408 0xFF ; --> FF00
410 0x0E ;
411 (Integer/parseInt "00001111" 2) ; "00001111" -> C
413 0xFA ; ; (FF00) -> A
414 0x00 ;
415 0xFF ;
417 0xA1 ; ; (AND A C) -> A
418 0x4F ; ; A -> C
420 ;; combine the B and C registers into the input number
421 0x79 ; ; C -> A
422 0xB0 ; ; (OR A B) -> A
423 0x2F ; ; negate A
425 0xEA ; ; store A into input-number
426 0x52 ;
427 0xD3 ;
429 0xC3 ; ; return to beginning
430 0x1D ;
431 0xD3 ;
432 ])
434 (defn input-number []
435 (-> (tick (mid-game))
436 (IE! 0) ; disable interrupts
437 (inject-item-assembly (input-number-assembly))))
439 (defn test-input-number
440 "Input freestyle buttons and observe the effects at the repl."
441 []
442 (set-state! (input-number))
443 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
445 (defn write-memory-assembly []
446 [0x18 ;D31D ; jump over
447 0x02 ;D31E ; the next 2 bytes
448 0x00 ;D31F ; frame-count
449 0x00 ;D320 ; v-blank-prev
451 0xFA ;D321
452 0x41 ;D322 ; load (FF41) into A
453 0xFF ;D323 ; this contains mode flags
455 ;; if we're in v-blank, the bit-1 is 0
456 ;; and bit-2 is 1 Otherwise, it is not v-blank.
457 0xCB ;D324 ; test bit-1 of A
458 0x4F ;D325
460 0xC2 ;D326 ; if bit-1 is not 0
461 0x44 ;D327 ; GOTO not-v-blank
462 0xD3 ;D328
464 0xCB ;D329 ; test bit-0 of A
465 0x47 ;D32A
467 0xCA ;D32B ; if bit-0 is not 1
468 0x44 ;D32C ; GOTO not-v-blank
469 0xD3 ;D32D
471 ;;; in v-blank mode
473 ;; if v-blank-prev was 0,
474 ;; increment frame-count
476 0xFA ;D32E ; load v-blank-prev to A
477 0x20 ;D32F
478 0xD3 ;D330
480 0xCB ;D331
481 0x47 ;D332 ; test bit-0 of A
483 0x20 ;D333 ; skip next section
484 0x07 ;D334 ; if v-blank-prev was not zero
486 ;; v-blank was 0, increment frame-count
487 0xFA ;D335 ; load frame-count into A
488 0x1F ;D336
489 0xD3 ;D337
491 0x3C ;D338 ; inc A
493 0xEA ;D339 ; load A into frame-count
494 0x1F ;D33A
495 0xD3 ;D33B
497 ;; set v-blank-prev to 1
498 0x3E ;D33C ; load 1 into A
499 0x01 ;D33D
501 0xEA ;D33E ; load A into v-blank-prev
502 0x20 ;D33F
503 0xD3 ;D340
505 0xC3 ;D341 ; GOTO input handling code
506 0x4E ;D342
507 0xD3 ;D343
509 ;;; not in v-blank mode
510 ;; set v-blank-prev to 0
511 0x3E ;D344 ; load 0 into A
512 0x00 ;D345
514 0xEA ;D346 ; load A into v-blank-prev
515 0x20 ;D347
516 0xD3 ;D348
518 0xC3 ;D349 ; return to beginning
519 0x1D ;D34A
520 0xD3 ;D34B
522 0x00 ;D34C ; these are here
523 0x00 ;D34D ; for glue
526 ;;; calculate input number based on button presses
527 0x18 ;D34E ; skip next 3 bytes
528 0x03 ;D34F
529 ;D350
530 (Integer/parseInt "00100000" 2) ; select directional pad
531 ;D351
532 (Integer/parseInt "00010000" 2) ; select buttons
533 0x00 ;D352 ; input-number
535 ;; select directional pad, store low bits in B
537 0xFA ;D353 ; load (D350) into A
538 0x50 ;D354 -->
539 0xD3 ;D355 --> D31F
541 0xEA ;D356 ; load (A), which is
542 0x00 ;D357 --> ; 00010000, into FF00
543 0xFF ;D358 --> FF00
545 0x06 ;D359
546 ;D35A
547 (Integer/parseInt "11110000" 2) ; "11110000" -> B
548 0xFA ;D35B ; (FF00) -> A
549 0x00 ;D35C
550 0xFF ;D35D
552 0xCB ;D35E ; swap nybbles on A
553 0x37 ;D35F
554 0xA0 ;D360 ; (AND A B) -> A
555 0x47 ;D361 ; A -> B
557 ;; select buttons store bottom bits in C
559 0xFA ;D362 ; load (D351) into A
560 0x51 ;D363 -->
561 0xD3 ;D364 --> D31F
563 0xEA ;D365 ; load (A), which is
564 0x00 ;D366 --> ; 00001000, into FF00
565 0xFF ;D367 --> FF00
567 0x0E ;D368
568 ;D369
569 (Integer/parseInt "00001111" 2) ; "00001111" -> C
571 0xFA ;D36A ; (FF00) -> A
572 0x00 ;D36B
573 0xFF ;D36C
575 0xA1 ;D36D ; (AND A C) -> A
576 0x4F ;D36E ; A -> C
578 ;; combine the B and C registers into the input number
579 0x79 ;D36F ; C -> A
580 0xB0 ;D370 ; (OR A B) -> A
581 0x2F ;D371 ; negate A
583 0xEA ;D372 ; store A into input-number
584 0x52 ;D373
585 0xD3 ;D374
587 0xC3 ;D375 ; GOTO state machine
588 ;;0x1D
589 0x80 ;D376
590 0xD3 ;D377
592 0x00 ;D378
593 0x00 ;D379
594 0x00 ;D37A
595 0x00 ;D37B ; these are here because
596 0x00 ;D37C ; I messed up :(
597 0x00 ;D37D
598 0x00 ;D37E
599 0x00 ;D37F
601 ;; beginning of main state machine
602 0x18 ;D380 ; Declaration of variables
603 0x05 ;D381 ; 5 variables:
604 0x00 ;D382 ; current-mode
605 0x00 ;D383 ; bytes-to-write
606 0x00 ;D384 ; start-point
607 0x00 ;D385 ; unused
608 0x00 ;D386 ; unused
611 ;; banch on current mode
612 0xFA ;D387 ; load current-mode (0xD382)
613 0x82 ;D388 ; into A
614 0xD3 ;D389
615 0x00 ;D38A
618 ;; GOTO Mode 0 (input-mode) if current-mode is 0
619 0xFE ;D38B
620 0x00 ;D38C ; compare A with 0x00
622 0xCA ;D38D ; goto Mode 0 if A == 0
623 0xA8 ;D38E
624 0xD3 ;D38F
626 ;; GOTO Mode 1 (set-length) if current-mode is 1
627 0xFE ;D390
628 0x01 ;D391 ; compare A with 0x01
630 0xCA ;D392
631 0xB1 ;D393
632 0xD3 ;D394 ; goto Mode 1 if A == 1
634 ;; GOTO Mode 2 (set-start-point) if current mode is 2
635 0xFE ;D395
636 0x02 ;D396 ; compare A with 0x02
638 0xCA ;D397
639 0xBF ;D398
640 0xD3 ;D399 ; goto Mode 2 if A == 2
642 0x00 ;D39A
643 0x00 ;D39B
644 0x00 ;D39C
645 0x00 ;D39D
646 0x00 ;D39E
647 0x00 ;D39F
648 0x00 ;D3A0
649 0x00 ;D3A1
650 0x00 ;D3A2
651 0x00 ;D3A3
652 0x00 ;D3A4
653 ;; End of Mode checking, goto beginning
654 0xC3 ;D3A5
655 0x1D ;D3A6
656 0xD3 ;D3A7
659 ;; Mode 0 -- input-mode mode
660 ;; means that we are waiting for a mode, so set the mode to
661 ;; whatever is currently in input-number. If nothing is
662 ;; entered, then the program stays in input-mode mode
664 ;; set current-mode to input-number
665 0xFA ;D3A8 ; load input-number (0xD352)
666 0x52 ;D3A9 ; into A
667 0xD3 ;D3AA
669 0xEA ;D3AB ; load A into current-mode
670 0x82 ;D3AC ; (0xD382)
671 0xD3 ;D3AD
673 0xC3 ;D3AE ; go back to beginning
674 0x1D ;D3AF
675 0xD3 ;D3B0
676 ;; End Mode 0
679 ;; Mode 1 -- set-length mode
680 ;; This is the header for writing things to memory.
681 ;; User specifies the number of bytes to write.
682 ;; Mode is auto advanced to Mode 2 after this mode
683 ;; completes.
685 ;; Set bytes left to write to input-number;
686 ;; set current-mode to 0x02.
687 0xFA ;D3B1 ; load input-number (0xD352)
688 0x52 ;D3B2 ; into A
689 0xD3 ;D3B3
691 0xEA ;D3B4 ; load A into bytes-left-to-write
692 0x83 ;D3B5 ; (0xD383)
693 0xD3 ;D3B6
695 0x3E ;D3B7 ; load 0x02 into A.
696 0x02 ;D3B8
698 0xEA ;D3B9 ; load A to current-mode
699 0x82 ;D3BA ; advancing from Mode 1 to
700 0xD3 ;D3BB ; Mode 2
702 0xC3 ;D3BC ; go back to beginning
703 0x1D ;D3BD
704 0xD3 ;D3BE
705 ;; End Mode 1
708 ;; Mode 2 -- set start-point mode
709 ;; Final part of the header for writing things to memory.
710 ;; User specifies the start location in RAM to which
711 ;; data will be written.
712 ;; Mode is auto advanced to Mode 3 after this mode completes.
714 ;; Set start-point to input-number;
715 ;; set current mode to 0x03.
716 0xFA ;D3BF ; load input-number (0xD352)
717 0x52 ;D3C0 ; into A
718 0xD3 ;D3C1
720 0xEA ;D3C2 ; load A into start-point
721 0x84 ;D3C3 ; (0xD384)
722 0xD3 ;D3C4
724 0x3E ;D3C5 ; load 0x03 into A.
725 0x03 ;D3C6
727 0xEA ;D3C7 ; load A to current-mode,
728 0x82 ;D3C8 ; advancing from Mode 2 to
729 0xD3 ;D3C9 ; Mode 3.
731 0xC3 ;D3CA ; go back to beginning
732 0x1D ;D3CB
733 0xD3 ;D3CC
734 ;;End Mode 2
736 0x00 ;D3CD
737 0x00 ;D3CE
738 0x00 ;D3CF
739 0x00 ;D3D0
740 0x00 ;D3D1
741 0x00 ;D3D2
742 0x00 ;D3D3
743 0x00 ;D3D4
744 0x00 ;D3D5
745 0x00 ;D3D6
747 ;; Mode 3 -- write bytes mode
748 ;; This is where RAM manipulation happens.
749 ;; User supplies bytes every frame, which are written
750 ;; sequentially to
755 0xC3 ; ; Complete Loop
756 0x1D ;
757 0xD3 ;
761 ])
764 (def frame-count 0xD31F)
765 (def input 0xD352)
766 (def current-mode 0xD382)
768 (defn write-memory []
769 (-> (tick (mid-game))
770 (IE! 0) ; disable interrupts
771 (inject-item-assembly (write-memory-assembly))))
773 (defn test-write-memory []
774 (set-state! (write-memory))
775 (dorun
776 (dotimes [_ 5000]
777 (view-memory (step @current-state) current-mode))))
779 (def bytes-to-write 0xD383)
780 (def start-point 0xD384)
782 (defn test-mode-2 []
783 (->
784 (write-memory)
785 (view-memory bytes-to-write)
786 (view-memory start-point)
787 (step)
788 (step [:a])
789 (step [:b])
790 (step [:start])
791 (step [])
792 (view-memory bytes-to-write)
793 (view-memory start-point)))