comparison clojure/com/aurellem/assembly.clj @ 128:203d64e16156

dylan added some code
author Robert McIntyre <rlm@mit.edu>
date Sat, 17 Mar 2012 19:18:01 -0500
parents 901ee6b648da
children 5e4feb77f2d8
comparison
equal deleted inserted replaced
127:901ee6b648da 128:203d64e16156
63 63
64 (def buttons-port 0xFF00) 64 (def buttons-port 0xFF00)
65 65
66 (defn A [state] 66 (defn A [state]
67 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8)) 67 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
68
69 (defn B [state]
70 (bit-shift-right (bit-and 0x0000FF00 (BC state)) 8))
71
72 (defn C [state]
73 (bit-and 0xFF (BC state)))
68 74
69 (defn binary-str [num] 75 (defn binary-str [num]
70 (format "%08d" 76 (format "%08d"
71 (Integer/parseInt 77 (Integer/parseInt
72 (Integer/toBinaryString num) 10))) 78 (Integer/toBinaryString num) 10)))
432 0xC3 ; ; return to beginning 438 0xC3 ; ; return to beginning
433 0x1D ; 439 0x1D ;
434 0xD3 ; 440 0xD3 ;
435 ]) 441 ])
436 442
443
444 (defn print-pc [state]
445 (println (format "PC: 0x%04X" (PC state)))
446 state)
447
448 (defn print-op [state]
449 (println (format "OP: 0x%02X" (aget (memory state) (PC state))))
450 state)
451
452 (defn d-tick
453 ([state]
454 (-> state print-pc print-op tick)))
455
437 (defn input-number [] 456 (defn input-number []
438 (-> (tick (mid-game)) 457 (-> (tick (mid-game))
439 (IE! 0) ; disable interrupts 458 (IE! 0) ; disable interrupts
440 (inject-item-assembly (input-number-assembly)))) 459 (inject-item-assembly (input-number-assembly))))
441 460
443 "Input freestyle buttons and observe the effects at the repl." 462 "Input freestyle buttons and observe the effects at the repl."
444 [] 463 []
445 (set-state! (input-number)) 464 (set-state! (input-number))
446 (dotimes [_ 90000] (step (view-memory @current-state 0xD352)))) 465 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
447 466
467 (defn d2 []
468 (->
469 (write-mem-dyl)
470 (view-memory 0xD31F)
471 step step step step step
472 (view-memory 0xD31F)))
473
474 (defn dylan []
475 (->
476 (write-mem-dyl)
477 (tick)
478 (tick)
479 (tick)
480 (tick)
481 (tick)
482 (tick)
483 (tick)
484 (tick)
485 (tick)
486 (tick)
487 (tick)
488 (tick)
489
490 (tick)
491 (tick)
492 (tick)
493 (tick)
494 (tick)
495
496 (d-tick)
497 (view-register "A" A)
498 (view-register "B" B)
499 (view-register "C" C)
500
501 ))
502
503
504
505
506 (defn write-memory-assembly* []
507 [
508 0x18 ;; D31D
509 0x02
510 0x00 ;; frame-count D31F
511 0x00 ;; v-blank-prev D320
512
513 0xFA ;; load modes into A
514 0x41
515 0xFF
516
517 0x47
518
519 0xCB
520 0x2F
521 0x2F
522
523 0xA0
524 0xE6
525 0x01
526 0x47 ;; now B contains (VB==1)
527
528 0xFA ;; load v-blank-prev
529 0x20
530 0xD3
531
532 0x2F
533
534 0xA0
535 0x4F ;; now C contains increment?
536
537 0xFA ;; load frame count
538 0x1F
539 0xD3
540
541 0x81 ;; add A+C->A
542 0xEA ;; spit A --> fc
543 0x1F
544 0xD3
545
546 0x78 ;; B->A
547
548 0xEA ;; spit A --> vbprev
549 0x20
550 0xD3
551
552 0xC3 ;D40F ; go back to beginning
553 0x1D ;D410
554 0xD3 ;D411
555 ]
556 )
557
558 (defn write-mem-dyl []
559 (-> (tick (mid-game))
560 (IE! 0)
561 (inject-item-assembly (write-memory-assembly*))))
562
563
564
448 (defn write-memory-assembly [] 565 (defn write-memory-assembly []
449 566 [
450 [
451 ;; Main Timing Loop 567 ;; Main Timing Loop
452 ;; Constantly check for v-blank and Trigger main state machine on 568 ;; Constantly check for v-blank and Trigger main state machine on
453 ;; every transtion from v-blank to non-v-blank. 569 ;; every transtion from v-blank to non-v-blank.
454 570
455 0x18 ; D31D ; Variable declaration 571 0x18 ; D31D ; Variable declaration
535 0xEA ; D34A 651 0xEA ; D34A
536 0x1F ; D34B ; load A into frame-count 652 0x1F ; D34B ; load A into frame-count
537 0xD3 ; D34C 653 0xD3 ; D34C
538 654
539 0x00 ; D34D ; glue :) 655 0x00 ; D34D ; glue :)
540
541
542 ;;;;;;;;; BEGIN RLM DEBUG
543 ;;0xC3 ;; jump to beginning
544 ;;0x1D
545 ;;0xD3
546 ;;;;;;;;; END RLM DEBUG
547
548 656
549 0x18 ;D34E ; skip next 3 bytes 657 0x18 ;D34E ; skip next 3 bytes
550 0x03 ;D34F 658 0x03 ;D34F
551 ;D350 659 ;D350
552 (Integer/parseInt "00100000" 2) ; select directional pad 660 (Integer/parseInt "00100000" 2) ; select directional pad
931 (step [:b]) 1039 (step [:b])
932 (step [:start]) 1040 (step [:start])
933 (step []) 1041 (step [])
934 (view-memory frame-count))) 1042 (view-memory frame-count)))
935 1043
936 (defn test-mode-4 [] 1044 (defn test-mode-4
937 (-> 1045 ([] (test-mode-4 (write-memory)))
938 (write-memory) 1046 ([target-state]
939 (#(do (println "memory from 0xC00F to 0xC01F:" 1047 (->
940 (subvec (vec (memory %)) 0xC00F 0xC01F)) %)) 1048 target-state
941 (view-memory current-mode) 1049 (#(do (println "memory from 0xC00F to 0xC01F:"
942 (step []) 1050 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
943 (step []) 1051 (view-memory current-mode)
944 (step []) 1052 (step [])
945 (#(do (println "after three steps") %)) 1053 (step [])
946 (view-memory current-mode) 1054 (step [])
947 1055 (#(do (println "after three steps") %))
948 ;; Activate memory writing mode 1056 (view-memory current-mode)
949 1057
950 (#(do (println "step with [:a]") %)) 1058 ;; Activate memory writing mode
951 (step [:a]) 1059
952 (view-memory current-mode) 1060 (#(do (println "step with [:a]") %))
953 (view-memory bytes-to-write) 1061 (step [:a])
954 (view-memory start-point-high) 1062 (view-memory current-mode)
955 (view-memory start-point-low) 1063 (view-memory bytes-to-write)
956 1064 (view-memory start-point-high)
957 ;; Specify four bytes to be written 1065 (view-memory start-point-low)
958 1066
959 (#(do (println "step with [:select]")%)) 1067 ;; Specify four bytes to be written
960 (step [:select]) 1068
961 (view-memory current-mode) 1069 (#(do (println "step with [:select]")%))
962 (view-memory bytes-to-write) 1070 (step [:select])
963 (view-memory start-point-high) 1071 (view-memory current-mode)
964 (view-memory start-point-low) 1072 (view-memory bytes-to-write)
965 1073 (view-memory start-point-high)
966 ;; Specify target memory address as 0xC00F 1074 (view-memory start-point-low)
967 1075
968 (#(do (println "step with [:u :d]")%)) 1076 ;; Specify target memory address as 0xC00F
969 (step [:u :d]) 1077
970 (view-memory current-mode) 1078 (#(do (println "step with [:u :d]")%))
971 (view-memory bytes-to-write) 1079 (step [:u :d])
972 (view-memory start-point-high) 1080 (view-memory current-mode)
973 (view-memory start-point-low) 1081 (view-memory bytes-to-write)
974 1082 (view-memory start-point-high)
975 (#(do (println "step with [:a :b :start :select]")%)) 1083 (view-memory start-point-low)
976 (step [:a :b :start :select]) 1084
977 (view-memory current-mode) 1085 (#(do (println "step with [:a :b :start :select]")%))
978 (view-memory bytes-to-write) 1086 (step [:a :b :start :select])
979 (view-memory start-point-high) 1087 (view-memory current-mode)
980 (view-memory start-point-low) 1088 (view-memory bytes-to-write)
981 1089 (view-memory start-point-high)
982 ;; Start reprogramming memory 1090 (view-memory start-point-low)
983 1091
984 (#(do (println "step with [:a]")%)) 1092 ;; Start reprogramming memory
985 (step [:a]) 1093
986 (view-memory current-mode) 1094 (#(do (println "step with [:a]")%))
987 (view-memory bytes-written) 1095 (step [:a])
988 1096 (view-memory current-mode)
989 (#(do (println "step with [:b]")%)) 1097 (view-memory bytes-written)
990 (step [:b]) 1098
991 (view-memory current-mode) 1099 (#(do (println "step with [:b]")%))
992 (view-memory bytes-written) 1100 (step [:b])
993 1101 (view-memory current-mode)
994 (#(do (println "step with [:a :b]")%)) 1102 (view-memory bytes-written)
995 (step [:a :b]) 1103
996 (view-memory current-mode) 1104 (#(do (println "step with [:a :b]")%))
997 (view-memory bytes-written) 1105 (step [:a :b])
998 1106 (view-memory current-mode)
999 (#(do (println "step with [:select]")%)) 1107 (view-memory bytes-written)
1000 (step [:select]) 1108
1001 (view-memory current-mode) 1109 (#(do (println "step with [:select]")%))
1002 (view-memory bytes-written) 1110 (step [:select])
1003 1111 (view-memory current-mode)
1004 ;; Reprogramming done, program ready for more commands. 1112 (view-memory bytes-written)
1005 1113
1006 (#(do (println "step with []")%)) 1114 ;; Reprogramming done, program ready for more commands.
1007 (step []) 1115
1008 (view-memory current-mode) 1116 (#(do (println "step with []")%))
1009 (view-memory bytes-written) 1117 (step [])
1010 1118 (view-memory current-mode)
1011 (#(do (println "memory from 0xC00F to 0xC01F:" 1119 (view-memory bytes-written)
1012 (subvec (vec (memory %)) 0xC00F 0xC01F)) %)))) 1120
1013 1121 (#(do (println "memory from 0xC00F to 0xC01F:"
1014 1122 (subvec (vec (memory %)) 0xC00F 0xC01F)) %)))))
1123
1124