changeset 103:3305490a8f62

working on understanding basic opcodes
author Robert McIntyre <rlm@mit.edu>
date Mon, 12 Mar 2012 22:51:56 -0500
parents 4d9ce3188655
children d2e00ea31508
files clojure/com/aurellem/items.clj
diffstat 1 files changed, 254 insertions(+), 150 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/items.clj	Mon Mar 12 20:03:05 2012 -0500
     1.2 +++ b/clojure/com/aurellem/items.clj	Mon Mar 12 22:51:56 2012 -0500
     1.3 @@ -18,147 +18,148 @@
     1.4  
     1.5  (def item-code->item-name
     1.6    (hash-map
     1.7 -   1     :master-ball
     1.8 -   2     :ultra-ball
     1.9 -   3     :great-ball
    1.10 -   4     :poke-ball
    1.11 -   5     :town-map
    1.12 -   6     :bicycle
    1.13 -   8     :safari-ball
    1.14 -   9     :pokedex
    1.15 -   10    :moon-stone 
    1.16 -   11    :antidote
    1.17 -   12    :burn-heal
    1.18 -   13    :ice-heal
    1.19 -   14    :awakening
    1.20 -   15    :parlyz-heal
    1.21 -   16    :full-restore
    1.22 -   17    :max-potion
    1.23 -   18    :hyper-potion
    1.24 -   19    :super-potion
    1.25 -   20    :potion
    1.26 -   21    :boulderbadge
    1.27 -   22    :cascadebadge
    1.28 -   23    :thunderbadge
    1.29 -   24    :rainbowbadge
    1.30 -   25    :soulbadge
    1.31 -   26    :marshbadge
    1.32 -   27    :volcanobadge
    1.33 -   28    :earthbadge
    1.34 -   29    :escape-rope
    1.35 -   30    :repel
    1.36 -   31    :old-amber
    1.37 -   32    :fire-stone
    1.38 -   33    :thunderstone
    1.39 -   34    :water-stone
    1.40 -   35    :hp-up
    1.41 -   36    :protein
    1.42 -   37    :iron
    1.43 -   38    :carbos
    1.44 -   39    :calcium
    1.45 -   40    :rare-candy
    1.46 -   41    :dome-fossil
    1.47 -   42    :helix-fossil
    1.48 -   43    :secret-key
    1.49 -   45    :bike-voucher
    1.50 -   46    :x-accuracy
    1.51 -   47    :leaf-stone
    1.52 -   48    :card-key
    1.53 -   49    :nugget
    1.54 -   50    :pp-up
    1.55 -   51    :poke-doll
    1.56 -   52    :full-heal
    1.57 -   53    :revive
    1.58 -   54    :max-revive
    1.59 -   55    :guard-spec
    1.60 -   56    :super-repel
    1.61 -   57    :max-repel
    1.62 -   58    :dire-hit
    1.63 -   59    :coin
    1.64 -   60    :fresh-water
    1.65 -   61    :soda-pop
    1.66 -   62    :lemonade
    1.67 -   63    :s.s.ticket
    1.68 -   64    :gold-teeth
    1.69 -   65    :x-attach
    1.70 -   66    :x-defend
    1.71 -   67    :x-speed
    1.72 -   68    :x-special
    1.73 -   69    :coin-case
    1.74 -   70    :oaks-parcel
    1.75 -   71    :itemfinder
    1.76 -   72    :silph-scope
    1.77 -   73    :poke-flute
    1.78 -   74    :lift-key
    1.79 -   75    :exp.all
    1.80 -   76    :old-rod
    1.81 -   77    :good-rod
    1.82 -   78    :super-rod
    1.83 -   79    :pp-up
    1.84 -   80    :ether
    1.85 -   81    :max-ether
    1.86 -   82    :elixer
    1.87 -   83    :max-elixer
    1.88 -   196   :HM01  ;; cut
    1.89 -   197   :HM02  ;; fly
    1.90 -   198   :HM03  ;; surf
    1.91 -   199   :HM04  ;; strength
    1.92 -   200   :HM05  ;; flash
    1.93 -   201   :TM01  ;; mega punch
    1.94 -   202   :TM02  ;; razor wind
    1.95 -   203   :TM03  ;; swords dance
    1.96 -   204   :TM04  ;; whirlwind
    1.97 -   205   :TM05  ;; mega kick
    1.98 -   206   :TM06  ;; toxic
    1.99 -   207   :TM07  ;; horn drill
   1.100 -   208   :TM08  ;; body slam
   1.101 -   209   :TM09  ;; take down
   1.102 -   210   :TM10  ;; double-edge
   1.103 -   211   :TM11  ;; bubblebeam
   1.104 -   212   :TM12  ;; water gun
   1.105 -   213   :TM13  ;; ice beam 
   1.106 -   214   :TM14  ;; blizzard
   1.107 -   215   :TM15  ;; hyper beam
   1.108 -   216   :TM16  ;; pay day
   1.109 -   217   :TM17  ;; submission
   1.110 -   218   :TM18  ;; counter
   1.111 -   219   :TM19  ;; seismic toss
   1.112 -   220   :TM20  ;; rage
   1.113 -   221   :TM21  ;; mega drain
   1.114 -   222   :TM22  ;; solarbeam
   1.115 -   223   :TM23  ;; dragon rage
   1.116 -   224   :TM24  ;; thunderbolt
   1.117 -   225   :TM25  ;; thunder
   1.118 -   226   :TM26  ;; earthquake
   1.119 -   227   :TM27  ;; fissure
   1.120 -   228   :TM28  ;; dig
   1.121 -   229   :TM29  ;; psychic 
   1.122 -   230   :TM30  ;; teleport
   1.123 -   231   :TM31  ;; mimic
   1.124 -   232   :TM32  ;; double team
   1.125 -   233   :TM33  ;; reflect
   1.126 -   234   :TM34  ;; bide
   1.127 -   235   :TM35  ;; metronome
   1.128 -   236   :TM36  ;; self destruct
   1.129 -   237   :TM37  ;; eggbomb
   1.130 -   238   :TM38  ;; fire blast
   1.131 -   239   :TM39  ;; swift
   1.132 -   240   :TM40  ;; skull bash
   1.133 -   241   :TM41  ;; softboiled
   1.134 -   242   :TM42  ;; dream eater
   1.135 -   243   :TM43  ;; sky attack
   1.136 -   244   :TM44  ;; rest
   1.137 -   245   :TM45  ;; thunder wave
   1.138 -   246   :TM46  ;; psywave
   1.139 -   247   :TM47  ;; explosion
   1.140 -   248   :TM48  ;; rock slide
   1.141 -   249   :TM49  ;; tri attack
   1.142 -   250   :TM50  ;; substitute
   1.143 -   251   :TM51  ;; "cut"
   1.144 -   252   :TM52  ;; "fly"
   1.145 -   253   :TM53  ;; "surf"
   1.146 -   254   :TM54  ;; "strength"
   1.147 -   255   :end-of-list-sentinel))
   1.148 +   0x01 :master-ball       
   1.149 +   0x02 :ultra-ball
   1.150 +   0x03 :great-ball
   1.151 +   0x04 :poke-ball
   1.152 +   0x05 :town-map
   1.153 +   0x06 :bicycle
   1.154 +   0x08 :safari-ball
   1.155 +   0x09 :pokedex
   1.156 +   0x0A :moon-stone
   1.157 +   0x0B :antidote
   1.158 +   0x0C :burn-heal
   1.159 +   0x0D :ice-heal
   1.160 +   0x0E :awakening
   1.161 +   0x0F :parlyz-heal
   1.162 +   0x10 :full-restore
   1.163 +   0x11 :max-potion
   1.164 +   0x12 :hyper-potion
   1.165 +   0x13 :super-potion
   1.166 +   0x14 :potion
   1.167 +   0x15 :boulderbadge
   1.168 +   0x16 :cascadebadge
   1.169 +   0x17 :thunderbadge
   1.170 +   0x18 :rainbowbadge
   1.171 +   0x19 :soulbadge
   1.172 +   0x1A :marshbadge
   1.173 +   0x1B :volcanobadge
   1.174 +   0x1C :earthbadge
   1.175 +   0x1D :escape-rope
   1.176 +   0x1E :repel
   1.177 +   0x1F :old-amber
   1.178 +   0x20 :fire-stone
   1.179 +   0x21 :thunderstone
   1.180 +   0x22 :water-stone
   1.181 +   0x23 :hp-up
   1.182 +   0x24 :protein
   1.183 +   0x25 :iron
   1.184 +   0x26 :carbos
   1.185 +   0x27 :calcium
   1.186 +   0x28 :rare-candy
   1.187 +   0x29 :dome-fossil
   1.188 +   0x2A :helix-fossil
   1.189 +   0x2B :secret-key
   1.190 +   0x2D :bike-voucher
   1.191 +   0x2E :x-accuracy
   1.192 +   0x2F :leaf-stone
   1.193 +   0x30 :card-key
   1.194 +   0x31 :nugget
   1.195 +   0x32 :pp-up
   1.196 +   0x33 :poke-doll
   1.197 +   0x34 :full-heal
   1.198 +   0x35 :revive
   1.199 +   0x36 :max-revive
   1.200 +   0x37 :guard-spec
   1.201 +   0x38 :super-repel
   1.202 +   0x39 :max-repel
   1.203 +   0x3A :dire-hit
   1.204 +   0x3B :coin
   1.205 +   0x3C :fresh-water
   1.206 +   0x3D :soda-pop
   1.207 +   0x3E :lemonade
   1.208 +   0x3F :s.s.ticket
   1.209 +   0x40 :gold-teeth
   1.210 +   0x41 :x-attach
   1.211 +   0x42 :x-defend
   1.212 +   0x43 :x-speed
   1.213 +   0x44 :x-special
   1.214 +   0x45 :coin-case
   1.215 +   0x46 :oaks-parcel
   1.216 +   0x47 :itemfinder
   1.217 +   0x48 :silph-scope
   1.218 +   0x49 :poke-flute
   1.219 +   0x4A :lift-key
   1.220 +   0x4B :exp.all
   1.221 +   0x4C :old-rod
   1.222 +   0x4D :good-rod
   1.223 +   0x4E :super-rod
   1.224 +   0x4F :pp-up
   1.225 +   0x50 :ether
   1.226 +   0x51 :max-ether
   1.227 +   0x52 :elixer
   1.228 +   0x53 :max-elixer
   1.229 +   0xC4 :HM01     ;; cut		   
   1.230 +   0xC5 :HM02     ;; fly		   
   1.231 +   0xC6 :HM03     ;; surf		   
   1.232 +   0xC7 :HM04     ;; strength	   
   1.233 +   0xC8 :HM05     ;; flash	   
   1.234 +   0xC9 :TM01     ;; mega punch	   
   1.235 +   0xCA :TM02     ;; razor wind	   
   1.236 +   0xCB :TM03     ;; swords dance	   
   1.237 +   0xCC :TM04     ;; whirlwind	   
   1.238 +   0xCD :TM05     ;; mega kick	   
   1.239 +   0xCE :TM06     ;; toxic	   
   1.240 +   0xCF :TM07     ;; horn drill	   
   1.241 +   0xD0 :TM08     ;; body slam	   
   1.242 +   0xD1 :TM09     ;; take down	   
   1.243 +   0xD2 :TM10     ;; double-edge	   
   1.244 +   0xD3 :TM11     ;; bubblebeam	   
   1.245 +   0xD4 :TM12     ;; water gun	   
   1.246 +   0xD5 :TM13     ;; ice beam 	   
   1.247 +   0xD6 :TM14     ;; blizzard	   
   1.248 +   0xD7 :TM15     ;; hyper beam	   
   1.249 +   0xD8 :TM16     ;; pay day	   
   1.250 +   0xD9 :TM17     ;; submission	   
   1.251 +   0xDA :TM18     ;; counter	   
   1.252 +   0xDB :TM19     ;; seismic toss	   
   1.253 +   0xDC :TM20     ;; rage		   
   1.254 +   0xDD :TM21     ;; mega drain	   
   1.255 +   0xDE :TM22     ;; solarbeam	   
   1.256 +   0xDF :TM23     ;; dragon rage	   
   1.257 +   0xE0 :TM24     ;; thunderbolt	   
   1.258 +   0xE1 :TM25     ;; thunder	   
   1.259 +   0xE2 :TM26     ;; earthquake	   
   1.260 +   0xE3 :TM27     ;; fissure	   
   1.261 +   0xE4 :TM28     ;; dig		   
   1.262 +   0xE5 :TM29     ;; psychic 	   
   1.263 +   0xE6 :TM30     ;; teleport	   
   1.264 +   0xE7 :TM31     ;; mimic	   
   1.265 +   0xE8 :TM32     ;; double team	   
   1.266 +   0xE9 :TM33     ;; reflect	   
   1.267 +   0xEA :TM34     ;; bide		   
   1.268 +   0xEB :TM35     ;; metronome	   
   1.269 +   0xEC :TM36     ;; self destruct   
   1.270 +   0xED :TM37     ;; eggbomb	   
   1.271 +   0xEE :TM38     ;; fire blast	   
   1.272 +   0xEF :TM39     ;; swift	   
   1.273 +   0xF0 :TM40     ;; skull bash	   
   1.274 +   0xF1 :TM41     ;; softboiled	   
   1.275 +   0xF2 :TM42     ;; dream eater	   
   1.276 +   0xF3 :TM43     ;; sky attack	   
   1.277 +   0xF4 :TM44     ;; rest		   
   1.278 +   0xF5 :TM45     ;; thunder wave	   
   1.279 +   0xF6 :TM46     ;; psywave	   
   1.280 +   0xF7 :TM47     ;; explosion	   
   1.281 +   0xF8 :TM48     ;; rock slide	   
   1.282 +   0xF9 :TM49     ;; tri attack	   
   1.283 +   0xFA :TM50     ;; substitute	   
   1.284 +   0xFB :TM51     ;; "cut"	   
   1.285 +   0xFC :TM52     ;; "fly"	   
   1.286 +   0xFD :TM53     ;; "surf"	   
   1.287 +   0xFE :TM54     ;; "strength"      
   1.288 +   0xFF :end-of-list-sentinel
   1.289 +   ))
   1.290  
   1.291  (def item-name->item-code
   1.292    (zipmap (vals item-code->item-name)
   1.293 @@ -168,7 +169,9 @@
   1.294    (let [items (item-list state)]
   1.295      (map
   1.296       (fn [[item-code quantity]]
   1.297 -          [(item-code->item-name item-code)
   1.298 +       [(item-code->item-name
   1.299 +         item-code
   1.300 +         (str ":0x" (.toUpperCase (Integer/toHexString item-code))))
   1.301             quantity])
   1.302       (partition
   1.303        2
   1.304 @@ -202,6 +205,16 @@
   1.305                    [(item-name->item-code item-name)
   1.306                     quantity]) inventory)
   1.307             [(item-name->item-code :end-of-list-sentinel)])))
   1.308 +
   1.309 +(defn set-inv-mem [^SaveState state inv-codes]
   1.310 +  (set-state! state)
   1.311 +  (let [mem (memory state)]
   1.312 +    (dorun (map (fn [index val]
   1.313 +                  (aset mem index val))
   1.314 +                (range item-list-start
   1.315 +                       (+ item-list-start (count inv-codes))) inv-codes))
   1.316 +    (write-memory! mem)))
   1.317 +  
   1.318    
   1.319  (defn set-inventory [^SaveState state new-inventory]
   1.320    (set-state! state)
   1.321 @@ -215,6 +228,14 @@
   1.322      (write-memory! mem)
   1.323      (update-state)))
   1.324  
   1.325 +(defn give
   1.326 +  ([^SaveState state items]
   1.327 +     (set-inventory state
   1.328 +                    (concat items (inventory state))))
   1.329 +  ([items]
   1.330 +     (give @current-state items)))
   1.331 +  
   1.332 +
   1.333  (def gliched-tms
   1.334    [[:TM51 1]
   1.335     [:TM52 1]
   1.336 @@ -246,19 +267,20 @@
   1.337    "This is my first assembly/item program!
   1.338     it just increments BC by one.
   1.339  
   1.340 -   The code places a single 'great ball' at the beginning of the
   1.341 +   The code places a 3 'great balls' at the beginning of the
   1.342     inventory, then directly sets the program counter to start
   1.343 -   executing at the position of the 'great ball' in memory.
   1.344 +   executing at the position of the 'great balls' in memory.
   1.345  
   1.346     Since a 'great ball' is represented in memory as 0x03, which
   1.347     corresponts to the opcode which increments BC by one, that is
   1.348 -   what happens.
   1.349 +   what happens. Then the program counter to the 0x03 quantity entry
   1.350 +   and BC is incremented again.
   1.351  
   1.352     Obviously, the game crashes more or less immediately after the
   1.353 -   program counter advances past the 'great ball' into the next items
   1.354 -   in the inventory."
   1.355 +   program counter advances past the 'great balls' into the next items
   1.356 +   in the inventory, thus I call shutdown! before anything bad happens."
   1.357    []
   1.358 -  (set-inventory (read-state 578544) [[:great-ball 1]])
   1.359 +  (set-inventory (read-state 578544) [[:great-ball 3]])
   1.360    (print-inventory)
   1.361    (println "3 ticks") (tick) (tick) (tick)
   1.362    (println "PC before:" (PC))
   1.363 @@ -269,5 +291,87 @@
   1.364    (println "one tick")
   1.365    (tick)
   1.366    (println "PC after one tick:" (PC))
   1.367 -  (println "BC after one tick:" (BC)))
   1.368 -		
   1.369 \ No newline at end of file
   1.370 +  (println "BC after one tick:" (BC))
   1.371 +  (tick)
   1.372 +  (println "PC after two ticks:" (PC))
   1.373 +  (println "BC after two ticks:" (BC))
   1.374 +
   1.375 +  (shutdown!))
   1.376 +
   1.377 +
   1.378 +(defn test-opcodes-1
   1.379 +  []
   1.380 +  (set-inv-mem (read-state 578544)
   1.381 +               [20 0x02 0x00 0x00 0x02 0x00 0x00
   1.382 +                0x00 0x0 0xFF])
   1.383 +  (print-inventory)
   1.384 +  (println "3 ticks") (tick) (tick) (tick)
   1.385 +  (println "PC before:" (PC))
   1.386 +  (println "BC before:" (BC))
   1.387 +  (println "AF:" (AF))
   1.388 +  (PC! (inc item-list-start))
   1.389 +  (BC! (+ 1 item-list-start))
   1.390 +  (println "PC after setting:" (PC))
   1.391 +  (println "data at PC:" (aget (memory) (PC)))
   1.392 +  (println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.393 +  
   1.394 +  (println "one tick")
   1.395 +  (tick)
   1.396 +  (println "PC after one tick:" (PC))
   1.397 +  (println "BC after one tick:" (BC))
   1.398 +  (println "data at PC:" (aget (memory) (PC)))
   1.399 +  (println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.400 +  (tick)
   1.401 +  (AF! 0xFFFF)
   1.402 +  (println "PC after two ticks:" (PC))
   1.403 +  (println "BC after two ticks:" (BC))
   1.404 +  (println "data at PC:" (aget (memory) (PC)))
   1.405 +  (println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.406 +  (tick)
   1.407 +  (println "PC after three ticks:" (PC))
   1.408 +  (println "BC after three ticks:" (BC))
   1.409 +  (println "data at PC:" (aget (memory) (PC)))
   1.410 +  (println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.411 +  (tick)
   1.412 +  (println "PC after four ticks:" (PC))
   1.413 +  (println "BC after four ticks:" (BC))
   1.414 +  (println "data at PC:" (aget (memory) (PC)))
   1.415 +  (println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.416 +  (tick)
   1.417 +  (println "PC after five ticks:" (PC))
   1.418 +  (println "BC after five ticks:" (BC))
   1.419 +  (println "data at PC:" (aget (memory) (PC)))
   1.420 +  (println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.421 +  (print-inventory)
   1.422 +  (shutdown!))
   1.423 +
   1.424 +
   1.425 +
   1.426 +(defn test-opcodes-2
   1.427 +  []
   1.428 +  (set-inv-mem (read-state 578544)
   1.429 +               [20 0x08 0x1D 0xD3 0x00 0x00 0x00
   1.430 +                0x00 0x0 0xFF])
   1.431 +  (print-inventory)
   1.432 +  (println "3 ticks") (tick) (tick) (tick)
   1.433 +  (println "PC before:" (PC))
   1.434 +  (println "SP:" (SP))
   1.435 +  (PC! (inc item-list-start))
   1.436 +  (println "PC after setting:" (PC))
   1.437 +  (println "SP:" (Integer/toBinaryString (SP)))
   1.438 +  (println "data at PC:" (aget (memory) (PC)))
   1.439 +  (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
   1.440 +  (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
   1.441 +  (println "one tick")
   1.442 +  (tick)
   1.443 +  (println "PC after one tick:" (PC))
   1.444 +  (println "data at PC:" (aget (memory) (PC)))
   1.445 +  (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
   1.446 +  (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
   1.447 +  (tick) (tick) (tick)
   1.448 +  (println "PC aftter four tick:" (PC))
   1.449 +  (println "data at PC:" (aget (memory) (PC)))
   1.450 +  (println "data at 0xD31D:" (aget (memory) 0xD31D))
   1.451 +  
   1.452 +  (print-inventory)
   1.453 +  (shutdown!))