comparison clojure/com/aurellem/items.clj @ 106:3a60bb14a64a

better functional assembly interface; removed frame numbers from SaveStates
author Robert McIntyre <rlm@mit.edu>
date Wed, 14 Mar 2012 21:37:37 -0500
parents 2f8089eacab9
children ad96e9464d6a
comparison
equal deleted inserted replaced
105:2f8089eacab9 106:3a60bb14a64a
212 (let [mem (memory state)] 212 (let [mem (memory state)]
213 (dorun (map (fn [index val] 213 (dorun (map (fn [index val]
214 (aset mem index val)) 214 (aset mem index val))
215 (range item-list-start 215 (range item-list-start
216 (+ item-list-start (count inv-codes))) inv-codes)) 216 (+ item-list-start (count inv-codes))) inv-codes))
217 (write-memory! mem))) 217 (write-memory! mem)
218 (update-state)))
218 219
219 220
220 (defn set-inventory [^SaveState state new-inventory] 221 (defn set-inventory [^SaveState state new-inventory]
221 (set-state! state) 222 (set-state! state)
222 (let [mem (memory state) 223 (let [mem (memory state)
233 ([^SaveState state items] 234 ([^SaveState state items]
234 (set-inventory state 235 (set-inventory state
235 (concat items (inventory state)))) 236 (concat items (inventory state))))
236 ([items] 237 ([items]
237 (give @current-state items))) 238 (give @current-state items)))
238 239
240 (defn clear-inventory
241 ([^SaveState state]
242 (set-inventory state []))
243 ([] (clear-inventory @current-state)))
239 244
240 (def gliched-tms 245 (def gliched-tms
241 [[:TM51 1] 246 [[:TM51 1]
242 [:TM52 1] 247 [:TM52 1]
243 [:TM53 1] 248 [:TM53 1]
262 [:thunderbadge 1] 267 [:thunderbadge 1]
263 [:rainbowbadge 1] 268 [:rainbowbadge 1]
264 [:soulbadge 1] 269 [:soulbadge 1]
265 ]) 270 ])
266 271
267 (defn run-item-program
268 "This is my first assembly/item program!
269 it just increments BC by one.
270
271 The code places a 3 'great balls' at the beginning of the
272 inventory, then directly sets the program counter to start
273 executing at the position of the 'great balls' in memory.
274
275 Since a 'great ball' is represented in memory as 0x03, which
276 corresponts to the opcode which increments BC by one, that is
277 what happens. Then the program counter to the 0x03 quantity entry
278 and BC is incremented again.
279
280 Obviously, the game crashes more or less immediately after the
281 program counter advances past the 'great balls' into the next items
282 in the inventory, thus I call shutdown! before anything bad happens."
283 []
284 (set-inventory (read-state 578544) [[:great-ball 3]])
285 (print-inventory)
286 (println "3 ticks") (tick) (tick) (tick)
287 (println "PC before:" (PC))
288 (println "BC before:" (BC))
289 (PC! (inc item-list-start))
290 (println "PC after setting:" (PC))
291 (println "data at PC:" (aget (memory) (PC)))
292 (println "one tick")
293 (tick)
294 (println "PC after one tick:" (PC))
295 (println "BC after one tick:" (BC))
296 (tick)
297 (println "PC after two ticks:" (PC))
298 (println "BC after two ticks:" (BC))
299
300 (shutdown!))
301
302
303 (defn test-opcodes-1
304 []
305 (set-inv-mem (read-state 578544)
306 [20 0x02 0x00 0x00 0x02 0x00 0x00
307 0x00 0x0 0xFF])
308 (print-inventory)
309 (println "3 ticks") (tick) (tick) (tick)
310 (println "PC before:" (PC))
311 (println "BC before:" (BC))
312 (println "AF:" (AF))
313 (PC! (inc item-list-start))
314 (BC! (+ 1 item-list-start))
315 (println "PC after setting:" (PC))
316 (println "data at PC:" (aget (memory) (PC)))
317 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
318
319 (println "one tick")
320 (tick)
321 (println "PC after one tick:" (PC))
322 (println "BC after one tick:" (BC))
323 (println "data at PC:" (aget (memory) (PC)))
324 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
325 (tick)
326 (AF! 0xFFFF)
327 (println "PC after two ticks:" (PC))
328 (println "BC after two ticks:" (BC))
329 (println "data at PC:" (aget (memory) (PC)))
330 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
331 (tick)
332 (println "PC after three ticks:" (PC))
333 (println "BC after three ticks:" (BC))
334 (println "data at PC:" (aget (memory) (PC)))
335 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
336 (tick)
337 (println "PC after four ticks:" (PC))
338 (println "BC after four ticks:" (BC))
339 (println "data at PC:" (aget (memory) (PC)))
340 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
341 (tick)
342 (println "PC after five ticks:" (PC))
343 (println "BC after five ticks:" (BC))
344 (println "data at PC:" (aget (memory) (PC)))
345 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
346 (print-inventory)
347 (shutdown!))
348
349
350
351 (defn test-opcodes-2
352 []
353 (set-inv-mem (read-state 578544)
354 [20 0x08 0x1D 0xD3 0x00 0x00 0x00
355 0x00 0x0 0xFF])
356 (print-inventory)
357 (println "3 ticks") (tick) (tick) (tick)
358 (println "PC before:" (PC))
359 (println "SP:" (SP))
360 (PC! (inc item-list-start))
361 (println "PC after setting:" (PC))
362 (println "SP:" (Integer/toBinaryString (SP)))
363 (println "data at PC:" (aget (memory) (PC)))
364 (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
365 (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
366 (println "one tick")
367 (tick)
368 (println "PC after one tick:" (PC))
369 (println "data at PC:" (aget (memory) (PC)))
370 (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
371 (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
372 (tick) (tick) (tick)
373 (println "PC aftter four tick:" (PC))
374 (println "data at PC:" (aget (memory) (PC)))
375 (println "data at 0xD31D:" (aget (memory) 0xD31D))
376
377 (print-inventory)
378 (shutdown!))