# HG changeset patch # User Robert McIntyre # Date 1331610716 18000 # Node ID 3305490a8f62e34557fcb0a8d40c9aa4463901a5 # Parent 4d9ce3188655351863aa4641c17066180e780f8d working on understanding basic opcodes diff -r 4d9ce3188655 -r 3305490a8f62 clojure/com/aurellem/items.clj --- a/clojure/com/aurellem/items.clj Mon Mar 12 20:03:05 2012 -0500 +++ b/clojure/com/aurellem/items.clj Mon Mar 12 22:51:56 2012 -0500 @@ -18,147 +18,148 @@ (def item-code->item-name (hash-map - 1 :master-ball - 2 :ultra-ball - 3 :great-ball - 4 :poke-ball - 5 :town-map - 6 :bicycle - 8 :safari-ball - 9 :pokedex - 10 :moon-stone - 11 :antidote - 12 :burn-heal - 13 :ice-heal - 14 :awakening - 15 :parlyz-heal - 16 :full-restore - 17 :max-potion - 18 :hyper-potion - 19 :super-potion - 20 :potion - 21 :boulderbadge - 22 :cascadebadge - 23 :thunderbadge - 24 :rainbowbadge - 25 :soulbadge - 26 :marshbadge - 27 :volcanobadge - 28 :earthbadge - 29 :escape-rope - 30 :repel - 31 :old-amber - 32 :fire-stone - 33 :thunderstone - 34 :water-stone - 35 :hp-up - 36 :protein - 37 :iron - 38 :carbos - 39 :calcium - 40 :rare-candy - 41 :dome-fossil - 42 :helix-fossil - 43 :secret-key - 45 :bike-voucher - 46 :x-accuracy - 47 :leaf-stone - 48 :card-key - 49 :nugget - 50 :pp-up - 51 :poke-doll - 52 :full-heal - 53 :revive - 54 :max-revive - 55 :guard-spec - 56 :super-repel - 57 :max-repel - 58 :dire-hit - 59 :coin - 60 :fresh-water - 61 :soda-pop - 62 :lemonade - 63 :s.s.ticket - 64 :gold-teeth - 65 :x-attach - 66 :x-defend - 67 :x-speed - 68 :x-special - 69 :coin-case - 70 :oaks-parcel - 71 :itemfinder - 72 :silph-scope - 73 :poke-flute - 74 :lift-key - 75 :exp.all - 76 :old-rod - 77 :good-rod - 78 :super-rod - 79 :pp-up - 80 :ether - 81 :max-ether - 82 :elixer - 83 :max-elixer - 196 :HM01 ;; cut - 197 :HM02 ;; fly - 198 :HM03 ;; surf - 199 :HM04 ;; strength - 200 :HM05 ;; flash - 201 :TM01 ;; mega punch - 202 :TM02 ;; razor wind - 203 :TM03 ;; swords dance - 204 :TM04 ;; whirlwind - 205 :TM05 ;; mega kick - 206 :TM06 ;; toxic - 207 :TM07 ;; horn drill - 208 :TM08 ;; body slam - 209 :TM09 ;; take down - 210 :TM10 ;; double-edge - 211 :TM11 ;; bubblebeam - 212 :TM12 ;; water gun - 213 :TM13 ;; ice beam - 214 :TM14 ;; blizzard - 215 :TM15 ;; hyper beam - 216 :TM16 ;; pay day - 217 :TM17 ;; submission - 218 :TM18 ;; counter - 219 :TM19 ;; seismic toss - 220 :TM20 ;; rage - 221 :TM21 ;; mega drain - 222 :TM22 ;; solarbeam - 223 :TM23 ;; dragon rage - 224 :TM24 ;; thunderbolt - 225 :TM25 ;; thunder - 226 :TM26 ;; earthquake - 227 :TM27 ;; fissure - 228 :TM28 ;; dig - 229 :TM29 ;; psychic - 230 :TM30 ;; teleport - 231 :TM31 ;; mimic - 232 :TM32 ;; double team - 233 :TM33 ;; reflect - 234 :TM34 ;; bide - 235 :TM35 ;; metronome - 236 :TM36 ;; self destruct - 237 :TM37 ;; eggbomb - 238 :TM38 ;; fire blast - 239 :TM39 ;; swift - 240 :TM40 ;; skull bash - 241 :TM41 ;; softboiled - 242 :TM42 ;; dream eater - 243 :TM43 ;; sky attack - 244 :TM44 ;; rest - 245 :TM45 ;; thunder wave - 246 :TM46 ;; psywave - 247 :TM47 ;; explosion - 248 :TM48 ;; rock slide - 249 :TM49 ;; tri attack - 250 :TM50 ;; substitute - 251 :TM51 ;; "cut" - 252 :TM52 ;; "fly" - 253 :TM53 ;; "surf" - 254 :TM54 ;; "strength" - 255 :end-of-list-sentinel)) + 0x01 :master-ball + 0x02 :ultra-ball + 0x03 :great-ball + 0x04 :poke-ball + 0x05 :town-map + 0x06 :bicycle + 0x08 :safari-ball + 0x09 :pokedex + 0x0A :moon-stone + 0x0B :antidote + 0x0C :burn-heal + 0x0D :ice-heal + 0x0E :awakening + 0x0F :parlyz-heal + 0x10 :full-restore + 0x11 :max-potion + 0x12 :hyper-potion + 0x13 :super-potion + 0x14 :potion + 0x15 :boulderbadge + 0x16 :cascadebadge + 0x17 :thunderbadge + 0x18 :rainbowbadge + 0x19 :soulbadge + 0x1A :marshbadge + 0x1B :volcanobadge + 0x1C :earthbadge + 0x1D :escape-rope + 0x1E :repel + 0x1F :old-amber + 0x20 :fire-stone + 0x21 :thunderstone + 0x22 :water-stone + 0x23 :hp-up + 0x24 :protein + 0x25 :iron + 0x26 :carbos + 0x27 :calcium + 0x28 :rare-candy + 0x29 :dome-fossil + 0x2A :helix-fossil + 0x2B :secret-key + 0x2D :bike-voucher + 0x2E :x-accuracy + 0x2F :leaf-stone + 0x30 :card-key + 0x31 :nugget + 0x32 :pp-up + 0x33 :poke-doll + 0x34 :full-heal + 0x35 :revive + 0x36 :max-revive + 0x37 :guard-spec + 0x38 :super-repel + 0x39 :max-repel + 0x3A :dire-hit + 0x3B :coin + 0x3C :fresh-water + 0x3D :soda-pop + 0x3E :lemonade + 0x3F :s.s.ticket + 0x40 :gold-teeth + 0x41 :x-attach + 0x42 :x-defend + 0x43 :x-speed + 0x44 :x-special + 0x45 :coin-case + 0x46 :oaks-parcel + 0x47 :itemfinder + 0x48 :silph-scope + 0x49 :poke-flute + 0x4A :lift-key + 0x4B :exp.all + 0x4C :old-rod + 0x4D :good-rod + 0x4E :super-rod + 0x4F :pp-up + 0x50 :ether + 0x51 :max-ether + 0x52 :elixer + 0x53 :max-elixer + 0xC4 :HM01 ;; cut + 0xC5 :HM02 ;; fly + 0xC6 :HM03 ;; surf + 0xC7 :HM04 ;; strength + 0xC8 :HM05 ;; flash + 0xC9 :TM01 ;; mega punch + 0xCA :TM02 ;; razor wind + 0xCB :TM03 ;; swords dance + 0xCC :TM04 ;; whirlwind + 0xCD :TM05 ;; mega kick + 0xCE :TM06 ;; toxic + 0xCF :TM07 ;; horn drill + 0xD0 :TM08 ;; body slam + 0xD1 :TM09 ;; take down + 0xD2 :TM10 ;; double-edge + 0xD3 :TM11 ;; bubblebeam + 0xD4 :TM12 ;; water gun + 0xD5 :TM13 ;; ice beam + 0xD6 :TM14 ;; blizzard + 0xD7 :TM15 ;; hyper beam + 0xD8 :TM16 ;; pay day + 0xD9 :TM17 ;; submission + 0xDA :TM18 ;; counter + 0xDB :TM19 ;; seismic toss + 0xDC :TM20 ;; rage + 0xDD :TM21 ;; mega drain + 0xDE :TM22 ;; solarbeam + 0xDF :TM23 ;; dragon rage + 0xE0 :TM24 ;; thunderbolt + 0xE1 :TM25 ;; thunder + 0xE2 :TM26 ;; earthquake + 0xE3 :TM27 ;; fissure + 0xE4 :TM28 ;; dig + 0xE5 :TM29 ;; psychic + 0xE6 :TM30 ;; teleport + 0xE7 :TM31 ;; mimic + 0xE8 :TM32 ;; double team + 0xE9 :TM33 ;; reflect + 0xEA :TM34 ;; bide + 0xEB :TM35 ;; metronome + 0xEC :TM36 ;; self destruct + 0xED :TM37 ;; eggbomb + 0xEE :TM38 ;; fire blast + 0xEF :TM39 ;; swift + 0xF0 :TM40 ;; skull bash + 0xF1 :TM41 ;; softboiled + 0xF2 :TM42 ;; dream eater + 0xF3 :TM43 ;; sky attack + 0xF4 :TM44 ;; rest + 0xF5 :TM45 ;; thunder wave + 0xF6 :TM46 ;; psywave + 0xF7 :TM47 ;; explosion + 0xF8 :TM48 ;; rock slide + 0xF9 :TM49 ;; tri attack + 0xFA :TM50 ;; substitute + 0xFB :TM51 ;; "cut" + 0xFC :TM52 ;; "fly" + 0xFD :TM53 ;; "surf" + 0xFE :TM54 ;; "strength" + 0xFF :end-of-list-sentinel + )) (def item-name->item-code (zipmap (vals item-code->item-name) @@ -168,7 +169,9 @@ (let [items (item-list state)] (map (fn [[item-code quantity]] - [(item-code->item-name item-code) + [(item-code->item-name + item-code + (str ":0x" (.toUpperCase (Integer/toHexString item-code)))) quantity]) (partition 2 @@ -202,6 +205,16 @@ [(item-name->item-code item-name) quantity]) inventory) [(item-name->item-code :end-of-list-sentinel)]))) + +(defn set-inv-mem [^SaveState state inv-codes] + (set-state! state) + (let [mem (memory state)] + (dorun (map (fn [index val] + (aset mem index val)) + (range item-list-start + (+ item-list-start (count inv-codes))) inv-codes)) + (write-memory! mem))) + (defn set-inventory [^SaveState state new-inventory] (set-state! state) @@ -215,6 +228,14 @@ (write-memory! mem) (update-state))) +(defn give + ([^SaveState state items] + (set-inventory state + (concat items (inventory state)))) + ([items] + (give @current-state items))) + + (def gliched-tms [[:TM51 1] [:TM52 1] @@ -246,19 +267,20 @@ "This is my first assembly/item program! it just increments BC by one. - The code places a single 'great ball' at the beginning of the + The code places a 3 'great balls' at the beginning of the inventory, then directly sets the program counter to start - executing at the position of the 'great ball' in memory. + executing at the position of the 'great balls' in memory. Since a 'great ball' is represented in memory as 0x03, which corresponts to the opcode which increments BC by one, that is - what happens. + what happens. Then the program counter to the 0x03 quantity entry + and BC is incremented again. Obviously, the game crashes more or less immediately after the - program counter advances past the 'great ball' into the next items - in the inventory." + program counter advances past the 'great balls' into the next items + in the inventory, thus I call shutdown! before anything bad happens." [] - (set-inventory (read-state 578544) [[:great-ball 1]]) + (set-inventory (read-state 578544) [[:great-ball 3]]) (print-inventory) (println "3 ticks") (tick) (tick) (tick) (println "PC before:" (PC)) @@ -269,5 +291,87 @@ (println "one tick") (tick) (println "PC after one tick:" (PC)) - (println "BC after one tick:" (BC))) - \ No newline at end of file + (println "BC after one tick:" (BC)) + (tick) + (println "PC after two ticks:" (PC)) + (println "BC after two ticks:" (BC)) + + (shutdown!)) + + +(defn test-opcodes-1 + [] + (set-inv-mem (read-state 578544) + [20 0x02 0x00 0x00 0x02 0x00 0x00 + 0x00 0x0 0xFF]) + (print-inventory) + (println "3 ticks") (tick) (tick) (tick) + (println "PC before:" (PC)) + (println "BC before:" (BC)) + (println "AF:" (AF)) + (PC! (inc item-list-start)) + (BC! (+ 1 item-list-start)) + (println "PC after setting:" (PC)) + (println "data at PC:" (aget (memory) (PC))) + (println "data at " (BC) "(BC):" (aget (memory) (BC))) + + (println "one tick") + (tick) + (println "PC after one tick:" (PC)) + (println "BC after one tick:" (BC)) + (println "data at PC:" (aget (memory) (PC))) + (println "data at " (BC) "(BC):" (aget (memory) (BC))) + (tick) + (AF! 0xFFFF) + (println "PC after two ticks:" (PC)) + (println "BC after two ticks:" (BC)) + (println "data at PC:" (aget (memory) (PC))) + (println "data at " (BC) "(BC):" (aget (memory) (BC))) + (tick) + (println "PC after three ticks:" (PC)) + (println "BC after three ticks:" (BC)) + (println "data at PC:" (aget (memory) (PC))) + (println "data at " (BC) "(BC):" (aget (memory) (BC))) + (tick) + (println "PC after four ticks:" (PC)) + (println "BC after four ticks:" (BC)) + (println "data at PC:" (aget (memory) (PC))) + (println "data at " (BC) "(BC):" (aget (memory) (BC))) + (tick) + (println "PC after five ticks:" (PC)) + (println "BC after five ticks:" (BC)) + (println "data at PC:" (aget (memory) (PC))) + (println "data at " (BC) "(BC):" (aget (memory) (BC))) + (print-inventory) + (shutdown!)) + + + +(defn test-opcodes-2 + [] + (set-inv-mem (read-state 578544) + [20 0x08 0x1D 0xD3 0x00 0x00 0x00 + 0x00 0x0 0xFF]) + (print-inventory) + (println "3 ticks") (tick) (tick) (tick) + (println "PC before:" (PC)) + (println "SP:" (SP)) + (PC! (inc item-list-start)) + (println "PC after setting:" (PC)) + (println "SP:" (Integer/toBinaryString (SP))) + (println "data at PC:" (aget (memory) (PC))) + (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D))) + (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E))) + (println "one tick") + (tick) + (println "PC after one tick:" (PC)) + (println "data at PC:" (aget (memory) (PC))) + (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D))) + (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E))) + (tick) (tick) (tick) + (println "PC aftter four tick:" (PC)) + (println "data at PC:" (aget (memory) (PC))) + (println "data at 0xD31D:" (aget (memory) 0xD31D)) + + (print-inventory) + (shutdown!))