annotate clojure/com/aurellem/assembly.clj @ 139:74ec1ac044bb

write-memory-assembly* confirmed...stage one.
author Dylan Holmes <ocsenave@gmail.com>
date Mon, 19 Mar 2012 05:12:05 -0500
parents 2b69cbe8a5b9
children aa5b5927e5fe
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@128 69 (defn B [state]
rlm@128 70 (bit-shift-right (bit-and 0x0000FF00 (BC state)) 8))
rlm@128 71
ocsenave@139 72 (defn D [state]
ocsenave@139 73 (bit-shift-right (bit-and 0x0000FF00 (DE state)) 8))
ocsenave@139 74
ocsenave@139 75 (defn H [state]
ocsenave@139 76 (bit-shift-right (bit-and 0x0000FF00 (HL state)) 8))
ocsenave@139 77
rlm@128 78 (defn C [state]
rlm@128 79 (bit-and 0xFF (BC state)))
ocsenave@139 80 (defn F [state]
ocsenave@139 81 (bit-and 0xFF (AF state)))
ocsenave@139 82 (defn E [state]
ocsenave@139 83 (bit-and 0xFF (DE state)))
ocsenave@139 84 (defn L [state]
ocsenave@139 85 (bit-and 0xFF (HL state)))
ocsenave@139 86
ocsenave@139 87
ocsenave@139 88
ocsenave@139 89
rlm@128 90
rlm@110 91 (defn binary-str [num]
rlm@110 92 (format "%08d"
rlm@110 93 (Integer/parseInt
rlm@110 94 (Integer/toBinaryString num) 10)))
rlm@110 95
rlm@110 96 (defn view-register [state name reg-fn]
rlm@110 97 (println (format "%s: %s" name
rlm@110 98 (binary-str (reg-fn state))))
rlm@110 99 state)
rlm@110 100
rlm@107 101 (defn view-memory [state mem]
rlm@110 102 (println (format "mem 0x%04X = %s" mem
rlm@110 103 (binary-str (aget (memory state) mem))))
rlm@107 104 state)
rlm@107 105
rlm@118 106 (defn trace [state]
rlm@124 107 (loop [program-counters [(first (registers @current-state)) ]
rlm@124 108 opcodes [(aget (memory @current-state) (PC @current-state))]]
rlm@118 109 (let [frame-boundary?
rlm@118 110 (com.aurellem.gb.Gb/tick)]
rlm@118 111 (if frame-boundary?
rlm@118 112 [program-counters opcodes]
rlm@118 113 (recur
rlm@118 114 (conj program-counters
rlm@118 115 (first (registers @current-state)))
rlm@118 116 (conj opcodes
rlm@118 117 (aget (memory @current-state)
rlm@118 118 (PC @current-state))))))))
rlm@124 119
rlm@124 120 (defn print-trace [state n]
rlm@124 121 (let [[program-counters opcodes] (trace state)]
rlm@124 122 (dorun (map (fn [pc op] (println (format "%04X: 0x%02X" pc op)))
rlm@124 123 (take n program-counters)
rlm@124 124 (take n opcodes)))))
rlm@124 125
rlm@118 126 (defn good-trace []
rlm@118 127 (-> (mid-game) (tick) (IE! 0)
rlm@118 128 (set-inv-mem [0x00 0x00 0X00 0x00])
rlm@118 129 (PC! item-list-start)(print-interrupt)
rlm@118 130 (info) (tick) (info) (tick) (info)))
rlm@118 131
rlm@113 132 (defn read-down-button []
rlm@110 133 (-> (tick (mid-game))
rlm@110 134 (IE! 0) ; disable interrupts
rlm@110 135 (inject-item-assembly
rlm@118 136 ;; write 00010000 to 0xFF00 to select joypad
rlm@118 137 [0x18 ;D31D ; jump over
rlm@118 138 0x01 ;D31E ; the next 8 bits
rlm@118 139 ;D31F
rlm@118 140 (Integer/parseInt "00100000" 2) ; data section
rlm@118 141
rlm@118 142 0xFA ;D320 ; load (D31F) into A
rlm@118 143 0x1F ;D321 -->
rlm@118 144 0xD3 ;D322 --> D31F
rlm@107 145
rlm@118 146 0xEA ;D323 ; load (A), which is
rlm@118 147 0x00 ;D324 --> ; 00010000, into FF00
rlm@118 148 0xFF ;D325 --> FF00
rlm@118 149
rlm@118 150 0x18 ;D326 ; this is the place where
rlm@118 151 0x01 ;D327 ; we will store whether
rlm@118 152 0x00 ;D328 ; "down" is pressed.
rlm@107 153
rlm@118 154 0xFA ;D329 ; (FF00) -> A
rlm@118 155 0x00 ;D32A
rlm@118 156 0xFF ;D32B
rlm@110 157
rlm@118 158 0xCB ;D32C ; Test whether "down"
rlm@118 159 0x5F ;D32D ; is pressed.
rlm@111 160
rlm@118 161 0x28 ;D32E ; if down is pressed,
rlm@118 162 0x03 ;D32F ; skip the next section
rlm@118 163 ; of code.
rlm@118 164 ;; down-is-not-pressed
rlm@118 165 0xC3 ;D330
rlm@118 166 0x1D ;D331 ; return to beginning
rlm@118 167 0xD3 ;D332
rlm@118 168
rlm@118 169 ;; down-is-pressed
rlm@118 170 0xEA ;D334 ; write A to D328 if
rlm@118 171 0x28 ;D335 ; "down" was pressed
rlm@118 172 0xD3 ;D336
rlm@111 173
rlm@118 174 0xC3 ;D330
rlm@118 175 0x1D ;D331 ; return to beginning
rlm@118 176 0xD3 ;D332
rlm@118 177 ])))
rlm@111 178
rlm@118 179 (defn test-read-down []
rlm@118 180 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)
rlm@118 181 (view-memory (step (step (read-down-button))) 0xD328)))
rlm@113 182
rlm@113 183 (defn count-frames []
rlm@113 184 (-> (tick (mid-game))
rlm@113 185 (IE! 0) ; disable interrupts
rlm@113 186 (inject-item-assembly
rlm@113 187 [0x18 ;D31D ; jump over
rlm@113 188 0x02 ;D31E ; the next 2 bytes
rlm@113 189 0x00 ;D31F ; frame-count
rlm@113 190 0x00 ;D320 ; v-blank-prev
rlm@113 191
rlm@113 192 0xFA ;D321
rlm@113 193 0x41 ;D322 ; load (FF41) into A
rlm@113 194 0xFF ;D323 ; this contains mode flags
rlm@113 195
rlm@113 196 ;; if we're in v-blank, the bit-1 is 0
rlm@113 197 ;; and bit-2 is 1 Otherwise, it is not v-blank.
rlm@113 198 0xCB ;D324 ; test bit-1 of A
rlm@113 199 0x4F ;D325
rlm@113 200
rlm@113 201 0xC2 ;D326 ; if bit-1 is not 0
rlm@115 202 0x44 ;D327 ; GOTO not-v-blank
rlm@113 203 0xD3 ;D328
rlm@113 204
rlm@113 205 0xCB ;D329 ; test bit-0 of A
rlm@113 206 0x47 ;D32A
rlm@113 207
rlm@113 208 0xCA ;D32B ; if bit-0 is not 1
rlm@115 209 0x44 ;D32C ; GOTO not-v-blank
rlm@113 210 0xD3 ;D32D
rlm@114 211 ;;; in v-blank mode
rlm@113 212 ;; if v-blank-prev was 0,
rlm@113 213 ;; increment frame-count
rlm@113 214
rlm@113 215 0xFA ;D32E ; load v-blank-prev to A
rlm@113 216 0x20 ;D32F
rlm@113 217 0xD3 ;D330
rlm@113 218
rlm@113 219 0xCB ;D331
rlm@113 220 0x47 ;D332 ; test bit-0 of A
rlm@113 221
rlm@113 222 0x20 ;D333 ; skip next section
rlm@113 223 0x07 ;D334 ; if v-blank-prev was not zero
rlm@113 224
rlm@113 225 ;; v-blank was 0, increment frame-count
rlm@113 226 0xFA ;D335 ; load frame-count into A
rlm@113 227 0x1F ;D336
rlm@113 228 0xD3 ;D337
rlm@113 229
rlm@113 230 0x3C ;D338 ; inc A
rlm@113 231
rlm@113 232 0xEA ;D339 ; load A into frame-count
rlm@113 233 0x1F ;D33A
rlm@113 234 0xD3 ;D33B
rlm@113 235
rlm@114 236 ;; set v-blank-prev to 1
rlm@113 237 0x3E ;D33C ; load 1 into A
rlm@113 238 0x01 ;D33D
rlm@113 239
rlm@113 240 0xEA ;D33E ; load A into v-blank-prev
rlm@113 241 0x20 ;D33F
rlm@113 242 0xD3 ;D340
rlm@113 243
rlm@115 244 0xC3 ;D341 ; return to beginning
rlm@115 245 0x1D ;D342
rlm@115 246 0xD3 ;D343
rlm@113 247
rlm@114 248 ;;; not in v-blank mode
rlm@114 249 ;; set v-blank-prev to 0
rlm@115 250 0x3E ;D344 ; load 0 into A
rlm@115 251 0x00 ;D345
rlm@113 252
rlm@115 253 0xEA ;D346 ; load A into v-blank-prev
rlm@115 254 0x20 ;D347
rlm@115 255 0xD3 ;D348
rlm@115 256
rlm@115 257 0xC3 ;D349 ; return to beginning
rlm@115 258 0x1D ;D34A
rlm@115 259 0xD3 ;D34B
rlm@113 260 ])))
rlm@113 261
rlm@114 262 (defn step-count-frames []
rlm@113 263 (-> (read-down-button)
rlm@110 264 (info)
rlm@110 265 (tick) ;; skip over data section
rlm@110 266 (info)
rlm@110 267 (view-register "Register A" A)
rlm@110 268 (tick) ;; load-data into A
rlm@110 269 (view-register "Register A" A)
rlm@110 270 (info)
rlm@110 271 (view-memory 0xFF00)
rlm@110 272 (tick) ;; load A into 0xFF00
rlm@110 273 (view-memory 0xFF00)
rlm@111 274 (info)
rlm@111 275 (tick)
rlm@111 276 (info)
rlm@111 277 (tick)
rlm@111 278 (info)
rlm@111 279 (tick)
rlm@111 280 (info)
rlm@111 281 (tick)
rlm@111 282 (info)
rlm@111 283 (tick)
rlm@111 284 (info)
rlm@111 285 (tick)
rlm@111 286 (print-inventory)))
rlm@111 287
rlm@115 288 (defn test-count-frames []
rlm@115 289 (= 255 (aget (memory ((apply comp (repeat 255 step))
rlm@115 290 (count-frames)))
rlm@115 291 0xD31F)))
rlm@115 292
rlm@115 293 ;; specs for main bootstrap program
rlm@115 294 ;; starts in "mode-select" mode
rlm@115 295 ;; Each button press takes place in a single frame.
rlm@115 296 ;; mode-select-mode takes one of the main buttons
rlm@115 297 ;; which selects one of up to eight modes
rlm@115 298 ;; mode 1 activated by the "A" button
rlm@115 299 ;; the next two button presses indicates the start
rlm@115 300 ;; memory location which to which the bootstrap
rlm@115 301 ;; program will write.
rlm@115 302 ;; This is done by using each of the eight buttons to
rlm@115 303 ;; spell out an 8 bit number. The order of buttons is
rlm@116 304 ;; [:d :u :l :r :start :select :b :a]
rlm@116 305 ;; [:a :start :l] --> 00101001
rlm@114 306
rlm@115 307 ;; the next button press determines how many bytes are to be
rlm@115 308 ;; written, starting at the start position.
rlm@114 309
rlm@115 310 ;; then, the actual bytes are entered and are written to the
rlm@115 311 ;; start address in sequence.
rlm@114 312
rlm@118 313 (defn input-number-assembly []
rlm@118 314 [0x18 ;D31D ; jump over
rlm@118 315 0x02 ;D31E ; the next 2 bytes
rlm@118 316 0x00 ;D31F ; frame-count
rlm@118 317 0x00 ;D320 ; v-blank-prev
rlm@118 318
rlm@118 319 0xFA ;D321
rlm@118 320 0x41 ;D322 ; load (FF41) into A
rlm@118 321 0xFF ;D323 ; this contains mode flags
rlm@118 322
rlm@118 323 ;; if we're in v-blank, the bit-1 is 0
rlm@118 324 ;; and bit-2 is 1 Otherwise, it is not v-blank.
rlm@118 325 0xCB ;D324 ; test bit-1 of A
rlm@118 326 0x4F ;D325
rlm@114 327
rlm@118 328 0xC2 ;D326 ; if bit-1 is not 0
rlm@118 329 0x44 ;D327 ; GOTO not-v-blank
rlm@118 330 0xD3 ;D328
rlm@118 331
rlm@118 332 0xCB ;D329 ; test bit-0 of A
rlm@118 333 0x47 ;D32A
rlm@118 334
rlm@118 335 0xCA ;D32B ; if bit-0 is not 1
rlm@118 336 0x44 ;D32C ; GOTO not-v-blank
rlm@118 337 0xD3 ;D32D
rlm@118 338
rlm@118 339 ;;; in v-blank mode
rlm@118 340
rlm@118 341 ;; if v-blank-prev was 0,
rlm@118 342 ;; increment frame-count
rlm@118 343
rlm@118 344 0xFA ;D32E ; load v-blank-prev to A
rlm@118 345 0x20 ;D32F
rlm@118 346 0xD3 ;D330
rlm@118 347
rlm@118 348 0xCB ;D331
rlm@118 349 0x47 ;D332 ; test bit-0 of A
rlm@118 350
rlm@118 351 0x20 ;D333 ; skip next section
rlm@118 352 0x07 ;D334 ; if v-blank-prev was not zero
rlm@118 353
rlm@118 354 ;; v-blank was 0, increment frame-count
rlm@118 355 0xFA ;D335 ; load frame-count into A
rlm@118 356 0x1F ;D336
rlm@118 357 0xD3 ;D337
rlm@118 358
rlm@118 359 0x3C ;D338 ; inc A
rlm@118 360
rlm@118 361 0xEA ;D339 ; load A into frame-count
rlm@118 362 0x1F ;D33A
rlm@118 363 0xD3 ;D33B
rlm@118 364
rlm@118 365 ;; set v-blank-prev to 1
rlm@118 366 0x3E ;D33C ; load 1 into A
rlm@118 367 0x01 ;D33D
rlm@118 368
rlm@118 369 0xEA ;D33E ; load A into v-blank-prev
rlm@118 370 0x20 ;D33F
rlm@118 371 0xD3 ;D340
rlm@118 372
rlm@118 373 0xC3 ;D341 ; GOTO input handling code
rlm@118 374 0x4E ;D342
rlm@118 375 0xD3 ;D343
rlm@118 376
rlm@118 377 ;;; not in v-blank mode
rlm@118 378 ;; set v-blank-prev to 0
rlm@118 379 0x3E ;D344 ; load 0 into A
rlm@118 380 0x00 ;D345
rlm@118 381
rlm@118 382 0xEA ;D346 ; load A into v-blank-prev
rlm@118 383 0x20 ;D347
rlm@118 384 0xD3 ;D348
rlm@118 385
rlm@118 386 0xC3 ;D349 ; return to beginning
rlm@118 387 0x1D ;D34A
rlm@118 388 0xD3 ;D34B
rlm@118 389
rlm@118 390 0x00 ;D34C ; these are here
rlm@118 391 0x00 ;D34D ; for glue
rlm@118 392
rlm@118 393
rlm@118 394 ;;; calculate input number based on button presses
rlm@118 395 0x18 ;D34E ; skip next 3 bytes
rlm@118 396 0x03 ;D34F
rlm@118 397 ;D350
rlm@118 398 (Integer/parseInt "00100000" 2) ; select directional pad
rlm@118 399 ;D351
rlm@118 400 (Integer/parseInt "00010000" 2) ; select buttons
rlm@118 401 0x00 ;D352 ; input-number
rlm@118 402
rlm@118 403 ;; select directional pad, store low bits in B
rlm@118 404
rlm@118 405 0xFA ;D353 ; load (D350) into A
rlm@118 406 0x50 ;D354 -->
rlm@118 407 0xD3 ;D355 --> D31F
rlm@118 408
ocsenave@135 409 0xEA ;D356 ; load A, which is
rlm@118 410 0x00 ;D357 --> ; 00010000, into FF00
rlm@118 411 0xFF ;D358 --> FF00
rlm@118 412
rlm@118 413 0x06 ;D359
rlm@118 414 ;D35A
rlm@118 415 (Integer/parseInt "11110000" 2) ; "11110000" -> B
rlm@118 416 0xFA ;D35B ; (FF00) -> A
rlm@118 417 0x00 ;D35C
rlm@118 418 0xFF ;D35D
rlm@118 419
rlm@118 420 0xCB ;D35E ; swap nybbles on A
rlm@118 421 0x37 ;D35F
rlm@118 422 0xA0 ;D360 ; (AND A B) -> A
rlm@118 423 0x47 ;D361 ; A -> B
rlm@118 424
rlm@118 425 ;; select buttons store bottom bits in C
rlm@118 426
rlm@118 427 0xFA ; ; load (D351) into A
rlm@118 428 0x51 ; -->
rlm@118 429 0xD3 ; --> D31F
rlm@118 430
rlm@118 431 0xEA ; ; load (A), which is
rlm@118 432 0x00 ; --> ; 00001000, into FF00
rlm@118 433 0xFF ; --> FF00
rlm@118 434
rlm@118 435 0x0E ;
rlm@118 436 (Integer/parseInt "00001111" 2) ; "00001111" -> C
rlm@118 437
rlm@118 438 0xFA ; ; (FF00) -> A
rlm@118 439 0x00 ;
rlm@118 440 0xFF ;
rlm@118 441
rlm@118 442 0xA1 ; ; (AND A C) -> A
rlm@118 443 0x4F ; ; A -> C
rlm@118 444
rlm@118 445 ;; combine the B and C registers into the input number
rlm@118 446 0x79 ; ; C -> A
rlm@118 447 0xB0 ; ; (OR A B) -> A
rlm@118 448 0x2F ; ; negate A
rlm@118 449
rlm@118 450 0xEA ; ; store A into input-number
rlm@118 451 0x52 ;
rlm@118 452 0xD3 ;
rlm@118 453
rlm@118 454 0xC3 ; ; return to beginning
rlm@118 455 0x1D ;
rlm@118 456 0xD3 ;
rlm@118 457 ])
rlm@114 458
rlm@128 459
rlm@128 460 (defn print-pc [state]
rlm@128 461 (println (format "PC: 0x%04X" (PC state)))
rlm@128 462 state)
rlm@128 463
rlm@128 464 (defn print-op [state]
rlm@128 465 (println (format "OP: 0x%02X" (aget (memory state) (PC state))))
rlm@128 466 state)
rlm@128 467
rlm@128 468 (defn d-tick
rlm@128 469 ([state]
rlm@128 470 (-> state print-pc print-op tick)))
rlm@128 471
rlm@115 472 (defn input-number []
rlm@115 473 (-> (tick (mid-game))
rlm@115 474 (IE! 0) ; disable interrupts
rlm@118 475 (inject-item-assembly (input-number-assembly))))
rlm@115 476
rlm@117 477 (defn test-input-number
rlm@117 478 "Input freestyle buttons and observe the effects at the repl."
rlm@117 479 []
rlm@117 480 (set-state! (input-number))
rlm@117 481 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
rlm@115 482
rlm@128 483
rlm@128 484
rlm@128 485
ocsenave@135 486
ocsenave@135 487
ocsenave@135 488
ocsenave@135 489
ocsenave@135 490
ocsenave@135 491
ocsenave@135 492
ocsenave@135 493
ocsenave@135 494
ocsenave@135 495
ocsenave@135 496
ocsenave@135 497
ocsenave@135 498
ocsenave@135 499
ocsenave@135 500
ocsenave@135 501
ocsenave@135 502
ocsenave@135 503
ocsenave@135 504
ocsenave@135 505
ocsenave@135 506
ocsenave@135 507
ocsenave@135 508
ocsenave@135 509
ocsenave@135 510
ocsenave@136 511 (defn write-memory-assembly*
ocsenave@136 512 "Currently, grabs input from the user each frame."
ocsenave@136 513 []
rlm@128 514 [
ocsenave@135 515 ;; --------- FRAME METRONOME
ocsenave@138 516 0x18 ;; jump ahead to cleanup. first time only.
ocsenave@138 517 0x40 ;; v-blank-prev [D31E]
rlm@128 518
ocsenave@138 519 0xFA ;; load modes into A [D31F]
rlm@128 520 0x41
rlm@128 521 0xFF
ocsenave@135 522
ocsenave@135 523 0x47 ;; A -> B
ocsenave@135 524 0xCB ;; rotate A
rlm@128 525 0x2F
ocsenave@135 526 0x2F ;; invert A
rlm@128 527
rlm@128 528 0xA0
ocsenave@135 529 0x47 ;; now B_0 contains (VB==1)
rlm@128 530
rlm@128 531 0xFA ;; load v-blank-prev
ocsenave@138 532 0x1E
rlm@128 533 0xD3
rlm@128 534
ocsenave@135 535 0x2F ;; complement v-blank-prev
ocsenave@135 536
ocsenave@135 537 0xA0 ;; A & B --> A
ocsenave@135 538 0x4F ;; now C_0 contains increment?
ocsenave@135 539
rlm@128 540
rlm@128 541 0x78 ;; B->A
rlm@128 542 0xEA ;; spit A --> vbprev
ocsenave@138 543 0x1E
rlm@128 544 0xD3
rlm@128 545
ocsenave@136 546 0xCB ;test C_0
ocsenave@136 547 0x41
ocsenave@136 548 0x20 ; JUMP ahead to button input if nonzero
ocsenave@138 549 0x02
ocsenave@138 550 0x18 ; JUMP back to frame metronome (D31F)
ocsenave@139 551 0xE7
ocsenave@135 552
ocsenave@135 553 ;; -------- GET BUTTON INPUT
ocsenave@137 554
ocsenave@137 555 ;; btw, C_0 is now 1
ocsenave@135 556 ;; prepare to select bits
ocsenave@135 557
ocsenave@137 558 0x06 ;; load 0x00 into B
ocsenave@138 559 0x00 ;; to initialize for "OR" loop
ocsenave@137 560
ocsenave@137 561 0x3E ;; load 0x20 into A, to measure dpad
ocsenave@135 562 0x20
ocsenave@135 563
ocsenave@135 564
ocsenave@138 565 0xE0 ;; load A into [FF00] ;; start of OR loop [D33C]
ocsenave@135 566 0x00
ocsenave@137 567
ocsenave@137 568 0xF0 ;; load A from [FF00]
ocsenave@135 569 0x00
ocsenave@135 570
ocsenave@135 571 0xE6 ;; bitmask 00001111
ocsenave@135 572 0x0F
ocsenave@135 573
ocsenave@135 574 0xB0 ;; A or B --> A
ocsenave@135 575 0xCB
ocsenave@135 576 0x41 ;; test bit 0 of C
ocsenave@137 577 0x28 ;; JUMP forward if 0
ocsenave@135 578 0x08
ocsenave@135 579
ocsenave@135 580 0x47 ;; A -> B
ocsenave@135 581 0xCB ;; swap B nybbles
ocsenave@135 582 0x30
ocsenave@135 583 0x0C ;; increment C
ocsenave@137 584 0x3E ;; load 0x10 into A, to measure btns
ocsenave@135 585 0x10
ocsenave@135 586 0x18 ;; JUMP back to "load A into [FF00]" [20 steps?]
ocsenave@137 587 0xED
ocsenave@135 588
ocsenave@138 589
ocsenave@138 590 ;; ------ TAKE ACTION BASED ON USER INPUT
ocsenave@138 591
ocsenave@139 592 ;; "input mode"
ocsenave@138 593 ;; mode 0x00 : select mode
ocsenave@138 594 ;; mode 0x08 : select bytes-to-write
ocsenave@138 595 ;; mode 0x10 : select hi-bit
ocsenave@138 596 ;; mode 0x18 : select lo-bit
ocsenave@138 597
ocsenave@139 598 ;; "output mode"
ocsenave@139 599 ;; mode 0x20 : write bytes
ocsenave@138 600 ;; mode 0xFF : jump PC
ocsenave@138 601
ocsenave@138 602
ocsenave@138 603 ;; registers
ocsenave@138 604 ;; D : mode select
ocsenave@138 605 ;; E : count of bytes to write
ocsenave@138 606 ;; H : address-high
ocsenave@138 607 ;; L : address-low
ocsenave@138 608
ocsenave@135 609 ;; now A contains the pressed keys
ocsenave@138 610 0x2F ; complement A, by request. [D34F]
ocsenave@137 611
ocsenave@138 612 0x47 ; A->B ;; now B contains the pressed keys
ocsenave@138 613 0x7B ; E->A ;; now A contains the count.
ocsenave@138 614
ocsenave@139 615 0xCB ; test bit 5 of D (are we in o/p mode?)
ocsenave@139 616 0x6A
ocsenave@138 617 0x28 ; if test == 0, skip this o/p section
ocsenave@138 618 0x13 ; JUMP
ocsenave@137 619
ocsenave@138 620 0xCB ; else, test bit 0 of D (fragile; are we in pc mode?)
ocsenave@138 621 0x42
ocsenave@138 622 0x28 ; if test == 0, skip the following command
ocsenave@138 623 0x01
ocsenave@137 624
ocsenave@138 625 ;; output mode I: moving the program counter
ocsenave@138 626 0xE9 ; ** move PC to (HL)
ocsenave@137 627
ocsenave@138 628 ;; output mode II: writing bytes
ocsenave@138 629 0xFE ; A compare 0. finished writing?
ocsenave@138 630 0x00
ocsenave@139 631 0x20 ; if we are not finished, skip cleanup
ocsenave@138 632 0x04 ; JUMP
ocsenave@137 633
ocsenave@138 634 ;; CLEANUP
ocsenave@138 635 ;; btw, A is already zero.
ocsenave@138 636 0xAF ; zero A [D35F]
ocsenave@138 637 0x57 ; A->D; makes D=0.
ocsenave@138 638 0x18 ; end of frame
ocsenave@138 639 0xBC
ocsenave@138 640
ocsenave@138 641 ;; ---- end of cleanup
ocsenave@137 642
ocsenave@138 643
ocsenave@138 644 ;; continue writing bytes
ocsenave@139 645 0x1D ;; decrement E, the number of bytes to write [D363]
ocsenave@138 646 0x78 ;; B->A; now A contains the pressed keys
ocsenave@138 647 0x77 ;; copy A to (HL)
ocsenave@138 648 0x23 ;; increment HL
ocsenave@139 649 0x18 ;; end frame. [goto D31F]
ocsenave@139 650 0xB6 ;; TODO: set skip length backwards
ocsenave@137 651
ocsenave@137 652
ocsenave@138 653 ;; ---- end of o/p section
ocsenave@138 654
ocsenave@139 655 ;; i/p mode
ocsenave@139 656 ;; adhere to the mode discipline:
ocsenave@139 657 ;; D must be one of 0x00 0x08 0x10 0x18.
ocsenave@139 658
ocsenave@138 659 0x3E ;; load the constant 57 into A. [D369]
ocsenave@138 660 0x57
ocsenave@138 661 0x82 ;; add the mode to A
ocsenave@138 662 0xEA ;; store A into "thing to execute"
ocsenave@138 663 0x74
ocsenave@135 664 0xD3
ocsenave@138 665
ocsenave@138 666 0x3E ;; load the constant 8 into A
ocsenave@138 667 0x08
ocsenave@138 668 0x82 ;; add the mode to A
ocsenave@139 669
ocsenave@138 670 0x57 ;; store the incremented mode into D
ocsenave@138 671 0x78 ;; B->A; now A contains the pressed keys
ocsenave@135 672
ocsenave@138 673 0x00 ;; var: thing to execute [D374]
ocsenave@138 674
ocsenave@138 675 0x18 ;; end frame
ocsenave@139 676 0xA8 ;; TODO: set jump correctly
rlm@128 677 ]
rlm@128 678 )
rlm@128 679
rlm@128 680 (defn write-mem-dyl []
rlm@128 681 (-> (tick (mid-game))
rlm@128 682 (IE! 0)
rlm@128 683 (inject-item-assembly (write-memory-assembly*))))
rlm@131 684
rlm@131 685
ocsenave@138 686 (defn dylan* []
ocsenave@138 687 (->
ocsenave@138 688 (write-mem-dyl)
ocsenave@138 689
ocsenave@138 690 (tick)
ocsenave@138 691 (tick)
ocsenave@138 692 (tick)
ocsenave@138 693 (tick)
ocsenave@138 694 (tick)
ocsenave@138 695 (tick)
ocsenave@138 696 (tick)
ocsenave@138 697 (tick)
ocsenave@138 698 (tick)
ocsenave@138 699 (tick)
ocsenave@138 700 (tick)
ocsenave@138 701 (tick)
ocsenave@138 702 (tick)
ocsenave@138 703 (tick)
ocsenave@138 704 (tick)
ocsenave@138 705 (tick)
ocsenave@138 706 (tick)
ocsenave@138 707 (tick)
ocsenave@138 708 (tick)
ocsenave@138 709 (tick)
ocsenave@138 710 (tick)
ocsenave@138 711 (tick)
ocsenave@138 712 (tick)
ocsenave@138 713 (tick)
ocsenave@138 714 (tick)
ocsenave@138 715 (tick)
ocsenave@138 716 (tick)
ocsenave@138 717 (tick)
ocsenave@138 718 (tick)
ocsenave@138 719 (tick)
ocsenave@138 720 (tick)
ocsenave@138 721 (tick)
ocsenave@138 722 (tick)
ocsenave@138 723 (tick)
ocsenave@138 724 (tick)
ocsenave@138 725 (tick)
ocsenave@138 726
ocsenave@138 727 ;;(view-memory 0xD374)
ocsenave@138 728 (tick)
ocsenave@138 729 (tick)
ocsenave@138 730 (tick)
ocsenave@138 731 (tick)
ocsenave@138 732 (tick)
ocsenave@138 733 (tick)
ocsenave@138 734 (tick)
ocsenave@138 735 (tick)
ocsenave@138 736 (tick)
ocsenave@138 737 (tick)
ocsenave@138 738 (tick)
ocsenave@138 739 (tick)
ocsenave@138 740 (tick)
ocsenave@138 741 (tick)
ocsenave@138 742 (tick)
ocsenave@138 743 ;;(view-memory 0xD374)
ocsenave@138 744 (d-tick)
ocsenave@138 745
ocsenave@138 746 (view-register "A" A)
ocsenave@138 747 (view-register "B" B)
ocsenave@138 748 (view-register "C" C))
ocsenave@138 749
ocsenave@138 750 )
ocsenave@138 751
ocsenave@138 752
ocsenave@135 753 (defn dylan []
ocsenave@135 754 (->
ocsenave@135 755 (write-mem-dyl)
ocsenave@135 756 (tick)
ocsenave@135 757 (tick)
ocsenave@135 758 (tick)
ocsenave@135 759 (tick)
ocsenave@135 760 (tick)
ocsenave@135 761 (tick)
ocsenave@135 762 (tick)
ocsenave@135 763 (tick)
ocsenave@135 764 (tick)
ocsenave@135 765 (tick)
ocsenave@135 766 (tick)
ocsenave@135 767 (tick)
ocsenave@135 768 (tick)
ocsenave@135 769 (tick)
ocsenave@136 770 (tick) ;; first loop
ocsenave@136 771
ocsenave@136 772
ocsenave@135 773 (tick)
ocsenave@135 774 (tick)
ocsenave@135 775 (tick)
ocsenave@135 776 (tick)
ocsenave@135 777 (tick)
ocsenave@135 778 (tick)
ocsenave@135 779 (tick)
ocsenave@135 780 (tick)
ocsenave@135 781 (tick)
ocsenave@135 782 (tick)
ocsenave@135 783 (tick)
ocsenave@135 784 (tick)
ocsenave@136 785 (tick) ;; dpad bits
ocsenave@136 786
ocsenave@135 787 (tick)
ocsenave@135 788 (tick)
ocsenave@136 789 (tick)
ocsenave@136 790 (tick)
ocsenave@136 791 (tick)
ocsenave@136 792 (tick)
ocsenave@136 793 (tick)
ocsenave@136 794 (tick)
ocsenave@136 795 (d-tick)
ocsenave@135 796
ocsenave@135 797
ocsenave@135 798
ocsenave@135 799 (view-register "A" A)
ocsenave@135 800 (view-register "B" B)
ocsenave@135 801 (view-register "C" C)
ocsenave@135 802
ocsenave@135 803 ))
ocsenave@135 804
ocsenave@135 805
ocsenave@135 806
ocsenave@135 807
rlm@131 808 (defn d2 []
rlm@131 809 (->
rlm@131 810 (write-mem-dyl)
rlm@131 811 (view-memory 0xD31F)
rlm@131 812 step step step step step
rlm@131 813 (view-memory 0xD31F)))
rlm@131 814
rlm@131 815
rlm@131 816
ocsenave@135 817
ocsenave@135 818
ocsenave@135 819
ocsenave@135 820
ocsenave@135 821
ocsenave@135 822
ocsenave@135 823
ocsenave@135 824
ocsenave@135 825
ocsenave@135 826
ocsenave@135 827
ocsenave@135 828
ocsenave@135 829
rlm@131 830
rlm@131 831
rlm@128 832
rlm@128 833
rlm@118 834 (defn write-memory-assembly []
rlm@129 835 [
rlm@123 836 ;; Main Timing Loop
rlm@123 837 ;; Constantly check for v-blank and Trigger main state machine on
rlm@123 838 ;; every transtion from v-blank to non-v-blank.
rlm@129 839
rlm@123 840 0x18 ; D31D ; Variable declaration
rlm@123 841 0x02 ; D31E
rlm@123 842 0x00 ; D31F ; frame-count
rlm@123 843 0x00 ; D320 ; v-blank-prev
rlm@129 844
rlm@129 845 0xF0 ; D321 ; load v-blank mode flags into A
rlm@129 846 0x41
rlm@129 847 0x00
rlm@123 848
rlm@123 849
rlm@123 850 ;; Branch dependent on v-blank. v-blank happens when the last two
rlm@123 851 ;; bits in A are "01"
rlm@123 852 0xCB ; D324
rlm@123 853 0x4F ; D325
rlm@123 854
rlm@123 855 0xC2 ; D326 ; if bit-1 is not 0, then
rlm@124 856 0x3E ; D327 ; GOTO non-v-blank.
rlm@123 857 0xD3 ; D328
rlm@123 858
rlm@123 859 0xCB ; D329
rlm@123 860 0x47 ; D32A
rlm@123 861
rlm@123 862 0xCA ; D32B ; if bit-0 is not 1, then
rlm@124 863 0x3E ; D32C ; GOTO non-v-blank.
rlm@123 864 0xD3 ; D32D
rlm@123 865
rlm@123 866 ;; V-Blank
rlm@123 867 ;; Activate state-machine if this is a transition event.
rlm@123 868
rlm@123 869 0xFA ; D32E ; load v-bank-prev into A
rlm@123 870 0x20 ; D32F
rlm@123 871 0xD3 ; D330
rlm@123 872
rlm@124 873 0xFE ; D331 ; compare A to 0. >--------\
rlm@124 874 0x00 ; D332 \
rlm@124 875 ; |
rlm@124 876 ;; set v-blank-prev to 1. |
rlm@124 877 0x3E ; D333 ; load 1 into A. |
rlm@124 878 0x01 ; D334 |
rlm@124 879 ; |
rlm@124 880 0xEA ; D335 ; load A into v-blank-prev |
rlm@124 881 0x20 ; D336 |
rlm@124 882 0xD3 ; D337 |
rlm@124 883 ; /
rlm@124 884 ;; if v-blank-prev was 0, activate state-machine <------/
rlm@124 885 0xCA ; D338 ; if v-blank-prev
rlm@123 886 0x46 ; D339 ; was 0,
rlm@123 887 0xD3 ; D33A ; GOTO state-machine
rlm@123 888
rlm@123 889 0xC3 ; D33B
rlm@123 890 0x1D ; D33C
rlm@123 891 0xD3 ; D33D ; GOTO beginning
rlm@123 892 ;; END V-blank
rlm@123 893
rlm@123 894 ;; Non-V-Blank
rlm@123 895 ;; Set v-blank-prev to 0
rlm@123 896 0x3E ; D33E ; load 0 into A
rlm@123 897 0x00 ; D33F
rlm@123 898
rlm@123 899 0xEA ; D340 ; load A into v-blank-prev
rlm@123 900 0x20 ; D341
rlm@123 901 0xD3 ; D342
rlm@118 902
rlm@123 903 0xC3 ; D343
rlm@123 904 0x1D ; D344
rlm@123 905 0xD3 ; D345 ; GOTO beginning
rlm@123 906 ;; END Not-V-Blank
rlm@115 907
rlm@124 908
rlm@123 909 ;; Main State Machine -- Input Section
rlm@123 910 ;; This is called once every frame.
rlm@123 911 ;; It collects input and uses it to drive the
rlm@123 912 ;; state transitions.
rlm@123 913
rlm@123 914 ;; Increment frame-count
rlm@123 915 0xFA ; D346 ; load frame-count into A
rlm@123 916 0x1F ; D347
rlm@123 917 0xD3 ; D348
rlm@118 918
rlm@123 919 0x3C ; D349 ; inc A
rlm@115 920
rlm@123 921 0xEA ; D34A
rlm@123 922 0x1F ; D34B ; load A into frame-count
rlm@123 923 0xD3 ; D34C
rlm@118 924
rlm@123 925 0x00 ; D34D ; glue :)
rlm@118 926
rlm@118 927 0x18 ;D34E ; skip next 3 bytes
rlm@118 928 0x03 ;D34F
rlm@123 929 ;D350
rlm@118 930 (Integer/parseInt "00100000" 2) ; select directional pad
rlm@123 931 ;D351
rlm@118 932 (Integer/parseInt "00010000" 2) ; select buttons
rlm@118 933 0x00 ;D352 ; input-number
rlm@118 934
rlm@123 935 ;; select directional pad; store low bits in B
rlm@118 936
rlm@118 937 0xFA ;D353 ; load (D350) into A
rlm@118 938 0x50 ;D354 -->
rlm@123 939 0xD3 ;D355 --> D350
rlm@118 940
rlm@129 941 0xE0 ;D356 ; load (A), which is
rlm@118 942 0x00 ;D357 --> ; 00010000, into FF00
rlm@129 943 0x00 ;D358 --> FF00 ;; NO-OP
rlm@118 944
rlm@118 945 0x06 ;D359
rlm@123 946 ;D35A
rlm@118 947 (Integer/parseInt "11110000" 2) ; "11110000" -> B
rlm@129 948 0xF0 ;D35B ; (FF00) -> A
rlm@118 949 0x00 ;D35C
rlm@129 950 0x00 ;D35D ;; NO-OP
rlm@118 951
rlm@118 952 0xCB ;D35E ; swap nybbles on A
rlm@118 953 0x37 ;D35F
rlm@118 954 0xA0 ;D360 ; (AND A B) -> A
rlm@118 955 0x47 ;D361 ; A -> B
rlm@118 956
rlm@123 957 ;; select buttons; store bottom bits in C
rlm@118 958
rlm@118 959 0xFA ;D362 ; load (D351) into A
rlm@118 960 0x51 ;D363 -->
rlm@123 961 0xD3 ;D364 --> D351
rlm@118 962
rlm@129 963 0xE0 ;D365 ; load (A), which is
rlm@118 964 0x00 ;D366 --> ; 00001000, into FF00
rlm@129 965 0x00 ;D367 --> FF00 ;; NO-OP
rlm@118 966
rlm@118 967 0x0E ;D368
rlm@118 968 ;D369
rlm@118 969 (Integer/parseInt "00001111" 2) ; "00001111" -> C
rlm@118 970
rlm@129 971 0xF0 ;D36A ; (FF00) -> A
rlm@118 972 0x00 ;D36B
rlm@129 973 0x00 ;D36C
rlm@118 974
rlm@118 975 0xA1 ;D36D ; (AND A C) -> A
rlm@118 976 0x4F ;D36E ; A -> C
rlm@118 977
rlm@118 978 ;; combine the B and C registers into the input number
rlm@118 979 0x79 ;D36F ; C -> A
rlm@118 980 0xB0 ;D370 ; (OR A B) -> A
rlm@118 981 0x2F ;D371 ; negate A
rlm@118 982
rlm@118 983 0xEA ;D372 ; store A into input-number
rlm@118 984 0x52 ;D373
rlm@118 985 0xD3 ;D374
rlm@118 986
rlm@124 987 0x00 ;D375
rlm@124 988 0x00 ;D376
rlm@124 989 0x00 ;D377
rlm@118 990 0x00 ;D378
rlm@118 991 0x00 ;D379
rlm@118 992 0x00 ;D37A
rlm@118 993 0x00 ;D37B ; these are here because
rlm@118 994 0x00 ;D37C ; I messed up :(
rlm@118 995 0x00 ;D37D
rlm@118 996 0x00 ;D37E
rlm@118 997 0x00 ;D37F
rlm@118 998
rlm@118 999 ;; beginning of main state machine
rlm@118 1000 0x18 ;D380 ; Declaration of variables
rlm@118 1001 0x05 ;D381 ; 5 variables:
rlm@118 1002 0x00 ;D382 ; current-mode
rlm@122 1003 0x00 ;D383 ; bytes-to-write
rlm@126 1004 0x00 ;D384 ; bytes-written
rlm@126 1005 0x00 ;D385 ; start-point-high
rlm@126 1006 0x00 ;D386 ; start-point-low
rlm@118 1007
rlm@118 1008
rlm@118 1009 ;; banch on current mode
rlm@118 1010 0xFA ;D387 ; load current-mode (0xD382)
rlm@118 1011 0x82 ;D388 ; into A
rlm@118 1012 0xD3 ;D389
rlm@118 1013 0x00 ;D38A
rlm@118 1014
rlm@120 1015
rlm@120 1016 ;; GOTO Mode 0 (input-mode) if current-mode is 0
rlm@118 1017 0xFE ;D38B
rlm@118 1018 0x00 ;D38C ; compare A with 0x00
rlm@118 1019
rlm@120 1020 0xCA ;D38D ; goto Mode 0 if A == 0
rlm@118 1021 0xA8 ;D38E
rlm@118 1022 0xD3 ;D38F
rlm@118 1023
rlm@121 1024 ;; GOTO Mode 1 (set-length) if current-mode is 1
rlm@120 1025 0xFE ;D390
rlm@120 1026 0x01 ;D391 ; compare A with 0x01
rlm@120 1027
rlm@120 1028 0xCA ;D392
rlm@120 1029 0xB1 ;D393
rlm@120 1030 0xD3 ;D394 ; goto Mode 1 if A == 1
rlm@120 1031
rlm@126 1032 ;; GOTO Mode 2 (set-start-point-high) if current mode is 2
rlm@121 1033 0xFE ;D395
rlm@121 1034 0x02 ;D396 ; compare A with 0x02
rlm@121 1035
rlm@121 1036 0xCA ;D397
rlm@121 1037 0xBF ;D398
rlm@121 1038 0xD3 ;D399 ; goto Mode 2 if A == 2
rlm@121 1039
rlm@126 1040 ;; GOTO Mode 3 (set-start-point-low) if current mode is 3
rlm@125 1041 0xFE ;D39A
rlm@125 1042 0x03 ;D39B
rlm@125 1043
rlm@126 1044 0xCA ;D39C
rlm@126 1045 0xCD ;D39D
rlm@126 1046 0xD3 ;D39E ; goto Mode 3 if A == 3
rlm@126 1047
rlm@127 1048 ;; GOTO Mode 4 (write-memory) if current mode is 4
rlm@127 1049 0xFE ;D39F
rlm@127 1050 0x04 ;D3A0
rlm@127 1051
rlm@127 1052 0xCA ;D3A1
rlm@127 1053 0xDB ;D3A2
rlm@127 1054 0xD3 ;D3A3
rlm@127 1055
rlm@118 1056 0x00 ;D3A4
rlm@118 1057 ;; End of Mode checking, goto beginning
rlm@118 1058 0xC3 ;D3A5
rlm@118 1059 0x1D ;D3A6
rlm@118 1060 0xD3 ;D3A7
rlm@120 1061
rlm@120 1062
rlm@120 1063 ;; Mode 0 -- input-mode mode
rlm@120 1064 ;; means that we are waiting for a mode, so set the mode to
rlm@120 1065 ;; whatever is currently in input-number. If nothing is
rlm@120 1066 ;; entered, then the program stays in input-mode mode
rlm@120 1067
rlm@118 1068 ;; set current-mode to input-number
rlm@118 1069 0xFA ;D3A8 ; load input-number (0xD352)
rlm@118 1070 0x52 ;D3A9 ; into A
rlm@118 1071 0xD3 ;D3AA
rlm@118 1072
rlm@118 1073 0xEA ;D3AB ; load A into current-mode
rlm@118 1074 0x82 ;D3AC ; (0xD382)
rlm@118 1075 0xD3 ;D3AD
rlm@118 1076
rlm@118 1077 0xC3 ;D3AE ; go back to beginning
rlm@118 1078 0x1D ;D3AF
rlm@118 1079 0xD3 ;D3B0
rlm@120 1080 ;; End Mode 0
rlm@118 1081
rlm@120 1082
rlm@121 1083 ;; Mode 1 -- set-length mode
rlm@121 1084 ;; This is the header for writing things to memory.
rlm@121 1085 ;; User specifies the number of bytes to write.
rlm@120 1086 ;; Mode is auto advanced to Mode 2 after this mode
rlm@120 1087 ;; completes.
rlm@120 1088
rlm@121 1089 ;; Set bytes left to write to input-number;
rlm@120 1090 ;; set current-mode to 0x02.
rlm@120 1091 0xFA ;D3B1 ; load input-number (0xD352)
rlm@120 1092 0x52 ;D3B2 ; into A
rlm@120 1093 0xD3 ;D3B3
rlm@120 1094
rlm@120 1095 0xEA ;D3B4 ; load A into bytes-left-to-write
rlm@120 1096 0x83 ;D3B5 ; (0xD383)
rlm@120 1097 0xD3 ;D3B6
rlm@120 1098
rlm@121 1099 0x3E ;D3B7 ; load 0x02 into A.
rlm@120 1100 0x02 ;D3B8
rlm@120 1101
rlm@120 1102 0xEA ;D3B9 ; load A to current-mode
rlm@120 1103 0x82 ;D3BA ; advancing from Mode 1 to
rlm@120 1104 0xD3 ;D3BB ; Mode 2
rlm@120 1105
rlm@120 1106 0xC3 ;D3BC ; go back to beginning
rlm@120 1107 0x1D ;D3BD
rlm@120 1108 0xD3 ;D3BE
rlm@120 1109 ;; End Mode 1
rlm@121 1110
rlm@121 1111
rlm@126 1112 ;; Mode 2 -- set start-point-high mode
rlm@126 1113 ;; Middle part of the header for writing things to memory.
rlm@121 1114 ;; User specifies the start location in RAM to which
rlm@121 1115 ;; data will be written.
rlm@121 1116 ;; Mode is auto advanced to Mode 3 after this mode completes.
rlm@121 1117
rlm@126 1118 ;; Set start-point-high to input-number;
rlm@121 1119 ;; set current mode to 0x03.
rlm@121 1120 0xFA ;D3BF ; load input-number (0xD352)
rlm@121 1121 0x52 ;D3C0 ; into A
rlm@121 1122 0xD3 ;D3C1
rlm@121 1123
rlm@126 1124 0xEA ;D3C2 ; load A into start-point-high
rlm@126 1125 0x85 ;D3C3 ; (0xD385)
rlm@121 1126 0xD3 ;D3C4
rlm@121 1127
rlm@121 1128 0x3E ;D3C5 ; load 0x03 into A.
rlm@121 1129 0x03 ;D3C6
rlm@121 1130
rlm@121 1131 0xEA ;D3C7 ; load A to current-mode,
rlm@121 1132 0x82 ;D3C8 ; advancing from Mode 2 to
rlm@121 1133 0xD3 ;D3C9 ; Mode 3.
rlm@120 1134
rlm@121 1135 0xC3 ;D3CA ; go back to beginning
rlm@121 1136 0x1D ;D3CB
rlm@121 1137 0xD3 ;D3CC
rlm@121 1138 ;;End Mode 2
rlm@125 1139
rlm@118 1140
rlm@126 1141 ;; Mode 3 -- set-start-point-low mode
rlm@126 1142 ;; Final part of header for writing things to memory.
rlm@126 1143 ;; User specifies the low bytes of 16 bit start-point.
rlm@126 1144
rlm@126 1145 ;; Set start-point-low to input-number;
rlm@126 1146 ;; set current mode to 0x04
rlm@126 1147 0xFA ;D3CD ; load input-number into A
rlm@126 1148 0x52 ;D3CE
rlm@126 1149 0xD3 ;D3CF
rlm@126 1150
rlm@126 1151 0xEA ;D3D0 ; load A into start-point-low
rlm@126 1152 0x86 ;D3D1
rlm@126 1153 0xD3 ;D3D2
rlm@126 1154
rlm@126 1155 0x3E ;D3D3 ; load 0x04 into A.
rlm@126 1156 0x04 ;D3D4
rlm@126 1157
rlm@126 1158 0xEA ;D3D5 ; load A to current-mode,
rlm@126 1159 0x82 ;D3D6 ; advancing from Mode 3 to
rlm@126 1160 0xD3 ;D3D7 ; Mode 4.
rlm@126 1161
rlm@126 1162 0xC3 ;D3D8 ; go back to beginning
rlm@126 1163 0x1D ;D3D9
rlm@126 1164 0xD3 ;D3DA
rlm@126 1165
rlm@126 1166 ;; Mode 4 -- write bytes mode
rlm@125 1167
rlm@125 1168 ;; This is where RAM manipulation happens. User supplies
rlm@125 1169 ;; bytes every frame, which are written sequentially to
rlm@125 1170 ;; start-point until bytes-to-write have been written. Once
rlm@125 1171 ;; bytes-to-write have been written, the mode is reset to 0.
rlm@125 1172
rlm@125 1173 ;; compare bytes-written with bytes-to-write.
rlm@125 1174 ;; if they are the same, then reset mode to 0
rlm@125 1175
rlm@126 1176 0xFA ;D3DB ; load bytes-to-write into A
rlm@126 1177 0x83 ;D3DC
rlm@126 1178 0xD3 ;D3DD
rlm@127 1179
rlm@126 1180 0x47 ;D3DE ; load A into B
rlm@127 1181
rlm@126 1182 0xFA ;D3DF ; load bytes-written into A
rlm@126 1183 0x84 ;D3E0
rlm@126 1184 0xD3 ;D3E1
rlm@127 1185
rlm@126 1186 0xB8 ;D3E2 ; compare A with B
rlm@127 1187
rlm@126 1188 0xCA ;D3E3 ; if they are equal, go to cleanup
rlm@127 1189 0x07 ;D3E4
rlm@127 1190 0xD4 ;D3E5
rlm@125 1191
rlm@125 1192 ;; Write Memory Section
rlm@125 1193 ;; Write the input-number, interpreted as an 8-bit number,
rlm@125 1194 ;; into the current target register, determined by
rlm@125 1195 ;; (+ start-point bytes-written).
rlm@125 1196 ;; Then, increment bytes-written by 1.
rlm@125 1197
rlm@127 1198 0xFA ;D3E6 ; load start-point-high into A
rlm@127 1199 0x85 ;D3E7
rlm@127 1200 0xD3 ;D3E8
rlm@127 1201
rlm@127 1202 0x67 ;D3E9 ; load A into H
rlm@127 1203
rlm@127 1204 0xFA ;D3EA ; load start-point-low into A
rlm@127 1205 0x86 ;D3EB
rlm@127 1206 0xD3 ;D3EC
rlm@127 1207
rlm@127 1208 0x6F ;D3ED ; load A into L
rlm@127 1209
rlm@127 1210 0xFA ;D3EE ; load bytes-written into A
rlm@127 1211 0x84 ;D3EF
rlm@127 1212 0xD3 ;D3F0
rlm@127 1213
rlm@127 1214 0x00 ;D3F1 ; These are here because
rlm@127 1215 0x00 ;D3F2 ; I screwed up again.
rlm@125 1216 0x00 ;D3F3
rlm@125 1217
rlm@127 1218 0x85 ;D3F4 ; add L to A; store A in L.
rlm@127 1219 0x6F ;D3F5
rlm@121 1220
rlm@127 1221 0x30 ;D3F6 ; If the addition overflowed,
rlm@127 1222 0x01 ;D3F7
rlm@127 1223 0x24 ;D3F8 ; increment H.
rlm@118 1224
rlm@127 1225 ;; Now, HL points to the correct place in memory
rlm@127 1226
rlm@127 1227 0xFA ;D3F9 ; load input-number into A
rlm@127 1228 0x52 ;D3FA
rlm@127 1229 0xD3 ;D3FB
rlm@118 1230
rlm@127 1231 0x77 ;D3FC ; load A into (HL)
rlm@118 1232
rlm@127 1233 0xFA ;D3FD ; load bytes-written into A
rlm@127 1234 0x84 ;D3FE
rlm@127 1235 0xD3 ;D3FF
rlm@118 1236
rlm@127 1237 0x3C ;D400 ; increment A
rlm@127 1238
rlm@127 1239 0xEA ;D401 ; load A into bytes-written
rlm@127 1240 0x84 ;D402
rlm@127 1241 0xD3 ;D403
rlm@127 1242
rlm@127 1243 0xC3 ;D404 ; go back to beginning.
rlm@127 1244 0x1D ;D405
rlm@127 1245 0xD3 ;D406
rlm@127 1246 ;; End Write Memory Section
rlm@127 1247
rlm@127 1248 ;; Mode 4 Cleanup Section
rlm@127 1249 ;; reset bytes-written to 0
rlm@127 1250 ;; set mode to 0
rlm@127 1251 0x3E ;D407 ; load 0 into A
rlm@127 1252 0x00 ;D408
rlm@127 1253
rlm@127 1254 0xEA ;D409 ; load A into bytes-written
rlm@127 1255 0x84 ;D40A
rlm@127 1256 0xD3 ;D40B
rlm@127 1257
rlm@127 1258 0xEA ;D40C ; load A into current-mode
rlm@127 1259 0x82 ;D40D
rlm@127 1260 0xD3 ;D40E
rlm@127 1261
rlm@127 1262 0xC3 ;D40F ; go back to beginning
rlm@127 1263 0x1D ;D410
rlm@127 1264 0xD3 ;D411
rlm@127 1265
rlm@127 1266 ;; End Mode 4
rlm@118 1267
rlm@118 1268 ])
rlm@118 1269
rlm@118 1270
rlm@123 1271
rlm@118 1272 (def frame-count 0xD31F)
rlm@118 1273 (def input 0xD352)
rlm@127 1274 (def current-mode 0xD382)
rlm@127 1275 (def bytes-to-write 0xD383)
rlm@127 1276 (def bytes-written 0xD384)
rlm@127 1277 (def start-point-high 0xD385)
rlm@127 1278 (def start-point-low 0xD386)
rlm@126 1279
rlm@127 1280
rlm@118 1281
rlm@118 1282 (defn write-memory []
rlm@118 1283 (-> (tick (mid-game))
rlm@118 1284 (IE! 0) ; disable interrupts
rlm@118 1285 (inject-item-assembly (write-memory-assembly))))
rlm@119 1286
rlm@119 1287 (defn test-write-memory []
rlm@119 1288 (set-state! (write-memory))
rlm@119 1289 (dorun
rlm@119 1290 (dotimes [_ 5000]
rlm@119 1291 (view-memory (step @current-state) current-mode))))
rlm@122 1292
rlm@122 1293 (def bytes-to-write 0xD383)
rlm@122 1294 (def start-point 0xD384)
rlm@122 1295
rlm@125 1296 (defn print-blank-assembly
rlm@125 1297 [start end]
rlm@125 1298 (dorun
rlm@125 1299 (map
rlm@125 1300 #(println (format "0x00 ;%04X " %))
rlm@125 1301 (range start end))))
rlm@125 1302
rlm@122 1303 (defn test-mode-2 []
rlm@122 1304 (->
rlm@122 1305 (write-memory)
rlm@123 1306 (view-memory frame-count)
rlm@122 1307 (step)
rlm@122 1308 (step [:a])
rlm@122 1309 (step [:b])
rlm@122 1310 (step [:start])
rlm@122 1311 (step [])
rlm@123 1312 (view-memory frame-count)))
rlm@123 1313
ocsenave@139 1314
ocsenave@139 1315
ocsenave@139 1316 (defn dylan-test-mode
ocsenave@139 1317 ([] (dylan-test-mode (write-mem-dyl)))
ocsenave@139 1318 ([target-state]
ocsenave@139 1319 (let [
ocsenave@139 1320 v-blank-prev 54046
ocsenave@139 1321 btn-register 65280
ocsenave@139 1322 eggs 0xD374
ocsenave@139 1323 ]
ocsenave@139 1324
ocsenave@139 1325 (->
ocsenave@139 1326 target-state
ocsenave@139 1327
ocsenave@139 1328 (tick)
ocsenave@139 1329 (tick)
ocsenave@139 1330 (tick)
ocsenave@139 1331 (tick);; jumps back to beginning
ocsenave@139 1332
ocsenave@139 1333 (tick)
ocsenave@139 1334 (tick)
ocsenave@139 1335 (tick)
ocsenave@139 1336 (tick)
ocsenave@139 1337 (tick)
ocsenave@139 1338 (tick)
ocsenave@139 1339 (tick)
ocsenave@139 1340 (tick)
ocsenave@139 1341 (tick)
ocsenave@139 1342 (tick)
ocsenave@139 1343 (tick)
ocsenave@139 1344 (tick)
ocsenave@139 1345
ocsenave@139 1346
ocsenave@139 1347 (tick)
ocsenave@139 1348 (tick)
ocsenave@139 1349 (tick)
ocsenave@139 1350 (tick)
ocsenave@139 1351 (tick)
ocsenave@139 1352 (tick)
ocsenave@139 1353 (tick)
ocsenave@139 1354 (tick)
ocsenave@139 1355 (tick)
ocsenave@139 1356 (tick)
ocsenave@139 1357 (tick)
ocsenave@139 1358 (tick)
ocsenave@139 1359 (tick)
ocsenave@139 1360 (tick)
ocsenave@139 1361 (tick)
ocsenave@139 1362 (tick)
ocsenave@139 1363 (tick)
ocsenave@139 1364 (tick)
ocsenave@139 1365 (tick)
ocsenave@139 1366 (tick)
ocsenave@139 1367 (tick) ;; just complemented A
ocsenave@139 1368
ocsenave@139 1369 (tick)
ocsenave@139 1370 (DE! 0x0800)
ocsenave@139 1371 (AF! 0xCF00) ;; change inputs @ A
ocsenave@139 1372 (tick)
ocsenave@139 1373 (tick)
ocsenave@139 1374 (tick)
ocsenave@139 1375 (tick)
ocsenave@139 1376 (tick)
ocsenave@139 1377
ocsenave@139 1378 ;;(view-memory eggs)
ocsenave@139 1379 (tick)
ocsenave@139 1380 (tick)
ocsenave@139 1381 ;;(view-memory eggs)
ocsenave@139 1382 (tick)
ocsenave@139 1383 (tick)
ocsenave@139 1384 (tick)
ocsenave@139 1385 (tick)
ocsenave@139 1386
ocsenave@139 1387 (d-tick)
ocsenave@139 1388
ocsenave@139 1389
ocsenave@139 1390 ;;(view-memory btn-register)
ocsenave@139 1391 (view-register "A" A)
ocsenave@139 1392 (view-register "B" B)
ocsenave@139 1393
ocsenave@139 1394 ;;(view-register "C" C)
ocsenave@139 1395 (view-register "D" D)
ocsenave@139 1396 (view-register "E" E)
ocsenave@139 1397 (view-register "H" H)
ocsenave@139 1398 (view-register "L" L)
ocsenave@139 1399 ))))
ocsenave@139 1400
ocsenave@139 1401
ocsenave@139 1402
ocsenave@139 1403
ocsenave@139 1404
ocsenave@139 1405
rlm@128 1406 (defn test-mode-4
rlm@128 1407 ([] (test-mode-4 (write-memory)))
rlm@128 1408 ([target-state]
rlm@128 1409 (->
rlm@128 1410 target-state
rlm@128 1411 (#(do (println "memory from 0xC00F to 0xC01F:"
rlm@128 1412 (subvec (vec (memory %)) 0xC00F 0xC01F)) %))
rlm@128 1413 (view-memory current-mode)
rlm@128 1414 (step [])
rlm@128 1415 (step [])
rlm@128 1416 (step [])
rlm@128 1417 (#(do (println "after three steps") %))
rlm@128 1418 (view-memory current-mode)
rlm@127 1419
rlm@128 1420 ;; Activate memory writing mode
rlm@128 1421
rlm@128 1422 (#(do (println "step with [:a]") %))
rlm@128 1423 (step [:a])
rlm@128 1424 (view-memory current-mode)
rlm@128 1425 (view-memory bytes-to-write)
rlm@128 1426 (view-memory start-point-high)
rlm@128 1427 (view-memory start-point-low)
rlm@127 1428
rlm@128 1429 ;; Specify four bytes to be written
rlm@128 1430
rlm@128 1431 (#(do (println "step with [:select]")%))
rlm@128 1432 (step [:select])
rlm@128 1433 (view-memory current-mode)
rlm@128 1434 (view-memory bytes-to-write)
rlm@128 1435 (view-memory start-point-high)
rlm@128 1436 (view-memory start-point-low)
rlm@127 1437
rlm@128 1438 ;; Specify target memory address as 0xC00F
rlm@128 1439
rlm@128 1440 (#(do (println "step with [:u :d]")%))
rlm@128 1441 (step [:u :d])
rlm@128 1442 (view-memory current-mode)
rlm@128 1443 (view-memory bytes-to-write)
rlm@128 1444 (view-memory start-point-high)
rlm@128 1445 (view-memory start-point-low)
rlm@127 1446
rlm@128 1447 (#(do (println "step with [:a :b :start :select]")%))
rlm@128 1448 (step [:a :b :start :select])
rlm@128 1449 (view-memory current-mode)
rlm@128 1450 (view-memory bytes-to-write)
rlm@128 1451 (view-memory start-point-high)
rlm@128 1452 (view-memory start-point-low)
rlm@127 1453
rlm@128 1454 ;; Start reprogramming memory
rlm@127 1455
rlm@128 1456 (#(do (println "step with [:a]")%))
rlm@128 1457 (step [:a])
rlm@128 1458 (view-memory current-mode)
rlm@128 1459 (view-memory bytes-written)
rlm@127 1460
rlm@128 1461 (#(do (println "step with [:b]")%))
rlm@128 1462 (step [:b])
rlm@128 1463 (view-memory current-mode)
rlm@128 1464 (view-memory bytes-written)
rlm@127 1465
rlm@128 1466 (#(do (println "step with [:a :b]")%))
rlm@128 1467 (step [:a :b])
rlm@128 1468 (view-memory current-mode)
rlm@128 1469 (view-memory bytes-written)
rlm@127 1470
rlm@128 1471 (#(do (println "step with [:select]")%))
rlm@128 1472 (step [:select])
rlm@128 1473 (view-memory current-mode)
rlm@128 1474 (view-memory bytes-written)
rlm@127 1475
rlm@128 1476 ;; Reprogramming done, program ready for more commands.
rlm@127 1477
rlm@128 1478 (#(do (println "step with []")%))
rlm@128 1479 (step [])
rlm@128 1480 (view-memory current-mode)
rlm@128 1481 (view-memory bytes-written)
rlm@128 1482
rlm@128 1483 (#(do (println "memory from 0xC00F to 0xC01F:"
rlm@128 1484 (subvec (vec (memory %)) 0xC00F 0xC01F)) %)))))
rlm@123 1485