view clojure/com/aurellem/run/bootstrap_0.clj @ 373:a6f212ae29a3

Found some tile sprites for printable characters. Neato.
author Dylan Holmes <ocsenave@gmail.com>
date Mon, 09 Apr 2012 03:46:04 -0500
parents 3b3cd62b6106
children 08f8284e2f1b
line wrap: on
line source
1 (ns com.aurellem.run.bootstrap-0
2 (:use (com.aurellem.gb saves gb-driver util
3 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-name
12 ([] (to-rival-name (boot-root)))
13 ([script]
14 (->> script
15 title
16 oak
17 name-entry-rlm
18 (scroll-text 5))))
20 (defn-memo name-rival-bootstrap
21 ([] (name-rival-bootstrap (to-rival-name)))
22 ([script]
23 (->> script
24 (first-difference [] [:a] AF)
25 (first-difference [] [:r] DE)
26 (play-moves
27 [[]
28 [] [] [:r] [] [:d] [:a] ;; L
29 [:r] [] [:r] [] [:r] [] [:r] []
30 [:r] [] [:d] [] [:d] [:a] ;; [PK]
31 [:u] [] [:l] [] [:l] []
32 [:l] [] [:l] [] [:l] [:a] ;; U
33 [:r] [] [:r] [] [:r] []
34 [:r] [] [:r] [] [:d] [:a] ;; [PK]
35 [] [:a] ;; [PK]
36 [] [:a] ;; [PK]
37 [:r] [] [:d] [:a] ;; END
38 ]))))
40 (defn-memo leave-house
41 ([] (leave-house (name-rival-bootstrap)))
42 ([script]
43 (->> script
44 finish-title
45 walk-to-stairs
46 walk-to-door
47 (walk [↓ ↓]))))
49 (defn-memo to-pallet-town-edge
50 ([] (to-pallet-town-edge (leave-house)))
51 ([script]
52 (->> script
53 (walk [→ → → → →
54 ↑ ↑ ↑ ↑ ↑ ↑]))))
56 (defn-memo start-pikachu-battle
57 ([] (start-pikachu-battle
58 (to-pallet-town-edge)))
59 ([script]
60 (->> script
61 (first-difference [:b] [:b :a] DE)
62 scroll-text
63 (do-nothing 200)
64 (play-moves [[:b]]))))
66 (defn-memo capture-pikachu
67 ([] (capture-pikachu (start-pikachu-battle)))
68 ([script]
69 (->> script
70 (scroll-text 3))))
72 (defn-memo go-to-lab
73 ([] (go-to-lab (capture-pikachu)))
74 ([script]
75 (->> script
76 end-text
77 (scroll-text 5)
78 end-text
79 ;; oak walks you to his lab; no input required.
80 (do-nothing 400))))
82 (defn-memo talk-to-oak-in-lab
83 ([] (talk-to-oak-in-lab (go-to-lab)))
84 ([script]
85 (->> script
86 (scroll-text 14)
87 end-text)))
89 (defn-memo try-to-get-eevee
90 ([] (try-to-get-eevee (talk-to-oak-in-lab)))
91 ([script]
92 (->> script
93 ;; walk to pokeball
94 (walk [↓ → →])
95 ;; and try to grab it
96 (play-moves
97 (concat [↑ ↑ [:a]]
98 (repeat 100 [])))
99 (scroll-text 10)
100 (end-text))))
102 (defn-memo obtain-pikachu
103 ([] (obtain-pikachu (try-to-get-eevee)))
104 ([script]
105 (->> script
106 (scroll-text 6)
107 (end-text))))
110 (defn-memo begin-battle-with-rival
111 ([] (begin-battle-with-rival
112 (obtain-pikachu)))
113 ([script]
114 (->> script
115 (walk [↓ ↓ ↓])
116 (scroll-text 3)
117 (end-text)
118 (scroll-text))))
120 (defn-memo defeat-eevee
121 ([] (defeat-eevee
122 (begin-battle-with-rival)))
123 ([script]
124 (->> script
125 (do-nothing 400)
126 (play-moves [[:a]])
127 (critical-hit)
128 (do-nothing 200)
129 (scroll-text 2) ;; for eevee's tail-whip
130 (do-nothing 10)
131 (play-moves [[:a]])
132 (critical-hit)
133 (do-nothing 200)
134 (scroll-text 2) ;; tail whip again
135 (do-nothing 10)
136 (play-moves [[:a]])
137 (critical-hit)
138 (do-nothing 200))))
140 (defn-memo finish-rival-text
141 ([] (finish-rival-text
142 (defeat-eevee)))
143 ([script]
144 (->> script
145 (scroll-text 12)
146 (end-text))))
148 (defn-memo pikachu-comes-out
149 ([] (pikachu-comes-out
150 (finish-rival-text)))
151 ([script]
152 (->> script
153 (scroll-text 8)
154 (end-text))))
156 (defn-memo leave-oaks-lab
157 ([] (leave-oaks-lab
158 (pikachu-comes-out)))
159 ([script]
160 (->> script
161 (walk [↓ ↓ ↓ ↓ ↓ ↓]))))
163 (defn-memo oaks-lab->pallet-town-edge
164 ([] (oaks-lab->pallet-town-edge
165 (leave-oaks-lab)))
166 ([script]
167 (->> script
168 (walk [← ← ←
169 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ →]))))
171 (defn-memo pallet-edge->viridian-mart
172 ([] (pallet-edge->viridian-mart true
173 (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 (->> script
183 ;; leave straight grass
184 (walk-thru-grass
185 [↑ ↑ ↑ ↑ ↑])
187 (walk [↑ ↑ ↑ ↑])
189 (walk-thru-grass
190 [← ← ↑])
192 (walk [↑ ↑ ↑ ↑ → → → ])
194 (walk-thru-grass
195 [→ ↑ ↑ ←])
197 (walk
198 [← ←
199 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
200 → → → → ])
202 ;; this part is dependent on that
203 ;; stupid NPC in the grass patch
204 (walk-thru-grass
205 (concat dodge-1
206 [↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ]))
208 (walk
209 (concat
210 dodge-2
211 [← ← ←
212 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
213 ← ←
214 ↑ ↑ ↑ ↑
215 → → → → → → → → → →
216 ↑ ↑ ↑ ↑ ↑ ↑ ↑]))))))
218 (defn-memo get-oaks-parcel
219 ([] (get-oaks-parcel
220 (pallet-edge->viridian-mart)))
221 ([script]
222 (->> script
223 (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-lab
231 ([] (viridian-store->oaks-lab
232 (get-oaks-parcel)))
233 ([script]
234 (->> script
235 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
236 ← ← ← ← ← ← ← ← ←
237 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
238 ← ←
239 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
240 ↓ ↓ ↓ ↓ ↓ ↓ ↓
241 → → → → → → → →
242 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
243 ← ← ← ← ←
244 ↓ ↓ ↓ ↓
245 ])
246 (walk-thru-grass
247 [↓ ↓ ↓ ↓ ↓ ↓ ↓])
248 (walk [↓ ↓ ← ↓ ↓ ↓ ←
249 ↓ ↓ ↓ ↓ ↓ ↓
250 → → → ↑])
252 (do-nothing 1))))
255 (defn-memo viridian-store->oaks-lab-like-a-boss
256 ([] (viridian-store->oaks-lab-like-a-boss
257 (get-oaks-parcel)))
258 ([script]
259 (->> script
260 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
261 ← ← ← ← ← ← ← ← ←
262 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])
264 (walk-thru-grass
265 [↓ ↓ ↓ ↓ ↓])
267 (walk
268 [↓ ↓ ← ↓
269 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
270 → → → ↓])
272 (walk-thru-grass
273 [↓ ↓ ↓])
275 (walk [↓ ← ← ↓ ↓ ↓ ↓ ↓ ↓])
277 (walk-thru-grass
278 [↓ ↓ ↓ ↓ ↓ ↓])
280 (walk [↓ ↓ ↓ ← ↓ ↓ ↓
281 ↓ ↓ ↓ ↓ ↓
282 → → → ↑]))))
284 (defn-memo deliver-oaks-parcel
285 ([] (deliver-oaks-parcel
286 (viridian-store->oaks-lab-like-a-boss)))
287 ([script]
288 (->> script
289 (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-mart
307 ([] (return-to-viridian-mart
308 (deliver-oaks-parcel)))
309 ([script]
310 (->> script
311 oaks-lab->pallet-town-edge
312 (pallet-edge->viridian-mart false))))
314 (defn-memo walk-to-counter
315 ([] (walk-to-counter
316 (return-to-viridian-mart)))
317 ([script]
318 (->> script
319 (walk [↑ ↑ ←]))))
323 ;; useful addresses
324 52262 ;; --- current-cursor-offset
325 52278 ;; --- current screen-offset
328 (defn exp-item-list []
329 (clojure.pprint/pprint
330 (apply harmonic-compare
331 (map read-state
332 ["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 position
341 ;; for all lists in the game (start list, pokemon list, shop
342 ;; lists, inventory lists, battle list, basically
343 ;; everything!)
345 (def list-cursor-offset-address 52262)
346 (def list-screen-offset-address 52278)
348 (defn list-offset
349 ([^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/pprint
357 (apply memory-compare
358 (map read-state
359 ["1-item"
360 "2-items"
361 "3-items"
362 "4-items"
363 ]))))
365 (def item-quantity-selected-address 65432)
367 (defn item-quantity-selected
368 ([^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 set-cursor-relative
374 "Assumes the arrow keys currently control the cursor.
375 Moves the cursor n steps relative to its current
376 position."
377 [n script]
378 (let [key (if (< 0 n) ↓ ↑)]
379 (multiple-times
380 (Math/abs n)
381 (partial first-difference
382 [] key list-offset)
383 script)))
385 (defn set-cursor
386 "Assumes the arrow keys currently control the cursor. Sets
387 the cursor to the desired position. Works for any menu
388 that uses a cursor including the start menu, item menu,
389 pokemon menu, and battle menu."
390 [n [moves state :as script]]
391 (let [current-position (list-offset state)
392 difference (- n current-position)]
393 (println difference)
394 (set-cursor-relative difference script)))
396 (defn set-quantity
397 "Set the quantity of an item to buy or sell to the desired value
398 using the fewest possible button presses."
399 ([total-quantity desired-quantity [moves state :as script]]
400 (cond (= desired-quantity 1) (do (println "1 of 1") script)
401 (= total-quantity desired-quantity)
402 (do (println "get everything!")
403 (delayed-difference [] ↓ 5 item-quantity-selected
404 script))
405 true
406 (let [current-quantity (item-quantity-selected state)
407 loop-point (if (= 0 total-quantity) 0x100 total-quantity)
408 distance (- desired-quantity current-quantity)
409 loop-distance (int(* -1 (Math/signum (float distance))
410 (- loop-point (Math/abs distance))))
411 best-path (first (sort-by #(Math/abs %)
412 [distance loop-distance]))
413 direction (if (< 0 best-path) ↑ ↓)]
414 (println "best-path" best-path)
415 (println "current-quantity" current-quantity)
416 (println "desired-quantity" desired-quantity)
417 (println "options" [distance loop-distance])
418 (reduce
419 (fn [script _]
420 (delayed-difference [] direction 5 item-quantity-selected
421 script))
422 script
423 (range (Math/abs best-path))))))
424 ([desired-quantity [moves state :as script]]
425 (set-quantity 99 desired-quantity script)))
427 (defn activate-start-menu [script]
428 (first-difference [:b] [:b :start] AF script))
430 (defn wait-until [script-fn script]
431 (let [wait-time
432 (- (dec (count (first (script-fn script))))
433 (count (first script)))]
434 (println "wait-time" wait-time)
435 (do-nothing wait-time script)))
437 (defn select-menu-entry
438 ([test-direction [moves state :as script]]
439 (->> script
440 (wait-until (partial set-cursor-relative test-direction))
441 (play-moves [[] [:a] []])))
442 ([[moves state :as script]]
443 (select-menu-entry
444 1 script)))
446 (defn restart
447 "The two button presses after a restart event are converted to
448 blanks. Due to weirdness with the VBM format. To compensate, ensure
449 that the two button presses after restart are both blanks."
450 [script]
451 (play-moves [[:restart] [] []] script))
453 (defn-memo do-save-corruption
454 ([] (do-save-corruption
455 (walk-to-counter)))
456 ([script] (do-save-corruption 4 script))
457 ([n script]
458 (->> script
459 activate-start-menu
460 (set-cursor n)
461 select-menu-entry
462 select-menu-entry
463 (play-moves
464 ;; this section is copied from speedrun-2942 and corrupts
465 ;; the save so that the total number of pokemon is set to
466 ;; 0xFF, allowing manipulation of non-pokemon data in RAM
467 ;; via the pokemon interface.
468 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
469 [] [] [] [] [] [] [] [] [] [] [] []])
470 (restart)
471 (title)
472 (first-difference [] [:start] AF)
473 (first-difference [] [:a] AF))))
475 (defn gen-corrupted-checkpoint! []
476 (let [[cor-moves cor-save] (do-save-corruption)]
477 (write-moves! cor-moves "cor-checkpoint")
478 (write-state! cor-save "cor-checkpoint")))
480 (defn corrupted-checkpoint []
481 [(read-moves "cor-checkpoint")
482 (read-state "cor-checkpoint")])
484 (def menu do-nothing )
487 (defn investivate-close-menu []
488 (clojure.pprint/pprint
489 (apply harmonic-compare
490 (map read-state
491 ["start-up-1"
492 "start-down-1"
493 ;;"start-up-2"
494 ;;"start-down-2"
495 ;;"start-up-3"
496 ;;"start-down-3"
497 ;;"computer-up-1"
498 ;;"computer-down-2"
499 "computer-up-2"
500 "computer-down-2"
501 "pokemon-up-1"
502 "pokemon-down-1"
503 "pokemon-up-2"
504 "pokemon-down-2"
505 "item-up-1"
506 "item-down-1"
507 "save-up-1"
508 "save-down-1"
509 "item-nest-up-1"
510 "item-nest-down-1"]))))
512 (def list-nesting-depth-address 50339)
514 (defn current-depth
515 ([^SaveState state] (aget (memory state) list-nesting-depth-address))
516 ([] (current-depth @current-state)))
519 (defn close-menu [script]
520 (delayed-difference
521 [] [:b] 50
522 current-depth
523 script))
526 (defn purchase-item
527 "Assumes that the cursor is over the desired item, and purchases
528 quantity of that item."
529 [n script]
530 (->> script
531 select-menu-entry
532 (set-quantity n)
533 (first-difference [] [:a] AF)
534 scroll-text
535 select-menu-entry
536 scroll-text))
538 (defn-memo corrupt-item-list
539 "Corrupt the num-of-items variable by switching a corrupted pokemon
540 into out-of-bounds memory."
541 ([] (corrupt-item-list
542 ;;(corrupted-checkpoint)
543 (do-save-corruption)
544 ))
545 ([script] (corrupt-item-list 1))
546 ([n script]
547 (->> script
548 activate-start-menu
549 (set-cursor n) ; select "POKEMON"
550 select-menu-entry ; from main menu.
551 (set-cursor 5) ; select 6th pokemon
552 select-menu-entry
553 (set-cursor 1)
554 select-menu-entry
555 (repeat-until-different [] list-offset)
556 (set-cursor 9)
557 select-menu-entry ; switch 6th with 10th
558 close-menu
559 close-menu)))
561 (defn-memo get-lots-of-money
562 "Sell 0xFE cancel buttons to make a tremendous amount of money."
563 ([] (get-lots-of-money (corrupt-item-list)))
564 ([script]
565 (->> script
566 (first-difference [] [:a] AF) ; talk to shopkeep
567 (repeat-until-different [] list-offset)
568 (set-cursor 1)
569 select-menu-entry
570 (repeat-until-different [] list-offset)
571 select-menu-entry
572 (set-quantity 0xFF 0xF7)
573 (first-difference [] [:a] AF)
574 select-menu-entry
575 close-menu)))
577 (defn note [str script]
578 (println str) script)
580 (defn-memo buy-bootstrapping-items
581 "Buy items that will become part of the bootstrapping
582 program."
583 ([] (buy-bootstrapping-items (get-lots-of-money)))
584 ([script]
585 (->> script
586 close-menu
587 select-menu-entry
588 (purchase-item 1) ; buying a pokeball overflows
589 ; the item-counter from 0xFF to 0x00
590 ; repairing the item-list.
591 (set-cursor 1)
592 (purchase-item 1) ; these other items are here to
593 ; protect the burn heals when the
594 (set-cursor 2) ; item list is corrupted again.
595 (purchase-item 1)
597 (set-cursor 3)
598 (purchase-item 1)
600 (set-cursor 4) ; 95 burn-heals spells out the
601 (purchase-item 96) ; return address to the pokemon
602 ; kernel. 96 so that they can be
603 ; deposited without causing a shift.
605 close-menu ; stop talking to shopkeep
606 (wait-until select-menu-entry)
607 (play-moves [[:b]])
608 end-text)))
610 (defn-memo corrupt-item-list-again
611 ([] (corrupt-item-list-again (buy-bootstrapping-items)))
612 ([script]
613 (->> script
614 activate-start-menu
615 (set-cursor-relative 0)
616 select-menu-entry
618 ;; repair list-offset for pokemon-list
619 (set-cursor-relative -1)
621 (set-cursor 4) ; switching it to
622 select-menu-entry ; tenth place.
623 (set-cursor 1)
624 select-menu-entry ; select "switch" on 5th
626 (repeat-until-different [] list-offset)
627 (set-cursor 9) ; goto 10th pokemon
628 select-menu-entry ; do switch
629 close-menu
630 close-menu)))
632 (defn-memo leave-viridian-store
633 ([] (leave-viridian-store (corrupt-item-list-again)))
634 ([script]
635 (->> script
636 ;; leave store
637 (walk [↓ ↓ → ↓]))))
639 (defn force-encounter [direction script]
640 (delayed-improbability-search
641 600
642 #(search-string % "Wild")
643 (partial move direction) script))
645 (defn-memo fight-wild-pokemon
646 ([] (fight-wild-pokemon (leave-viridian-store)))
647 ([script]
648 (->> script
649 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
650 ← ← ← ← ← ← ← ←
651 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])
652 (force-encounter →))))
654 (defn-memo run-from-pokemon
655 ([] (run-from-pokemon (fight-wild-pokemon)))
656 ([script]
657 (->> script
658 (scroll-text)
659 (play-moves [[:a]])
660 (wait-until select-menu-entry)
661 (set-cursor 1)
662 (first-difference [] → AF)
663 (scroll-text)
664 (scroll-text))))
666 (defn-memo to-poke-center-computer
667 ([] (to-poke-center-computer
668 (run-from-pokemon)))
669 ([script]
670 (->> script
671 (walk-thru-grass [→ → ↑])
672 (walk [↑ ← ← ←
673 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
674 ← ←
675 ↑ ↑ ↑ ↑
676 → → → → ↑])
677 (walk [→ →
678 ↑ ↑ ↑
679 → → → → → → → → →])
680 (first-difference [] ↑ AF))))
682 (defn-memo begin-deposits
683 ([] (begin-deposits
684 (to-poke-center-computer)))
685 ([script]
686 (->> script
687 ;; access PC
688 (scroll-text 2)
690 ;; access item storage
691 (menu [[:a] [:d] [:a]])
692 (scroll-text 2)
694 ;; begin deposit
695 (menu [[:d] [:a]])
696 (do-nothing 40))))
698 (defn deposit-n-items
699 [n script]
700 (->> script
701 (do-nothing 100)
702 (play-moves [[:a]])
703 (do-nothing 80)
704 (multiple-times
705 (dec n)
706 (fn [script]
707 (->> script
708 (play-moves [[:u]])
709 (do-nothing 1))))
710 (play-moves [[:a]])
711 (scroll-text)))
713 (defn deposit-one-item
714 [script]
715 (->> script
716 (do-nothing 100)
717 (play-moves [[:a]])
718 (do-nothing 80)
719 (play-moves [[:a]])
720 (scroll-text)))
722 (defn-memo create-header
723 ([] (create-header (begin-deposits)))
724 ([script]
725 (->> script
726 (multiple-times 33 deposit-one-item)
727 (do-nothing 1))))
729 (defn bootstrap-init []
730 [(read-moves "bootstrap-init")
731 (read-state "bootstrap-init")])
733 (defn create-bootstrap-program
734 ([] (create-bootstrap-program
735 (create-header)))
736 ([script]
737 (->> script
738 (do-nothing 120)
739 (menu [↓ ↓ ↓ ↓ ↓ ↓ ↓])
740 ;;(deposit-n-items 33)
742 (menu (repeat 17 ↓))
746 (do-nothing 1))))
749 (defn test-pc-item-program []
750 (-> (read-state "bootstrap-init")
751 (set-memory pc-item-list-start 50)
752 (set-memory-range
753 map-function-address-start [0x8B 0xD5])
754 (set-memory-range
755 (inc pc-item-list-start)
756 (flatten
757 [(repeat
758 28
759 [0xFF 0x01])
760 [;; second part of item manipulation program
761 0x00 ;; this starts at address 0xD56C
762 0x2A ;; save (HL)=(target) to A, increment HL
764 0x00
765 0x47 ;; save A to B
767 0x00
768 0x3A ;; save (target+1) to A, decrement HL
770 0x00
771 0x22 ;; A -> target, increment HL [(target+1) -> target]
773 0x00
774 0x70 ;; load B into target+1 [(target) -> target+1]
776 0x00
777 0xC3 ;; first part of absolute jump
779 0x0C ;; return control to pokemon kernel
780 0x5F]
781 (repeat
782 5
783 [0xFF 0x01])
785 [;; first part of item manipulation program
786 0x00
787 0x21 ;; load target into HL
789 0x94 ;; this is the target address
790 0xD5
792 0x00 ;; relative jump back to first part
793 0x18
795 0xE1 ;; of program
796 0x01
798 0xFF ;; spacer
799 0x01
801 0x04 ;; target ID (pokeball)
802 0x3E ;; target Quantity (lemonade)
803 ]]))))
809 (defn basic-writer [target-address limit return-address]
810 (let [[target-high target-low] (disect-bytes-2 target-address)
811 [return-high return-low] (disect-bytes-2 return-address)]
812 (flatten
813 [0xF3 ;; disable interrupts
815 0x1E ;; load limit into E
816 limit
818 0x21 ;; load target into HL
819 target-low
820 target-high
822 ;; load 1 into C.
823 0x0E ;; C == 1 means input-first nybble
824 0x01 ;; C == 0 means input-second nybble
826 ;; Input Section
828 0x3E ;; load 0x20 into A, to measure dpad
829 0x20
831 0xE0 ;; load A into [FF00]
832 0x00
834 0xF0 ;; load 0xFF00 into A to get
835 0x00 ;; d-pad presses
837 0xE6
838 0x0F ;; select bottom four bits of A
840 0xB8 ;; see if input is different (CP A B)
842 0x28 ;; repeat above steps if input is not different
843 ;; (jump relative backwards if B != A)
844 0xF5 ;; (literal -11)
846 0x47 ;; load A into B
848 0x0D ;; dec C
849 ;; branch based on C:
850 0x20 ;; JR NZ
851 0x07 ;; skip "input first nybble" below
854 ;; input first nybble
856 0xCB
857 0x37 ;; swap nybbles on A
859 0x57 ;; A -> D
861 0x18
862 0xEC ;; literal -20 -- go back to input section
864 ;; input second nybble
866 0x0C ;; inc C
868 0xE6 ;; select bottom bits
869 0x0F
871 0xB2 ;; (OR A D) -> A
873 0x22 ;; (do (A -> (HL)) (INC HL))
875 0x1D ;; (DEC E)
877 0x20 ;; jump back to input section if not done
878 0xE4 ;; literal -28
880 0xFB ;; re-enable interrupts
882 0xC3
883 return-low
884 return-high ])))
887 (defn test-basic-writer []
888 (-> (read-state "bootstrap-init")
889 (set-memory pc-item-list-start 50)
890 (set-memory-range
891 map-function-address-start
892 (reverse (disect-bytes-2 (inc pc-item-list-start))))
893 (set-memory-range
894 (inc pc-item-list-start)
895 (basic-writer 0xD162 10 0x5F0C))))
897 (defn debug-basic-writer []
898 (PC! (test-basic-writer) (inc pc-item-list-start)))
900 (defn d-ticks [state n]
901 (reduce (fn [state _] (d-tick state))
902 state (range n)))
904 (defn d-print [state message]
905 (println message) state)
907 (defn dddd
908 []
909 (-> (debug-basic-writer)
910 (d-ticks 20)
911 (set-memory 0xFF00 0xFF)
912 (d-print "============== second cycle")
913 (d-ticks 14)
914 (d-print "============== end")
915 (d-ticks 20)))
917 ;;TMs at celadon store ---
918 ;;01 (any-number) mega punch
919 ;;02 (any-number) razor wind
920 ;;05 (any-number) mega kick
921 ;;07 (any-number) hyper beam
922 ;;09 (any-number) take down
923 ;;13 (only 1) ice beam
924 ;;17 (any-number) submission
925 ;;18 (only 1) counter
926 ;;32 (any-number) double team
927 ;;33 (any-number) reflect
928 ;;37 (any-number) egg bomb
929 ;;48 (only 1) rock slide
930 ;;49 (only 1) tri attack
933 ;; no-ops
934 ;; 0x00
935 ;; 0xB8 - 0xBF (compares) :garbage
936 ;; 0x3F clear carry flag :s.s.ticket
937 ;; 0x37 set carry flag :guard-spec [!]
938 ;; 0x33 increment SP :poke-doll [!]
939 ;; 0x3B decrement SP :coin
941 ;;0x7F A->A :garbage
942 ;;0x40 B->B :gold-teeth
943 ;;0x49 C->C :poke-flute
944 ;;0x52 D->D :elixer
945 ;;0x5B E->E :garbage
946 ;;0x6D L->L :garbage
947 ;;0x64 H->H :garbage
950 ;;0xC5 push BC :HM02
951 ;;0xD5 push DE :TM13 (ice-beam)
952 ;;0xE5 push HL :TM29 (psychic)
953 ;;0xF5 push AF :TM45 (thunder-wave)
955 ;; 0xA7 (AND A A) :garbage
956 ;; 0xB7 (OR A A) :garbage
958 ;; 0x2F (CPL A) :leaf-stone
961 (defn item-writer
962 "This is the basic writer, optimized to be made of valid
963 item-quantity pairs."
964 [target-address limit return-address]
965 (let [[target-high target-low] (disect-bytes-2 target-address)
966 [return-high return-low] (disect-bytes-2 return-address)]
967 (flatten
968 [
969 ;;0xC5 ;; push junk onto stack
970 ;;0xD5
971 ;;0xE5
972 ;;0xF5
973 0x33 ;; (item-hack) set increment stack pointer no-op
974 0x1E ;; load limit into E
975 limit
976 0x3F ;; (item-hack) set carry flag no-op
978 ;; load 2 into C.
979 0x0E ;; C == 1 means input-first nybble
980 0x04 ;; C == 0 means input-second nybble
982 0x21 ;; load target into HL
983 target-low
984 target-high
985 0x37 ;; (item-hack) set carry flag no-op
987 0x2F ;; (item-hack) cpl A
988 0x2F ;; (item-hack) cpl A --together a spacer no-op
990 0x00 ;; (item-hack) no-op
991 0xF3 ;; disable interrupts
992 ;; Input Section
994 0x3E ;; load 0x20 into A, to measure buttons
995 0x10
997 0x00 ;; (item-hack) no-op
998 0xE0 ;; load A into [FF00]
999 0x00
1001 0xF0 ;; load 0xFF00 into A to get
1002 0x00 ;; button presses
1004 0xE6
1005 0x0F ;; select bottom four bits of A
1006 0x37 ;; (item-hack) set carry flag no-op
1008 0x00 ;; (item-hack) no-op
1009 0xB8 ;; see if input is different (CP A B)
1011 0x00 ;; (item-hack) (INC SP)
1012 0x28 ;; repeat above steps if input is not different
1013 ;; (jump relative backwards if B != A)
1014 0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)
1016 0x47 ;; load A into B
1018 0x0D ;; dec C
1019 0x37 ;; (item-hack) set-carry flag
1020 ;; branch based on C:
1021 0x20 ;; JR NZ
1022 23 ;; skip "input second nybble" and "jump to target" below
1024 ;; input second nybble
1026 0x0C ;; inc C
1027 0x0C ;; inc C
1029 0x00 ;; (item-hack) no-op
1030 0xE6 ;; select bottom bits
1031 0x0F
1032 0x37 ;; (item-hack) set-carry flag no-op
1034 0x00 ;; (item-hack) no-op
1035 0xB2 ;; (OR A D) -> A
1037 0x22 ;; (do (A -> (HL)) (INC HL))
1039 0x1D ;; (DEC E)
1041 0x00 ;; (item-hack)
1042 0x20 ;; jump back to input section if not done
1043 0xDA ;; literal -36 == TM 18 (counter)
1044 0x01 ;; (item-hack) set BC to literal (no-op)
1046 ;; jump to target
1047 0x00 ;; (item-hack) these two bytes can be anything.
1048 0x01
1050 0x00 ;; (item-hack) no-op
1051 0xBF ;; (CP A A) ensures Z
1053 0xCA ;; (item-hack) jump if Z
1054 return-low
1055 return-high
1056 0x01 ;; (item-hack) will never be reached.
1060 ;; input first nybble
1061 0x00
1062 0xCB
1063 0x37 ;; swap nybbles on A
1065 0x57 ;; A -> D
1067 0x37 ;; (item-hack) set carry flag no-op
1068 0x18 ;; relative jump backwards
1069 0xCD ;; literal -51 == TM05; go back to input section
1070 0x01 ;; (item-hack) will never reach this instruction
1072 ])))
1074 (defn test-item-writer []
1075 (-> (read-state "bootstrap-init")
1076 (set-memory pc-item-list-start 50)
1077 (set-memory-range
1078 map-function-address-start
1079 (reverse (disect-bytes-2 (inc pc-item-list-start))))
1080 (set-memory-range
1081 (inc pc-item-list-start)
1082 (item-writer 0xD162 201 0xD162))))
1084 (defn item-writer-state []
1085 (read-state "item-writer"))
1087 (defn test-item-writer-2 []
1088 (let [orig (item-writer-state)]
1089 (-> orig
1090 (print-listing 0xD162 (+ 0xD162 20))
1091 (run-moves (reduce concat
1092 (repeat 10 [[:a :b :start :select] []])))
1093 ((fn [_] (println "===========") _))
1094 (print-listing 0xD162 (+ 0xD162 20)))))