annotate clojure/com/aurellem/assembly.clj @ 126:86f92deacd2a

completed mode 3
author Robert McIntyre <rlm@mit.edu>
date Sat, 17 Mar 2012 02:25:48 -0500
parents d2e00c923bad
children 901ee6b648da
rev   line source
rlm@105 1 (ns com.aurellem.assembly
rlm@105 2 (:use (com.aurellem gb-driver vbm title items))
rlm@105 3 (:import [com.aurellem.gb_driver SaveState]))
rlm@105 4
rlm@106 5 (defn mid-game []
rlm@106 6 (read-state "mid-game"))
rlm@105 7
rlm@105 8 (defn inject-assembly
rlm@107 9 ([^SaveState state
rlm@105 10 program-counter registers
rlm@105 11 assembly-code]
rlm@105 12 (let [scratch-memory (memory state)]
rlm@105 13 ;; inject assembly code
rlm@105 14 (dorun (map (fn [index val]
rlm@105 15 (aset scratch-memory index val))
rlm@105 16 (range program-counter
rlm@105 17 (+ program-counter (count assembly-code)))
rlm@105 18 assembly-code))
rlm@106 19 (-> state
rlm@106 20 (write-memory! scratch-memory)
rlm@106 21 (write-registers! registers)
rlm@112 22 (PC! program-counter)))))
rlm@105 23
rlm@105 24 (defn inject-item-assembly
rlm@105 25 ([^SaveState state assembly-code]
rlm@105 26 (inject-assembly state (inc item-list-start)
rlm@105 27 (registers state)
rlm@105 28 assembly-code))
rlm@105 29 ([assembly-code]
rlm@105 30 (inject-item-assembly @current-state assembly-code)))
rlm@105 31
rlm@105 32 (defn info
rlm@105 33 ([^SaveState state]
rlm@107 34 (println (format "PC: 0x%04X" (PC state)))
rlm@106 35 (println "Instruction:"
rlm@106 36 (format "0x%02X" (aget (memory state) (PC state))))
rlm@105 37 state))
rlm@105 38
rlm@107 39 (defn print-interrupt
rlm@107 40 [^SaveState state]
rlm@107 41 (println (format "IE: %d" (IE state)))
rlm@107 42 state)
rlm@107 43
rlm@118 44 (defn print-listing [state begin end]
rlm@118 45 (dorun (map
rlm@118 46 (fn [opcode line]
rlm@118 47 (println (format "0x%04X: 0x%02X" line opcode)))
rlm@118 48 (subvec (vec (memory state)) begin end)
rlm@118 49 (range begin end)))
rlm@118 50 state)
rlm@118 51
rlm@105 52 (defn run-assembly
rlm@105 53 ([info-fn assembly n]
rlm@105 54 (let [final-state
rlm@105 55 (reduce (fn [state _]
rlm@105 56 (tick (info-fn state)))
rlm@107 57 (inject-item-assembly
rlm@107 58 (mid-game) assembly)
rlm@105 59 (range n))]
rlm@105 60 final-state))
rlm@105 61 ([assembly n]
rlm@105 62 (run-assembly info assembly n)))
rlm@107 63
rlm@107 64 (def buttons-port 0xFF00)
rlm@107 65
rlm@107 66 (defn A [state]
rlm@107 67 (bit-shift-right (bit-and 0x0000FF00 (AF state)) 8))
rlm@107 68
rlm@110 69 (defn binary-str [num]
rlm@110 70 (format "%08d"
rlm@110 71 (Integer/parseInt
rlm@110 72 (Integer/toBinaryString num) 10)))
rlm@110 73
rlm@110 74 (defn view-register [state name reg-fn]
rlm@110 75 (println (format "%s: %s" name
rlm@110 76 (binary-str (reg-fn state))))
rlm@110 77 state)
rlm@110 78
rlm@107 79 (defn view-memory [state mem]
rlm@110 80 (println (format "mem 0x%04X = %s" mem
rlm@110 81 (binary-str (aget (memory state) mem))))
rlm@107 82 state)
rlm@107 83
rlm@118 84 (defn trace [state]
rlm@124 85 (loop [program-counters [(first (registers @current-state)) ]
rlm@124 86 opcodes [(aget (memory @current-state) (PC @current-state))]]
rlm@118 87 (let [frame-boundary?
rlm@118 88 (com.aurellem.gb.Gb/tick)]
rlm@118 89 (if frame-boundary?
rlm@118 90 [program-counters opcodes]
rlm@118 91 (recur
rlm@118 92 (conj program-counters
rlm@118 93 (first (registers @current-state)))
rlm@118 94 (conj opcodes
rlm@118 95 (aget (memory @current-state)
rlm@118 96 (PC @current-state))))))))
rlm@124 97
rlm@124 98 (defn print-trace [state n]
rlm@124 99 (let [[program-counters opcodes] (trace state)]
rlm@124 100 (dorun (map (fn [pc op] (println (format "%04X: 0x%02X" pc op)))
rlm@124 101 (take n program-counters)
rlm@124 102 (take n opcodes)))))
rlm@124 103
rlm@118 104 (defn good-trace []
rlm@118 105 (-> (mid-game) (tick) (IE! 0)
rlm@118 106 (set-inv-mem [0x00 0x00 0X00 0x00])
rlm@118 107 (PC! item-list-start)(print-interrupt)
rlm@118 108 (info) (tick) (info) (tick) (info)))
rlm@118 109
rlm@113 110 (defn read-down-button []
rlm@110 111 (-> (tick (mid-game))
rlm@110 112 (IE! 0) ; disable interrupts
rlm@110 113 (inject-item-assembly
rlm@118 114 ;; write 00010000 to 0xFF00 to select joypad
rlm@118 115 [0x18 ;D31D ; jump over
rlm@118 116 0x01 ;D31E ; the next 8 bits
rlm@118 117 ;D31F
rlm@118 118 (Integer/parseInt "00100000" 2) ; data section
rlm@118 119
rlm@118 120 0xFA ;D320 ; load (D31F) into A
rlm@118 121 0x1F ;D321 -->
rlm@118 122 0xD3 ;D322 --> D31F
rlm@107 123
rlm@118 124 0xEA ;D323 ; load (A), which is
rlm@118 125 0x00 ;D324 --> ; 00010000, into FF00
rlm@118 126 0xFF ;D325 --> FF00
rlm@118 127
rlm@118 128 0x18 ;D326 ; this is the place where
rlm@118 129 0x01 ;D327 ; we will store whether
rlm@118 130 0x00 ;D328 ; "down" is pressed.
rlm@107 131
rlm@118 132 0xFA ;D329 ; (FF00) -> A
rlm@118 133 0x00 ;D32A
rlm@118 134 0xFF ;D32B
rlm@110 135
rlm@118 136 0xCB ;D32C ; Test whether "down"
rlm@118 137 0x5F ;D32D ; is pressed.
rlm@111 138
rlm@118 139 0x28 ;D32E ; if down is pressed,
rlm@118 140 0x03 ;D32F ; skip the next section
rlm@118 141 ; of code.
rlm@118 142 ;; down-is-not-pressed
rlm@118 143 0xC3 ;D330
rlm@118 144 0x1D ;D331 ; return to beginning
rlm@118 145 0xD3 ;D332
rlm@118 146
rlm@118 147 ;; down-is-pressed
rlm@118 148 0xEA ;D334 ; write A to D328 if
rlm@118 149 0x28 ;D335 ; "down" was pressed
rlm@118 150 0xD3 ;D336
rlm@111 151
rlm@118 152 0xC3 ;D330
rlm@118 153 0x1D ;D331 ; return to beginning
rlm@118 154 0xD3 ;D332
rlm@118 155 ])))
rlm@111 156
rlm@118 157 (defn test-read-down []
rlm@118 158 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)
rlm@118 159 (view-memory (step (step (read-down-button))) 0xD328)))
rlm@113 160
rlm@113 161 (defn count-frames []
rlm@113 162 (-> (tick (mid-game))
rlm@113 163 (IE! 0) ; disable interrupts
rlm@113 164 (inject-item-assembly
rlm@113 165 [0x18 ;D31D ; jump over
rlm@113 166 0x02 ;D31E ; the next 2 bytes
rlm@113 167 0x00 ;D31F ; frame-count
rlm@113 168 0x00 ;D320 ; v-blank-prev
rlm@113 169
rlm@113 170 0xFA ;D321
rlm@113 171 0x41 ;D322 ; load (FF41) into A
rlm@113 172 0xFF ;D323 ; this contains mode flags
rlm@113 173
rlm@113 174 ;; if we're in v-blank, the bit-1 is 0
rlm@113 175 ;; and bit-2 is 1 Otherwise, it is not v-blank.
rlm@113 176 0xCB ;D324 ; test bit-1 of A
rlm@113 177 0x4F ;D325
rlm@113 178
rlm@113 179 0xC2 ;D326 ; if bit-1 is not 0
rlm@115 180 0x44 ;D327 ; GOTO not-v-blank
rlm@113 181 0xD3 ;D328
rlm@113 182
rlm@113 183 0xCB ;D329 ; test bit-0 of A
rlm@113 184 0x47 ;D32A
rlm@113 185
rlm@113 186 0xCA ;D32B ; if bit-0 is not 1
rlm@115 187 0x44 ;D32C ; GOTO not-v-blank
rlm@113 188 0xD3 ;D32D
rlm@114 189 ;;; in v-blank mode
rlm@113 190 ;; if v-blank-prev was 0,
rlm@113 191 ;; increment frame-count
rlm@113 192
rlm@113 193 0xFA ;D32E ; load v-blank-prev to A
rlm@113 194 0x20 ;D32F
rlm@113 195 0xD3 ;D330
rlm@113 196
rlm@113 197 0xCB ;D331
rlm@113 198 0x47 ;D332 ; test bit-0 of A
rlm@113 199
rlm@113 200 0x20 ;D333 ; skip next section
rlm@113 201 0x07 ;D334 ; if v-blank-prev was not zero
rlm@113 202
rlm@113 203 ;; v-blank was 0, increment frame-count
rlm@113 204 0xFA ;D335 ; load frame-count into A
rlm@113 205 0x1F ;D336
rlm@113 206 0xD3 ;D337
rlm@113 207
rlm@113 208 0x3C ;D338 ; inc A
rlm@113 209
rlm@113 210 0xEA ;D339 ; load A into frame-count
rlm@113 211 0x1F ;D33A
rlm@113 212 0xD3 ;D33B
rlm@113 213
rlm@114 214 ;; set v-blank-prev to 1
rlm@113 215 0x3E ;D33C ; load 1 into A
rlm@113 216 0x01 ;D33D
rlm@113 217
rlm@113 218 0xEA ;D33E ; load A into v-blank-prev
rlm@113 219 0x20 ;D33F
rlm@113 220 0xD3 ;D340
rlm@113 221
rlm@115 222 0xC3 ;D341 ; return to beginning
rlm@115 223 0x1D ;D342
rlm@115 224 0xD3 ;D343
rlm@113 225
rlm@114 226 ;;; not in v-blank mode
rlm@114 227 ;; set v-blank-prev to 0
rlm@115 228 0x3E ;D344 ; load 0 into A
rlm@115 229 0x00 ;D345
rlm@113 230
rlm@115 231 0xEA ;D346 ; load A into v-blank-prev
rlm@115 232 0x20 ;D347
rlm@115 233 0xD3 ;D348
rlm@115 234
rlm@115 235 0xC3 ;D349 ; return to beginning
rlm@115 236 0x1D ;D34A
rlm@115 237 0xD3 ;D34B
rlm@113 238 ])))
rlm@113 239
rlm@114 240 (defn step-count-frames []
rlm@113 241 (-> (read-down-button)
rlm@110 242 (info)
rlm@110 243 (tick) ;; skip over data section
rlm@110 244 (info)
rlm@110 245 (view-register "Register A" A)
rlm@110 246 (tick) ;; load-data into A
rlm@110 247 (view-register "Register A" A)
rlm@110 248 (info)
rlm@110 249 (view-memory 0xFF00)
rlm@110 250 (tick) ;; load A into 0xFF00
rlm@110 251 (view-memory 0xFF00)
rlm@111 252 (info)
rlm@111 253 (tick)
rlm@111 254 (info)
rlm@111 255 (tick)
rlm@111 256 (info)
rlm@111 257 (tick)
rlm@111 258 (info)
rlm@111 259 (tick)
rlm@111 260 (info)
rlm@111 261 (tick)
rlm@111 262 (info)
rlm@111 263 (tick)
rlm@111 264 (print-inventory)))
rlm@111 265
rlm@115 266 (defn test-count-frames []
rlm@115 267 (= 255 (aget (memory ((apply comp (repeat 255 step))
rlm@115 268 (count-frames)))
rlm@115 269 0xD31F)))
rlm@115 270
rlm@115 271 ;; specs for main bootstrap program
rlm@115 272 ;; starts in "mode-select" mode
rlm@115 273 ;; Each button press takes place in a single frame.
rlm@115 274 ;; mode-select-mode takes one of the main buttons
rlm@115 275 ;; which selects one of up to eight modes
rlm@115 276 ;; mode 1 activated by the "A" button
rlm@115 277 ;; the next two button presses indicates the start
rlm@115 278 ;; memory location which to which the bootstrap
rlm@115 279 ;; program will write.
rlm@115 280 ;; This is done by using each of the eight buttons to
rlm@115 281 ;; spell out an 8 bit number. The order of buttons is
rlm@116 282 ;; [:d :u :l :r :start :select :b :a]
rlm@116 283 ;; [:a :start :l] --> 00101001
rlm@114 284
rlm@115 285 ;; the next button press determines how many bytes are to be
rlm@115 286 ;; written, starting at the start position.
rlm@114 287
rlm@115 288 ;; then, the actual bytes are entered and are written to the
rlm@115 289 ;; start address in sequence.
rlm@114 290
rlm@118 291 (defn input-number-assembly []
rlm@118 292 [0x18 ;D31D ; jump over
rlm@118 293 0x02 ;D31E ; the next 2 bytes
rlm@118 294 0x00 ;D31F ; frame-count
rlm@118 295 0x00 ;D320 ; v-blank-prev
rlm@118 296
rlm@118 297 0xFA ;D321
rlm@118 298 0x41 ;D322 ; load (FF41) into A
rlm@118 299 0xFF ;D323 ; this contains mode flags
rlm@118 300
rlm@118 301 ;; if we're in v-blank, the bit-1 is 0
rlm@118 302 ;; and bit-2 is 1 Otherwise, it is not v-blank.
rlm@118 303 0xCB ;D324 ; test bit-1 of A
rlm@118 304 0x4F ;D325
rlm@114 305
rlm@118 306 0xC2 ;D326 ; if bit-1 is not 0
rlm@118 307 0x44 ;D327 ; GOTO not-v-blank
rlm@118 308 0xD3 ;D328
rlm@118 309
rlm@118 310 0xCB ;D329 ; test bit-0 of A
rlm@118 311 0x47 ;D32A
rlm@118 312
rlm@118 313 0xCA ;D32B ; if bit-0 is not 1
rlm@118 314 0x44 ;D32C ; GOTO not-v-blank
rlm@118 315 0xD3 ;D32D
rlm@118 316
rlm@118 317 ;;; in v-blank mode
rlm@118 318
rlm@118 319 ;; if v-blank-prev was 0,
rlm@118 320 ;; increment frame-count
rlm@118 321
rlm@118 322 0xFA ;D32E ; load v-blank-prev to A
rlm@118 323 0x20 ;D32F
rlm@118 324 0xD3 ;D330
rlm@118 325
rlm@118 326 0xCB ;D331
rlm@118 327 0x47 ;D332 ; test bit-0 of A
rlm@118 328
rlm@118 329 0x20 ;D333 ; skip next section
rlm@118 330 0x07 ;D334 ; if v-blank-prev was not zero
rlm@118 331
rlm@118 332 ;; v-blank was 0, increment frame-count
rlm@118 333 0xFA ;D335 ; load frame-count into A
rlm@118 334 0x1F ;D336
rlm@118 335 0xD3 ;D337
rlm@118 336
rlm@118 337 0x3C ;D338 ; inc A
rlm@118 338
rlm@118 339 0xEA ;D339 ; load A into frame-count
rlm@118 340 0x1F ;D33A
rlm@118 341 0xD3 ;D33B
rlm@118 342
rlm@118 343 ;; set v-blank-prev to 1
rlm@118 344 0x3E ;D33C ; load 1 into A
rlm@118 345 0x01 ;D33D
rlm@118 346
rlm@118 347 0xEA ;D33E ; load A into v-blank-prev
rlm@118 348 0x20 ;D33F
rlm@118 349 0xD3 ;D340
rlm@118 350
rlm@118 351 0xC3 ;D341 ; GOTO input handling code
rlm@118 352 0x4E ;D342
rlm@118 353 0xD3 ;D343
rlm@118 354
rlm@118 355 ;;; not in v-blank mode
rlm@118 356 ;; set v-blank-prev to 0
rlm@118 357 0x3E ;D344 ; load 0 into A
rlm@118 358 0x00 ;D345
rlm@118 359
rlm@118 360 0xEA ;D346 ; load A into v-blank-prev
rlm@118 361 0x20 ;D347
rlm@118 362 0xD3 ;D348
rlm@118 363
rlm@118 364 0xC3 ;D349 ; return to beginning
rlm@118 365 0x1D ;D34A
rlm@118 366 0xD3 ;D34B
rlm@118 367
rlm@118 368 0x00 ;D34C ; these are here
rlm@118 369 0x00 ;D34D ; for glue
rlm@118 370
rlm@118 371
rlm@118 372 ;;; calculate input number based on button presses
rlm@118 373 0x18 ;D34E ; skip next 3 bytes
rlm@118 374 0x03 ;D34F
rlm@118 375 ;D350
rlm@118 376 (Integer/parseInt "00100000" 2) ; select directional pad
rlm@118 377 ;D351
rlm@118 378 (Integer/parseInt "00010000" 2) ; select buttons
rlm@118 379 0x00 ;D352 ; input-number
rlm@118 380
rlm@118 381 ;; select directional pad, store low bits in B
rlm@118 382
rlm@118 383 0xFA ;D353 ; load (D350) into A
rlm@118 384 0x50 ;D354 -->
rlm@118 385 0xD3 ;D355 --> D31F
rlm@118 386
rlm@118 387 0xEA ;D356 ; load (A), which is
rlm@118 388 0x00 ;D357 --> ; 00010000, into FF00
rlm@118 389 0xFF ;D358 --> FF00
rlm@118 390
rlm@118 391 0x06 ;D359
rlm@118 392 ;D35A
rlm@118 393 (Integer/parseInt "11110000" 2) ; "11110000" -> B
rlm@118 394 0xFA ;D35B ; (FF00) -> A
rlm@118 395 0x00 ;D35C
rlm@118 396 0xFF ;D35D
rlm@118 397
rlm@118 398 0xCB ;D35E ; swap nybbles on A
rlm@118 399 0x37 ;D35F
rlm@118 400 0xA0 ;D360 ; (AND A B) -> A
rlm@118 401 0x47 ;D361 ; A -> B
rlm@118 402
rlm@118 403 ;; select buttons store bottom bits in C
rlm@118 404
rlm@118 405 0xFA ; ; load (D351) into A
rlm@118 406 0x51 ; -->
rlm@118 407 0xD3 ; --> D31F
rlm@118 408
rlm@118 409 0xEA ; ; load (A), which is
rlm@118 410 0x00 ; --> ; 00001000, into FF00
rlm@118 411 0xFF ; --> FF00
rlm@118 412
rlm@118 413 0x0E ;
rlm@118 414 (Integer/parseInt "00001111" 2) ; "00001111" -> C
rlm@118 415
rlm@118 416 0xFA ; ; (FF00) -> A
rlm@118 417 0x00 ;
rlm@118 418 0xFF ;
rlm@118 419
rlm@118 420 0xA1 ; ; (AND A C) -> A
rlm@118 421 0x4F ; ; A -> C
rlm@118 422
rlm@118 423 ;; combine the B and C registers into the input number
rlm@118 424 0x79 ; ; C -> A
rlm@118 425 0xB0 ; ; (OR A B) -> A
rlm@118 426 0x2F ; ; negate A
rlm@118 427
rlm@118 428 0xEA ; ; store A into input-number
rlm@118 429 0x52 ;
rlm@118 430 0xD3 ;
rlm@118 431
rlm@118 432 0xC3 ; ; return to beginning
rlm@118 433 0x1D ;
rlm@118 434 0xD3 ;
rlm@118 435 ])
rlm@114 436
rlm@115 437 (defn input-number []
rlm@115 438 (-> (tick (mid-game))
rlm@115 439 (IE! 0) ; disable interrupts
rlm@118 440 (inject-item-assembly (input-number-assembly))))
rlm@115 441
rlm@117 442 (defn test-input-number
rlm@117 443 "Input freestyle buttons and observe the effects at the repl."
rlm@117 444 []
rlm@117 445 (set-state! (input-number))
rlm@117 446 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
rlm@115 447
rlm@118 448 (defn write-memory-assembly []
rlm@124 449
rlm@123 450 [
rlm@123 451 ;; Main Timing Loop
rlm@123 452 ;; Constantly check for v-blank and Trigger main state machine on
rlm@123 453 ;; every transtion from v-blank to non-v-blank.
rlm@118 454
rlm@123 455 0x18 ; D31D ; Variable declaration
rlm@123 456 0x02 ; D31E
rlm@123 457 0x00 ; D31F ; frame-count
rlm@123 458 0x00 ; D320 ; v-blank-prev
rlm@123 459
rlm@123 460 0xFA ; D321 ; load v-blank mode flags into A
rlm@123 461 0x41 ; D322
rlm@123 462 0xFF ; D323
rlm@123 463
rlm@123 464 ;; Branch dependent on v-blank. v-blank happens when the last two
rlm@123 465 ;; bits in A are "01"
rlm@123 466 0xCB ; D324
rlm@123 467 0x4F ; D325
rlm@123 468
rlm@123 469 0xC2 ; D326 ; if bit-1 is not 0, then
rlm@124 470 0x3E ; D327 ; GOTO non-v-blank.
rlm@123 471 0xD3 ; D328
rlm@123 472
rlm@123 473 0xCB ; D329
rlm@123 474 0x47 ; D32A
rlm@123 475
rlm@123 476 0xCA ; D32B ; if bit-0 is not 1, then
rlm@124 477 0x3E ; D32C ; GOTO non-v-blank.
rlm@123 478 0xD3 ; D32D
rlm@123 479
rlm@123 480 ;; V-Blank
rlm@123 481 ;; Activate state-machine if this is a transition event.
rlm@123 482
rlm@123 483 0xFA ; D32E ; load v-bank-prev into A
rlm@123 484 0x20 ; D32F
rlm@123 485 0xD3 ; D330
rlm@123 486
rlm@124 487 0xFE ; D331 ; compare A to 0. >--------\
rlm@124 488 0x00 ; D332 \
rlm@124 489 ; |
rlm@124 490 ;; set v-blank-prev to 1. |
rlm@124 491 0x3E ; D333 ; load 1 into A. |
rlm@124 492 0x01 ; D334 |
rlm@124 493 ; |
rlm@124 494 0xEA ; D335 ; load A into v-blank-prev |
rlm@124 495 0x20 ; D336 |
rlm@124 496 0xD3 ; D337 |
rlm@124 497 ; /
rlm@124 498 ;; if v-blank-prev was 0, activate state-machine <------/
rlm@124 499 0xCA ; D338 ; if v-blank-prev
rlm@123 500 0x46 ; D339 ; was 0,
rlm@123 501 0xD3 ; D33A ; GOTO state-machine
rlm@123 502
rlm@123 503 0xC3 ; D33B
rlm@123 504 0x1D ; D33C
rlm@123 505 0xD3 ; D33D ; GOTO beginning
rlm@123 506 ;; END V-blank
rlm@123 507
rlm@123 508 ;; Non-V-Blank
rlm@123 509 ;; Set v-blank-prev to 0
rlm@123 510 0x3E ; D33E ; load 0 into A
rlm@123 511 0x00 ; D33F
rlm@123 512
rlm@123 513 0xEA ; D340 ; load A into v-blank-prev
rlm@123 514 0x20 ; D341
rlm@123 515 0xD3 ; D342
rlm@118 516
rlm@123 517 0xC3 ; D343
rlm@123 518 0x1D ; D344
rlm@123 519 0xD3 ; D345 ; GOTO beginning
rlm@123 520 ;; END Not-V-Blank
rlm@115 521
rlm@124 522
rlm@123 523 ;; Main State Machine -- Input Section
rlm@123 524 ;; This is called once every frame.
rlm@123 525 ;; It collects input and uses it to drive the
rlm@123 526 ;; state transitions.
rlm@123 527
rlm@123 528 ;; Increment frame-count
rlm@123 529 0xFA ; D346 ; load frame-count into A
rlm@123 530 0x1F ; D347
rlm@123 531 0xD3 ; D348
rlm@118 532
rlm@123 533 0x3C ; D349 ; inc A
rlm@115 534
rlm@123 535 0xEA ; D34A
rlm@123 536 0x1F ; D34B ; load A into frame-count
rlm@123 537 0xD3 ; D34C
rlm@118 538
rlm@123 539 0x00 ; D34D ; glue :)
rlm@123 540
rlm@123 541
rlm@123 542 ;;;;;;;;; BEGIN RLM DEBUG
rlm@124 543 ;;0xC3 ;; jump to beginning
rlm@124 544 ;;0x1D
rlm@124 545 ;;0xD3
rlm@123 546 ;;;;;;;;; END RLM DEBUG
rlm@118 547
rlm@118 548
rlm@118 549 0x18 ;D34E ; skip next 3 bytes
rlm@118 550 0x03 ;D34F
rlm@123 551 ;D350
rlm@118 552 (Integer/parseInt "00100000" 2) ; select directional pad
rlm@123 553 ;D351
rlm@118 554 (Integer/parseInt "00010000" 2) ; select buttons
rlm@118 555 0x00 ;D352 ; input-number
rlm@118 556
rlm@123 557 ;; select directional pad; store low bits in B
rlm@118 558
rlm@118 559 0xFA ;D353 ; load (D350) into A
rlm@118 560 0x50 ;D354 -->
rlm@123 561 0xD3 ;D355 --> D350
rlm@118 562
rlm@118 563 0xEA ;D356 ; load (A), which is
rlm@118 564 0x00 ;D357 --> ; 00010000, into FF00
rlm@118 565 0xFF ;D358 --> FF00
rlm@118 566
rlm@118 567 0x06 ;D359
rlm@123 568 ;D35A
rlm@118 569 (Integer/parseInt "11110000" 2) ; "11110000" -> B
rlm@118 570 0xFA ;D35B ; (FF00) -> A
rlm@118 571 0x00 ;D35C
rlm@118 572 0xFF ;D35D
rlm@118 573
rlm@118 574 0xCB ;D35E ; swap nybbles on A
rlm@118 575 0x37 ;D35F
rlm@118 576 0xA0 ;D360 ; (AND A B) -> A
rlm@118 577 0x47 ;D361 ; A -> B
rlm@118 578
rlm@123 579 ;; select buttons; store bottom bits in C
rlm@118 580
rlm@118 581 0xFA ;D362 ; load (D351) into A
rlm@118 582 0x51 ;D363 -->
rlm@123 583 0xD3 ;D364 --> D351
rlm@118 584
rlm@118 585 0xEA ;D365 ; load (A), which is
rlm@118 586 0x00 ;D366 --> ; 00001000, into FF00
rlm@118 587 0xFF ;D367 --> FF00
rlm@118 588
rlm@118 589 0x0E ;D368
rlm@118 590 ;D369
rlm@118 591 (Integer/parseInt "00001111" 2) ; "00001111" -> C
rlm@118 592
rlm@118 593 0xFA ;D36A ; (FF00) -> A
rlm@118 594 0x00 ;D36B
rlm@118 595 0xFF ;D36C
rlm@118 596
rlm@118 597 0xA1 ;D36D ; (AND A C) -> A
rlm@118 598 0x4F ;D36E ; A -> C
rlm@118 599
rlm@118 600 ;; combine the B and C registers into the input number
rlm@118 601 0x79 ;D36F ; C -> A
rlm@118 602 0xB0 ;D370 ; (OR A B) -> A
rlm@118 603 0x2F ;D371 ; negate A
rlm@118 604
rlm@118 605 0xEA ;D372 ; store A into input-number
rlm@118 606 0x52 ;D373
rlm@118 607 0xD3 ;D374
rlm@118 608
rlm@124 609 0x00 ;D375
rlm@124 610 0x00 ;D376
rlm@124 611 0x00 ;D377
rlm@118 612 0x00 ;D378
rlm@118 613 0x00 ;D379
rlm@118 614 0x00 ;D37A
rlm@118 615 0x00 ;D37B ; these are here because
rlm@118 616 0x00 ;D37C ; I messed up :(
rlm@118 617 0x00 ;D37D
rlm@118 618 0x00 ;D37E
rlm@118 619 0x00 ;D37F
rlm@118 620
rlm@118 621 ;; beginning of main state machine
rlm@118 622 0x18 ;D380 ; Declaration of variables
rlm@118 623 0x05 ;D381 ; 5 variables:
rlm@118 624 0x00 ;D382 ; current-mode
rlm@122 625 0x00 ;D383 ; bytes-to-write
rlm@126 626 0x00 ;D384 ; bytes-written
rlm@126 627 0x00 ;D385 ; start-point-high
rlm@126 628 0x00 ;D386 ; start-point-low
rlm@118 629
rlm@118 630
rlm@118 631 ;; banch on current mode
rlm@118 632 0xFA ;D387 ; load current-mode (0xD382)
rlm@118 633 0x82 ;D388 ; into A
rlm@118 634 0xD3 ;D389
rlm@118 635 0x00 ;D38A
rlm@118 636
rlm@120 637
rlm@120 638 ;; GOTO Mode 0 (input-mode) if current-mode is 0
rlm@118 639 0xFE ;D38B
rlm@118 640 0x00 ;D38C ; compare A with 0x00
rlm@118 641
rlm@120 642 0xCA ;D38D ; goto Mode 0 if A == 0
rlm@118 643 0xA8 ;D38E
rlm@118 644 0xD3 ;D38F
rlm@118 645
rlm@121 646 ;; GOTO Mode 1 (set-length) if current-mode is 1
rlm@120 647 0xFE ;D390
rlm@120 648 0x01 ;D391 ; compare A with 0x01
rlm@120 649
rlm@120 650 0xCA ;D392
rlm@120 651 0xB1 ;D393
rlm@120 652 0xD3 ;D394 ; goto Mode 1 if A == 1
rlm@120 653
rlm@126 654 ;; GOTO Mode 2 (set-start-point-high) if current mode is 2
rlm@121 655 0xFE ;D395
rlm@121 656 0x02 ;D396 ; compare A with 0x02
rlm@121 657
rlm@121 658 0xCA ;D397
rlm@121 659 0xBF ;D398
rlm@121 660 0xD3 ;D399 ; goto Mode 2 if A == 2
rlm@121 661
rlm@126 662 ;; GOTO Mode 3 (set-start-point-low) if current mode is 3
rlm@125 663 0xFE ;D39A
rlm@125 664 0x03 ;D39B
rlm@125 665
rlm@126 666 0xCA ;D39C
rlm@126 667 0xCD ;D39D
rlm@126 668 0xD3 ;D39E ; goto Mode 3 if A == 3
rlm@126 669
rlm@126 670 0x00 ;D39F
rlm@118 671 0x00 ;D3A0
rlm@118 672 0x00 ;D3A1
rlm@118 673 0x00 ;D3A2
rlm@118 674 0x00 ;D3A3
rlm@118 675 0x00 ;D3A4
rlm@118 676 ;; End of Mode checking, goto beginning
rlm@118 677 0xC3 ;D3A5
rlm@118 678 0x1D ;D3A6
rlm@118 679 0xD3 ;D3A7
rlm@120 680
rlm@120 681
rlm@120 682 ;; Mode 0 -- input-mode mode
rlm@120 683 ;; means that we are waiting for a mode, so set the mode to
rlm@120 684 ;; whatever is currently in input-number. If nothing is
rlm@120 685 ;; entered, then the program stays in input-mode mode
rlm@120 686
rlm@118 687 ;; set current-mode to input-number
rlm@118 688 0xFA ;D3A8 ; load input-number (0xD352)
rlm@118 689 0x52 ;D3A9 ; into A
rlm@118 690 0xD3 ;D3AA
rlm@118 691
rlm@118 692 0xEA ;D3AB ; load A into current-mode
rlm@118 693 0x82 ;D3AC ; (0xD382)
rlm@118 694 0xD3 ;D3AD
rlm@118 695
rlm@118 696 0xC3 ;D3AE ; go back to beginning
rlm@118 697 0x1D ;D3AF
rlm@118 698 0xD3 ;D3B0
rlm@120 699 ;; End Mode 0
rlm@118 700
rlm@120 701
rlm@121 702 ;; Mode 1 -- set-length mode
rlm@121 703 ;; This is the header for writing things to memory.
rlm@121 704 ;; User specifies the number of bytes to write.
rlm@120 705 ;; Mode is auto advanced to Mode 2 after this mode
rlm@120 706 ;; completes.
rlm@120 707
rlm@121 708 ;; Set bytes left to write to input-number;
rlm@120 709 ;; set current-mode to 0x02.
rlm@120 710 0xFA ;D3B1 ; load input-number (0xD352)
rlm@120 711 0x52 ;D3B2 ; into A
rlm@120 712 0xD3 ;D3B3
rlm@120 713
rlm@120 714 0xEA ;D3B4 ; load A into bytes-left-to-write
rlm@120 715 0x83 ;D3B5 ; (0xD383)
rlm@120 716 0xD3 ;D3B6
rlm@120 717
rlm@121 718 0x3E ;D3B7 ; load 0x02 into A.
rlm@120 719 0x02 ;D3B8
rlm@120 720
rlm@120 721 0xEA ;D3B9 ; load A to current-mode
rlm@120 722 0x82 ;D3BA ; advancing from Mode 1 to
rlm@120 723 0xD3 ;D3BB ; Mode 2
rlm@120 724
rlm@120 725 0xC3 ;D3BC ; go back to beginning
rlm@120 726 0x1D ;D3BD
rlm@120 727 0xD3 ;D3BE
rlm@120 728 ;; End Mode 1
rlm@121 729
rlm@121 730
rlm@126 731 ;; Mode 2 -- set start-point-high mode
rlm@126 732 ;; Middle part of the header for writing things to memory.
rlm@121 733 ;; User specifies the start location in RAM to which
rlm@121 734 ;; data will be written.
rlm@121 735 ;; Mode is auto advanced to Mode 3 after this mode completes.
rlm@121 736
rlm@126 737 ;; Set start-point-high to input-number;
rlm@121 738 ;; set current mode to 0x03.
rlm@121 739 0xFA ;D3BF ; load input-number (0xD352)
rlm@121 740 0x52 ;D3C0 ; into A
rlm@121 741 0xD3 ;D3C1
rlm@121 742
rlm@126 743 0xEA ;D3C2 ; load A into start-point-high
rlm@126 744 0x85 ;D3C3 ; (0xD385)
rlm@121 745 0xD3 ;D3C4
rlm@121 746
rlm@121 747 0x3E ;D3C5 ; load 0x03 into A.
rlm@121 748 0x03 ;D3C6
rlm@121 749
rlm@121 750 0xEA ;D3C7 ; load A to current-mode,
rlm@121 751 0x82 ;D3C8 ; advancing from Mode 2 to
rlm@121 752 0xD3 ;D3C9 ; Mode 3.
rlm@120 753
rlm@121 754 0xC3 ;D3CA ; go back to beginning
rlm@121 755 0x1D ;D3CB
rlm@121 756 0xD3 ;D3CC
rlm@121 757 ;;End Mode 2
rlm@125 758
rlm@118 759
rlm@126 760 ;; Mode 3 -- set-start-point-low mode
rlm@126 761 ;; Final part of header for writing things to memory.
rlm@126 762 ;; User specifies the low bytes of 16 bit start-point.
rlm@126 763
rlm@126 764 ;; Set start-point-low to input-number;
rlm@126 765 ;; set current mode to 0x04
rlm@126 766 0xFA ;D3CD ; load input-number into A
rlm@126 767 0x52 ;D3CE
rlm@126 768 0xD3 ;D3CF
rlm@126 769
rlm@126 770 0xEA ;D3D0 ; load A into start-point-low
rlm@126 771 0x86 ;D3D1
rlm@126 772 0xD3 ;D3D2
rlm@126 773
rlm@126 774 0x3E ;D3D3 ; load 0x04 into A.
rlm@126 775 0x04 ;D3D4
rlm@126 776
rlm@126 777 0xEA ;D3D5 ; load A to current-mode,
rlm@126 778 0x82 ;D3D6 ; advancing from Mode 3 to
rlm@126 779 0xD3 ;D3D7 ; Mode 4.
rlm@126 780
rlm@126 781 0xC3 ;D3D8 ; go back to beginning
rlm@126 782 0x1D ;D3D9
rlm@126 783 0xD3 ;D3DA
rlm@126 784
rlm@126 785 ;; Mode 4 -- write bytes mode
rlm@125 786
rlm@125 787 ;; This is where RAM manipulation happens. User supplies
rlm@125 788 ;; bytes every frame, which are written sequentially to
rlm@125 789 ;; start-point until bytes-to-write have been written. Once
rlm@125 790 ;; bytes-to-write have been written, the mode is reset to 0.
rlm@125 791
rlm@125 792 ;; compare bytes-written with bytes-to-write.
rlm@125 793 ;; if they are the same, then reset mode to 0
rlm@125 794
rlm@126 795 0xFA ;D3DB ; load bytes-to-write into A
rlm@126 796 0x83 ;D3DC
rlm@126 797 0xD3 ;D3DD
rlm@126 798 0x47 ;D3DE ; load A into B
rlm@126 799 0xFA ;D3DF ; load bytes-written into A
rlm@126 800 0x84 ;D3E0
rlm@126 801 0xD3 ;D3E1
rlm@126 802 0xB8 ;D3E2 ; compare A with B
rlm@126 803 0xCA ;D3E3 ; if they are equal, go to cleanup
rlm@125 804
rlm@125 805 ;; Write Memory Section
rlm@125 806 ;; Write the input-number, interpreted as an 8-bit number,
rlm@125 807 ;; into the current target register, determined by
rlm@125 808 ;; (+ start-point bytes-written).
rlm@125 809 ;; Then, increment bytes-written by 1.
rlm@125 810
rlm@125 811 0x00 ;D3D6
rlm@125 812 0x00 ;D3D6
rlm@125 813 0x00 ;D3D7
rlm@125 814 0x00 ;D3D8
rlm@125 815 0x00 ;D3D9
rlm@125 816 0x00 ;D3DA
rlm@125 817 0x00 ;D3DB
rlm@125 818 0x00 ;D3DC
rlm@125 819 0x00 ;D3DD
rlm@125 820 0x00 ;D3DE
rlm@125 821 0x00 ;D3DF
rlm@125 822 0x00 ;D3E0
rlm@125 823 0x00 ;D3E1
rlm@125 824 0x00 ;D3E2
rlm@125 825 0x00 ;D3E3
rlm@125 826 0x00 ;D3E4
rlm@125 827 0x00 ;D3E5
rlm@125 828 0x00 ;D3E6
rlm@125 829 0x00 ;D3E7
rlm@125 830 0x00 ;D3E8
rlm@125 831 0x00 ;D3E9
rlm@125 832 0x00 ;D3EA
rlm@125 833 0x00 ;D3EB
rlm@125 834 0x00 ;D3EC
rlm@125 835 0x00 ;D3ED
rlm@125 836 0x00 ;D3EE
rlm@125 837 0x00 ;D3EF
rlm@125 838 0x00 ;D3F0
rlm@125 839 0x00 ;D3F1
rlm@125 840 0x00 ;D3F2
rlm@125 841 0x00 ;D3F3
rlm@125 842 0x00 ;D3F4
rlm@125 843 0x00 ;D3F5
rlm@125 844 0x00 ;D3F6
rlm@125 845 0x00 ;D3F7
rlm@125 846 0x00 ;D3F8
rlm@125 847 0x00 ;D3F9
rlm@125 848 0x00 ;D3FA
rlm@125 849 0x00 ;D3FB
rlm@125 850 0x00 ;D3FC
rlm@125 851 0x00 ;D3FD
rlm@125 852 0x00 ;D3FE
rlm@125 853
rlm@121 854
rlm@118 855
rlm@118 856
rlm@118 857
rlm@118 858 0xC3 ; ; Complete Loop
rlm@118 859 0x1D ;
rlm@118 860 0xD3 ;
rlm@118 861
rlm@118 862
rlm@118 863
rlm@118 864 ])
rlm@118 865
rlm@118 866
rlm@123 867
rlm@118 868 (def frame-count 0xD31F)
rlm@118 869 (def input 0xD352)
rlm@118 870 (def current-mode 0xD382)
rlm@124 871 (def bytes-to-write 0xD383)
rlm@126 872 (def start-point-high 0xD385)
rlm@126 873 (def start-point-low 0xD386)
rlm@126 874
rlm@125 875 (def bytes-written 0xD385)
rlm@118 876
rlm@118 877 (defn write-memory []
rlm@118 878 (-> (tick (mid-game))
rlm@118 879 (IE! 0) ; disable interrupts
rlm@118 880 (inject-item-assembly (write-memory-assembly))))
rlm@119 881
rlm@119 882 (defn test-write-memory []
rlm@119 883 (set-state! (write-memory))
rlm@119 884 (dorun
rlm@119 885 (dotimes [_ 5000]
rlm@119 886 (view-memory (step @current-state) current-mode))))
rlm@122 887
rlm@122 888 (def bytes-to-write 0xD383)
rlm@122 889 (def start-point 0xD384)
rlm@122 890
rlm@125 891 (defn print-blank-assembly
rlm@125 892 [start end]
rlm@125 893 (dorun
rlm@125 894 (map
rlm@125 895 #(println (format "0x00 ;%04X " %))
rlm@125 896 (range start end))))
rlm@125 897
rlm@122 898 (defn test-mode-2 []
rlm@122 899 (->
rlm@122 900 (write-memory)
rlm@123 901 (view-memory frame-count)
rlm@122 902 (step)
rlm@122 903 (step [:a])
rlm@122 904 (step [:b])
rlm@122 905 (step [:start])
rlm@122 906 (step [])
rlm@123 907 (view-memory frame-count)))
rlm@123 908
rlm@124 909 (defn test-mode-2 []
rlm@124 910 (->
rlm@124 911 (write-memory)
rlm@124 912 (view-memory current-mode)
rlm@124 913 (step [])
rlm@124 914 (step [])
rlm@124 915 (step [])
rlm@124 916 (#(do (println "after three steps") %))
rlm@124 917 (view-memory current-mode)
rlm@124 918 (step [:a])
rlm@124 919 (#(do (println "step with [:a]") %))
rlm@124 920 (view-memory current-mode)
rlm@124 921 (view-memory bytes-to-write)
rlm@126 922 (view-memory start-point-high)
rlm@126 923 (view-memory start-point-low)
rlm@126 924 (#(do (println "step with [:a :b :start]")%))
rlm@126 925 (step [:a :b :start])
rlm@126 926 (view-memory current-mode)
rlm@126 927 (view-memory bytes-to-write)
rlm@126 928 (view-memory start-point-high)
rlm@126 929 (view-memory start-point-low)
rlm@126 930 (#(do (println "step with [:u :d]")%))
rlm@126 931 (step [:u :d])
rlm@126 932 (view-memory current-mode)
rlm@126 933 (view-memory bytes-to-write)
rlm@126 934 (view-memory start-point-high)
rlm@126 935 (view-memory start-point-low)
rlm@124 936 (#(do (println "step with [:u :d :l :r]")%))
rlm@125 937 (step [:u :d :l :r])
rlm@124 938 (view-memory current-mode)
rlm@124 939 (view-memory bytes-to-write)
rlm@126 940 (view-memory start-point-high)
rlm@126 941 (view-memory start-point-low)
rlm@126 942
rlm@124 943 ))
rlm@123 944
rlm@123 945