diff clojure/com/aurellem/gb/rlm_assembly.clj @ 379:f86bd04bd9fc

simplifying frame-metronome
author Robert McIntyre <rlm@mit.edu>
date Wed, 11 Apr 2012 12:35:31 -0500
parents 5c4a30521d09
children 4d2767423266
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb/rlm_assembly.clj	Wed Apr 11 11:43:51 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/rlm_assembly.clj	Wed Apr 11 12:35:31 2012 -0500
     1.3 @@ -64,7 +64,8 @@
     1.4      (+ 256 n) n))
     1.5  
     1.6  (defn frame-metronome []
     1.7 -  (let [timing-loop
     1.8 +  (let [init [0x06 155] ;; init B to out of LY range
     1.9 +        timing-loop
    1.10          [0x47    ;; A->B
    1.11           
    1.12           0x26 
    1.13 @@ -76,36 +77,44 @@
    1.14           0x2C
    1.15  
    1.16           0x7E]   ;; (HL) -> A, now A = LY (vertical line coord)
    1.17 -        jump-if-not-144
    1.18 -        [0xFE
    1.19 -         144     ;; compare LY (in A) with 144
    1.20 -         0x20    ;; jump back to beginning if LY != 144 (not-v-blank)
    1.21 -         (->signed-8-bit (+ -4 (- (count timing-loop))))]
    1.22          continue-if-different
    1.23          [0xB8    ;; compare A with B
    1.24           0x28
    1.25           (->signed-8-bit
    1.26 -          (+ -3 (- (+ (count timing-loop) (count jump-if-not-144)))))]]
    1.27 -    (concat timing-loop jump-if-not-144 continue-if-different)))
    1.28 +          (+ -3 (- (count timing-loop))))
    1.29 +         ]
    1.30 +        continue-if-144
    1.31 +        [0xFE
    1.32 +         144     ;; compare LY (in A) with 144
    1.33 +         0x20    ;; jump back to beginning if LY != 144 (not-v-blank)
    1.34 +         (->signed-8-bit
    1.35 +          (+ -4 (- (count timing-loop))
    1.36 +             (- (count continue-if-different))))]]
    1.37  
    1.38 -(defn test-frame-metronome []
    1.39 -  (let [inc-C [0x0C 0x18
    1.40 -               (->signed-8-bit
    1.41 -                (+ -3 (- (count (frame-metronome)))))]
    1.42 -        program (concat (frame-metronome) inc-C)
    1.43 -        count-frames
    1.44 -        (-> (tick (mid-game))
    1.45 -            (IE! 0)
    1.46 -            (BC! 0)
    1.47 -            (set-memory-range pokemon-list-start program)
    1.48 -            (PC! pokemon-list-start))
    1.49 -        steps 151]
    1.50 -    (assert
    1.51 -     (= 151
    1.52 -        (C (run-moves count-frames (repeat steps [])))))
    1.53 -    count-frames))
    1.54 +    (concat init timing-loop continue-if-different continue-if-144)))
    1.55  
    1.56  
    1.57 +(defn test-frame-metronome
    1.58 +  "Ensure that frame-metronome ticks exactly once every frame."
    1.59 +  ([] (test-frame-metronome 151))
    1.60 +  ([steps]
    1.61 +     (let [inc-C [0x0C 0x18
    1.62 +                  (->signed-8-bit
    1.63 +                   (+ -3 (- (count (frame-metronome)))))]
    1.64 +           program (concat (frame-metronome) inc-C)
    1.65 +           count-frames
    1.66 +           (-> (tick (mid-game))
    1.67 +               (IE! 0)
    1.68 +               (BC! 0)
    1.69 +               (set-memory-range pokemon-list-start program)
    1.70 +               (PC! pokemon-list-start))
    1.71 +           C-after-moves (C (run-moves count-frames (repeat steps [])))]
    1.72 +       (println "C:" C-after-moves)
    1.73 +       (assert (= steps C-after-moves))
    1.74 +
    1.75 +       (println "C =" steps "after" steps "steps")
    1.76 +       count-frames)))
    1.77 +
    1.78  (defn main-bootstrap-program [start-address]
    1.79    (let [[start-high start-low] (disect-bytes-2 start-address)
    1.80          ]