view clojure/com/aurellem/run/bootstrap_0.clj @ 343:652c5abee892

constructing exact item-pc layout for item-writer.
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Apr 2012 08:27:55 -0500
parents 4e0b64ea7b66
children 9366539d29b6
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 (let [current-quantity (item-quantity-selected state)
401 loop-point (if (> total-quantity 99) 0xFF 99)
402 distance (- desired-quantity current-quantity)
403 loop-distance (int(* -1 (Math/signum (float distance))
404 (- loop-point (Math/abs distance))))
405 best-path (first (sort-by #(Math/abs %)
406 [distance loop-distance]))
407 direction (if (< 0 best-path) ↑ ↓)]
408 (println "best-path" best-path)
409 (reduce
410 (fn [script _]
411 (delayed-difference [] direction 5 item-quantity-selected
412 script))
414 script
415 (range (Math/abs best-path)))))
416 ([desired-quantity [moves state :as script]]
417 (set-quantity 99 desired-quantity script)))
419 (defn activate-start-menu [script]
420 (first-difference [:b] [:b :start] AF script))
422 (defn wait-until [script-fn script]
423 (let [wait-time
424 (- (dec (count (first (script-fn script))))
425 (count (first script)))]
426 (println "wait-time" wait-time)
427 (do-nothing wait-time script)))
429 (defn select-menu-entry [script]
430 (->> script
431 (wait-until (partial set-cursor-relative 1))
432 (play-moves [[:a] []])))
434 (defn restart
435 "The two button presses after a restart event are converted to
436 blanks. Due to weirdness with the VBM format. To compensate, ensure
437 that the two button presses after restart are both blanks."
438 [script]
439 (play-moves [[:restart] [] []] script))
441 (defn-memo do-save-corruption
442 ([] (do-save-corruption
443 (walk-to-counter)))
444 ([script]
445 (->> script
446 activate-start-menu
447 (set-cursor 4)
448 select-menu-entry
449 select-menu-entry
450 (play-moves
451 ;; this section is copied from speedrun-2942 and corrupts
452 ;; the save so that the total number of pokemon is set to
453 ;; 0xFF, allowing manipulation of non-pokemon data in RAM
454 ;; via the pokemon interface.
455 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
456 [] [] [] [] [] [] [] [] [] [] [] []])
457 (restart)
458 (title)
459 (first-difference [] [:start] AF)
460 (first-difference [] [:a] AF))))
462 (defn gen-corrupted-checkpoint! []
463 (let [[cor-moves cor-save] (do-save-corruption)]
464 (write-moves! cor-moves "cor-checkpoint")
465 (write-state! cor-save "cor-checkpoint")))
467 (defn corrupted-checkpoint []
468 [(read-moves "cor-checkpoint")
469 (read-state "cor-checkpoint")])
471 (def menu do-nothing )
473 (defn close-menu [script]
474 (first-difference [] [:b] AF script))
476 (defn purchase-item
477 "Assumes that the cursor is over the desired item, and purchases
478 quantity of that item."
479 [n script]
480 (->> script
481 select-menu-entry
482 (set-quantity n)
483 (first-difference [] [:a] AF)
484 scroll-text
485 select-menu-entry
486 scroll-text))
488 (defn-memo corrupt-item-list
489 "Corrupt the num-of-items variable by switching a corrupted pokemon
490 into out-of-bounds memory."
491 ([] (corrupt-item-list
492 ;;(corrupted-checkpoint)
493 (do-save-corruption)
494 ))
495 ([script]
496 (->> script
497 activate-start-menu
498 (set-cursor 1) ; select "POKEMON" from
499 select-menu-entry ; from main menu.
500 (set-cursor 5) ; select 6th pokemon
501 select-menu-entry
502 (set-cursor 1)
503 select-menu-entry
504 (repeat-until-different [] list-offset)
505 (set-cursor 9)
506 select-menu-entry ; switch 6th with 10th
507 close-menu
508 close-menu)))
510 (defn-memo get-lots-of-money
511 "Sell 0xFE cancel buttons to make a tremendous amount of money."
512 ([] (get-lots-of-money (corrupt-item-list)))
513 ([script]
514 (->> script
515 (first-difference [] [:a] AF) ; talk to shopkeep
516 (repeat-until-different [] list-offset)
517 (set-cursor 1)
518 select-menu-entry
519 (repeat-until-different [] list-offset)
520 select-menu-entry
521 (set-quantity 0xFF 0xF7)
522 (first-difference [] [:a] AF)
523 select-menu-entry
524 close-menu)))
526 (defn note [str script]
527 (println str) script)
529 (defn-memo buy-bootstrapping-items
530 "Buy items that will become part of the bootstrapping
531 program."
532 ([] (buy-bootstrapping-items (get-lots-of-money)))
533 ([script]
534 (->> script
535 close-menu
536 select-menu-entry
537 (purchase-item 1) ; buying a pokeball overflows
538 ; the item-counter from 0xFF to 0x00
539 ; repairing the item-list.
540 (set-cursor 1)
541 (purchase-item 1) ; these other items are here to
542 ; protect the burn heals when the
543 (set-cursor 2) ; item list is corrupted again.
544 (purchase-item 1)
546 (set-cursor 3)
547 (purchase-item 1)
549 (set-cursor 4) ; 95 burn-heals spells out the
550 (purchase-item 96) ; return address to the pokemon
551 ; kernel. 96 so that they can be
552 ; deposited without causing a shift.
554 close-menu ; stop talking to shopkeep
555 (wait-until select-menu-entry)
556 (play-moves [[:b]])
557 end-text)))
559 (defn-memo corrupt-item-list-again
560 ([] (corrupt-item-list-again (buy-bootstrapping-items)))
561 ([script]
562 (->> script
563 activate-start-menu
564 (set-cursor-relative 0)
565 select-menu-entry
567 ;; repair list-offset for pokemon-list
568 (set-cursor-relative -1)
570 (set-cursor 4) ; switching it to
571 select-menu-entry ; tenth place.
572 (set-cursor 1)
573 select-menu-entry ; select "switch" on 5th
575 (repeat-until-different [] list-offset)
576 (set-cursor 9) ; goto 10th pokemon
577 select-menu-entry ; do switch
578 close-menu
579 close-menu)))
581 (defn-memo leave-viridian-store
582 ([] (leave-viridian-store (corrupt-item-list-again)))
583 ([script]
584 (->> script
585 ;; leave store
586 (walk [↓ ↓ → ↓]))))
588 (defn force-encounter [direction script]
589 (delayed-improbability-search
590 600
591 #(search-string % "Wild")
592 (partial move direction) script))
594 (defn-memo fight-wild-pokemon
595 ([] (fight-wild-pokemon (leave-viridian-store)))
596 ([script]
597 (->> script
598 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
599 ← ← ← ← ← ← ← ←
600 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])
601 (force-encounter →))))
603 (defn-memo run-from-pokemon
604 ([] (run-from-pokemon (fight-wild-pokemon)))
605 ([script]
606 (->> script
607 (scroll-text)
608 (play-moves [[:a]])
609 (wait-until select-menu-entry)
610 (set-cursor 1)
611 (first-difference [] → AF)
612 (scroll-text)
613 (scroll-text))))
615 (defn-memo to-poke-center-computer
616 ([] (to-poke-center-computer
617 (run-from-pokemon)))
618 ([script]
619 (->> script
620 (walk-thru-grass [→ → ↑])
621 (walk [↑ ← ← ←
622 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
623 ← ←
624 ↑ ↑ ↑ ↑
625 → → → → ↑])
626 (walk [→ →
627 ↑ ↑ ↑
628 → → → → → → → → →])
629 (first-difference [] ↑ AF))))
631 (defn-memo begin-deposits
632 ([] (begin-deposits
633 (to-poke-center-computer)))
634 ([script]
635 (->> script
636 ;; access PC
637 (scroll-text 2)
639 ;; access item storage
640 (menu [[:a] [:d] [:a]])
641 (scroll-text 2)
643 ;; begin deposit
644 (menu [[:d] [:a]])
645 (do-nothing 40))))
647 (defn deposit-n-items
648 [n script]
649 (->> script
650 (do-nothing 100)
651 (play-moves [[:a]])
652 (do-nothing 80)
653 (multiple-times
654 (dec n)
655 (fn [script]
656 (->> script
657 (play-moves [[:u]])
658 (do-nothing 1))))
659 (play-moves [[:a]])
660 (scroll-text)))
662 (defn deposit-one-item
663 [script]
664 (->> script
665 (do-nothing 100)
666 (play-moves [[:a]])
667 (do-nothing 80)
668 (play-moves [[:a]])
669 (scroll-text)))
671 (defn-memo create-header
672 ([] (create-header (begin-deposits)))
673 ([script]
674 (->> script
675 (multiple-times 33 deposit-one-item)
676 (do-nothing 1))))
678 (defn bootstrap-init []
679 [(read-moves "bootstrap-init")
680 (read-state "bootstrap-init")])
682 (defn create-bootstrap-program
683 ([] (create-bootstrap-program
684 (create-header)))
685 ([script]
686 (->> script
687 (do-nothing 120)
688 (menu [↓ ↓ ↓ ↓ ↓ ↓ ↓])
689 ;;(deposit-n-items 33)
691 (menu (repeat 17 ↓))
695 (do-nothing 1))))
698 (defn test-pc-item-program []
699 (-> (read-state "bootstrap-init")
700 (set-memory pc-item-list-start 50)
701 (set-memory-range
702 map-function-address-start [0x8B 0xD5])
703 (set-memory-range
704 (inc pc-item-list-start)
705 (flatten
706 [(repeat
707 28
708 [0xFF 0x01])
709 [;; second part of item manipulation program
710 0x00 ;; this starts at address 0xD56C
711 0x2A ;; save (HL)=(target) to A, increment HL
713 0x00
714 0x47 ;; save A to B
716 0x00
717 0x3A ;; save (target+1) to A, decrement HL
719 0x00
720 0x22 ;; A -> target, increment HL [(target+1) -> target]
722 0x00
723 0x70 ;; load B into target+1 [(target) -> target+1]
725 0x00
726 0xC3 ;; first part of absolute jump
728 0x0C ;; return control to pokemon kernel
729 0x5F]
730 (repeat
731 5
732 [0xFF 0x01])
734 [;; first part of item manipulation program
735 0x00
736 0x21 ;; load target into HL
738 0x94 ;; this is the target address
739 0xD5
741 0x00 ;; relative jump back to first part
742 0x18
744 0xE1 ;; of program
745 0x01
747 0xFF ;; spacer
748 0x01
750 0x04 ;; target ID (pokeball)
751 0x3E ;; target Quantity (lemonade)
752 ]]))))
758 (defn basic-writer [target-address limit return-address]
759 (let [[target-high target-low] (disect-bytes-2 target-address)
760 [return-high return-low] (disect-bytes-2 return-address)]
761 (flatten
762 [0xF3 ;; disable interrupts
764 0x1E ;; load limit into E
765 limit
767 0x21 ;; load target into HL
768 target-low
769 target-high
771 ;; load 1 into C.
772 0x0E ;; C == 1 means input-first nybble
773 0x01 ;; C == 0 means input-second nybble
775 ;; Input Section
777 0x3E ;; load 0x20 into A, to measure dpad
778 0x20
780 0xE0 ;; load A into [FF00]
781 0x00
783 0xF0 ;; load 0xFF00 into A to get
784 0x00 ;; d-pad presses
786 0xE6
787 0x0F ;; select bottom four bits of A
789 0xB8 ;; see if input is different (CP A B)
791 0x28 ;; repeat above steps if input is not different
792 ;; (jump relative backwards if B != A)
793 0xF5 ;; (literal -11)
795 0x47 ;; load A into B
797 0x0D ;; dec C
798 ;; branch based on C:
799 0x20 ;; JR NZ
800 0x07 ;; skip "input first nybble" below
803 ;; input first nybble
805 0xCB
806 0x37 ;; swap nybbles on A
808 0x57 ;; A -> D
810 0x18
811 0xEC ;; literal -20 -- go back to input section
813 ;; input second nybble
815 0x0C ;; inc C
817 0xE6 ;; select bottom bits
818 0x0F
820 0xB2 ;; (OR A D) -> A
822 0x22 ;; (do (A -> (HL)) (INC HL))
824 0x1D ;; (DEC E)
826 0x20 ;; jump back to input section if not done
827 0xE4 ;; literal -28
829 0xFB ;; re-enable interrupts
831 0xC3
832 return-low
833 return-high ])))
836 (defn test-basic-writer []
837 (-> (read-state "bootstrap-init")
838 (set-memory pc-item-list-start 50)
839 (set-memory-range
840 map-function-address-start
841 (reverse (disect-bytes-2 (inc pc-item-list-start))))
842 (set-memory-range
843 (inc pc-item-list-start)
844 (basic-writer 0xD162 10 0x5F0C))))
846 (defn debug-basic-writer []
847 (PC! (test-basic-writer) (inc pc-item-list-start)))
849 (defn d-ticks [state n]
850 (reduce (fn [state _] (d-tick state))
851 state (range n)))
853 (defn d-print [state message]
854 (println message) state)
856 (defn dddd
857 []
858 (-> (debug-basic-writer)
859 (d-ticks 20)
860 (set-memory 0xFF00 0xFF)
861 (d-print "============== second cycle")
862 (d-ticks 14)
863 (d-print "============== end")
864 (d-ticks 20)))
866 ;;TMs at celadon store ---
867 ;;01 (any-number) mega punch
868 ;;02 (any-number) razor wind
869 ;;05 (any-number) mega kick
870 ;;07 (any-number) hyper beam
871 ;;09 (any-number) take down
872 ;;13 (only 1) ice beam
873 ;;17 (any-number) submission
874 ;;18 (only 1) counter
875 ;;32 (any-number) double team
876 ;;33 (any-number) reflect
877 ;;37 (any-number) egg bomb
878 ;;48 (only 1) rock slide
879 ;;49 (only 1) tri attack
882 ;; no-ops
883 ;; 0x00
884 ;; 0xB8 - 0xBF (compares) :garbage
885 ;; 0x3F clear carry flag :s.s.ticket
886 ;; 0x37 set carry flag :guard-spec [!]
887 ;; 0x33 increment SP :poke-doll [!]
888 ;; 0x3B decrement SP :coin
890 ;;0x7F A->A :garbage
891 ;;0x40 B->B :gold-teeth
892 ;;0x49 C->C :poke-flute
893 ;;0x52 D->D :elixer
894 ;;0x5B E->E :garbage
895 ;;0x6D L->L :garbage
896 ;;0x64 H->H :garbage
899 ;;0xC5 push BC :HM02
900 ;;0xD5 push DE :TM13 (ice-beam)
901 ;;0xE5 push HL :TM29 (psychic)
902 ;;0xF5 push AF :TM45 (thunder-wave)
904 ;; 0xA7 (AND A A) :garbage
905 ;; 0xB7 (OR A A) :garbage
907 ;; 0x2F (CPL A) :leaf-stone
910 (defn item-writer
911 "This is the basic writer, optimized to be made of valid
912 item-quantity pairs."
913 [target-address limit return-address]
914 (let [[target-high target-low] (disect-bytes-2 target-address)
915 [return-high return-low] (disect-bytes-2 return-address)]
916 (flatten
917 [
918 ;;0xC5 ;; push junk onto stack
919 ;;0xD5
920 ;;0xE5
921 ;;0xF5
922 0x33 ;; (item-hack) set increment stack pointer no-op
923 0x1E ;; load limit into E
924 limit
925 0x3F ;; (item-hack) set carry flag no-op
927 ;; load 2 into C.
928 0x0E ;; C == 1 means input-first nybble
929 0x04 ;; C == 0 means input-second nybble
931 0x21 ;; load target into HL
932 target-low
933 target-high
934 0x37 ;; (item-hack) set carry flag no-op
936 0x2F ;; (item-hack) cpl A
937 0x2F ;; (item-hack) cpl A --together a spacer no-op
939 0x00 ;; (item-hack) no-op
940 0xF3 ;; disable interrupts
941 ;; Input Section
943 0x3E ;; load 0x20 into A, to measure buttons
944 0x10
946 0x00 ;; (item-hack) no-op
947 0xE0 ;; load A into [FF00]
948 0x00
950 0xF0 ;; load 0xFF00 into A to get
951 0x00 ;; button presses
953 0xE6
954 0x0F ;; select bottom four bits of A
955 0x37 ;; (item-hack) set carry flag no-op
957 0x00 ;; (item-hack) no-op
958 0xB8 ;; see if input is different (CP A B)
960 0x00 ;; (item-hack) (INC SP)
961 0x28 ;; repeat above steps if input is not different
962 ;; (jump relative backwards if B != A)
963 0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)
965 0x47 ;; load A into B
967 0x0D ;; dec C
968 0x37 ;; (item-hack) set-carry flag
969 ;; branch based on C:
970 0x20 ;; JR NZ
971 23 ;; skip "input second nybble" and "jump to target" below
973 ;; input second nybble
975 0x0C ;; inc C
976 0x0C ;; inc C
978 0x00 ;; (item-hack) no-op
979 0xE6 ;; select bottom bits
980 0x0F
981 0x37 ;; (item-hack) set-carry flag no-op
983 0x00 ;; (item-hack) no-op
984 0xB2 ;; (OR A D) -> A
986 0x22 ;; (do (A -> (HL)) (INC HL))
988 0x1D ;; (DEC E)
990 0x00 ;; (item-hack)
991 0x20 ;; jump back to input section if not done
992 0xDA ;; literal -36 == TM 18 (counter)
993 0x01 ;; (item-hack) set BC to literal (no-op)
995 ;; jump to target
996 0x00 ;; (item-hack) these two bytes can be anything.
997 0x01
999 0x00 ;; (item-hack) no-op
1000 0xBF ;; (CP A A) ensures Z
1002 0xCA ;; (item-hack) jump if Z
1003 return-low
1004 return-high
1005 0x01 ;; (item-hack) will never be reached.
1009 ;; input first nybble
1010 0x00
1011 0xCB
1012 0x37 ;; swap nybbles on A
1014 0x57 ;; A -> D
1016 0x37 ;; (item-hack) set carry flag no-op
1017 0x18 ;; relative jump backwards
1018 0xCD ;; literal -51 == TM05; go back to input section
1019 0x01 ;; (item-hack) will never reach this instruction
1021 ])))
1023 (defn test-item-writer []
1024 (-> (read-state "bootstrap-init")
1025 (set-memory pc-item-list-start 50)
1026 (set-memory-range
1027 map-function-address-start
1028 (reverse (disect-bytes-2 (inc pc-item-list-start))))
1029 (set-memory-range
1030 (inc pc-item-list-start)
1031 (item-writer 0xD162 201 0xD162))))
1033 (defn item-writer-state []
1034 (read-state "item-writer"))
1036 (defn test-item-writer-2 []
1037 (let [orig (item-writer-state)]
1038 (-> orig
1039 (print-listing 0xD162 (+ 0xD162 20))
1040 (run-moves (reduce concat
1041 (repeat 10 [[:a :b :start :select] []])))
1042 ((fn [_] (println "===========") _))
1043 (print-listing 0xD162 (+ 0xD162 20)))))
1045 (defn pc-item-writer-program
1046 []
1047 (let [limit 201
1048 [target-high target-low] (disect-bytes-2 0xD162)]
1049 (flatten
1050 [[0x00 ;; (item-hack) set increment stack pointer no-op
1051 0x1E ;; load limit into E
1052 limit
1053 0x3F ;; (item-hack) set carry flag no-op
1055 ;; load 2 into C.
1056 0x0E ;; C == 1 means input-first nybble
1057 0x04 ;; C == 0 means input-second nybble
1059 0x21 ;; load target into HL
1060 target-low
1061 target-high
1062 0x37 ;; (item-hack) set carry flag no-op
1064 0x2F ;; (item-hack) cpl A
1065 0x2F ;; (item-hack) cpl A --together a spacer no-op
1067 0x00 ;; (item-hack) no-op
1068 0xF3 ;; disable interrupts
1069 ;; Input Section
1071 0x3E ;; load 0x20 into A, to measure buttons
1072 0x10
1074 0x00 ;; (item-hack) no-op
1075 0xE0 ;; load A into [FF00]
1076 0x00
1078 0xF0 ;; load 0xFF00 into A to get
1079 0x00 ;; button presses
1081 0xE6
1082 0x0F ;; select bottom four bits of A
1083 0x37 ;; (item-hack) set carry flag no-op
1085 0x00 ;; (item-hack) no-op
1086 0xB8 ;; see if input is different (CP A B)
1088 0x00 ;; (item-hack) (INC SP)
1089 0x28 ;; repeat above steps if input is not different
1090 ;; (jump relative backwards if B != A)
1091 0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)
1093 0x47 ;; load A into B
1095 0x0D ;; dec C
1096 0x37 ;; (item-hack) set-carry flag
1097 ;; branch based on C:
1098 0x20 ;; JR NZ
1099 23 ;; skip "input second nybble" and "jump to target" below
1101 ;; input second nybble
1103 0x0C ;; inc C
1104 0x0C ;; inc C
1106 0x00 ;; (item-hack) no-op
1107 0xE6 ;; select bottom bits
1108 0x0F
1109 0x37 ;; (item-hack) set-carry flag no-op
1111 0x00 ;; (item-hack) no-op
1112 0xB2 ;; (OR A D) -> A
1114 0x22 ;; (do (A -> (HL)) (INC HL))
1116 0x1D ;; (DEC E)
1118 0x00 ;; (item-hack)
1119 0x20 ;; jump back to input section if not done
1120 0xDA ;; literal -36 == TM 18 (counter)
1121 0x01 ;; (item-hack) set BC to literal (no-op)
1123 ;; jump to target
1124 0x00 ;; (item-hack) these two bytes can be anything.
1125 0x01
1127 0x00 ;; (item-hack) no-op
1128 0xBF ;; (CP A A) ensures Z
1130 0xCA ;; (item-hack) jump if Z
1131 target-low
1132 target-high
1133 0x01 ;; (item-hack) will never be reached.
1135 ;; input first nybble
1136 0x00
1137 0xCB
1138 0x37 ;; swap nybbles on A
1140 0x57 ;; A -> D
1142 0x37 ;; (item-hack) set carry flag no-op
1143 0x18 ;; relative jump backwards
1144 0xCD ;; literal -51 == TM05; go back to input section
1145 0x01 ;; (item-hack) will never reach this instruction
1148 (repeat 8 [0xFF 0x01])
1150 [;; jump to actual program
1151 0x00
1152 0x37 ;; (item-hack) set carry flag no-op
1154 0x00
1155 0x21
1157 0x3A
1158 0xD5
1160 ;; 0x00
1161 ;; 0x44 ;; H -> B
1163 ;; 0x00
1164 ;; 0x7D ;; L -> A
1166 ;; 0x00
1167 ;; 0x7C ;; A -> H
1169 ;; 0x00
1170 ;; 0x68 ;; B -> L
1172 0x00
1173 0xE9 ;; jump to (HL)
1174 ]])))
1177 (defn test-pc-item-writer []
1178 (-> (read-state "bootstrap-init")
1179 (set-memory pc-item-list-start 50)
1180 (set-memory-range
1181 map-function-address-start
1182 [0x8B 0xD5])
1183 (set-memory-range
1184 (inc pc-item-list-start)
1185 (pc-item-writer-program))))
1187 (defn test-pc-item-writer-2 []
1188 (let [orig (read-state "pc-item-writer")]
1189 (-> orig
1190 (print-listing 0xD162 (+ 0xD162 20))
1191 (run-moves (reduce concat
1192 (repeat 10 [[:a :b :start :select] []])))
1193 ((fn [_] (println "===========") _))
1194 (print-listing 0xD162 (+ 0xD162 20)))))