comparison clojure/com/aurellem/exp/item_bridge.clj @ 233:49a709c555f2

succesful return of control to the game upon item injection.
author Robert McIntyre <rlm@mit.edu>
date Sat, 24 Mar 2012 16:19:43 -0500
parents 8523faa122b0
children 5becef6312b9
comparison
equal deleted inserted replaced
232:e1dd3624d295 233:49a709c555f2
146 (-> state 146 (-> state
147 (set-memory-range 0xD31D (loop-program)) 147 (set-memory-range 0xD31D (loop-program))
148 (set-memory-range 0xD36D [0x1D 0xD3]))) 148 (set-memory-range 0xD36D [0x1D 0xD3])))
149 ([] (prepare-memory @current-state))) 149 ([] (prepare-memory @current-state)))
150 150
151 (def memory-function-address-start 0xD36D)
152
153 (defn read-map-function-address
154 ([^SaveState state]
155 (let [mem (memory state)]
156 [(aget mem memory-function-address-start)
157 (aget mem (inc memory-function-address-start))]))
158 ([] (read-map-function-address @current-state)))
151 159
152 (defn succesful-PC-capture 160 (defn succesful-PC-capture
153 "This function demonstrates successful PC capturing by 161 "This function demonstrates successful PC capturing by
154 setting 0xD36D to the value of the start location of 162 setting 0xD36D to the value of the start location of
155 a specially prepared program. 163 a specially prepared program.
160 (dorun 168 (dorun
161 (map #(println (Integer/toHexString %)) 169 (map #(println (Integer/toHexString %))
162 (capture-program-counter 170 (capture-program-counter
163 (prepare-memory (menu-open-state)) 171 (prepare-memory (menu-open-state))
164 9000000)))) 172 9000000))))
165 173
174 (defn trampoline-assembly [^SaveState state]
175 (flatten
176 [0x3E ;;
177 0x3E ;; load lemonade into A
178
179 0xEA
180 0x1D
181 0xD3 ;; set first item to lemonade
182
183 0xC3 ;; return control to the game via absolute junp.
184 (read-map-function-address state)
185 ]))
186
187 (defn test-trampoline
188 "Demonstrates item-program execution via the map-function that
189 returns control to the main pokemon game after one loop."
190 []
191 (let [insertion-address 0xD33D
192 insertion-address-bits [0x3D 0xD3]]
193 (->
194 (menu-open-state)
195 (set-memory-range
196 insertion-address
197 (trampoline-assembly (menu-open-state)))
198 (set-memory-range
199 memory-function-address-start
200 insertion-address-bits))))