annotate clojure/com/aurellem/gb/rlm_assembly.clj @ 382:3c24216e0080

decreased frame-metronome by one opcode
author Robert McIntyre <rlm@mit.edu>
date Wed, 11 Apr 2012 13:17:08 -0500
parents 1bfa43d35403
children 9eae7e914bf0
rev   line source
rlm@377 1 (ns com.aurellem.gb.rlm-assembly
rlm@377 2 "Version of main bootstrap program that is valid output for the
rlm@377 3 item-writer program."
rlm@377 4 (:use (com.aurellem.gb gb-driver assembly util vbm constants))
rlm@377 5 (:use (com.aurellem.run bootstrap-1))
rlm@377 6 (:import [com.aurellem.gb.gb_driver SaveState]))
rlm@377 7
rlm@377 8 ;; Specs for Main Bootstrap Program
rlm@377 9
rlm@377 10 ;; Number-Input
rlm@377 11 ;; Number input works using all eight buttons to
rlm@377 12 ;; spell out an 8 bit number. The order of buttons is
rlm@377 13 ;; [:d :u :l :r :start :select :b :a] --> 11111111
rlm@377 14 ;; [ :l :start :a] --> 00101001
rlm@377 15
rlm@377 16 ;;; MODE-SELECT
rlm@377 17 ;; The bootstrap program starts in MODE-SELECT mode.
rlm@377 18 ;; MODE-SELECT transitions to one of three modes depending
rlm@377 19 ;; on which buttons are pressed:
rlm@377 20 ;; 0 (no-buttons) : MODE-SELECT
rlm@377 21 ;; 8 [:start] : WRITE-BYTES
rlm@377 22 ;; 0xFF (all-buttons) : JUMP
rlm@377 23
rlm@377 24 ;;; WRITE-BYTES
rlm@377 25
rlm@377 26 ;; WRITE-BYTES mode writes sequences of arbitray values to
rlm@377 27 ;; arbitray memory locations. It expects you to enter a
rlm@377 28 ;; header of three bytes describing what to write:
rlm@377 29
rlm@377 30 ;; Byte 0 : Number of Bytes to Write
rlm@377 31 ;; Byte 1 : Start Address High Byte
rlm@377 32 ;; Byte 1 : Start Address Low Byte
rlm@377 33
rlm@377 34 ;; Then, you enter the number of bytes specified in Byte 0
rlm@377 35 ;; they are written to the start address in
rlm@377 36 ;; sequence. After the last byte is written control
rlm@377 37 ;; returns to MODE-SELECT mode.
rlm@377 38
rlm@377 39 ;; Example: to write the sequence [1 2 3 4] starting at
rlm@377 40 ;; address 0xC01F enter
rlm@377 41 ;; Byte 0 : 4 (will write four bytes)
rlm@377 42 ;; Byte 1 : 0xC0 (high byte of 0xC01F)
rlm@377 43 ;; Byte 2 : 0x1F (low byte of 0xC01F)
rlm@377 44 ;; Byte 3 : 1 (write 1 to 0xC01F)
rlm@377 45 ;; Byte 4 : 2 (write 2 to 0xC020)
rlm@377 46 ;; Byte 5 : 3 (write 3 to 0xC021)
rlm@377 47 ;; Byte 6 : 4 (write 4 to 0xC022)
rlm@377 48
rlm@377 49 ;;; JUMP
rlm@377 50 ;; JUMP mode jumps program control to any arbitray
rlm@377 51 ;; location. It expects you to enter two bytes which
rlm@377 52 ;; correspond to the high and low bytes of the memory
rlm@377 53 ;; address to which you want to jump.
rlm@377 54 ;; Byte 0 : Jump Address High Byte
rlm@377 55 ;; Byte 1 : Jump Address Low Byte
rlm@377 56
rlm@377 57 ;; Example: to jump to address 0x1234 enter
rlm@377 58 ;; Byte 0 : 0x12 (high byte of 0x1234)
rlm@377 59 ;; Byte 1 : 0x34 (low byte of 0x1234)
rlm@377 60
rlm@377 61
rlm@378 62 (defn ->signed-8-bit [n]
rlm@378 63 (if (< n 0)
rlm@378 64 (+ 256 n) n))
rlm@378 65
rlm@382 66 0x01
rlm@382 67 0x44
rlm@382 68 0xFF
rlm@382 69 inc C
rlm@382 70 inc D
rlm@382 71
rlm@378 72 (defn frame-metronome []
rlm@381 73 (let [timing-loop
rlm@382 74 [0x01
rlm@382 75 0x43
rlm@382 76 0xFE ;; load 0xFF44 into BC without repeats
rlm@382 77 0x0C
rlm@382 78 0x04
rlm@378 79
rlm@382 80 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)
rlm@379 81 continue-if-144
rlm@379 82 [0xFE
rlm@379 83 144 ;; compare LY (in A) with 144
rlm@379 84 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
rlm@379 85 (->signed-8-bit
rlm@381 86 (+ -4 (- (count timing-loop))))]
rlm@381 87 spin-loop
rlm@382 88 [0x05 ;; dec B, which is 0xFF
rlm@382 89 0x20 ;; spin until B==0
rlm@381 90 0xFD]]
rlm@381 91 (concat timing-loop continue-if-144 spin-loop)))
rlm@378 92
rlm@377 93
rlm@379 94 (defn test-frame-metronome
rlm@379 95 "Ensure that frame-metronome ticks exactly once every frame."
rlm@379 96 ([] (test-frame-metronome 151))
rlm@379 97 ([steps]
rlm@382 98 (let [inc-D [0x14 0x18
rlm@379 99 (->signed-8-bit
rlm@379 100 (+ -3 (- (count (frame-metronome)))))]
rlm@382 101 program (concat (frame-metronome) inc-D)
rlm@379 102 count-frames
rlm@379 103 (-> (tick (mid-game))
rlm@379 104 (IE! 0)
rlm@382 105 (DE! 0)
rlm@379 106 (set-memory-range pokemon-list-start program)
rlm@379 107 (PC! pokemon-list-start))
rlm@382 108 D-after-moves (D (run-moves count-frames (repeat steps [])))]
rlm@382 109 (println "D:" D-after-moves)
rlm@382 110 (assert (= steps D-after-moves))
rlm@379 111
rlm@382 112 (println "D =" D-after-moves "after" steps "steps")
rlm@379 113 count-frames)))
rlm@379 114
rlm@377 115 (defn main-bootstrap-program [start-address]
rlm@378 116 (let [[start-high start-low] (disect-bytes-2 start-address)
rlm@378 117 ]
rlm@378 118 ))
rlm@378 119
rlm@378 120
rlm@378 121
rlm@378 122
rlm@378 123
rlm@378 124
rlm@378 125
rlm@378 126
rlm@378 127
rlm@377 128
rlm@377 129 ;;;;;; TESTS ;;;;;;
rlm@377 130
rlm@377 131 (defn bootstrap-base []
rlm@377 132 (let [program (main-bootstrap-program pokemon-list-start)]
rlm@377 133 ;; make sure program is valid output for item-writer
rlm@377 134 (bootstrap-pattern program)
rlm@377 135 (-> (tick (mid-game))
rlm@377 136 (set-memory-range pokemon-list-start program)
rlm@377 137 (PC! pokemon-list-start))))
rlm@377 138
rlm@377 139 (defn test-write-bytes-mode []
rlm@377 140 (let [target-address 0xC00F
rlm@377 141 [target-high target-low] (disect-bytes-2 target-address)
rlm@377 142 assembly [0xF3 0x18 0xFE 0x12]
rlm@377 143 get-mem-region #(subvec (vec (memory %))
rlm@377 144 target-address (+ target-address 20))
rlm@377 145 before (bootstrap-base)
rlm@377 146 after
rlm@377 147 (-> before
rlm@377 148 (step []) ; make sure it can handle blanks
rlm@377 149 (step []) ; at the beginning.
rlm@377 150 (step [])
rlm@377 151 (step [:start]) ; select WRITE-BYTES mode
rlm@377 152 (step (buttons 4)) ; write 4 bytes
rlm@377 153 (step (buttons target-high))
rlm@377 154 (step (buttons target-low))
rlm@377 155 (step (buttons (nth assembly 0)))
rlm@377 156 (step (buttons (nth assembly 1)))
rlm@377 157 (step (buttons (nth assembly 2)))
rlm@377 158 (step (buttons (nth assembly 3)))
rlm@377 159 (step [])
rlm@377 160 (step [])
rlm@377 161 (step []))]
rlm@377 162 (println "before :" (get-mem-region before))
rlm@377 163 (println "after :" (get-mem-region after))
rlm@377 164 (assert (= assembly (take 4 (get-mem-region after))))
rlm@377 165 after))
rlm@377 166
rlm@377 167 (defn test-jump-mode []
rlm@377 168 (let [target-address 0xC00F
rlm@377 169 [target-high target-low] (disect-bytes-2 target-address)
rlm@377 170 post-jump
rlm@377 171 (-> (test-write-bytes-mode)
rlm@377 172 (step [])
rlm@377 173 (step [])
rlm@377 174 (step [])
rlm@377 175 (step (buttons 0xFF)) ; Select JUMP mode.
rlm@377 176 (step (buttons target-high))
rlm@377 177 (step (buttons target-low)))
rlm@377 178 program-counters
rlm@377 179 (capture-program-counter
rlm@377 180 post-jump
rlm@377 181 10000)]
rlm@377 182 (println program-counters)
rlm@377 183 (assert (contains? (set program-counters) target-address))
rlm@377 184 post-jump))