changeset 338:92f0011925d2

created 40-byte program to bootstrap main bootstrap program
author Robert McIntyre <rlm@mit.edu>
date Sat, 07 Apr 2012 12:31:12 -0500
parents 2dd40f6b6a1f
children 93e74ed34305
files clojure/com/aurellem/gb/util.clj clojure/com/aurellem/run/bootstrap_0.clj
diffstat 2 files changed, 125 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb/util.clj	Sat Apr 07 07:43:17 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/util.clj	Sat Apr 07 12:31:12 2012 -0500
     1.3 @@ -223,11 +223,8 @@
     1.4    (File. user-home
     1.5           "proj/vba-clojure/clojure/com/aurellem/exp/cartography"))
     1.6  
     1.7 -
     1.8 -
     1.9  (defn print-D-memory
    1.10    ([^SaveState state]
    1.11 -     
    1.12       (let [descriptions
    1.13             (clojure.string/split-lines
    1.14              (slurp cartography))]
     2.1 --- a/clojure/com/aurellem/run/bootstrap_0.clj	Sat Apr 07 07:43:17 2012 -0500
     2.2 +++ b/clojure/com/aurellem/run/bootstrap_0.clj	Sat Apr 07 12:31:12 2012 -0500
     2.3 @@ -750,3 +750,128 @@
     2.4            0x04  ;; target ID        (pokeball)
     2.5            0x3E  ;; target Quantity  (lemonade)
     2.6            ]]))))
     2.7 +
     2.8 +
     2.9 +
    2.10 +
    2.11 +
    2.12 +(defn basic-writer [target-address limit return-address]
    2.13 +  (let [[target-high target-low] (disect-bytes-2 target-address)
    2.14 +        [return-high return-low] (disect-bytes-2 return-address)]
    2.15 +    (flatten
    2.16 +     [0xF3 ;; disable interrupts
    2.17 +      ;;0xC5 ;; push junk onto stack
    2.18 +      ;;0xD5
    2.19 +      ;;0xE5
    2.20 +      ;;0xF5
    2.21 +      
    2.22 +      0x1E  ;; load limit into E
    2.23 +      limit
    2.24 +
    2.25 +      0x21 ;; load target into HL
    2.26 +      target-low
    2.27 +      target-high
    2.28 +
    2.29 +      ;; load 1 into C.
    2.30 +      0x0E   ;; C == 1 means input-first nybble
    2.31 +      0x01   ;; C == 0 means input-second nybble
    2.32 +      
    2.33 +
    2.34 +      ;; Input Section
    2.35 +
    2.36 +      0x3E ;; load 0x20 into A, to measure dpad
    2.37 +      0x20 
    2.38 +
    2.39 +      0xE0 ;; load A into [FF00]
    2.40 +      0x00
    2.41 +
    2.42 +      0xF0 ;; load 0xFF00 into A to get
    2.43 +      0x00 ;; d-pad presses
    2.44 +
    2.45 +      0xE6
    2.46 +      0x0F ;; select bottom four bits of A
    2.47 +
    2.48 +      0xB8 ;; see if input is different (CP A B)
    2.49 +
    2.50 +      0x28 ;; repeat above steps if input is not different
    2.51 +           ;; (jump relative backwards if B != A)
    2.52 +      0xF5 ;; (literal -11) 
    2.53 +
    2.54 +      0x47 ;; load A into B
    2.55 +      
    2.56 +      0x0D ;; dec C
    2.57 +      ;; branch based on C:
    2.58 +      0x20 ;; JR NZ
    2.59 +      0x07 ;; skip "input first nybble" below
    2.60 +
    2.61 +      
    2.62 +      ;; input first nybble
    2.63 +     
    2.64 +      0xCB
    2.65 +      0x37  ;; swap nybbles on A
    2.66 +
    2.67 +      0x57  ;; A -> D
    2.68 +
    2.69 +      0x18
    2.70 +      0xEC  ;; literal -20 -- go back to input section
    2.71 +
    2.72 +      ;; input second nybble
    2.73 +
    2.74 +      0x0C ;; inc C
    2.75 +      
    2.76 +      0xE6 ;; select bottom bits
    2.77 +      0x0F
    2.78 +
    2.79 +      0xB2 ;; (OR A D) -> A
    2.80 +
    2.81 +      0x22 ;; (do (A -> (HL)) (INC HL))
    2.82 +
    2.83 +      0x1D ;; (DEC E)
    2.84 +
    2.85 +      0x20 ;; jump back to input section if not done
    2.86 +      0xE4 ;; literal -28
    2.87 +
    2.88 +      ;; cleanup
    2.89 +      ;;0xF1
    2.90 +      ;;0xE1
    2.91 +      ;;0xD1
    2.92 +      ;;0xC1
    2.93 +      
    2.94 +      0xFB ;; re-enable interrupts
    2.95 +      
    2.96 +      0xC3
    2.97 +      return-low
    2.98 +      return-high ])))
    2.99 +
   2.100 +
   2.101 +(defn test-basic-writer []
   2.102 +  (-> (read-state "bootstrap-init")
   2.103 +      (set-memory pc-item-list-start 50)
   2.104 +      (set-memory-range
   2.105 +       map-function-address-start
   2.106 +       (reverse (disect-bytes-2 (inc pc-item-list-start))))
   2.107 +      (set-memory-range
   2.108 +       (inc pc-item-list-start)
   2.109 +       (basic-writer 0xD162 10 0x5F0C))))
   2.110 +
   2.111 +(defn debug-basic-writer []
   2.112 +  (PC! (test-basic-writer) (inc pc-item-list-start)))
   2.113 +
   2.114 +(defn d-ticks [state n]
   2.115 +  (reduce (fn [state _] (d-tick state))
   2.116 +          state (range n)))
   2.117 +
   2.118 +(defn d-print [state message]
   2.119 +  (println message) state)
   2.120 +
   2.121 +(defn dddd
   2.122 +  []
   2.123 +  (-> (debug-basic-writer)
   2.124 +      (d-ticks 20)
   2.125 +      (set-memory 0xFF00 0xFF)
   2.126 +      (d-print "============== second cycle")
   2.127 +      (d-ticks 14)
   2.128 +      (d-print "============== end")
   2.129 +      (d-ticks 20)))
   2.130 +
   2.131 +  
   2.132 \ No newline at end of file