annotate clojure/com/aurellem/gb/rlm_assembly.clj @ 389:bb8978d370d8

saving progress.
author Robert McIntyre <rlm@mit.edu>
date Thu, 12 Apr 2012 12:08:36 -0500
parents a0d0e1a46b1d
children dbf7b5a2e9e7
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@378 66 (defn frame-metronome []
rlm@381 67 (let [timing-loop
rlm@388 68 [0x01 ; \
rlm@388 69 0x43 ; |
rlm@388 70 0xFE ; | load 0xFF44 into BC without repeats
rlm@388 71 0x0C ; |
rlm@388 72 0x04 ; /
rlm@388 73 0x0A] ;; (BC) -> A, now A = LY (vertical line coord)
rlm@379 74 continue-if-144
rlm@379 75 [0xFE
rlm@379 76 144 ;; compare LY (in A) with 144
rlm@379 77 0x20 ;; jump back to beginning if LY != 144 (not-v-blank)
rlm@379 78 (->signed-8-bit
rlm@381 79 (+ -4 (- (count timing-loop))))]
rlm@381 80 spin-loop
rlm@388 81 [0x05 ;; dec B, which is 0xFF
rlm@382 82 0x20 ;; spin until B==0
rlm@381 83 0xFD]]
rlm@381 84 (concat timing-loop continue-if-144 spin-loop)))
rlm@378 85
rlm@379 86 (defn test-frame-metronome
rlm@379 87 "Ensure that frame-metronome ticks exactly once every frame."
rlm@379 88 ([] (test-frame-metronome 151))
rlm@379 89 ([steps]
rlm@388 90 (let [inc-E [0x1C 0x18
rlm@379 91 (->signed-8-bit
rlm@379 92 (+ -3 (- (count (frame-metronome)))))]
rlm@388 93 program (concat (frame-metronome) inc-E)
rlm@379 94 count-frames
rlm@379 95 (-> (tick (mid-game))
rlm@379 96 (IE! 0)
rlm@388 97 (DE! 0)
rlm@379 98 (set-memory-range pokemon-list-start program)
rlm@379 99 (PC! pokemon-list-start))
rlm@388 100 E-after-moves (E (run-moves count-frames (repeat steps [])))]
rlm@388 101 (println "E:" E-after-moves)
rlm@388 102 (assert (= steps E-after-moves))
rlm@379 103
rlm@388 104 (println "E =" E-after-moves "after" steps "steps")
rlm@379 105 count-frames)))
rlm@379 106
rlm@384 107 (defn read-user-input []
rlm@388 108 [0x3E
rlm@388 109 0x20 ; prepare to measure d-pad
rlm@384 110
rlm@388 111 0x01 ;\
rlm@388 112 0x01 ; |
rlm@388 113 0xFE ; | load 0xFF00 into BC without repeats
rlm@388 114 0x04 ; |
rlm@388 115 0x0D ;/
rlm@386 116
rlm@388 117 0x02
rlm@388 118 0x0A ;; get D-pad info
rlm@385 119
rlm@385 120 0xF5 ;; push AF
rlm@385 121
rlm@385 122 0x3E
rlm@388 123 0x10 ; prepare to measure buttons
rlm@388 124
rlm@388 125 0x3F ;; clear carry flag no-op to prevent repeated nybbbles
rlm@385 126
rlm@388 127 0x02
rlm@388 128 0x0A ;; get button info
rlm@385 129
rlm@384 130 0xE6 ;; select bottom bits of A
rlm@384 131 0x0F
rlm@384 132
rlm@388 133 0x47 ;; A->B
rlm@385 134
rlm@385 135 0xF1 ;; pop AF
rlm@385 136
rlm@385 137 0xE6
rlm@385 138 0x0F ;; select bottom bits of A
rlm@385 139
rlm@384 140 0xCB
rlm@384 141 0x37 ;; swap A nybbles
rlm@387 142
rlm@388 143 0xB0 ;; (or A B) -> A
rlm@387 144
rlm@384 145 0x2F ;; (NOT A) -> A
rlm@384 146 ])
rlm@384 147
rlm@384 148 (defn test-read-user-input []
rlm@384 149 (let [program
rlm@384 150 (concat
rlm@384 151 (frame-metronome) (read-user-input)
rlm@388 152 [0x5F ;; A-> E
rlm@384 153 0x18
rlm@384 154 (->signed-8-bit
rlm@384 155 (+ (- (count (frame-metronome)))
rlm@384 156 (- (count (read-user-input)))
rlm@384 157 (- 3)))])
rlm@384 158 read-input
rlm@384 159 (-> (tick (mid-game))
rlm@384 160 (IE! 0)
rlm@384 161 (set-memory-range pokemon-list-start program)
rlm@384 162 (PC! pokemon-list-start))]
rlm@384 163 (dorun
rlm@385 164 (for [i (range 0x100)]
rlm@388 165 (assert (= (E (step read-input (buttons i))) i))))
rlm@385 166 (println "Tested all inputs.")
rlm@384 167 read-input))
rlm@384 168
rlm@385 169 (def mode-select-mode (Integer/parseInt "00000000" 2))
rlm@385 170 (def write-mode (Integer/parseInt "00000001" 2))
rlm@385 171 (def jump-mode (Integer/parseInt "10000000" 2))
rlm@385 172
rlm@385 173 (def input-high-addr-jump (Integer/parseInt "10000010" 2))
rlm@385 174 (def input-high-addr-write (Integer/parseInt "00000010" 2))
rlm@385 175 (def input-low-addr-jump (Integer/parseInt "10000100" 2))
rlm@385 176 (def input-low-addr-write (Integer/parseInt "00000100" 2))
rlm@385 177
rlm@385 178 (def input-write-num-mode (Integer/parseInt "00001000" 2))
rlm@385 179 (def do-write-mode (Integer/parseInt "00010000" 2))
rlm@385 180
rlm@377 181 (defn main-bootstrap-program [start-address]
rlm@378 182 (let [[start-high start-low] (disect-bytes-2 start-address)
rlm@385 183 jump-distance (+ (count (frame-metronome)
rlm@385 184 (read-user-input)))
rlm@385 185
rlm@385 186 init
rlm@389 187 [0xAF 0x5F] ;; 0->A; 0->E;
rlm@388 188
rlm@388 189
rlm@389 190 header
rlm@389 191 [0x47] ;; A->B
rlm@389 192
rlm@389 193 mode-select
rlm@389 194 [0xCB 0x43 ;; last-bit is zero
rlm@389 195 0x28 :to-beginning
rlm@389 196 0x5F];; A->E
rlm@389 197
rlm@389 198 input-section
rlm@389 199 [0xCB 0x4B ;; test bit 0 of E
rlm@389 200 0x28 0x03
rlm@389 201
rlm@389 202
rlm@389 203 0x57 ;; A->D
rlm@389 204
rlm@389 205 0x67 ;; A->H
rlm@389 206 0x6F ;; A->L
rlm@389 207
rlm@389 208 0x47 ;; A->B
rlm@389 209 0x4F ;; A->C
rlm@389 210
rlm@389 211
rlm@389 212
rlm@389 213
rlm@389 214
rlm@389 215
rlm@389 216
rlm@385 217
rlm@385 218 ;; HL = here
rlm@387 219 ;; add C to HL
rlm@385 220 ;; jp HL
rlm@385 221
rlm@387 222 prepare-HL
rlm@388 223 [0xD1 ;; pop DE causes D and E to be non-volitale
rlm@388 224
rlm@388 225 0x21 ;; load HL from literal nn
rlm@388 226 :dispatch-high
rlm@388 227 :dispatch-low]
rlm@387 228
rlm@385 229 mode-dispatch
rlm@388 230 [0xC5 ;; push BC
rlm@388 231 0x06 ;\
rlm@387 232 0x01 ; | 0->B without repeats
rlm@387 233 0x05 ;/
rlm@387 234 0x09 ;; add BC to HL
rlm@388 235 0xC1 ;; pop BC
rlm@387 236 0xE9] ;; jp
rlm@388 237
rlm@388 238 mode-select
rlm@388 239 [0x4F ;; A->C (this is the address of dispatch above)
rlm@388 240 0x18 ;; return
rlm@388 241 :jump-to-end]
rlm@385 242
rlm@388 243 input-bytes-to-write
rlm@388 244 [0x47 ;; A->B
rlm@388 245 0x18 ;; return
rlm@388 246 :jump-to-end]
rlm@385 247
rlm@387 248
rlm@385 249
rlm@385 250
rlm@388 251
rlm@388 252 cleanup
rlm@388 253 [0xD5 ;; push DE
rlm@388 254 0x18
rlm@388 255 0x??];; jump all the way back to frame-metronome
rlm@387 256
rlm@385 257
rlm@388 258 ;; (concat init (frame-metronome) (read-user-input)
rlm@388 259 ;; prepare-HL here mode-dispatch)
rlm@387 260
rlm@385 261
rlm@385 262
rlm@385 263
rlm@385 264
rlm@385 265
rlm@385 266
rlm@385 267
rlm@388 268 ;; ))
rlm@378 269
rlm@378 270
rlm@378 271
rlm@378 272
rlm@378 273
rlm@378 274
rlm@378 275
rlm@378 276
rlm@388 277 (comment
rlm@377 278
rlm@377 279 ;;;;;; TESTS ;;;;;;
rlm@377 280
rlm@377 281 (defn bootstrap-base []
rlm@377 282 (let [program (main-bootstrap-program pokemon-list-start)]
rlm@377 283 ;; make sure program is valid output for item-writer
rlm@377 284 (bootstrap-pattern program)
rlm@377 285 (-> (tick (mid-game))
rlm@377 286 (set-memory-range pokemon-list-start program)
rlm@377 287 (PC! pokemon-list-start))))
rlm@377 288
rlm@377 289 (defn test-write-bytes-mode []
rlm@377 290 (let [target-address 0xC00F
rlm@377 291 [target-high target-low] (disect-bytes-2 target-address)
rlm@377 292 assembly [0xF3 0x18 0xFE 0x12]
rlm@377 293 get-mem-region #(subvec (vec (memory %))
rlm@377 294 target-address (+ target-address 20))
rlm@377 295 before (bootstrap-base)
rlm@377 296 after
rlm@377 297 (-> before
rlm@377 298 (step []) ; make sure it can handle blanks
rlm@377 299 (step []) ; at the beginning.
rlm@377 300 (step [])
rlm@377 301 (step [:start]) ; select WRITE-BYTES mode
rlm@377 302 (step (buttons 4)) ; write 4 bytes
rlm@377 303 (step (buttons target-high))
rlm@377 304 (step (buttons target-low))
rlm@377 305 (step (buttons (nth assembly 0)))
rlm@377 306 (step (buttons (nth assembly 1)))
rlm@377 307 (step (buttons (nth assembly 2)))
rlm@377 308 (step (buttons (nth assembly 3)))
rlm@377 309 (step [])
rlm@377 310 (step [])
rlm@377 311 (step []))]
rlm@377 312 (println "before :" (get-mem-region before))
rlm@377 313 (println "after :" (get-mem-region after))
rlm@377 314 (assert (= assembly (take 4 (get-mem-region after))))
rlm@377 315 after))
rlm@377 316
rlm@377 317 (defn test-jump-mode []
rlm@377 318 (let [target-address 0xC00F
rlm@377 319 [target-high target-low] (disect-bytes-2 target-address)
rlm@377 320 post-jump
rlm@377 321 (-> (test-write-bytes-mode)
rlm@377 322 (step [])
rlm@377 323 (step [])
rlm@377 324 (step [])
rlm@377 325 (step (buttons 0xFF)) ; Select JUMP mode.
rlm@377 326 (step (buttons target-high))
rlm@377 327 (step (buttons target-low)))
rlm@377 328 program-counters
rlm@377 329 (capture-program-counter
rlm@377 330 post-jump
rlm@377 331 10000)]
rlm@377 332 (println program-counters)
rlm@377 333 (assert (contains? (set program-counters) target-address))
rlm@377 334 post-jump))
rlm@388 335 )