view clojure/com/aurellem/exp/item_bridge.clj @ 313:8e63b0bb8ea3

major refactoring; made (walk) more robust
author Robert McIntyre <rlm@mit.edu>
date Mon, 02 Apr 2012 10:58:16 -0500
parents b7f682bb3090
children 92c47a9cdaea
line wrap: on
line source
1 (ns com.aurellem.exp.item-bridge
2 (:use (com.aurellem.gb saves util constants gb-driver vbm items assembly))
3 (:use (com.aurellem.run title save-corruption))
4 ;;(:use (com.aurellem.exp pokemon))
5 (:import [com.aurellem.gb.gb_driver SaveState]))
7 (defn corrupt-item-state []
8 (second (destroy-item-end-of-list-marker)))
10 (defn corrupt-item-state []
11 (read-state "corrupt-items"))
13 (defn view-memory-range
14 ([start end]
15 (view-memory-range
16 @current-state start end))
17 ([state start end]
18 (dorun
19 (map (fn [loc val]
20 (println (format "%04X : %02X" loc val)))
21 (range start end) (subvec (vec (memory state)) start end)))
22 state))
24 (defn almost-broken
25 "if one more memory location is turned into 0x03, the game crashes."
26 [n]
27 (view-memory-range
28 (set-inv-mem (mid-game)
29 (concat [0xFF] (repeat 64 0x03)
30 (subvec (vec (memory (mid-game)))
31 (+ item-list-start 65)
32 (+ item-list-start 65 n))
33 (repeat (- 255 65 n) 0x03)))
34 item-list-start (+ item-list-start 255)))
36 (defn actually-broken
37 "if this memory location is turned into 0x03, the game crashes."
38 []
39 (set-memory (mid-game) 0xD35D 0x03))
42 ;; (almost-broken 20) more or less works
44 (defn capture-program-counter
45 "records the program counter for each tick"
46 [^SaveState state ticks]
47 (let [i (atom 0)]
48 (reduce (fn [[program-counters state] _]
49 (println (swap! i inc))
50 [(conj program-counters (PC state))
51 (tick state)])
52 [[] state]
53 (range ticks))))
56 (defn capture-program-counter
57 [^SaveState state ticks]
58 (tick state)
60 (loop [i 0
61 pcs []]
62 (if (= i ticks)
63 (filter (partial < 0x2000)(sort (set pcs)))
64 (do
65 (com.aurellem.gb.Gb/tick)
66 (recur (inc i)
67 (conj pcs (first (registers))))))))
69 (defn loop-program []
70 [0x00 ;0xD31D ;; disable-interrupts
72 0xC3 ;; loop forever
73 0x1D
74 0xD3])
76 (def map-function-address-start 0xD36D)
78 (defn test-loop []
79 (continue!
80 (-> (mid-game)
81 (set-memory-range 0xD31D (loop-program))
82 (set-memory-range
83 map-function-address-start
84 [0xD3 0x1D]))))
86 (defn-memo corrupt-moves []
87 (concat
88 (first
89 (->>
90 [[] (mid-game)]
91 (advance [:b] [:b :start])
92 (advance [] [:d])
93 (play-moves [[] [] [] [:d] [] [] [] [:d] [] [] [:a]])
94 scroll-text
95 (play-moves
96 ;; this section is copied from speedrun-2942
97 ;; and corrupts the save so that the end-of-list marker
98 ;; for the pokemon roster is destroyed, but the save is still
99 ;; playable.
100 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
101 [] [] [] [] [] [] [] [] [] [] [:select] [:restart]])
102 (title)
103 (advance [] [:start])
104 (advance [] [:a])
105 (advance [:a] [:a :start])))
106 [[]]))
108 (defn corrupt
109 "enter the codes to destroy the
110 pokemon list using save corruption"
111 ([^SaveState state]
112 (run-moves
113 state
114 (corrupt-moves)))
115 ([] (corrupt @current-state)))
117 (defn mid-game-corrupt []
118 (read-state "corrupt-mid-game"))
120 (defn gen-start-game-corrupt []
121 (->> (second (intro))
122 (advance [:b] [:a :b :start])
123 (play-moves (corrupt-moves))))
125 (defn start-game-corrupt []
126 (read-state "corrupt-start-game"))
128 (defn test-memory-fun [n]
129 (capture-program-counter
130 (set-memory-range
131 (tick (mid-game))
132 0xD36D
133 [0 0])
134 n))
136 ;;(def good (test-memory-fun 17000))
138 ;;(def bad (test-memory-fun 18000))
142 (defn menu-open-state []
143 (read-state "menu-open"))
145 (defn prepare-memory
146 ([^SaveState state]
147 (-> state
148 (set-memory-range 0xD31D (loop-program))
149 (set-memory-range 0xD36D [0x1D 0xD3])))
150 ([] (prepare-memory @current-state)))
152 (def memory-function-address-start 0xD36D)
154 (defn read-map-function-address
155 ([^SaveState state]
156 (let [mem (memory state)]
157 [(aget mem memory-function-address-start)
158 (aget mem (inc memory-function-address-start))]))
159 ([] (read-map-function-address @current-state)))
161 (defn succesful-PC-capture
162 "This function demonstrates successful PC capturing by
163 setting 0xD36D to the value of the start location of
164 a specially prepared program.
166 You must run the function and then exit the open menu
167 to see the effect."
168 []
169 (dorun
170 (map #(println (Integer/toHexString %))
171 (capture-program-counter
172 (prepare-memory (menu-open-state))
173 9000000))))
175 (defn trampoline-assembly [^SaveState state]
176 (flatten
177 [0x3E ;;
178 0x3E ;; load lemonade into A
180 0xEA
181 0x1D
182 0xD3 ;; set first item to lemonade
184 0xC3 ;; return control to the game via absolute jump.
185 (read-map-function-address state)
186 ]))
188 (defn test-trampoline
189 "Demonstrates item-program execution via the map-function that
190 returns control to the main pokemon game after one loop."
191 [assembly-fn state]
192 (let [insertion-address 0xD33D
193 insertion-address-bits [0x3D 0xD3]]
194 (->
195 state
196 (set-memory-range
197 insertion-address
198 (assembly-fn state))
199 (set-memory-range
200 memory-function-address-start
201 insertion-address-bits))))
203 (def lemonade-trampoline
204 (partial test-trampoline
205 trampoline-assembly
206 (menu-open-state)))
208 (defn trampoline-assembly-burn-heal [^SaveState state]
209 (flatten
210 [0x3E ;;
211 0x3E ;; load lemonade into A
213 0xEA
214 0x1D
215 0xD3 ;; set first item to lemonade
217 0xC3 ;; return control to the game via absolute jump
218 0x0C ;; to Route 3's map-function
219 0x55
220 ]))
224 (def pc-item-list-start 0xD539)
225 (def pc-item-list-width 101)
227 (def corrupted-items-width 512)
229 (defn items-record
230 ([^SaveState state]
231 (subvec (vec (memory state))
232 item-list-start
233 (+ item-list-start corrupted-items-width)))
234 ([] (items-record @current-state)))
236 (defn pc-items-record
237 ([^SaveState state]
238 (subvec (vec (memory state))
239 pc-item-list-start
240 (+ pc-item-list-width pc-item-list-start)))
241 ([] (pc-items-record @current-state)))
243 (defn print-listing-items
244 ([^SaveState state]
245 (print-listing state item-list-start
246 (+ item-list-start corrupted-items-width))
247 state)
248 ([] (print-listing-items @current-state)))
250 (defn print-listing-pc-items
251 ([^SaveState state]
252 (print-listing
253 state
254 pc-item-list-start
255 (+ pc-item-list-width pc-item-list-start))
256 state)
257 ([] (print-listing-pc-items @current-state)))