diff clojure/com/aurellem/experiments/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 9fad96094950
children
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/experiments/items.clj	Tue Mar 13 14:40:01 2012 -0500
     1.2 +++ b/clojure/com/aurellem/experiments/items.clj	Wed Mar 14 21:37:37 2012 -0500
     1.3 @@ -1,3 +1,7 @@
     1.4 +(ns com.aurellem.experiments.items
     1.5 +   (:use (com.aurellem gb-driver vbm title items))
     1.6 +   (:import [com.aurellem.gb_driver SaveState]))
     1.7 +
     1.8  ;; try just buying five potions in sequence and see what changes
     1.9  ;; each time.
    1.10  
    1.11 @@ -10,28 +14,27 @@
    1.12  
    1.13  ;; trying to find how items are represented in memory
    1.14  
    1.15 -(comment
    1.16 -  (def empty-inventory @current-state)
    1.17 -  
    1.18 -  (def one-potion @current-state)
    1.19 -  
    1.20 -  (def two-potions @current-state)
    1.21 -  
    1.22 -  (def three-potions @current-state)
    1.23 -  
    1.24 -  (def four-potions @current-state)
    1.25 -  
    1.26 -  (def five-potions @current-state)
    1.27 +(def zero-potions (read-state "zero-potions"))
    1.28 +
    1.29 +(def one-potion (read-state "one-potion"))
    1.30 +
    1.31 +(def two-potions (read-state "two-potions"))
    1.32 +
    1.33 +(def three-potions (read-state "three-potions"))
    1.34 +
    1.35 +(def four-potions (read-state "four-potions"))
    1.36 +
    1.37 +(def five-potions (read-state "five-potions"))
    1.38    
    1.39    
    1.40    ;; result
    1.41 -  (def canidates
    1.42 -    (apply common-differences
    1.43 -           (map (comp vec memory)
    1.44 -                [empty-inventory one-potion two-potions three-potions
    1.45 -                 four-potions five-potions])))
    1.46 +(defn canidates []
    1.47 +  (apply common-differences
    1.48 +         (map (comp vec memory)
    1.49 +              [zero-potions one-potion two-potions three-potions
    1.50 +               four-potions five-potions])))
    1.51  
    1.52 -  [55875 (37 15 49 27 14 44)]
    1.53 + (comment [55875 (37 15 49 27 14 44)]
    1.54    [55876 (30 1 49 56 55 23)]
    1.55    [49158 (154 191 78 135 70 73)]
    1.56    [54087 (49 40 37 34 25 22)]
    1.57 @@ -53,10 +56,6 @@
    1.58  
    1.59  
    1.60  
    1.61 -(def item-hack (read-state 7999))
    1.62 -
    1.63 -(def item-hack2 (read-state 75882))
    1.64 -
    1.65  (defn get-mem []
    1.66    (subvec (vec (memory @current-state)) 54040 (+ 54046 100)))
    1.67  
    1.68 @@ -92,14 +91,19 @@
    1.69  
    1.70  ;; now it's time to learn the item codes
    1.71  
    1.72 -(def item-hack-3 (read-state 77557))
    1.73 +(def inventory-begin
    1.74 +  (read-state "inventory-begin"))
    1.75 +
    1.76  (defn show-item
    1.77    "Run a saved pokemon with the first item replaced by the item named
    1.78     by n."
    1.79    [n]
    1.80 -  (set-state! item-hack-3)
    1.81 +  (set-state! inventory-begin)
    1.82    (let [mem (memory)]
    1.83 +    (aset mem 54044 1)
    1.84      (aset mem 54045 n)
    1.85 +    (aset mem 54046 1)
    1.86 +    (aset mem 54047 255)
    1.87      (write-memory! mem))
    1.88    (step)
    1.89    (->> [[] @current-state]
    1.90 @@ -372,3 +376,128 @@
    1.91  ;; 253   TM53
    1.92  ;; 254   TM54
    1.93  ;; 255   end-of-list-sentinel   
    1.94 +
    1.95 +
    1.96 +
    1.97 +(defn run-item-program
    1.98 +  "This is my first assembly/item program!
    1.99 +   it just increments BC by one.
   1.100 +
   1.101 +   The code places a 3 'great balls' at the beginning of the
   1.102 +   inventory, then directly sets the program counter to start
   1.103 +   executing at the position of the 'great balls' in memory.
   1.104 +
   1.105 +   Since a 'great ball' is represented in memory as 0x03, which
   1.106 +   corresponts to the opcode which increments BC by one, that is
   1.107 +   what happens. Then the program counter to the 0x03 quantity entry
   1.108 +   and BC is incremented again.
   1.109 +
   1.110 +   Obviously, the game crashes more or less immediately after the
   1.111 +   program counter advances past the 'great balls' into the next items
   1.112 +   in the inventory, thus I call shutdown! before anything bad happens."
   1.113 +  []
   1.114 +  (set-inventory (read-state "mid-game") [[:great-ball 3]])
   1.115 +  (print-inventory)
   1.116 +  (println "3 ticks") (tick) (tick) (tick)
   1.117 +  (println "PC before:" (PC))
   1.118 +  (println "BC before:" (BC))
   1.119 +  (PC! (inc item-list-start))
   1.120 +  (println "PC after setting:" (PC))
   1.121 +  (println "data at PC:" (aget (memory) (PC)))
   1.122 +  (println "one tick")
   1.123 +  (tick)
   1.124 +  (println "PC after one tick:" (PC))
   1.125 +  (println "BC after one tick:" (BC))
   1.126 +  (tick)
   1.127 +  (println "PC after two ticks:" (PC))
   1.128 +  (println "BC after two ticks:" (BC))
   1.129 +
   1.130 +  (shutdown!))
   1.131 +
   1.132 +
   1.133 +
   1.134 +
   1.135 +(defn test-opcodes-1
   1.136 +  []
   1.137 +  (let [final-state
   1.138 +        (->
   1.139 +         (read-state "mid-game")
   1.140 +         (set-inv-mem 
   1.141 +          [20 0x02 0x00 0x00 0x02 0x00 0x00
   1.142 +           0x00 0x0 0xFF])
   1.143 +         (print-inventory)
   1.144 +         ;;((fn [_] (println "3 ticks") _))
   1.145 +         (tick) (tick) (tick)
   1.146 +         
   1.147 +         ;;(println "PC before:" (PC))
   1.148 +         ;;(println "BC before:" (BC))
   1.149 +         ;;(println "AF:" (AF))
   1.150 +         (PC! (inc item-list-start))
   1.151 +         (BC! (+ 1 item-list-start))
   1.152 +         ;;(println "PC after setting:" (PC))
   1.153 +         ;;(println "data at PC:" (aget (memory) (PC)))
   1.154 +         ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.155 +         
   1.156 +         ;;(println "one tick")
   1.157 +         (tick)
   1.158 +         ;;(println "PC after one tick:" (PC))
   1.159 +         ;;(println "BC after one tick:" (BC))
   1.160 +         ;;(println "data at PC:" (aget (memory) (PC)))
   1.161 +         ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.162 +         (tick)
   1.163 +         (AF! 0xFFFF)
   1.164 +         ;;(println "PC after two ticks:" (PC))
   1.165 +         ;;(println "BC after two ticks:" (BC))
   1.166 +         ;;(println "data at PC:" (aget (memory) (PC)))
   1.167 +         ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.168 +         (tick)
   1.169 +         ;;(println "PC after three ticks:" (PC))
   1.170 +         ;;(println "BC after three ticks:" (BC))
   1.171 +         ;;(println "data at PC:" (aget (memory) (PC)))
   1.172 +         ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.173 +         (tick)
   1.174 +         ;;(println "PC after four ticks:" (PC))
   1.175 +         ;;(println "BC after four ticks:" (BC))
   1.176 +         ;;(println "data at PC:" (aget (memory) (PC)))
   1.177 +         ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.178 +         (tick)
   1.179 +         ;;(println "PC after five ticks:" (PC))
   1.180 +         ;;(println "BC after five ticks:" (BC))
   1.181 +         ;;(println "data at PC:" (aget (memory) (PC)))
   1.182 +         ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
   1.183 +         (print-inventory)
   1.184 +         )]
   1.185 +    
   1.186 +    (shutdown!)
   1.187 +    final-state))
   1.188 +
   1.189 +
   1.190 +
   1.191 +(defn test-opcodes-2
   1.192 +  []
   1.193 +  (set-inv-mem (read-state "mid-game")
   1.194 +               [20 0x08 0x1D 0xD3 0x00 0x00 0x00
   1.195 +                0x00 0x0 0xFF])
   1.196 +  (print-inventory)
   1.197 +  (println "3 ticks") (tick) (tick) (tick)
   1.198 +  (println "PC before:" (PC))
   1.199 +  (println "SP:" (SP))
   1.200 +  (PC! (inc item-list-start))
   1.201 +  (println "PC after setting:" (PC))
   1.202 +  (println "SP:" (Integer/toBinaryString (SP)))
   1.203 +  (println "data at PC:" (aget (memory) (PC)))
   1.204 +  (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
   1.205 +  (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
   1.206 +  (println "one tick")
   1.207 +  (tick)
   1.208 +  (println "PC after one tick:" (PC))
   1.209 +  (println "data at PC:" (aget (memory) (PC)))
   1.210 +  (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
   1.211 +  (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
   1.212 +  (tick) (tick) (tick)
   1.213 +  (println "PC aftter four tick:" (PC))
   1.214 +  (println "data at PC:" (aget (memory) (PC)))
   1.215 +  (println "data at 0xD31D:" (aget (memory) 0xD31D))
   1.216 +  
   1.217 +  (print-inventory)
   1.218 +  (shutdown!))