annotate clojure/com/aurellem/assembly.clj @ 118:be4ec0e60c16

mode 0 of bootstrapping state machine complete
author Robert McIntyre <rlm@mit.edu>
date Fri, 16 Mar 2012 20:03:10 -0500
parents bcb5c41626b4
children 6cbea8ab65b6
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@118 85 (loop [program-counters []
rlm@118 86 opcodes []]
rlm@118 87 (let [frame-boundary?
rlm@118 88 (com.aurellem.gb.Gb/tick)]
rlm@118 89 (println (count opcodes))
rlm@118 90 (if frame-boundary?
rlm@118 91 [program-counters opcodes]
rlm@118 92 (recur
rlm@118 93 (conj program-counters
rlm@118 94 (first (registers @current-state)))
rlm@118 95 (conj opcodes
rlm@118 96 (aget (memory @current-state)
rlm@118 97 (PC @current-state))))))))
rlm@118 98
rlm@118 99 (defn good-trace []
rlm@118 100 (-> (mid-game) (tick) (IE! 0)
rlm@118 101 (set-inv-mem [0x00 0x00 0X00 0x00])
rlm@118 102 (PC! item-list-start)(print-interrupt)
rlm@118 103 (info) (tick) (info) (tick) (info)))
rlm@118 104
rlm@113 105 (defn read-down-button []
rlm@110 106 (-> (tick (mid-game))
rlm@110 107 (IE! 0) ; disable interrupts
rlm@110 108 (inject-item-assembly
rlm@118 109 ;; write 00010000 to 0xFF00 to select joypad
rlm@118 110 [0x18 ;D31D ; jump over
rlm@118 111 0x01 ;D31E ; the next 8 bits
rlm@118 112 ;D31F
rlm@118 113 (Integer/parseInt "00100000" 2) ; data section
rlm@118 114
rlm@118 115 0xFA ;D320 ; load (D31F) into A
rlm@118 116 0x1F ;D321 -->
rlm@118 117 0xD3 ;D322 --> D31F
rlm@107 118
rlm@118 119 0xEA ;D323 ; load (A), which is
rlm@118 120 0x00 ;D324 --> ; 00010000, into FF00
rlm@118 121 0xFF ;D325 --> FF00
rlm@118 122
rlm@118 123 0x18 ;D326 ; this is the place where
rlm@118 124 0x01 ;D327 ; we will store whether
rlm@118 125 0x00 ;D328 ; "down" is pressed.
rlm@107 126
rlm@118 127 0xFA ;D329 ; (FF00) -> A
rlm@118 128 0x00 ;D32A
rlm@118 129 0xFF ;D32B
rlm@110 130
rlm@118 131 0xCB ;D32C ; Test whether "down"
rlm@118 132 0x5F ;D32D ; is pressed.
rlm@111 133
rlm@118 134 0x28 ;D32E ; if down is pressed,
rlm@118 135 0x03 ;D32F ; skip the next section
rlm@118 136 ; of code.
rlm@118 137 ;; down-is-not-pressed
rlm@118 138 0xC3 ;D330
rlm@118 139 0x1D ;D331 ; return to beginning
rlm@118 140 0xD3 ;D332
rlm@118 141
rlm@118 142 ;; down-is-pressed
rlm@118 143 0xEA ;D334 ; write A to D328 if
rlm@118 144 0x28 ;D335 ; "down" was pressed
rlm@118 145 0xD3 ;D336
rlm@111 146
rlm@118 147 0xC3 ;D330
rlm@118 148 0x1D ;D331 ; return to beginning
rlm@118 149 0xD3 ;D332
rlm@118 150 ])))
rlm@111 151
rlm@118 152 (defn test-read-down []
rlm@118 153 (= (view-memory (step (step (read-down-button) [:d])) 0xD328)
rlm@118 154 (view-memory (step (step (read-down-button))) 0xD328)))
rlm@113 155
rlm@113 156 (defn count-frames []
rlm@113 157 (-> (tick (mid-game))
rlm@113 158 (IE! 0) ; disable interrupts
rlm@113 159 (inject-item-assembly
rlm@113 160 [0x18 ;D31D ; jump over
rlm@113 161 0x02 ;D31E ; the next 2 bytes
rlm@113 162 0x00 ;D31F ; frame-count
rlm@113 163 0x00 ;D320 ; v-blank-prev
rlm@113 164
rlm@113 165 0xFA ;D321
rlm@113 166 0x41 ;D322 ; load (FF41) into A
rlm@113 167 0xFF ;D323 ; this contains mode flags
rlm@113 168
rlm@113 169 ;; if we're in v-blank, the bit-1 is 0
rlm@113 170 ;; and bit-2 is 1 Otherwise, it is not v-blank.
rlm@113 171 0xCB ;D324 ; test bit-1 of A
rlm@113 172 0x4F ;D325
rlm@113 173
rlm@113 174 0xC2 ;D326 ; if bit-1 is not 0
rlm@115 175 0x44 ;D327 ; GOTO not-v-blank
rlm@113 176 0xD3 ;D328
rlm@113 177
rlm@113 178 0xCB ;D329 ; test bit-0 of A
rlm@113 179 0x47 ;D32A
rlm@113 180
rlm@113 181 0xCA ;D32B ; if bit-0 is not 1
rlm@115 182 0x44 ;D32C ; GOTO not-v-blank
rlm@113 183 0xD3 ;D32D
rlm@113 184
rlm@114 185 ;;; in v-blank mode
rlm@113 186
rlm@113 187 ;; if v-blank-prev was 0,
rlm@113 188 ;; increment frame-count
rlm@113 189
rlm@113 190 0xFA ;D32E ; load v-blank-prev to A
rlm@113 191 0x20 ;D32F
rlm@113 192 0xD3 ;D330
rlm@113 193
rlm@113 194 0xCB ;D331
rlm@113 195 0x47 ;D332 ; test bit-0 of A
rlm@113 196
rlm@113 197 0x20 ;D333 ; skip next section
rlm@113 198 0x07 ;D334 ; if v-blank-prev was not zero
rlm@113 199
rlm@113 200 ;; v-blank was 0, increment frame-count
rlm@113 201 0xFA ;D335 ; load frame-count into A
rlm@113 202 0x1F ;D336
rlm@113 203 0xD3 ;D337
rlm@113 204
rlm@113 205 0x3C ;D338 ; inc A
rlm@113 206
rlm@113 207 0xEA ;D339 ; load A into frame-count
rlm@113 208 0x1F ;D33A
rlm@113 209 0xD3 ;D33B
rlm@113 210
rlm@114 211 ;; set v-blank-prev to 1
rlm@113 212 0x3E ;D33C ; load 1 into A
rlm@113 213 0x01 ;D33D
rlm@113 214
rlm@113 215 0xEA ;D33E ; load A into v-blank-prev
rlm@113 216 0x20 ;D33F
rlm@113 217 0xD3 ;D340
rlm@113 218
rlm@115 219 0xC3 ;D341 ; return to beginning
rlm@115 220 0x1D ;D342
rlm@115 221 0xD3 ;D343
rlm@113 222
rlm@114 223 ;;; not in v-blank mode
rlm@114 224 ;; set v-blank-prev to 0
rlm@115 225 0x3E ;D344 ; load 0 into A
rlm@115 226 0x00 ;D345
rlm@113 227
rlm@115 228 0xEA ;D346 ; load A into v-blank-prev
rlm@115 229 0x20 ;D347
rlm@115 230 0xD3 ;D348
rlm@115 231
rlm@115 232 0xC3 ;D349 ; return to beginning
rlm@115 233 0x1D ;D34A
rlm@115 234 0xD3 ;D34B
rlm@113 235 ])))
rlm@113 236
rlm@114 237 (defn step-count-frames []
rlm@113 238 (-> (read-down-button)
rlm@110 239 (info)
rlm@110 240 (tick) ;; skip over data section
rlm@110 241 (info)
rlm@110 242 (view-register "Register A" A)
rlm@110 243 (tick) ;; load-data into A
rlm@110 244 (view-register "Register A" A)
rlm@110 245 (info)
rlm@110 246 (view-memory 0xFF00)
rlm@110 247 (tick) ;; load A into 0xFF00
rlm@110 248 (view-memory 0xFF00)
rlm@111 249 (info)
rlm@111 250 (tick)
rlm@111 251 (info)
rlm@111 252 (tick)
rlm@111 253 (info)
rlm@111 254 (tick)
rlm@111 255 (info)
rlm@111 256 (tick)
rlm@111 257 (info)
rlm@111 258 (tick)
rlm@111 259 (info)
rlm@111 260 (tick)
rlm@111 261 (print-inventory)))
rlm@111 262
rlm@115 263 (defn test-count-frames []
rlm@115 264 (= 255 (aget (memory ((apply comp (repeat 255 step))
rlm@115 265 (count-frames)))
rlm@115 266 0xD31F)))
rlm@115 267
rlm@115 268 ;; specs for main bootstrap program
rlm@115 269 ;; starts in "mode-select" mode
rlm@115 270 ;; Each button press takes place in a single frame.
rlm@115 271 ;; mode-select-mode takes one of the main buttons
rlm@115 272 ;; which selects one of up to eight modes
rlm@115 273 ;; mode 1 activated by the "A" button
rlm@115 274 ;; the next two button presses indicates the start
rlm@115 275 ;; memory location which to which the bootstrap
rlm@115 276 ;; program will write.
rlm@115 277 ;; This is done by using each of the eight buttons to
rlm@115 278 ;; spell out an 8 bit number. The order of buttons is
rlm@116 279 ;; [:d :u :l :r :start :select :b :a]
rlm@116 280 ;; [:a :start :l] --> 00101001
rlm@114 281
rlm@115 282 ;; the next button press determines how many bytes are to be
rlm@115 283 ;; written, starting at the start position.
rlm@114 284
rlm@115 285 ;; then, the actual bytes are entered and are written to the
rlm@115 286 ;; start address in sequence.
rlm@114 287
rlm@118 288 (defn input-number-assembly []
rlm@118 289 [0x18 ;D31D ; jump over
rlm@118 290 0x02 ;D31E ; the next 2 bytes
rlm@118 291 0x00 ;D31F ; frame-count
rlm@118 292 0x00 ;D320 ; v-blank-prev
rlm@118 293
rlm@118 294 0xFA ;D321
rlm@118 295 0x41 ;D322 ; load (FF41) into A
rlm@118 296 0xFF ;D323 ; this contains mode flags
rlm@118 297
rlm@118 298 ;; if we're in v-blank, the bit-1 is 0
rlm@118 299 ;; and bit-2 is 1 Otherwise, it is not v-blank.
rlm@118 300 0xCB ;D324 ; test bit-1 of A
rlm@118 301 0x4F ;D325
rlm@114 302
rlm@118 303 0xC2 ;D326 ; if bit-1 is not 0
rlm@118 304 0x44 ;D327 ; GOTO not-v-blank
rlm@118 305 0xD3 ;D328
rlm@118 306
rlm@118 307 0xCB ;D329 ; test bit-0 of A
rlm@118 308 0x47 ;D32A
rlm@118 309
rlm@118 310 0xCA ;D32B ; if bit-0 is not 1
rlm@118 311 0x44 ;D32C ; GOTO not-v-blank
rlm@118 312 0xD3 ;D32D
rlm@118 313
rlm@118 314 ;;; in v-blank mode
rlm@118 315
rlm@118 316 ;; if v-blank-prev was 0,
rlm@118 317 ;; increment frame-count
rlm@118 318
rlm@118 319 0xFA ;D32E ; load v-blank-prev to A
rlm@118 320 0x20 ;D32F
rlm@118 321 0xD3 ;D330
rlm@118 322
rlm@118 323 0xCB ;D331
rlm@118 324 0x47 ;D332 ; test bit-0 of A
rlm@118 325
rlm@118 326 0x20 ;D333 ; skip next section
rlm@118 327 0x07 ;D334 ; if v-blank-prev was not zero
rlm@118 328
rlm@118 329 ;; v-blank was 0, increment frame-count
rlm@118 330 0xFA ;D335 ; load frame-count into A
rlm@118 331 0x1F ;D336
rlm@118 332 0xD3 ;D337
rlm@118 333
rlm@118 334 0x3C ;D338 ; inc A
rlm@118 335
rlm@118 336 0xEA ;D339 ; load A into frame-count
rlm@118 337 0x1F ;D33A
rlm@118 338 0xD3 ;D33B
rlm@118 339
rlm@118 340 ;; set v-blank-prev to 1
rlm@118 341 0x3E ;D33C ; load 1 into A
rlm@118 342 0x01 ;D33D
rlm@118 343
rlm@118 344 0xEA ;D33E ; load A into v-blank-prev
rlm@118 345 0x20 ;D33F
rlm@118 346 0xD3 ;D340
rlm@118 347
rlm@118 348 0xC3 ;D341 ; GOTO input handling code
rlm@118 349 0x4E ;D342
rlm@118 350 0xD3 ;D343
rlm@118 351
rlm@118 352 ;;; not in v-blank mode
rlm@118 353 ;; set v-blank-prev to 0
rlm@118 354 0x3E ;D344 ; load 0 into A
rlm@118 355 0x00 ;D345
rlm@118 356
rlm@118 357 0xEA ;D346 ; load A into v-blank-prev
rlm@118 358 0x20 ;D347
rlm@118 359 0xD3 ;D348
rlm@118 360
rlm@118 361 0xC3 ;D349 ; return to beginning
rlm@118 362 0x1D ;D34A
rlm@118 363 0xD3 ;D34B
rlm@118 364
rlm@118 365 0x00 ;D34C ; these are here
rlm@118 366 0x00 ;D34D ; for glue
rlm@118 367
rlm@118 368
rlm@118 369 ;;; calculate input number based on button presses
rlm@118 370 0x18 ;D34E ; skip next 3 bytes
rlm@118 371 0x03 ;D34F
rlm@118 372 ;D350
rlm@118 373 (Integer/parseInt "00100000" 2) ; select directional pad
rlm@118 374 ;D351
rlm@118 375 (Integer/parseInt "00010000" 2) ; select buttons
rlm@118 376 0x00 ;D352 ; input-number
rlm@118 377
rlm@118 378 ;; select directional pad, store low bits in B
rlm@118 379
rlm@118 380 0xFA ;D353 ; load (D350) into A
rlm@118 381 0x50 ;D354 -->
rlm@118 382 0xD3 ;D355 --> D31F
rlm@118 383
rlm@118 384 0xEA ;D356 ; load (A), which is
rlm@118 385 0x00 ;D357 --> ; 00010000, into FF00
rlm@118 386 0xFF ;D358 --> FF00
rlm@118 387
rlm@118 388 0x06 ;D359
rlm@118 389 ;D35A
rlm@118 390 (Integer/parseInt "11110000" 2) ; "11110000" -> B
rlm@118 391 0xFA ;D35B ; (FF00) -> A
rlm@118 392 0x00 ;D35C
rlm@118 393 0xFF ;D35D
rlm@118 394
rlm@118 395 0xCB ;D35E ; swap nybbles on A
rlm@118 396 0x37 ;D35F
rlm@118 397 0xA0 ;D360 ; (AND A B) -> A
rlm@118 398 0x47 ;D361 ; A -> B
rlm@118 399
rlm@118 400 ;; select buttons store bottom bits in C
rlm@118 401
rlm@118 402 0xFA ; ; load (D351) into A
rlm@118 403 0x51 ; -->
rlm@118 404 0xD3 ; --> D31F
rlm@118 405
rlm@118 406 0xEA ; ; load (A), which is
rlm@118 407 0x00 ; --> ; 00001000, into FF00
rlm@118 408 0xFF ; --> FF00
rlm@118 409
rlm@118 410 0x0E ;
rlm@118 411 (Integer/parseInt "00001111" 2) ; "00001111" -> C
rlm@118 412
rlm@118 413 0xFA ; ; (FF00) -> A
rlm@118 414 0x00 ;
rlm@118 415 0xFF ;
rlm@118 416
rlm@118 417 0xA1 ; ; (AND A C) -> A
rlm@118 418 0x4F ; ; A -> C
rlm@118 419
rlm@118 420 ;; combine the B and C registers into the input number
rlm@118 421 0x79 ; ; C -> A
rlm@118 422 0xB0 ; ; (OR A B) -> A
rlm@118 423 0x2F ; ; negate A
rlm@118 424
rlm@118 425 0xEA ; ; store A into input-number
rlm@118 426 0x52 ;
rlm@118 427 0xD3 ;
rlm@118 428
rlm@118 429 0xC3 ; ; return to beginning
rlm@118 430 0x1D ;
rlm@118 431 0xD3 ;
rlm@118 432 ])
rlm@114 433
rlm@115 434 (defn input-number []
rlm@115 435 (-> (tick (mid-game))
rlm@115 436 (IE! 0) ; disable interrupts
rlm@118 437 (inject-item-assembly (input-number-assembly))))
rlm@115 438
rlm@117 439 (defn test-input-number
rlm@117 440 "Input freestyle buttons and observe the effects at the repl."
rlm@117 441 []
rlm@117 442 (set-state! (input-number))
rlm@117 443 (dotimes [_ 90000] (step (view-memory @current-state 0xD352))))
rlm@115 444
rlm@118 445 (defn write-memory-assembly []
rlm@118 446 [0x18 ;D31D ; jump over
rlm@118 447 0x02 ;D31E ; the next 2 bytes
rlm@118 448 0x00 ;D31F ; frame-count
rlm@118 449 0x00 ;D320 ; v-blank-prev
rlm@118 450
rlm@118 451 0xFA ;D321
rlm@118 452 0x41 ;D322 ; load (FF41) into A
rlm@118 453 0xFF ;D323 ; this contains mode flags
rlm@118 454
rlm@118 455 ;; if we're in v-blank, the bit-1 is 0
rlm@118 456 ;; and bit-2 is 1 Otherwise, it is not v-blank.
rlm@118 457 0xCB ;D324 ; test bit-1 of A
rlm@118 458 0x4F ;D325
rlm@115 459
rlm@118 460 0xC2 ;D326 ; if bit-1 is not 0
rlm@118 461 0x44 ;D327 ; GOTO not-v-blank
rlm@118 462 0xD3 ;D328
rlm@118 463
rlm@118 464 0xCB ;D329 ; test bit-0 of A
rlm@118 465 0x47 ;D32A
rlm@115 466
rlm@118 467 0xCA ;D32B ; if bit-0 is not 1
rlm@118 468 0x44 ;D32C ; GOTO not-v-blank
rlm@118 469 0xD3 ;D32D
rlm@118 470
rlm@118 471 ;;; in v-blank mode
rlm@115 472
rlm@118 473 ;; if v-blank-prev was 0,
rlm@118 474 ;; increment frame-count
rlm@118 475
rlm@118 476 0xFA ;D32E ; load v-blank-prev to A
rlm@118 477 0x20 ;D32F
rlm@118 478 0xD3 ;D330
rlm@118 479
rlm@118 480 0xCB ;D331
rlm@118 481 0x47 ;D332 ; test bit-0 of A
rlm@118 482
rlm@118 483 0x20 ;D333 ; skip next section
rlm@118 484 0x07 ;D334 ; if v-blank-prev was not zero
rlm@118 485
rlm@118 486 ;; v-blank was 0, increment frame-count
rlm@118 487 0xFA ;D335 ; load frame-count into A
rlm@118 488 0x1F ;D336
rlm@118 489 0xD3 ;D337
rlm@118 490
rlm@118 491 0x3C ;D338 ; inc A
rlm@118 492
rlm@118 493 0xEA ;D339 ; load A into frame-count
rlm@118 494 0x1F ;D33A
rlm@118 495 0xD3 ;D33B
rlm@118 496
rlm@118 497 ;; set v-blank-prev to 1
rlm@118 498 0x3E ;D33C ; load 1 into A
rlm@118 499 0x01 ;D33D
rlm@118 500
rlm@118 501 0xEA ;D33E ; load A into v-blank-prev
rlm@118 502 0x20 ;D33F
rlm@118 503 0xD3 ;D340
rlm@118 504
rlm@118 505 0xC3 ;D341 ; GOTO input handling code
rlm@118 506 0x4E ;D342
rlm@118 507 0xD3 ;D343
rlm@118 508
rlm@118 509 ;;; not in v-blank mode
rlm@118 510 ;; set v-blank-prev to 0
rlm@118 511 0x3E ;D344 ; load 0 into A
rlm@118 512 0x00 ;D345
rlm@118 513
rlm@118 514 0xEA ;D346 ; load A into v-blank-prev
rlm@118 515 0x20 ;D347
rlm@118 516 0xD3 ;D348
rlm@118 517
rlm@118 518 0xC3 ;D349 ; return to beginning
rlm@118 519 0x1D ;D34A
rlm@118 520 0xD3 ;D34B
rlm@118 521
rlm@118 522 0x00 ;D34C ; these are here
rlm@118 523 0x00 ;D34D ; for glue
rlm@118 524
rlm@118 525
rlm@118 526 ;;; calculate input number based on button presses
rlm@118 527 0x18 ;D34E ; skip next 3 bytes
rlm@118 528 0x03 ;D34F
rlm@118 529 ;D350
rlm@118 530 (Integer/parseInt "00100000" 2) ; select directional pad
rlm@118 531 ;D351
rlm@118 532 (Integer/parseInt "00010000" 2) ; select buttons
rlm@118 533 0x00 ;D352 ; input-number
rlm@118 534
rlm@118 535 ;; select directional pad, store low bits in B
rlm@118 536
rlm@118 537 0xFA ;D353 ; load (D350) into A
rlm@118 538 0x50 ;D354 -->
rlm@118 539 0xD3 ;D355 --> D31F
rlm@118 540
rlm@118 541 0xEA ;D356 ; load (A), which is
rlm@118 542 0x00 ;D357 --> ; 00010000, into FF00
rlm@118 543 0xFF ;D358 --> FF00
rlm@118 544
rlm@118 545 0x06 ;D359
rlm@118 546 ;D35A
rlm@118 547 (Integer/parseInt "11110000" 2) ; "11110000" -> B
rlm@118 548 0xFA ;D35B ; (FF00) -> A
rlm@118 549 0x00 ;D35C
rlm@118 550 0xFF ;D35D
rlm@118 551
rlm@118 552 0xCB ;D35E ; swap nybbles on A
rlm@118 553 0x37 ;D35F
rlm@118 554 0xA0 ;D360 ; (AND A B) -> A
rlm@118 555 0x47 ;D361 ; A -> B
rlm@118 556
rlm@118 557 ;; select buttons store bottom bits in C
rlm@118 558
rlm@118 559 0xFA ;D362 ; load (D351) into A
rlm@118 560 0x51 ;D363 -->
rlm@118 561 0xD3 ;D364 --> D31F
rlm@118 562
rlm@118 563 0xEA ;D365 ; load (A), which is
rlm@118 564 0x00 ;D366 --> ; 00001000, into FF00
rlm@118 565 0xFF ;D367 --> FF00
rlm@118 566
rlm@118 567 0x0E ;D368
rlm@118 568 ;D369
rlm@118 569 (Integer/parseInt "00001111" 2) ; "00001111" -> C
rlm@118 570
rlm@118 571 0xFA ;D36A ; (FF00) -> A
rlm@118 572 0x00 ;D36B
rlm@118 573 0xFF ;D36C
rlm@118 574
rlm@118 575 0xA1 ;D36D ; (AND A C) -> A
rlm@118 576 0x4F ;D36E ; A -> C
rlm@118 577
rlm@118 578 ;; combine the B and C registers into the input number
rlm@118 579 0x79 ;D36F ; C -> A
rlm@118 580 0xB0 ;D370 ; (OR A B) -> A
rlm@118 581 0x2F ;D371 ; negate A
rlm@118 582
rlm@118 583 0xEA ;D372 ; store A into input-number
rlm@118 584 0x52 ;D373
rlm@118 585 0xD3 ;D374
rlm@118 586
rlm@118 587 0xC3 ;D375 ; GOTO state machine
rlm@118 588 ;;0x1D
rlm@118 589 0x80 ;D376
rlm@118 590 0xD3 ;D377
rlm@118 591
rlm@118 592 0x00 ;D378
rlm@118 593 0x00 ;D379
rlm@118 594 0x00 ;D37A
rlm@118 595 0x00 ;D37B ; these are here because
rlm@118 596 0x00 ;D37C ; I messed up :(
rlm@118 597 0x00 ;D37D
rlm@118 598 0x00 ;D37E
rlm@118 599 0x00 ;D37F
rlm@118 600
rlm@118 601 ;; beginning of main state machine
rlm@118 602 0x18 ;D380 ; Declaration of variables
rlm@118 603 0x05 ;D381 ; 5 variables:
rlm@118 604 0x00 ;D382 ; current-mode
rlm@118 605 0x00 ;D383 ; bytes-left-to-write
rlm@118 606 0x00 ;D384 ; unused
rlm@118 607 0x00 ;D385 ; unused
rlm@118 608 0x00 ;D386 ; unused
rlm@118 609
rlm@118 610
rlm@118 611 ;; banch on current mode
rlm@118 612 ;; mode 0 -- input-mode mode
rlm@118 613 ;; means that we are waiting for a mode, so set the mode to
rlm@118 614 ;; whatever is currently in input number. If nothing is
rlm@118 615 ;; entered, then the program stays in input-mode mode
rlm@118 616
rlm@118 617 0xFA ;D387 ; load current-mode (0xD382)
rlm@118 618 0x82 ;D388 ; into A
rlm@118 619 0xD3 ;D389
rlm@118 620
rlm@118 621 0x00 ;D38A
rlm@118 622
rlm@118 623 0xFE ;D38B
rlm@118 624 0x00 ;D38C ; compare A with 0x00
rlm@118 625
rlm@118 626 ;; TODO make this jump non-absolute
rlm@118 627
rlm@118 628 0xCA ;D38D ; GOTO Mode 0 if current-mode is 0
rlm@118 629 0xA8 ;D38E
rlm@118 630 0xD3 ;D38F
rlm@118 631
rlm@118 632 0x00 ;D390
rlm@118 633 0x00 ;D391
rlm@118 634 0x00 ;D392
rlm@118 635 0x00 ;D393
rlm@118 636 0x00 ;D394
rlm@118 637 0x00 ;D395
rlm@118 638 0x00 ;D396
rlm@118 639 0x00 ;D397
rlm@118 640 0x00 ;D398
rlm@118 641 0x00 ;D399
rlm@118 642 0x00 ;D39A
rlm@118 643 0x00 ;D39B
rlm@118 644 0x00 ;D39C
rlm@118 645 0x00 ;D39D
rlm@118 646 0x00 ;D39E
rlm@118 647 0x00 ;D39F
rlm@118 648 0x00 ;D3A0
rlm@118 649 0x00 ;D3A1
rlm@118 650 0x00 ;D3A2
rlm@118 651 0x00 ;D3A3
rlm@118 652 0x00 ;D3A4
rlm@118 653 ;; End of Mode checking, goto beginning
rlm@118 654 0xC3 ;D3A5
rlm@118 655 0x1D ;D3A6
rlm@118 656 0xD3 ;D3A7
rlm@118 657 ;; Mode 0
rlm@118 658 ;; set current-mode to input-number
rlm@118 659 0xFA ;D3A8 ; load input-number (0xD352)
rlm@118 660 0x52 ;D3A9 ; into A
rlm@118 661 0xD3 ;D3AA
rlm@118 662
rlm@118 663 0xEA ;D3AB ; load A into current-mode
rlm@118 664 0x82 ;D3AC ; (0xD382)
rlm@118 665 0xD3 ;D3AD
rlm@118 666
rlm@118 667 0xC3 ;D3AE ; go back to beginning
rlm@118 668 0x1D ;D3AF
rlm@118 669 0xD3 ;D3B0
rlm@118 670
rlm@118 671 0x00 ;D3B1
rlm@118 672 0x00 ;D3B2
rlm@118 673 0x00 ;D3B3
rlm@118 674 0x00 ;D3B4
rlm@118 675 0x00 ;D3B5
rlm@118 676 0x00 ;D3B6
rlm@118 677 0x00 ;D3B7
rlm@118 678 0x00 ;D3B8
rlm@118 679 0x00 ;D3B9
rlm@118 680 0x00 ;D3BA
rlm@118 681 0x00 ;D3BB
rlm@118 682 0x00 ;D3BC
rlm@118 683 0x00 ;D3BD
rlm@118 684 0x00 ;D3BE
rlm@118 685 0x00 ;D3BF
rlm@118 686 0x00 ;D3C0
rlm@118 687 0x00 ;D3C1
rlm@118 688 0x00 ;D3C2
rlm@118 689 0x00 ;D3C3
rlm@118 690 0x00 ;D3C4
rlm@118 691 0x00 ;D3C5
rlm@118 692 0x00 ;D3C6
rlm@118 693 0x00 ;D3C7
rlm@118 694 0x00 ;D3C8
rlm@118 695 0x00 ;D3C9
rlm@118 696 0x00 ;D3CA
rlm@118 697 0x00 ;D3CB
rlm@118 698 0x00 ;D3CC
rlm@118 699 0x00 ;D3CD
rlm@118 700 0x00 ;D3CE
rlm@118 701 0x00 ;D3CF
rlm@118 702 0x00 ;D3D0
rlm@118 703 0x00 ;D3D1
rlm@118 704 0x00 ;D3D2
rlm@118 705 0x00 ;D3D3
rlm@118 706 0x00 ;D3D4
rlm@118 707 0x00 ;D3D5
rlm@118 708 0x00 ;D3D6
rlm@118 709
rlm@118 710
rlm@118 711
rlm@118 712
rlm@118 713 0xC3 ; ; Complete Loop
rlm@118 714 0x1D ;
rlm@118 715 0xD3 ;
rlm@118 716
rlm@118 717
rlm@118 718
rlm@118 719 ])
rlm@118 720
rlm@118 721
rlm@118 722 (def frame-count 0xD31F)
rlm@118 723 (def input 0xD352)
rlm@118 724 (def current-mode 0xD382)
rlm@118 725
rlm@118 726 (defn write-memory []
rlm@118 727 (-> (tick (mid-game))
rlm@118 728 (IE! 0) ; disable interrupts
rlm@118 729 (inject-item-assembly (write-memory-assembly))))