comparison clojure/com/aurellem/assembly.clj @ 124:f8dadd9478a5

fixed problem with mode transitions
author Robert McIntyre <rlm@mit.edu>
date Sat, 17 Mar 2012 01:23:47 -0500
parents c9a280b8bd1c
children d2e00c923bad
comparison
equal deleted inserted replaced
123:c9a280b8bd1c 124:f8dadd9478a5
80 (println (format "mem 0x%04X = %s" mem 80 (println (format "mem 0x%04X = %s" mem
81 (binary-str (aget (memory state) mem)))) 81 (binary-str (aget (memory state) mem))))
82 state) 82 state)
83 83
84 (defn trace [state] 84 (defn trace [state]
85 (loop [program-counters [] 85 (loop [program-counters [(first (registers @current-state)) ]
86 opcodes []] 86 opcodes [(aget (memory @current-state) (PC @current-state))]]
87 (let [frame-boundary? 87 (let [frame-boundary?
88 (com.aurellem.gb.Gb/tick)] 88 (com.aurellem.gb.Gb/tick)]
89 (println (count opcodes))
90 (if frame-boundary? 89 (if frame-boundary?
91 [program-counters opcodes] 90 [program-counters opcodes]
92 (recur 91 (recur
93 (conj program-counters 92 (conj program-counters
94 (first (registers @current-state))) 93 (first (registers @current-state)))
95 (conj opcodes 94 (conj opcodes
96 (aget (memory @current-state) 95 (aget (memory @current-state)
97 (PC @current-state)))))))) 96 (PC @current-state))))))))
98 97
98 (defn print-trace [state n]
99 (let [[program-counters opcodes] (trace state)]
100 (dorun (map (fn [pc op] (println (format "%04X: 0x%02X" pc op)))
101 (take n program-counters)
102 (take n opcodes)))))
103
99 (defn good-trace [] 104 (defn good-trace []
100 (-> (mid-game) (tick) (IE! 0) 105 (-> (mid-game) (tick) (IE! 0)
101 (set-inv-mem [0x00 0x00 0X00 0x00]) 106 (set-inv-mem [0x00 0x00 0X00 0x00])
102 (PC! item-list-start)(print-interrupt) 107 (PC! item-list-start)(print-interrupt)
103 (info) (tick) (info) (tick) (info))) 108 (info) (tick) (info) (tick) (info)))
439 [] 444 []
440 (set-state! (input-number)) 445 (set-state! (input-number))
441 (dotimes [_ 90000] (step (view-memory @current-state 0xD352)))) 446 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
442 447
443 (defn write-memory-assembly [] 448 (defn write-memory-assembly []
449
444 [ 450 [
445 ;; Main Timing Loop 451 ;; Main Timing Loop
446 ;; Constantly check for v-blank and Trigger main state machine on 452 ;; Constantly check for v-blank and Trigger main state machine on
447 ;; every transtion from v-blank to non-v-blank. 453 ;; every transtion from v-blank to non-v-blank.
448 454
459 ;; bits in A are "01" 465 ;; bits in A are "01"
460 0xCB ; D324 466 0xCB ; D324
461 0x4F ; D325 467 0x4F ; D325
462 468
463 0xC2 ; D326 ; if bit-1 is not 0, then 469 0xC2 ; D326 ; if bit-1 is not 0, then
464 0x44 ; D327 ; GOTO non-v-blank. 470 0x3E ; D327 ; GOTO non-v-blank.
465 0xD3 ; D328 471 0xD3 ; D328
466 472
467 0xCB ; D329 473 0xCB ; D329
468 0x47 ; D32A 474 0x47 ; D32A
469 475
470 0xCA ; D32B ; if bit-0 is not 1, then 476 0xCA ; D32B ; if bit-0 is not 1, then
471 0x44 ; D32C ; GOTO non-v-blank. 477 0x3E ; D32C ; GOTO non-v-blank.
472 0xD3 ; D32D 478 0xD3 ; D32D
473 479
474 ;; V-Blank 480 ;; V-Blank
475 ;; Activate state-machine if this is a transition event. 481 ;; Activate state-machine if this is a transition event.
476 482
477 0xFA ; D32E ; load v-bank-prev into A 483 0xFA ; D32E ; load v-bank-prev into A
478 0x20 ; D32F 484 0x20 ; D32F
479 0xD3 ; D330 485 0xD3 ; D330
480 486
481 0xFE ; D331 ; compare A to 0. 487 0xFE ; D331 ; compare A to 0. >--------\
482 0x00 ; D332 488 0x00 ; D332 \
483 489 ; |
484 ;; set v-blank-prev to 1. 490 ;; set v-blank-prev to 1. |
485 0x3E ; D333 ; load 1 into A. 491 0x3E ; D333 ; load 1 into A. |
486 0x01 ; D334 492 0x01 ; D334 |
487 493 ; |
488 0xEA ; D335 ; load A into v-blank-prev 494 0xEA ; D335 ; load A into v-blank-prev |
489 0x20 ; D336 495 0x20 ; D336 |
490 0xD3 ; D337 496 0xD3 ; D337 |
491 497 ; /
492 ;; if v-blank-prev was 0, activate state-machine 498 ;; if v-blank-prev was 0, activate state-machine <------/
493 0xC2 ; D338 ; if v-blank-prev 499 0xCA ; D338 ; if v-blank-prev
494 0x46 ; D339 ; was 0, 500 0x46 ; D339 ; was 0,
495 0xD3 ; D33A ; GOTO state-machine 501 0xD3 ; D33A ; GOTO state-machine
496 502
497 0xC3 ; D33B 503 0xC3 ; D33B
498 0x1D ; D33C 504 0x1D ; D33C
511 0xC3 ; D343 517 0xC3 ; D343
512 0x1D ; D344 518 0x1D ; D344
513 0xD3 ; D345 ; GOTO beginning 519 0xD3 ; D345 ; GOTO beginning
514 ;; END Not-V-Blank 520 ;; END Not-V-Blank
515 521
522
516 ;; Main State Machine -- Input Section 523 ;; Main State Machine -- Input Section
517 ;; This is called once every frame. 524 ;; This is called once every frame.
518 ;; It collects input and uses it to drive the 525 ;; It collects input and uses it to drive the
519 ;; state transitions. 526 ;; state transitions.
520 527
531 538
532 0x00 ; D34D ; glue :) 539 0x00 ; D34D ; glue :)
533 540
534 541
535 ;;;;;;;;; BEGIN RLM DEBUG 542 ;;;;;;;;; BEGIN RLM DEBUG
536 0xC3 ;; jump to beginning 543 ;;0xC3 ;; jump to beginning
537 0x1D 544 ;;0x1D
538 0xD3 545 ;;0xD3
539
540 ;;;;;;;;; END RLM DEBUG 546 ;;;;;;;;; END RLM DEBUG
541 547
542 548
543 0x18 ;D34E ; skip next 3 bytes 549 0x18 ;D34E ; skip next 3 bytes
544 0x03 ;D34F 550 0x03 ;D34F
598 604
599 0xEA ;D372 ; store A into input-number 605 0xEA ;D372 ; store A into input-number
600 0x52 ;D373 606 0x52 ;D373
601 0xD3 ;D374 607 0xD3 ;D374
602 608
603 0xC3 ;D375 ; secret jump :) 609 0x00 ;D375
604 0x1D ;D376 610 0x00 ;D376
605 0xD3 ;D377 611 0x00 ;D377
606 0x00 ;D378 612 0x00 ;D378
607 0x00 ;D379 613 0x00 ;D379
608 0x00 ;D37A 614 0x00 ;D37A
609 0x00 ;D37B ; these are here because 615 0x00 ;D37B ; these are here because
610 0x00 ;D37C ; I messed up :( 616 0x00 ;D37C ; I messed up :(
777 783
778 784
779 (def frame-count 0xD31F) 785 (def frame-count 0xD31F)
780 (def input 0xD352) 786 (def input 0xD352)
781 (def current-mode 0xD382) 787 (def current-mode 0xD382)
788 (def bytes-to-write 0xD383)
789 (def start-point 0xD384)
790
782 791
783 (defn write-memory [] 792 (defn write-memory []
784 (-> (tick (mid-game)) 793 (-> (tick (mid-game))
785 (IE! 0) ; disable interrupts 794 (IE! 0) ; disable interrupts
786 (inject-item-assembly (write-memory-assembly)))) 795 (inject-item-assembly (write-memory-assembly))))
803 (step [:b]) 812 (step [:b])
804 (step [:start]) 813 (step [:start])
805 (step []) 814 (step [])
806 (view-memory frame-count))) 815 (view-memory frame-count)))
807 816
808 817 (defn test-mode-2 []
809 818 (->
810 819 (write-memory)
820 (view-memory current-mode)
821 (step [])
822 (step [])
823 (step [])
824 (#(do (println "after three steps") %))
825 (view-memory current-mode)
826 (step [:a])
827 (#(do (println "step with [:a]") %))
828 (view-memory current-mode)
829 (view-memory bytes-to-write)
830 (view-memory start-point)
831 (#(do (println "step with [:u :d :l :r]")%))
832 (step [:u :d])
833 (view-memory current-mode)
834 (view-memory bytes-to-write)
835 (view-memory start-point)
836 (#(do (println "step with [:u :d]")%))
837 (step [:u :d])
838 (view-memory current-mode)
839 (view-memory bytes-to-write)
840 (view-memory start-point)
841
842 ))
843
844