Mercurial > vba-clojure
view clojure/com/aurellem/run/bootstrap_0.clj @ 368:08f8284e2f1b
cleaning up code
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 10 Apr 2012 03:32:47 -0500 |
parents | 3b3cd62b6106 |
children | abcc522a3242 |
line wrap: on
line source
1 (ns com.aurellem.run.bootstrap-02 (:use (com.aurellem.gb saves gb-driver util3 items vbm characters money))4 (:use (com.aurellem.run util title save-corruption))5 (:use (com.aurellem.exp item-bridge))6 (:import [com.aurellem.gb.gb_driver SaveState]))8 (defn-memo boot-root []9 [ [] (root)])11 (defn-memo to-rival-name12 ([] (to-rival-name (boot-root)))13 ([script]14 (->> script15 title16 oak17 name-entry-rlm18 (scroll-text 5))))20 (defn-memo name-rival-bootstrap21 ([] (name-rival-bootstrap (to-rival-name)))22 ([script]23 (->> script24 (first-difference [] [:a] AF)25 (first-difference [] [:r] DE)26 (play-moves27 [[]28 [] [] [:r] [] [:d] [:a] ;; L29 [:r] [] [:r] [] [:r] [] [:r] []30 [:r] [] [:d] [] [:d] [:a] ;; [PK]31 [:u] [] [:l] [] [:l] []32 [:l] [] [:l] [] [:l] [:a] ;; U33 [:r] [] [:r] [] [:r] []34 [:r] [] [:r] [] [:d] [:a] ;; [PK]35 [] [:a] ;; [PK]36 [] [:a] ;; [PK]37 [:r] [] [:d] [:a] ;; END38 ]))))40 (defn-memo leave-house41 ([] (leave-house (name-rival-bootstrap)))42 ([script]43 (->> script44 finish-title45 walk-to-stairs46 walk-to-door47 (walk [↓ ↓]))))49 (defn-memo to-pallet-town-edge50 ([] (to-pallet-town-edge (leave-house)))51 ([script]52 (->> script53 (walk [→ → → → →54 ↑ ↑ ↑ ↑ ↑ ↑]))))56 (defn-memo start-pikachu-battle57 ([] (start-pikachu-battle58 (to-pallet-town-edge)))59 ([script]60 (->> script61 (first-difference [:b] [:b :a] DE)62 scroll-text63 (do-nothing 200)64 (play-moves [[:b]]))))66 (defn-memo capture-pikachu67 ([] (capture-pikachu (start-pikachu-battle)))68 ([script]69 (->> script70 (scroll-text 3))))72 (defn-memo go-to-lab73 ([] (go-to-lab (capture-pikachu)))74 ([script]75 (->> script76 end-text77 (scroll-text 5)78 end-text79 ;; oak walks you to his lab; no input required.80 (do-nothing 400))))82 (defn-memo talk-to-oak-in-lab83 ([] (talk-to-oak-in-lab (go-to-lab)))84 ([script]85 (->> script86 (scroll-text 14)87 end-text)))89 (defn-memo try-to-get-eevee90 ([] (try-to-get-eevee (talk-to-oak-in-lab)))91 ([script]92 (->> script93 ;; walk to pokeball94 (walk [↓ → →])95 ;; and try to grab it96 (play-moves97 (concat [↑ ↑ [:a]]98 (repeat 100 [])))99 (scroll-text 10)100 (end-text))))102 (defn-memo obtain-pikachu103 ([] (obtain-pikachu (try-to-get-eevee)))104 ([script]105 (->> script106 (scroll-text 6)107 (end-text))))110 (defn-memo begin-battle-with-rival111 ([] (begin-battle-with-rival112 (obtain-pikachu)))113 ([script]114 (->> script115 (walk [↓ ↓ ↓])116 (scroll-text 3)117 (end-text)118 (scroll-text))))120 (defn-memo defeat-eevee121 ([] (defeat-eevee122 (begin-battle-with-rival)))123 ([script]124 (->> script125 (do-nothing 400)126 (play-moves [[:a]])127 (critical-hit)128 (do-nothing 200)129 (scroll-text 2) ;; for eevee's tail-whip130 (do-nothing 10)131 (play-moves [[:a]])132 (critical-hit)133 (do-nothing 200)134 (scroll-text 2) ;; tail whip again135 (do-nothing 10)136 (play-moves [[:a]])137 (critical-hit)138 (do-nothing 200))))140 (defn-memo finish-rival-text141 ([] (finish-rival-text142 (defeat-eevee)))143 ([script]144 (->> script145 (scroll-text 12)146 (end-text))))148 (defn-memo pikachu-comes-out149 ([] (pikachu-comes-out150 (finish-rival-text)))151 ([script]152 (->> script153 (scroll-text 8)154 (end-text))))156 (defn-memo leave-oaks-lab157 ([] (leave-oaks-lab158 (pikachu-comes-out)))159 ([script]160 (->> script161 (walk [↓ ↓ ↓ ↓ ↓ ↓]))))163 (defn-memo oaks-lab->pallet-town-edge164 ([] (oaks-lab->pallet-town-edge165 (leave-oaks-lab)))166 ([script]167 (->> script168 (walk [← ← ←169 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ →]))))171 (defn-memo pallet-edge->viridian-mart172 ([] (pallet-edge->viridian-mart true173 (oaks-lab->pallet-town-edge)))174 ([dodge-stupid-guy? script]175 (let [dodge-1 (if dodge-stupid-guy?176 [→ →]177 [→])178 dodge-2 (if dodge-stupid-guy?179 [↑ ↑ ←]180 [↑ ↑])]182 (->> script183 ;; leave straight grass184 (walk-thru-grass185 [↑ ↑ ↑ ↑ ↑])187 (walk [↑ ↑ ↑ ↑])189 (walk-thru-grass190 [← ← ↑])192 (walk [↑ ↑ ↑ ↑ → → → ])194 (walk-thru-grass195 [→ ↑ ↑ ←])197 (walk198 [← ←199 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑200 → → → → ])202 ;; this part is dependent on that203 ;; stupid NPC in the grass patch204 (walk-thru-grass205 (concat dodge-1206 [↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ]))208 (walk209 (concat210 dodge-2211 [← ← ←212 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑213 ← ←214 ↑ ↑ ↑ ↑215 → → → → → → → → → →216 ↑ ↑ ↑ ↑ ↑ ↑ ↑]))))))218 (defn-memo get-oaks-parcel219 ([] (get-oaks-parcel220 (pallet-edge->viridian-mart)))221 ([script]222 (->> script223 (do-nothing 50)224 (end-text)225 (scroll-text 3)226 (do-nothing 197)227 (play-moves [[:a] []])228 (walk [↓ ↓ → ↓]))))230 (defn-memo viridian-store->oaks-lab231 ([] (viridian-store->oaks-lab232 (get-oaks-parcel)))233 ([script]234 (->> script235 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓236 ← ← ← ← ← ← ← ← ←237 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓238 ← ←239 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓240 ↓ ↓ ↓ ↓ ↓ ↓ ↓241 → → → → → → → →242 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓243 ← ← ← ← ←244 ↓ ↓ ↓ ↓245 ])246 (walk-thru-grass247 [↓ ↓ ↓ ↓ ↓ ↓ ↓])248 (walk [↓ ↓ ← ↓ ↓ ↓ ←249 ↓ ↓ ↓ ↓ ↓ ↓250 → → → ↑])252 (do-nothing 1))))255 (defn-memo viridian-store->oaks-lab-like-a-boss256 ([] (viridian-store->oaks-lab-like-a-boss257 (get-oaks-parcel)))258 ([script]259 (->> script260 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓261 ← ← ← ← ← ← ← ← ←262 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])264 (walk-thru-grass265 [↓ ↓ ↓ ↓ ↓])267 (walk268 [↓ ↓ ← ↓269 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓270 → → → ↓])272 (walk-thru-grass273 [↓ ↓ ↓])275 (walk [↓ ← ← ↓ ↓ ↓ ↓ ↓ ↓])277 (walk-thru-grass278 [↓ ↓ ↓ ↓ ↓ ↓])280 (walk [↓ ↓ ↓ ← ↓ ↓ ↓281 ↓ ↓ ↓ ↓ ↓282 → → → ↑]))))284 (defn-memo deliver-oaks-parcel285 ([] (deliver-oaks-parcel286 (viridian-store->oaks-lab-like-a-boss)))287 ([script]288 (->> script289 (walk [↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑])290 (play-moves [[] [:a]])291 (scroll-text 13)292 (end-text)293 (do-nothing 200)294 (scroll-text 2)295 (end-text)296 (scroll-text 2)297 (end-text)298 (scroll-text 8)299 (end-text)300 (scroll-text 9)301 (end-text)302 (scroll-text 7)303 (end-text)304 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓]))))306 (defn-memo return-to-viridian-mart307 ([] (return-to-viridian-mart308 (deliver-oaks-parcel)))309 ([script]310 (->> script311 oaks-lab->pallet-town-edge312 (pallet-edge->viridian-mart false))))314 (defn-memo walk-to-counter315 ([] (walk-to-counter316 (return-to-viridian-mart)))317 ([script]318 (->> script319 (walk [↑ ↑ ←]))))323 ;; useful addresses324 52262 ;; --- current-cursor-offset325 52278 ;; --- current screen-offset328 (defn exp-item-list []329 (clojure.pprint/pprint330 (apply harmonic-compare331 (map read-state332 ["up-1" "down-1"333 "up-2" "down-2"334 "up-3" "down-3"335 "up-4" "down-4"336 "up-5" "down-5"337 "up-6"]))))340 ;; turns out that these addresses are the cursor position341 ;; for all lists in the game (start list, pokemon list, shop342 ;; lists, inventory lists, battle list, basically343 ;; everything!)345 (def list-cursor-offset-address 52262)346 (def list-screen-offset-address 52278)348 (defn list-offset349 ([^SaveState state]350 (let [mem (memory state)]351 (+ (aget mem list-screen-offset-address)352 (aget mem list-cursor-offset-address))))353 ([] (list-offset @current-state)))355 (defn exp-item-selection []356 (clojure.pprint/pprint357 (apply memory-compare358 (map read-state359 ["1-item"360 "2-items"361 "3-items"362 "4-items"363 ]))))365 (def item-quantity-selected-address 65432)367 (defn item-quantity-selected368 ([^SaveState state]369 (println "items:" (aget (memory state) item-quantity-selected-address))370 (aget (memory state) item-quantity-selected-address))371 ([] (item-quantity-selected @current-state)))373 (defn wait-until374 ([script-fn default-key script]375 (let [wait-time376 (- (dec (count (first (script-fn script))))377 (count (first script)))]378 (println "wait-time" wait-time)379 (play-moves (repeat wait-time default-key) script)))380 ([script-fn script]381 (wait-until script-fn [] script)))383 (defn set-cursor-relative384 "Assumes the arrow keys currently control the cursor.385 Moves the cursor n steps relative to its current386 position."387 [n script]388 (let [key (if (< 0 n) ↓ ↑)]389 (multiple-times390 (Math/abs n)391 (partial first-difference392 [] key list-offset)393 script)))395 (defn set-cursor*396 [n [moves state :as script]]397 (let [current-position (list-offset state)398 difference (- n current-position)]399 (set-cursor-relative difference script)))401 (defn set-cursor402 "Assumes the arrow keys currently control the cursor. Sets403 the cursor to the desired position. Works for any menu404 that uses a cursor including the start menu, item menu,405 pokemon menu, and battle menu."406 [n [moves state :as script]]407 (->> script408 (wait-until (partial set-cursor-relative 1))409 (set-cursor* n)))411 (defn first-character [state]412 (aget (memory state) text-address))414 (defn set-quantity*415 "Set the quantity of an item to buy or sell to the desired value416 using the fewest possible button presses."417 [total-quantity desired-quantity [moves state :as script]]418 (cond (= desired-quantity 1) (do (println "1 of 1") script)419 (= total-quantity desired-quantity)420 (do (println "get everything!")421 (delayed-difference [] ↓ 5 item-quantity-selected422 script))423 true424 (let [current-quantity (item-quantity-selected state)425 loop-point (if (= 0 total-quantity) 0x100 total-quantity)426 distance (- desired-quantity current-quantity)427 loop-distance (int(* -1 (Math/signum (float distance))428 (- loop-point (Math/abs distance))))429 best-path (first (sort-by #(Math/abs %)430 [distance loop-distance]))431 direction (if (< 0 best-path) ↑ ↓)]432 (println "best-path" best-path)433 (println "current-quantity" current-quantity)434 (println "desired-quantity" desired-quantity)435 (println "options" [distance loop-distance])436 (reduce437 (fn [script _]438 (delayed-difference [] direction 5 item-quantity-selected439 script))440 script441 (range (Math/abs best-path))))))443 (defn set-quantity444 ([total-quantity desired-quantity [moves state :as script]]445 (->> script (wait-until (partial first-difference [] [:a]446 first-character))447 (set-quantity* total-quantity desired-quantity script)))448 ([desired-quantity [moves state :as script]]449 (set-quantity 99 desired-quantity script)))452 (defn activate-start-menu [script]453 (first-difference [:b] [:b :start] AF script))455 (defn select-menu-entry456 ([test-direction [moves state :as script]]457 (->> script458 (wait-until (partial set-cursor-relative test-direction))459 (play-moves [[] [:a] []])))460 ([[moves state :as script]]461 (select-menu-entry462 1 script)))464 (defn restart465 "The two button presses after a restart event are converted to466 blanks. Due to weirdness with the VBM format. To compensate, ensure467 that the two button presses after restart are both blanks."468 [script]469 (play-moves [[:restart] [] []] script))471 (defn-memo do-save-corruption472 ([] (do-save-corruption473 (walk-to-counter)))474 ([script] (do-save-corruption 4 script))475 ([n script]476 (->> script477 activate-start-menu478 (set-cursor n)479 select-menu-entry480 select-menu-entry481 (play-moves482 ;; this section is copied from speedrun-2942 and corrupts483 ;; the save so that the total number of pokemon is set to484 ;; 0xFF, allowing manipulation of non-pokemon data in RAM485 ;; via the pokemon interface.486 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []487 [] [] [] [] [] [] [] [] [] [] [] []])488 (restart)489 (title)490 (first-difference [] [:start] AF)491 (first-difference [] [:a] AF))))493 (defn gen-corrupted-checkpoint! []494 (let [[cor-moves cor-save] (do-save-corruption)]495 (write-moves! cor-moves "cor-checkpoint")496 (write-state! cor-save "cor-checkpoint")))498 (defn corrupted-checkpoint []499 [(read-moves "cor-checkpoint")500 (read-state "cor-checkpoint")])502 (def menu do-nothing )505 (defn investivate-close-menu []506 (clojure.pprint/pprint507 (apply harmonic-compare508 (map read-state509 ["start-up-1"510 "start-down-1"511 ;;"start-up-2"512 ;;"start-down-2"513 ;;"start-up-3"514 ;;"start-down-3"515 ;;"computer-up-1"516 ;;"computer-down-2"517 "computer-up-2"518 "computer-down-2"519 "pokemon-up-1"520 "pokemon-down-1"521 "pokemon-up-2"522 "pokemon-down-2"523 "item-up-1"524 "item-down-1"525 "save-up-1"526 "save-down-1"527 "item-nest-up-1"528 "item-nest-down-1"]))))530 (def list-nesting-depth-address 50339)532 (defn current-depth533 ([^SaveState state] (aget (memory state) list-nesting-depth-address))534 ([] (current-depth @current-state)))537 (defn close-menu [script]538 (delayed-difference539 [] [:b] 50540 current-depth541 script))544 (defn purchase-item545 "Assumes that the cursor is over the desired item, and purchases546 quantity of that item."547 [n script]548 (->> script549 select-menu-entry550 (set-quantity n)551 (first-difference [] [:a] AF)552 scroll-text553 select-menu-entry554 scroll-text))556 (defn-memo corrupt-item-list557 "Corrupt the num-of-items variable by switching a corrupted pokemon558 into out-of-bounds memory."559 ([] (corrupt-item-list560 ;;(corrupted-checkpoint)561 (do-save-corruption)562 ))563 ([script] (corrupt-item-list 1))564 ([n script]565 (->> script566 activate-start-menu567 (set-cursor n) ; select "POKEMON"568 select-menu-entry ; from main menu.569 (set-cursor 5) ; select 6th pokemon570 select-menu-entry571 (set-cursor 1)572 select-menu-entry573 (repeat-until-different [] list-offset)574 (set-cursor 9)575 select-menu-entry ; switch 6th with 10th576 close-menu577 close-menu)))579 (defn-memo get-lots-of-money580 "Sell 0xFE cancel buttons to make a tremendous amount of money."581 ([] (get-lots-of-money (corrupt-item-list)))582 ([script]583 (->> script584 (first-difference [] [:a] AF) ; talk to shopkeep585 (repeat-until-different [] list-offset)586 (set-cursor 1)587 select-menu-entry588 (repeat-until-different [] list-offset)589 select-menu-entry590 (set-quantity 0xFF 0xF7)591 (first-difference [] [:a] AF)592 select-menu-entry593 close-menu)))595 (defn note [str script]596 (println str) script)598 (defn-memo buy-bootstrapping-items599 "Buy items that will become part of the bootstrapping600 program."601 ([] (buy-bootstrapping-items (get-lots-of-money)))602 ([script]603 (->> script604 close-menu605 select-menu-entry606 (purchase-item 1) ; buying a pokeball overflows607 ; the item-counter from 0xFF to 0x00608 ; repairing the item-list.609 (set-cursor 1)610 (purchase-item 1) ; these other items are here to611 ; protect the burn heals when the612 (set-cursor 2) ; item list is corrupted again.613 (purchase-item 1)615 (set-cursor 3)616 (purchase-item 1)618 (set-cursor 4) ; 95 burn-heals spells out the619 (purchase-item 96) ; return address to the pokemon620 ; kernel. 96 so that they can be621 ; deposited without causing a shift.623 close-menu ; stop talking to shopkeep624 (wait-until select-menu-entry)625 (play-moves [[:b]])626 end-text)))628 (defn-memo corrupt-item-list-again629 ([] (corrupt-item-list-again (buy-bootstrapping-items)))630 ([script]631 (->> script632 activate-start-menu633 (set-cursor-relative 0)634 select-menu-entry636 ;; repair list-offset for pokemon-list637 (set-cursor-relative -1)639 (set-cursor 4) ; switching it to640 select-menu-entry ; tenth place.641 (set-cursor 1)642 select-menu-entry ; select "switch" on 5th644 (repeat-until-different [] list-offset)645 (set-cursor 9) ; goto 10th pokemon646 select-menu-entry ; do switch647 close-menu648 close-menu)))650 (defn-memo leave-viridian-store651 ([] (leave-viridian-store (corrupt-item-list-again)))652 ([script]653 (->> script654 ;; leave store655 (walk [↓ ↓ → ↓]))))657 (defn force-encounter [direction script]658 (delayed-improbability-search659 600660 #(search-string % "Wild")661 (partial move direction) script))663 (defn-memo fight-wild-pokemon664 ([] (fight-wild-pokemon (leave-viridian-store)))665 ([script]666 (->> script667 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓668 ← ← ← ← ← ← ← ←669 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])670 (force-encounter →))))672 (defn-memo run-from-pokemon673 ([] (run-from-pokemon (fight-wild-pokemon)))674 ([script]675 (->> script676 (scroll-text)677 (play-moves [[:a]])678 (wait-until select-menu-entry)679 (set-cursor 1)680 (first-difference [] → AF)681 (scroll-text)682 (scroll-text))))684 (defn-memo to-poke-center-computer685 ([] (to-poke-center-computer686 (run-from-pokemon)))687 ([script]688 (->> script689 (walk-thru-grass [→ → ↑])690 (walk [↑ ← ← ←691 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑692 ← ←693 ↑ ↑ ↑ ↑694 → → → → ↑])695 (walk [→ →696 ↑ ↑ ↑697 → → → → → → → → →])698 (first-difference [] ↑ AF))))700 (defn-memo begin-deposits701 ([] (begin-deposits702 (to-poke-center-computer)))703 ([script]704 (->> script705 ;; access PC706 (scroll-text 2)708 ;; access item storage709 (menu [[:a] [:d] [:a]])710 (scroll-text 2)712 ;; begin deposit713 (menu [[:d] [:a]])714 (do-nothing 40))))716 (defn deposit-n-items717 [n script]718 (->> script719 (do-nothing 100)720 (play-moves [[:a]])721 (do-nothing 80)722 (multiple-times723 (dec n)724 (fn [script]725 (->> script726 (play-moves [[:u]])727 (do-nothing 1))))728 (play-moves [[:a]])729 (scroll-text)))731 (defn deposit-one-item732 [script]733 (->> script734 (do-nothing 100)735 (play-moves [[:a]])736 (do-nothing 80)737 (play-moves [[:a]])738 (scroll-text)))740 (defn-memo create-header741 ([] (create-header (begin-deposits)))742 ([script]743 (->> script744 (multiple-times 33 deposit-one-item)745 (do-nothing 1))))747 (defn bootstrap-init []748 [(read-moves "bootstrap-init")749 (read-state "bootstrap-init")])751 (defn create-bootstrap-program752 ([] (create-bootstrap-program753 (create-header)))754 ([script]755 (->> script756 (do-nothing 120)757 (menu [↓ ↓ ↓ ↓ ↓ ↓ ↓])758 ;;(deposit-n-items 33)760 (menu (repeat 17 ↓))764 (do-nothing 1))))767 (defn test-pc-item-program []768 (-> (read-state "bootstrap-init")769 (set-memory pc-item-list-start 50)770 (set-memory-range771 map-function-address-start [0x8B 0xD5])772 (set-memory-range773 (inc pc-item-list-start)774 (flatten775 [(repeat776 28777 [0xFF 0x01])778 [;; second part of item manipulation program779 0x00 ;; this starts at address 0xD56C780 0x2A ;; save (HL)=(target) to A, increment HL782 0x00783 0x47 ;; save A to B785 0x00786 0x3A ;; save (target+1) to A, decrement HL788 0x00789 0x22 ;; A -> target, increment HL [(target+1) -> target]791 0x00792 0x70 ;; load B into target+1 [(target) -> target+1]794 0x00795 0xC3 ;; first part of absolute jump797 0x0C ;; return control to pokemon kernel798 0x5F]799 (repeat800 5801 [0xFF 0x01])803 [;; first part of item manipulation program804 0x00805 0x21 ;; load target into HL807 0x94 ;; this is the target address808 0xD5810 0x00 ;; relative jump back to first part811 0x18813 0xE1 ;; of program814 0x01816 0xFF ;; spacer817 0x01819 0x04 ;; target ID (pokeball)820 0x3E ;; target Quantity (lemonade)821 ]]))))827 (defn basic-writer [target-address limit return-address]828 (let [[target-high target-low] (disect-bytes-2 target-address)829 [return-high return-low] (disect-bytes-2 return-address)]830 (flatten831 [0xF3 ;; disable interrupts833 0x1E ;; load limit into E834 limit836 0x21 ;; load target into HL837 target-low838 target-high840 ;; load 1 into C.841 0x0E ;; C == 1 means input-first nybble842 0x01 ;; C == 0 means input-second nybble844 ;; Input Section846 0x3E ;; load 0x20 into A, to measure dpad847 0x20849 0xE0 ;; load A into [FF00]850 0x00852 0xF0 ;; load 0xFF00 into A to get853 0x00 ;; d-pad presses855 0xE6856 0x0F ;; select bottom four bits of A858 0xB8 ;; see if input is different (CP A B)860 0x28 ;; repeat above steps if input is not different861 ;; (jump relative backwards if B != A)862 0xF5 ;; (literal -11)864 0x47 ;; load A into B866 0x0D ;; dec C867 ;; branch based on C:868 0x20 ;; JR NZ869 0x07 ;; skip "input first nybble" below872 ;; input first nybble874 0xCB875 0x37 ;; swap nybbles on A877 0x57 ;; A -> D879 0x18880 0xEC ;; literal -20 -- go back to input section882 ;; input second nybble884 0x0C ;; inc C886 0xE6 ;; select bottom bits887 0x0F889 0xB2 ;; (OR A D) -> A891 0x22 ;; (do (A -> (HL)) (INC HL))893 0x1D ;; (DEC E)895 0x20 ;; jump back to input section if not done896 0xE4 ;; literal -28898 0xFB ;; re-enable interrupts900 0xC3901 return-low902 return-high ])))905 (defn test-basic-writer []906 (-> (read-state "bootstrap-init")907 (set-memory pc-item-list-start 50)908 (set-memory-range909 map-function-address-start910 (reverse (disect-bytes-2 (inc pc-item-list-start))))911 (set-memory-range912 (inc pc-item-list-start)913 (basic-writer 0xD162 10 0x5F0C))))915 (defn debug-basic-writer []916 (PC! (test-basic-writer) (inc pc-item-list-start)))918 (defn d-ticks [state n]919 (reduce (fn [state _] (d-tick state))920 state (range n)))922 (defn d-print [state message]923 (println message) state)925 (defn dddd926 []927 (-> (debug-basic-writer)928 (d-ticks 20)929 (set-memory 0xFF00 0xFF)930 (d-print "============== second cycle")931 (d-ticks 14)932 (d-print "============== end")933 (d-ticks 20)))935 ;;TMs at celadon store ---936 ;;01 (any-number) mega punch937 ;;02 (any-number) razor wind938 ;;05 (any-number) mega kick939 ;;07 (any-number) hyper beam940 ;;09 (any-number) take down941 ;;13 (only 1) ice beam942 ;;17 (any-number) submission943 ;;18 (only 1) counter944 ;;32 (any-number) double team945 ;;33 (any-number) reflect946 ;;37 (any-number) egg bomb947 ;;48 (only 1) rock slide948 ;;49 (only 1) tri attack951 ;; no-ops952 ;; 0x00953 ;; 0xB8 - 0xBF (compares) :garbage954 ;; 0x3F clear carry flag :s.s.ticket955 ;; 0x37 set carry flag :guard-spec [!]956 ;; 0x33 increment SP :poke-doll [!]957 ;; 0x3B decrement SP :coin959 ;;0x7F A->A :garbage960 ;;0x40 B->B :gold-teeth961 ;;0x49 C->C :poke-flute962 ;;0x52 D->D :elixer963 ;;0x5B E->E :garbage964 ;;0x6D L->L :garbage965 ;;0x64 H->H :garbage968 ;;0xC5 push BC :HM02969 ;;0xD5 push DE :TM13 (ice-beam)970 ;;0xE5 push HL :TM29 (psychic)971 ;;0xF5 push AF :TM45 (thunder-wave)973 ;; 0xA7 (AND A A) :garbage974 ;; 0xB7 (OR A A) :garbage976 ;; 0x2F (CPL A) :leaf-stone979 (defn item-writer980 "This is the basic writer, optimized to be made of valid981 item-quantity pairs."982 [target-address limit return-address]983 (let [[target-high target-low] (disect-bytes-2 target-address)984 [return-high return-low] (disect-bytes-2 return-address)]985 (flatten986 [987 ;;0xC5 ;; push junk onto stack988 ;;0xD5989 ;;0xE5990 ;;0xF5991 0x33 ;; (item-hack) set increment stack pointer no-op992 0x1E ;; load limit into E993 limit994 0x3F ;; (item-hack) set carry flag no-op996 ;; load 2 into C.997 0x0E ;; C == 1 means input-first nybble998 0x04 ;; C == 0 means input-second nybble1000 0x21 ;; load target into HL1001 target-low1002 target-high1003 0x37 ;; (item-hack) set carry flag no-op1005 0x2F ;; (item-hack) cpl A1006 0x2F ;; (item-hack) cpl A --together a spacer no-op1008 0x00 ;; (item-hack) no-op1009 0xF3 ;; disable interrupts1010 ;; Input Section1012 0x3E ;; load 0x20 into A, to measure buttons1013 0x101015 0x00 ;; (item-hack) no-op1016 0xE0 ;; load A into [FF00]1017 0x001019 0xF0 ;; load 0xFF00 into A to get1020 0x00 ;; button presses1022 0xE61023 0x0F ;; select bottom four bits of A1024 0x37 ;; (item-hack) set carry flag no-op1026 0x00 ;; (item-hack) no-op1027 0xB8 ;; see if input is different (CP A B)1029 0x00 ;; (item-hack) (INC SP)1030 0x28 ;; repeat above steps if input is not different1031 ;; (jump relative backwards if B != A)1032 0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)1034 0x47 ;; load A into B1036 0x0D ;; dec C1037 0x37 ;; (item-hack) set-carry flag1038 ;; branch based on C:1039 0x20 ;; JR NZ1040 23 ;; skip "input second nybble" and "jump to target" below1042 ;; input second nybble1044 0x0C ;; inc C1045 0x0C ;; inc C1047 0x00 ;; (item-hack) no-op1048 0xE6 ;; select bottom bits1049 0x0F1050 0x37 ;; (item-hack) set-carry flag no-op1052 0x00 ;; (item-hack) no-op1053 0xB2 ;; (OR A D) -> A1055 0x22 ;; (do (A -> (HL)) (INC HL))1057 0x1D ;; (DEC E)1059 0x00 ;; (item-hack)1060 0x20 ;; jump back to input section if not done1061 0xDA ;; literal -36 == TM 18 (counter)1062 0x01 ;; (item-hack) set BC to literal (no-op)1064 ;; jump to target1065 0x00 ;; (item-hack) these two bytes can be anything.1066 0x011068 0x00 ;; (item-hack) no-op1069 0xBF ;; (CP A A) ensures Z1071 0xCA ;; (item-hack) jump if Z1072 return-low1073 return-high1074 0x01 ;; (item-hack) will never be reached.1078 ;; input first nybble1079 0x001080 0xCB1081 0x37 ;; swap nybbles on A1083 0x57 ;; A -> D1085 0x37 ;; (item-hack) set carry flag no-op1086 0x18 ;; relative jump backwards1087 0xCD ;; literal -51 == TM05; go back to input section1088 0x01 ;; (item-hack) will never reach this instruction1090 ])))1092 (defn test-item-writer []1093 (-> (read-state "bootstrap-init")1094 (set-memory pc-item-list-start 50)1095 (set-memory-range1096 map-function-address-start1097 (reverse (disect-bytes-2 (inc pc-item-list-start))))1098 (set-memory-range1099 (inc pc-item-list-start)1100 (item-writer 0xD162 201 0xD162))))1102 (defn item-writer-state []1103 (read-state "item-writer"))1105 (defn test-item-writer-2 []1106 (let [orig (item-writer-state)]1107 (-> orig1108 (print-listing 0xD162 (+ 0xD162 20))1109 (run-moves (reduce concat1110 (repeat 10 [[:a :b :start :select] []])))1111 ((fn [_] (println "===========") _))1112 (print-listing 0xD162 (+ 0xD162 20)))))