annotate clojure/com/aurellem/assembly.clj @ 127:901ee6b648da

preliminary memory bootstrapping program complete.
author Robert McIntyre <rlm@mit.edu>
date Sat, 17 Mar 2012 03:43:42 -0500
parents 86f92deacd2a
children 203d64e16156
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@127 670 ;; GOTO Mode 4 (write-memory) if current mode is 4
rlm@127 671 0xFE ;D39F
rlm@127 672 0x04 ;D3A0
rlm@127 673
rlm@127 674 0xCA ;D3A1
rlm@127 675 0xDB ;D3A2
rlm@127 676 0xD3 ;D3A3
rlm@127 677
rlm@118 678 0x00 ;D3A4
rlm@118 679 ;; End of Mode checking, goto beginning
rlm@118 680 0xC3 ;D3A5
rlm@118 681 0x1D ;D3A6
rlm@118 682 0xD3 ;D3A7
rlm@120 683
rlm@120 684
rlm@120 685 ;; Mode 0 -- input-mode mode
rlm@120 686 ;; means that we are waiting for a mode, so set the mode to
rlm@120 687 ;; whatever is currently in input-number. If nothing is
rlm@120 688 ;; entered, then the program stays in input-mode mode
rlm@120 689
rlm@118 690 ;; set current-mode to input-number
rlm@118 691 0xFA ;D3A8 ; load input-number (0xD352)
rlm@118 692 0x52 ;D3A9 ; into A
rlm@118 693 0xD3 ;D3AA
rlm@118 694
rlm@118 695 0xEA ;D3AB ; load A into current-mode
rlm@118 696 0x82 ;D3AC ; (0xD382)
rlm@118 697 0xD3 ;D3AD
rlm@118 698
rlm@118 699 0xC3 ;D3AE ; go back to beginning
rlm@118 700 0x1D ;D3AF
rlm@118 701 0xD3 ;D3B0
rlm@120 702 ;; End Mode 0
rlm@118 703
rlm@120 704
rlm@121 705 ;; Mode 1 -- set-length mode
rlm@121 706 ;; This is the header for writing things to memory.
rlm@121 707 ;; User specifies the number of bytes to write.
rlm@120 708 ;; Mode is auto advanced to Mode 2 after this mode
rlm@120 709 ;; completes.
rlm@120 710
rlm@121 711 ;; Set bytes left to write to input-number;
rlm@120 712 ;; set current-mode to 0x02.
rlm@120 713 0xFA ;D3B1 ; load input-number (0xD352)
rlm@120 714 0x52 ;D3B2 ; into A
rlm@120 715 0xD3 ;D3B3
rlm@120 716
rlm@120 717 0xEA ;D3B4 ; load A into bytes-left-to-write
rlm@120 718 0x83 ;D3B5 ; (0xD383)
rlm@120 719 0xD3 ;D3B6
rlm@120 720
rlm@121 721 0x3E ;D3B7 ; load 0x02 into A.
rlm@120 722 0x02 ;D3B8
rlm@120 723
rlm@120 724 0xEA ;D3B9 ; load A to current-mode
rlm@120 725 0x82 ;D3BA ; advancing from Mode 1 to
rlm@120 726 0xD3 ;D3BB ; Mode 2
rlm@120 727
rlm@120 728 0xC3 ;D3BC ; go back to beginning
rlm@120 729 0x1D ;D3BD
rlm@120 730 0xD3 ;D3BE
rlm@120 731 ;; End Mode 1
rlm@121 732
rlm@121 733
rlm@126 734 ;; Mode 2 -- set start-point-high mode
rlm@126 735 ;; Middle part of the header for writing things to memory.
rlm@121 736 ;; User specifies the start location in RAM to which
rlm@121 737 ;; data will be written.
rlm@121 738 ;; Mode is auto advanced to Mode 3 after this mode completes.
rlm@121 739
rlm@126 740 ;; Set start-point-high to input-number;
rlm@121 741 ;; set current mode to 0x03.
rlm@121 742 0xFA ;D3BF ; load input-number (0xD352)
rlm@121 743 0x52 ;D3C0 ; into A
rlm@121 744 0xD3 ;D3C1
rlm@121 745
rlm@126 746 0xEA ;D3C2 ; load A into start-point-high
rlm@126 747 0x85 ;D3C3 ; (0xD385)
rlm@121 748 0xD3 ;D3C4
rlm@121 749
rlm@121 750 0x3E ;D3C5 ; load 0x03 into A.
rlm@121 751 0x03 ;D3C6
rlm@121 752
rlm@121 753 0xEA ;D3C7 ; load A to current-mode,
rlm@121 754 0x82 ;D3C8 ; advancing from Mode 2 to
rlm@121 755 0xD3 ;D3C9 ; Mode 3.
rlm@120 756
rlm@121 757 0xC3 ;D3CA ; go back to beginning
rlm@121 758 0x1D ;D3CB
rlm@121 759 0xD3 ;D3CC
rlm@121 760 ;;End Mode 2
rlm@125 761
rlm@118 762
rlm@126 763 ;; Mode 3 -- set-start-point-low mode
rlm@126 764 ;; Final part of header for writing things to memory.
rlm@126 765 ;; User specifies the low bytes of 16 bit start-point.
rlm@126 766
rlm@126 767 ;; Set start-point-low to input-number;
rlm@126 768 ;; set current mode to 0x04
rlm@126 769 0xFA ;D3CD ; load input-number into A
rlm@126 770 0x52 ;D3CE
rlm@126 771 0xD3 ;D3CF
rlm@126 772
rlm@126 773 0xEA ;D3D0 ; load A into start-point-low
rlm@126 774 0x86 ;D3D1
rlm@126 775 0xD3 ;D3D2
rlm@126 776
rlm@126 777 0x3E ;D3D3 ; load 0x04 into A.
rlm@126 778 0x04 ;D3D4
rlm@126 779
rlm@126 780 0xEA ;D3D5 ; load A to current-mode,
rlm@126 781 0x82 ;D3D6 ; advancing from Mode 3 to
rlm@126 782 0xD3 ;D3D7 ; Mode 4.
rlm@126 783
rlm@126 784 0xC3 ;D3D8 ; go back to beginning
rlm@126 785 0x1D ;D3D9
rlm@126 786 0xD3 ;D3DA
rlm@126 787
rlm@126 788 ;; Mode 4 -- write bytes mode
rlm@125 789
rlm@125 790 ;; This is where RAM manipulation happens. User supplies
rlm@125 791 ;; bytes every frame, which are written sequentially to
rlm@125 792 ;; start-point until bytes-to-write have been written. Once
rlm@125 793 ;; bytes-to-write have been written, the mode is reset to 0.
rlm@125 794
rlm@125 795 ;; compare bytes-written with bytes-to-write.
rlm@125 796 ;; if they are the same, then reset mode to 0
rlm@125 797
rlm@126 798 0xFA ;D3DB ; load bytes-to-write into A
rlm@126 799 0x83 ;D3DC
rlm@126 800 0xD3 ;D3DD
rlm@127 801
rlm@126 802 0x47 ;D3DE ; load A into B
rlm@127 803
rlm@126 804 0xFA ;D3DF ; load bytes-written into A
rlm@126 805 0x84 ;D3E0
rlm@126 806 0xD3 ;D3E1
rlm@127 807
rlm@126 808 0xB8 ;D3E2 ; compare A with B
rlm@127 809
rlm@126 810 0xCA ;D3E3 ; if they are equal, go to cleanup
rlm@127 811 0x07 ;D3E4
rlm@127 812 0xD4 ;D3E5
rlm@125 813
rlm@125 814 ;; Write Memory Section
rlm@125 815 ;; Write the input-number, interpreted as an 8-bit number,
rlm@125 816 ;; into the current target register, determined by
rlm@125 817 ;; (+ start-point bytes-written).
rlm@125 818 ;; Then, increment bytes-written by 1.
rlm@125 819
rlm@127 820 0xFA ;D3E6 ; load start-point-high into A
rlm@127 821 0x85 ;D3E7
rlm@127 822 0xD3 ;D3E8
rlm@127 823
rlm@127 824 0x67 ;D3E9 ; load A into H
rlm@127 825
rlm@127 826 0xFA ;D3EA ; load start-point-low into A
rlm@127 827 0x86 ;D3EB
rlm@127 828 0xD3 ;D3EC
rlm@127 829
rlm@127 830 0x6F ;D3ED ; load A into L
rlm@127 831
rlm@127 832 0xFA ;D3EE ; load bytes-written into A
rlm@127 833 0x84 ;D3EF
rlm@127 834 0xD3 ;D3F0
rlm@127 835
rlm@127 836 0x00 ;D3F1 ; These are here because
rlm@127 837 0x00 ;D3F2 ; I screwed up again.
rlm@125 838 0x00 ;D3F3
rlm@125 839
rlm@127 840 0x85 ;D3F4 ; add L to A; store A in L.
rlm@127 841 0x6F ;D3F5
rlm@121 842
rlm@127 843 0x30 ;D3F6 ; If the addition overflowed,
rlm@127 844 0x01 ;D3F7
rlm@127 845 0x24 ;D3F8 ; increment H.
rlm@118 846
rlm@127 847 ;; Now, HL points to the correct place in memory
rlm@127 848
rlm@127 849 0xFA ;D3F9 ; load input-number into A
rlm@127 850 0x52 ;D3FA
rlm@127 851 0xD3 ;D3FB
rlm@118 852
rlm@127 853 0x77 ;D3FC ; load A into (HL)
rlm@118 854
rlm@127 855 0xFA ;D3FD ; load bytes-written into A
rlm@127 856 0x84 ;D3FE
rlm@127 857 0xD3 ;D3FF
rlm@118 858
rlm@127 859 0x3C ;D400 ; increment A
rlm@127 860
rlm@127 861 0xEA ;D401 ; load A into bytes-written
rlm@127 862 0x84 ;D402
rlm@127 863 0xD3 ;D403
rlm@127 864
rlm@127 865 0xC3 ;D404 ; go back to beginning.
rlm@127 866 0x1D ;D405
rlm@127 867 0xD3 ;D406
rlm@127 868 ;; End Write Memory Section
rlm@127 869
rlm@127 870 ;; Mode 4 Cleanup Section
rlm@127 871 ;; reset bytes-written to 0
rlm@127 872 ;; set mode to 0
rlm@127 873 0x3E ;D407 ; load 0 into A
rlm@127 874 0x00 ;D408
rlm@127 875
rlm@127 876 0xEA ;D409 ; load A into bytes-written
rlm@127 877 0x84 ;D40A
rlm@127 878 0xD3 ;D40B
rlm@127 879
rlm@127 880 0xEA ;D40C ; load A into current-mode
rlm@127 881 0x82 ;D40D
rlm@127 882 0xD3 ;D40E
rlm@127 883
rlm@127 884 0xC3 ;D40F ; go back to beginning
rlm@127 885 0x1D ;D410
rlm@127 886 0xD3 ;D411
rlm@127 887
rlm@127 888 ;; End Mode 4
rlm@118 889
rlm@118 890 ])
rlm@118 891
rlm@118 892
rlm@123 893
rlm@118 894 (def frame-count 0xD31F)
rlm@118 895 (def input 0xD352)
rlm@127 896 (def current-mode 0xD382)
rlm@127 897 (def bytes-to-write 0xD383)
rlm@127 898 (def bytes-written 0xD384)
rlm@127 899 (def start-point-high 0xD385)
rlm@127 900 (def start-point-low 0xD386)
rlm@126 901
rlm@127 902
rlm@118 903
rlm@118 904 (defn write-memory []
rlm@118 905 (-> (tick (mid-game))
rlm@118 906 (IE! 0) ; disable interrupts
rlm@118 907 (inject-item-assembly (write-memory-assembly))))
rlm@119 908
rlm@119 909 (defn test-write-memory []
rlm@119 910 (set-state! (write-memory))
rlm@119 911 (dorun
rlm@119 912 (dotimes [_ 5000]
rlm@119 913 (view-memory (step @current-state) current-mode))))
rlm@122 914
rlm@122 915 (def bytes-to-write 0xD383)
rlm@122 916 (def start-point 0xD384)
rlm@122 917
rlm@125 918 (defn print-blank-assembly
rlm@125 919 [start end]
rlm@125 920 (dorun
rlm@125 921 (map
rlm@125 922 #(println (format "0x00 ;%04X " %))
rlm@125 923 (range start end))))
rlm@125 924
rlm@122 925 (defn test-mode-2 []
rlm@122 926 (->
rlm@122 927 (write-memory)
rlm@123 928 (view-memory frame-count)
rlm@122 929 (step)
rlm@122 930 (step [:a])
rlm@122 931 (step [:b])
rlm@122 932 (step [:start])
rlm@122 933 (step [])
rlm@123 934 (view-memory frame-count)))
rlm@123 935
rlm@127 936 (defn test-mode-4 []
rlm@124 937 (->
rlm@124 938 (write-memory)
rlm@127 939 (#(do (println "memory from 0xC00F to 0xC01F:"
rlm@127 940 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
rlm@124 941 (view-memory current-mode)
rlm@124 942 (step [])
rlm@124 943 (step [])
rlm@124 944 (step [])
rlm@124 945 (#(do (println "after three steps") %))
rlm@124 946 (view-memory current-mode)
rlm@127 947
rlm@127 948 ;; Activate memory writing mode
rlm@127 949
rlm@127 950 (#(do (println "step with [:a]") %))
rlm@124 951 (step [:a])
rlm@124 952 (view-memory current-mode)
rlm@124 953 (view-memory bytes-to-write)
rlm@126 954 (view-memory start-point-high)
rlm@126 955 (view-memory start-point-low)
rlm@127 956
rlm@127 957 ;; Specify four bytes to be written
rlm@127 958
rlm@127 959 (#(do (println "step with [:select]")%))
rlm@127 960 (step [:select])
rlm@126 961 (view-memory current-mode)
rlm@126 962 (view-memory bytes-to-write)
rlm@126 963 (view-memory start-point-high)
rlm@126 964 (view-memory start-point-low)
rlm@127 965
rlm@127 966 ;; Specify target memory address as 0xC00F
rlm@127 967
rlm@126 968 (#(do (println "step with [:u :d]")%))
rlm@126 969 (step [:u :d])
rlm@126 970 (view-memory current-mode)
rlm@126 971 (view-memory bytes-to-write)
rlm@126 972 (view-memory start-point-high)
rlm@126 973 (view-memory start-point-low)
rlm@127 974
rlm@127 975 (#(do (println "step with [:a :b :start :select]")%))
rlm@127 976 (step [:a :b :start :select])
rlm@124 977 (view-memory current-mode)
rlm@124 978 (view-memory bytes-to-write)
rlm@126 979 (view-memory start-point-high)
rlm@126 980 (view-memory start-point-low)
rlm@127 981
rlm@127 982 ;; Start reprogramming memory
rlm@127 983
rlm@127 984 (#(do (println "step with [:a]")%))
rlm@127 985 (step [:a])
rlm@127 986 (view-memory current-mode)
rlm@127 987 (view-memory bytes-written)
rlm@127 988
rlm@127 989 (#(do (println "step with [:b]")%))
rlm@127 990 (step [:b])
rlm@127 991 (view-memory current-mode)
rlm@127 992 (view-memory bytes-written)
rlm@127 993
rlm@127 994 (#(do (println "step with [:a :b]")%))
rlm@127 995 (step [:a :b])
rlm@127 996 (view-memory current-mode)
rlm@127 997 (view-memory bytes-written)
rlm@127 998
rlm@127 999 (#(do (println "step with [:select]")%))
rlm@127 1000 (step [:select])
rlm@127 1001 (view-memory current-mode)
rlm@127 1002 (view-memory bytes-written)
rlm@127 1003
rlm@127 1004 ;; Reprogramming done, program ready for more commands.
rlm@127 1005
rlm@127 1006 (#(do (println "step with []")%))
rlm@127 1007 (step [])
rlm@127 1008 (view-memory current-mode)
rlm@127 1009 (view-memory bytes-written)
rlm@126 1010
rlm@127 1011 (#(do (println "memory from 0xC00F to 0xC01F:"
rlm@127 1012 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))))
rlm@123 1013
rlm@123 1014