diff clojure/com/aurellem/run/bootstrap_1.clj @ 416:21b8b3350b20

everything works :) now I have total control over the game.
author Robert McIntyre <rlm@mit.edu>
date Sat, 14 Apr 2012 05:41:55 -0500
parents f2f1e0b8c1c7
children 0b6624c1291c
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/bootstrap_1.clj	Sat Apr 14 04:09:51 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/bootstrap_1.clj	Sat Apr 14 05:41:55 2012 -0500
     1.3 @@ -779,18 +779,18 @@
     1.4                         0xD162 (+ 0xD162 (count pattern)))
     1.5            pattern))))
     1.6  
     1.7 -(defn launch-main-bootstrap-program
     1.8 +(defn-memo launch-main-bootstrap-program
     1.9    ([] (launch-main-bootstrap-program
    1.10         (control-checkpoint)
    1.11         ;;(launch-bootstrap-program)
    1.12         ))
    1.13    ([script]
    1.14 -     (->> script
    1.15 -          (play-moves
    1.16 -           (bootstrap-pattern (main-bootstrap-program)))
    1.17 -          (play-moves
    1.18 -           (take 263 (interleave (repeat 1000 [:b])
    1.19 -                                 (repeat 1000 [])))))))
    1.20 +       (->> script
    1.21 +            (play-moves
    1.22 +             (bootstrap-pattern (main-bootstrap-program)))
    1.23 +            (play-moves
    1.24 +             (take 253 (interleave (repeat 1000 [:b])
    1.25 +                                   (repeat 1000 [])))))))
    1.26  
    1.27  (defn test-main-bootstrap-integrety
    1.28    []
    1.29 @@ -811,17 +811,32 @@
    1.30            (map buttons
    1.31                 [set-H-mode target-high 0x00
    1.32                  set-L-mode target-low  0x00])))))
    1.33 -                
    1.34 +
    1.35 +(defn write-RAM-segment
    1.36 +  "Assumes that the game is under control of the main-bootstrap
    1.37 +   program in MODE-SELECT mode and that target-address has been
    1.38 +   appropriately set, and writes 255 bytes or less to RAM."
    1.39 +  [segment script]
    1.40 +  (->> script
    1.41 +       (play-moves
    1.42 +        (map buttons
    1.43 +             [write-mode (count segment)]))
    1.44 +       (play-moves (map buttons segment))
    1.45 +       (play-moves [[]])))
    1.46 +
    1.47  (defn write-RAM
    1.48    "Assumes that the game is under control of the main-bootstrap
    1.49     program in MODE-SELECT mode, and rewrites RAM starting at
    1.50     'start-address with 'new-ram."
    1.51    [start-address new-ram script]
    1.52 -  (->> script
    1.53 -       (set-target-address start-address)
    1.54 -       (play-moves [(buttons (count new-ram))])
    1.55 -       (play-moves (map buttons new-ram))))
    1.56 -
    1.57 +  (loop [s (set-target-address start-address script)
    1.58 +         to-write new-ram]
    1.59 +    (if (< (count to-write) 0x100)
    1.60 +      (write-RAM-segment to-write s)
    1.61 +      (recur
    1.62 +       (write-RAM-segment (take 0xFF to-write) s)
    1.63 +       (drop 0xFF to-write)))))
    1.64 +          
    1.65  (defn transfer-control
    1.66    "Assumes that the game is under control of the main-bootstrap
    1.67     program in MODE-SELECT mode, and jumps to the target-address."
    1.68 @@ -830,17 +845,23 @@
    1.69         (set-target-address target-address)
    1.70         (play-moves [(buttons jump-mode)])))
    1.71  
    1.72 -(defn relocate-main-bootstrap
    1.73 +(def box-target (+ 90 pokemon-box-1-address))
    1.74 +
    1.75 +(defn-memo relocate-main-bootstrap
    1.76    ([] (relocate-main-bootstrap (launch-main-bootstrap-program)))
    1.77    ([script]
    1.78       (let [target (+ 90 pokemon-box-1-address)]
    1.79         (->> script
    1.80 -            (do-nothing 500)))))
    1.81 +            (do-nothing 2)
    1.82 +            (write-RAM target (main-bootstrap-program target))
    1.83 +            (do-nothing 1)
    1.84 +            (transfer-control target)
    1.85 +            (do-nothing 1)))))
    1.86  
    1.87  (def mid-game-data
    1.88    (subvec (vec (memory (mid-game)))
    1.89            pokemon-list-start
    1.90 -          (+ pokemon-list-start 100)))
    1.91 +          (+ pokemon-list-start 697)))
    1.92  
    1.93  (def mid-game-map-address 0x46BC)
    1.94  
    1.95 @@ -849,9 +870,39 @@
    1.96    ([script]
    1.97       (->> script
    1.98            (do-nothing 10)
    1.99 -          (write-RAM pokemon-list-start mid-game-data))))
   1.100 +          (write-RAM pokemon-list-start
   1.101 +                     mid-game-data))))
   1.102 +(defn test-set-data
   1.103 +  ([] (test-set-data (relocate-main-bootstrap)))
   1.104 +  ([script]
   1.105 +     (->> script
   1.106 +          (do-nothing 10)
   1.107 +          (write-RAM pokemon-list-start
   1.108 +                     (repeat 500 0xCC)))))
   1.109 +      
   1.110 +(defn test-mid-game-transfer []
   1.111 +  (= (subvec (vec (memory (second (set-mid-game-data))))
   1.112 +             pokemon-list-start
   1.113 +             (+ pokemon-list-start 500))
   1.114 +     (subvec (vec (memory (mid-game)))
   1.115 +             pokemon-list-start
   1.116 +             (+ pokemon-list-start 500))))
   1.117  
   1.118 -
   1.119 +(defn return-to-pokemon-kernel
   1.120 +  ([] (return-to-pokemon-kernel (set-mid-game-data)))
   1.121 +  ([script]
   1.122 +     (let [scratch (+ 200 pokemon-box-1-address)
   1.123 +           return-program
   1.124 +           (flatten
   1.125 +            [0xFB
   1.126 +             0xC3
   1.127 +             (reverse (disect-bytes-2 mid-game-map-address))])]
   1.128 +       (->> script
   1.129 +            (write-RAM scratch return-program)
   1.130 +            (transfer-control scratch)
   1.131 +            (do-nothing 1)))))
   1.132 +                                              
   1.133 +       
   1.134  
   1.135  
   1.136