# HG changeset patch # User Robert McIntyre # Date 1334165731 18000 # Node ID f86bd04bd9fc4f424140163c9f07174eebbde0ce # Parent 5c4a30521d099ddab93b998ab6d843d2a4cfe15c simplifying frame-metronome diff -r 5c4a30521d09 -r f86bd04bd9fc clojure/com/aurellem/gb/rlm_assembly.clj --- a/clojure/com/aurellem/gb/rlm_assembly.clj Wed Apr 11 11:43:51 2012 -0500 +++ b/clojure/com/aurellem/gb/rlm_assembly.clj Wed Apr 11 12:35:31 2012 -0500 @@ -64,7 +64,8 @@ (+ 256 n) n)) (defn frame-metronome [] - (let [timing-loop + (let [init [0x06 155] ;; init B to out of LY range + timing-loop [0x47 ;; A->B 0x26 @@ -76,36 +77,44 @@ 0x2C 0x7E] ;; (HL) -> A, now A = LY (vertical line coord) - jump-if-not-144 - [0xFE - 144 ;; compare LY (in A) with 144 - 0x20 ;; jump back to beginning if LY != 144 (not-v-blank) - (->signed-8-bit (+ -4 (- (count timing-loop))))] continue-if-different [0xB8 ;; compare A with B 0x28 (->signed-8-bit - (+ -3 (- (+ (count timing-loop) (count jump-if-not-144)))))]] - (concat timing-loop jump-if-not-144 continue-if-different))) + (+ -3 (- (count timing-loop)))) + ] + continue-if-144 + [0xFE + 144 ;; compare LY (in A) with 144 + 0x20 ;; jump back to beginning if LY != 144 (not-v-blank) + (->signed-8-bit + (+ -4 (- (count timing-loop)) + (- (count continue-if-different))))]] -(defn test-frame-metronome [] - (let [inc-C [0x0C 0x18 - (->signed-8-bit - (+ -3 (- (count (frame-metronome)))))] - program (concat (frame-metronome) inc-C) - count-frames - (-> (tick (mid-game)) - (IE! 0) - (BC! 0) - (set-memory-range pokemon-list-start program) - (PC! pokemon-list-start)) - steps 151] - (assert - (= 151 - (C (run-moves count-frames (repeat steps []))))) - count-frames)) + (concat init timing-loop continue-if-different continue-if-144))) +(defn test-frame-metronome + "Ensure that frame-metronome ticks exactly once every frame." + ([] (test-frame-metronome 151)) + ([steps] + (let [inc-C [0x0C 0x18 + (->signed-8-bit + (+ -3 (- (count (frame-metronome)))))] + program (concat (frame-metronome) inc-C) + count-frames + (-> (tick (mid-game)) + (IE! 0) + (BC! 0) + (set-memory-range pokemon-list-start program) + (PC! pokemon-list-start)) + C-after-moves (C (run-moves count-frames (repeat steps [])))] + (println "C:" C-after-moves) + (assert (= steps C-after-moves)) + + (println "C =" steps "after" steps "steps") + count-frames))) + (defn main-bootstrap-program [start-address] (let [[start-high start-low] (disect-bytes-2 start-address) ]