# HG changeset patch
# User Robert McIntyre <rlm@mit.edu>
# Date 1334244038 18000
# Node ID a0d0e1a46b1d49a4bb22439ea6ac46ce3a004e52
# Parent  47d44bb54d32a52836e5bcea05dc44b4df73d5a8
had to change programs to prevent repeats.

diff -r 47d44bb54d32 -r a0d0e1a46b1d clojure/com/aurellem/gb/rlm_assembly.clj
--- a/clojure/com/aurellem/gb/rlm_assembly.clj	Thu Apr 12 09:21:40 2012 -0500
+++ b/clojure/com/aurellem/gb/rlm_assembly.clj	Thu Apr 12 10:20:38 2012 -0500
@@ -65,20 +65,12 @@
 
 (defn frame-metronome []
   (let [timing-loop
-        [;;0x01 ; \
-         ;;0x43 ;  |
-         ;;0xFE ;  |  load 0xFF44 into BC without repeats
-         ;;0x0C ;  |
-         ;;0x04 ; /
-         ;;0x0A ;; (BC) -> A, now A = LY (vertical line coord)
-         
-         0x11 ; \                                           
-         0x43 ;  |                                           
-         0xFE ;  |  load 0xFF44 into DE without repeats      
-         0x1C ;  |                                           
-         0x14 ; /                                           
-         0x1A ;; (DE) -> A, now A = LY (vertical line coord)
-         ]
+        [0x01 ; \
+         0x43 ;  |
+         0xFE ;  |  load 0xFF44 into BC without repeats
+         0x0C ;  |
+         0x04 ; /
+         0x0A] ;; (BC) -> A, now A = LY (vertical line coord)
         continue-if-144
         [0xFE
          144     ;; compare LY (in A) with 144
@@ -86,8 +78,7 @@
          (->signed-8-bit
           (+ -4 (- (count timing-loop))))]
         spin-loop
-        [;;0x05 ;; dec B, which is 0xFF
-         0x15 ;; dec D, which is 0xFF
+        [0x05 ;; dec B, which is 0xFF
          0x20 ;; spin until B==0
          0xFD]]
     (concat timing-loop continue-if-144 spin-loop)))
@@ -96,55 +87,50 @@
   "Ensure that frame-metronome ticks exactly once every frame."
   ([] (test-frame-metronome 151))
   ([steps]
-     (let [inc-B [0x04 0x18
+     (let [inc-E [0x1C 0x18
                   (->signed-8-bit
                    (+ -3 (- (count (frame-metronome)))))]
-           program (concat (frame-metronome) inc-B)
+           program (concat (frame-metronome) inc-E)
            count-frames
            (-> (tick (mid-game))
                (IE! 0)
-               (BC! 0)
+               (DE! 0)
                (set-memory-range pokemon-list-start program)
                (PC! pokemon-list-start))
-           B-after-moves (B (run-moves count-frames (repeat steps [])))]
-       (println "B:" B-after-moves) 
-       (assert (= steps B-after-moves))
+           E-after-moves (E (run-moves count-frames (repeat steps [])))]
+       (println "E:" E-after-moves) 
+       (assert (= steps E-after-moves))
 
-       (println "B =" B-after-moves "after" steps "steps")
+       (println "E =" E-after-moves "after" steps "steps")
        count-frames)))
 
 (defn read-user-input []
-  [;;0x01 ;\
-   ;;0x01 ; |
-   ;;0xFE ; |  load 0xFF00 into BC without repeats
-   ;;0x04 ; |
-   ;;0x0D ;/
+  [0x3E
+   0x20 ; prepare to measure d-pad
 
-   0x11 ; \                                           
-   0x01 ;  |                                           
-   0xFE ;  |  load 0xFF44 into DE without repeats      
-   0x14 ;  |                                           
-   0x1D ; /                                           
+   0x01 ;\
+   0x01 ; |
+   0xFE ; |  load 0xFF00 into BC without repeats
+   0x04 ; |
+   0x0D ;/
    
-   0x3E
-   (Integer/parseInt "00100000" 2) ; prepare to measure d-pad
-
-   0x12 
-   0x1A ;; get D-pad info
+   0x02 
+   0x0A ;; get D-pad info
    
    0xF5 ;; push AF
       
    0x3E
-   (Integer/parseInt "00010000" 2) ; prepare to measure buttons
+   0x10 ; prepare to measure buttons
+
+   0x3F ;; clear carry flag no-op to prevent repeated nybbbles
    
-   0x12
-   0x1A ;; get button info
-
+   0x02
+   0x0A ;; get button info
    
    0xE6 ;; select bottom bits of A
    0x0F
 
-   0x57 ;; A->D
+   0x47 ;; A->B
 
    0xF1 ;; pop AF
 
@@ -154,8 +140,7 @@
    0xCB
    0x37 ;; swap A nybbles
 
-   0x5A ;; D->E            \ necessary to 
-   0xB3 ;; (or A D) -> A   / prevent repeats.
+   0xB0 ;; (or A B) -> A
  
    0x2F ;; (NOT A) -> A
    ])
@@ -164,7 +149,7 @@
   (let [program
         (concat
          (frame-metronome) (read-user-input)
-         [0x47 ;; A->B
+         [0x5F ;; A-> E
           0x18
           (->signed-8-bit
            (+ (- (count (frame-metronome)))
@@ -177,7 +162,7 @@
             (PC! pokemon-list-start))]
     (dorun
       (for [i (range 0x100)]
-        (assert (= (B (step read-input (buttons i))) i))))
+        (assert (= (E (step read-input (buttons i))) i))))
      (println "Tested all inputs.")
     read-input))
 
@@ -193,12 +178,6 @@
 (def input-write-num-mode  (Integer/parseInt "00001000" 2))
 (def do-write-mode         (Integer/parseInt "00010000" 2))
 
-
-
-
-
-
-
 (defn main-bootstrap-program [start-address]
   (let [[start-high start-low] (disect-bytes-2 start-address)
         jump-distance (+ (count (frame-metronome)
@@ -206,59 +185,51 @@
 
         init
         [0xAF 0x4F] ;; 0->A; 0->C;
+
+
         
         ;; HL = here
         ;; add C to HL
         ;; jp HL
         
         prepare-HL
-        [0x21] ;; load HL from literal nn
-               ;; nn == here defined below
+        [0xD1 ;; pop DE causes D and E to be non-volitale
+
+         0x21           ;; load HL from literal nn
+         :dispatch-high 
+         :dispatch-low]
          
         mode-dispatch
-        [0x06  ;\
+        [0xC5 ;; push BC
+         0x06  ;\
          0x01  ; | 0->B without repeats
          0x05  ;/
          0x09  ;; add BC to HL
+         0xC1 ;; pop BC
          0xE9] ;; jp
+        
+        mode-select
+        [0x4F  ;; A->C  (this is the address of dispatch above)
+         0x18  ;; return
+         :jump-to-end]
 
-        here
-        (disect-bytes-2 (+ 2 start-address (count init)
-                           jump-distance prepare-HL
-                           (count mode-dispatch)))
-        
+        input-bytes-to-write
+        [0x47  ;; A->B
+         0x18  ;; return
+         :jump-to-end]
 
-
-        ;;(here) jr end
-        
-        ;;stuff
-        ;;modify E
-        ;;jr metronome
-        
-        ;;stuff
-        ;;jr metronome
-                        
-        input-number
-
-         [0x47  ;; A->B
-          0x1E  ;;
-          input-high-write-jump
-          0x18
-          (jump-distance ??)]
         
 
         
-        input-high-write
-        [0x
+        
+         cleanup
+         [0xD5 ;; push DE
+          0x18
+          0x??];; jump all the way back to frame-metronome
 
-        
-        
-        
-        
 
-
-         (concat init (frame-metronome) (read-user-input)
-                 prepare-HL here mode-dispatch)
+;;          (concat init (frame-metronome) (read-user-input)
+;;                  prepare-HL here mode-dispatch)
                  
         
         
@@ -267,7 +238,7 @@
          
 
     
-    ))
+;;     ))
 
         
      
@@ -276,7 +247,7 @@
 
         
     
-
+(comment
 
 ;;;;;; TESTS ;;;;;;
 
@@ -334,3 +305,4 @@
     (println program-counters)
     (assert (contains? (set program-counters) target-address))
     post-jump))
+)
\ No newline at end of file