rlm@377: (ns com.aurellem.gb.rlm-assembly rlm@377: "Version of main bootstrap program that is valid output for the rlm@377: item-writer program." rlm@377: (:use (com.aurellem.gb gb-driver assembly util vbm constants)) rlm@377: (:use (com.aurellem.run bootstrap-1)) rlm@377: (:import [com.aurellem.gb.gb_driver SaveState])) rlm@377: rlm@377: ;; Specs for Main Bootstrap Program rlm@377: rlm@377: ;; Number-Input rlm@377: ;; Number input works using all eight buttons to rlm@377: ;; spell out an 8 bit number. The order of buttons is rlm@377: ;; [:d :u :l :r :start :select :b :a] --> 11111111 rlm@377: ;; [ :l :start :a] --> 00101001 rlm@377: rlm@377: ;;; MODE-SELECT rlm@377: ;; The bootstrap program starts in MODE-SELECT mode. rlm@377: ;; MODE-SELECT transitions to one of three modes depending rlm@377: ;; on which buttons are pressed: rlm@377: ;; 0 (no-buttons) : MODE-SELECT rlm@377: ;; 8 [:start] : WRITE-BYTES rlm@377: ;; 0xFF (all-buttons) : JUMP rlm@377: rlm@377: ;;; WRITE-BYTES rlm@377: rlm@377: ;; WRITE-BYTES mode writes sequences of arbitray values to rlm@377: ;; arbitray memory locations. It expects you to enter a rlm@377: ;; header of three bytes describing what to write: rlm@377: rlm@377: ;; Byte 0 : Number of Bytes to Write rlm@377: ;; Byte 1 : Start Address High Byte rlm@377: ;; Byte 1 : Start Address Low Byte rlm@377: rlm@377: ;; Then, you enter the number of bytes specified in Byte 0 rlm@377: ;; they are written to the start address in rlm@377: ;; sequence. After the last byte is written control rlm@377: ;; returns to MODE-SELECT mode. rlm@377: rlm@377: ;; Example: to write the sequence [1 2 3 4] starting at rlm@377: ;; address 0xC01F enter rlm@377: ;; Byte 0 : 4 (will write four bytes) rlm@377: ;; Byte 1 : 0xC0 (high byte of 0xC01F) rlm@377: ;; Byte 2 : 0x1F (low byte of 0xC01F) rlm@377: ;; Byte 3 : 1 (write 1 to 0xC01F) rlm@377: ;; Byte 4 : 2 (write 2 to 0xC020) rlm@377: ;; Byte 5 : 3 (write 3 to 0xC021) rlm@377: ;; Byte 6 : 4 (write 4 to 0xC022) rlm@377: rlm@377: ;;; JUMP rlm@377: ;; JUMP mode jumps program control to any arbitray rlm@377: ;; location. It expects you to enter two bytes which rlm@377: ;; correspond to the high and low bytes of the memory rlm@377: ;; address to which you want to jump. rlm@377: ;; Byte 0 : Jump Address High Byte rlm@377: ;; Byte 1 : Jump Address Low Byte rlm@377: rlm@377: ;; Example: to jump to address 0x1234 enter rlm@377: ;; Byte 0 : 0x12 (high byte of 0x1234) rlm@377: ;; Byte 1 : 0x34 (low byte of 0x1234) rlm@377: rlm@377: rlm@377: rlm@377: (defn main-bootstrap-program [start-address] rlm@377: (let [[start-high start-low] (disect-bytes-2 start-address)] rlm@377: [0xF3 0x18 0xFE])) rlm@377: rlm@377: ;;;;;; TESTS ;;;;;; rlm@377: rlm@377: (defn bootstrap-base [] rlm@377: (let [program (main-bootstrap-program pokemon-list-start)] rlm@377: ;; make sure program is valid output for item-writer rlm@377: (bootstrap-pattern program) rlm@377: (-> (tick (mid-game)) rlm@377: (set-memory-range pokemon-list-start program) rlm@377: (PC! pokemon-list-start)))) rlm@377: rlm@377: (defn test-write-bytes-mode [] rlm@377: (let [target-address 0xC00F rlm@377: [target-high target-low] (disect-bytes-2 target-address) rlm@377: assembly [0xF3 0x18 0xFE 0x12] rlm@377: get-mem-region #(subvec (vec (memory %)) rlm@377: target-address (+ target-address 20)) rlm@377: before (bootstrap-base) rlm@377: after rlm@377: (-> before rlm@377: (step []) ; make sure it can handle blanks rlm@377: (step []) ; at the beginning. rlm@377: (step []) rlm@377: (step [:start]) ; select WRITE-BYTES mode rlm@377: (step (buttons 4)) ; write 4 bytes rlm@377: (step (buttons target-high)) rlm@377: (step (buttons target-low)) rlm@377: (step (buttons (nth assembly 0))) rlm@377: (step (buttons (nth assembly 1))) rlm@377: (step (buttons (nth assembly 2))) rlm@377: (step (buttons (nth assembly 3))) rlm@377: (step []) rlm@377: (step []) rlm@377: (step []))] rlm@377: (println "before :" (get-mem-region before)) rlm@377: (println "after :" (get-mem-region after)) rlm@377: (assert (= assembly (take 4 (get-mem-region after)))) rlm@377: after)) rlm@377: rlm@377: (defn test-jump-mode [] rlm@377: (let [target-address 0xC00F rlm@377: [target-high target-low] (disect-bytes-2 target-address) rlm@377: post-jump rlm@377: (-> (test-write-bytes-mode) rlm@377: (step []) rlm@377: (step []) rlm@377: (step []) rlm@377: (step (buttons 0xFF)) ; Select JUMP mode. rlm@377: (step (buttons target-high)) rlm@377: (step (buttons target-low))) rlm@377: program-counters rlm@377: (capture-program-counter rlm@377: post-jump rlm@377: 10000)] rlm@377: (println program-counters) rlm@377: (assert (contains? (set program-counters) target-address)) rlm@377: post-jump))