diff clojure/com/aurellem/run/bootstrap_0.clj @ 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
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/bootstrap_0.clj	Sat Apr 07 07:43:17 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/bootstrap_0.clj	Sat Apr 07 12:31:12 2012 -0500
     1.3 @@ -750,3 +750,128 @@
     1.4            0x04  ;; target ID        (pokeball)
     1.5            0x3E  ;; target Quantity  (lemonade)
     1.6            ]]))))
     1.7 +
     1.8 +
     1.9 +
    1.10 +
    1.11 +
    1.12 +(defn basic-writer [target-address limit return-address]
    1.13 +  (let [[target-high target-low] (disect-bytes-2 target-address)
    1.14 +        [return-high return-low] (disect-bytes-2 return-address)]
    1.15 +    (flatten
    1.16 +     [0xF3 ;; disable interrupts
    1.17 +      ;;0xC5 ;; push junk onto stack
    1.18 +      ;;0xD5
    1.19 +      ;;0xE5
    1.20 +      ;;0xF5
    1.21 +      
    1.22 +      0x1E  ;; load limit into E
    1.23 +      limit
    1.24 +
    1.25 +      0x21 ;; load target into HL
    1.26 +      target-low
    1.27 +      target-high
    1.28 +
    1.29 +      ;; load 1 into C.
    1.30 +      0x0E   ;; C == 1 means input-first nybble
    1.31 +      0x01   ;; C == 0 means input-second nybble
    1.32 +      
    1.33 +
    1.34 +      ;; Input Section
    1.35 +
    1.36 +      0x3E ;; load 0x20 into A, to measure dpad
    1.37 +      0x20 
    1.38 +
    1.39 +      0xE0 ;; load A into [FF00]
    1.40 +      0x00
    1.41 +
    1.42 +      0xF0 ;; load 0xFF00 into A to get
    1.43 +      0x00 ;; d-pad presses
    1.44 +
    1.45 +      0xE6
    1.46 +      0x0F ;; select bottom four bits of A
    1.47 +
    1.48 +      0xB8 ;; see if input is different (CP A B)
    1.49 +
    1.50 +      0x28 ;; repeat above steps if input is not different
    1.51 +           ;; (jump relative backwards if B != A)
    1.52 +      0xF5 ;; (literal -11) 
    1.53 +
    1.54 +      0x47 ;; load A into B
    1.55 +      
    1.56 +      0x0D ;; dec C
    1.57 +      ;; branch based on C:
    1.58 +      0x20 ;; JR NZ
    1.59 +      0x07 ;; skip "input first nybble" below
    1.60 +
    1.61 +      
    1.62 +      ;; input first nybble
    1.63 +     
    1.64 +      0xCB
    1.65 +      0x37  ;; swap nybbles on A
    1.66 +
    1.67 +      0x57  ;; A -> D
    1.68 +
    1.69 +      0x18
    1.70 +      0xEC  ;; literal -20 -- go back to input section
    1.71 +
    1.72 +      ;; input second nybble
    1.73 +
    1.74 +      0x0C ;; inc C
    1.75 +      
    1.76 +      0xE6 ;; select bottom bits
    1.77 +      0x0F
    1.78 +
    1.79 +      0xB2 ;; (OR A D) -> A
    1.80 +
    1.81 +      0x22 ;; (do (A -> (HL)) (INC HL))
    1.82 +
    1.83 +      0x1D ;; (DEC E)
    1.84 +
    1.85 +      0x20 ;; jump back to input section if not done
    1.86 +      0xE4 ;; literal -28
    1.87 +
    1.88 +      ;; cleanup
    1.89 +      ;;0xF1
    1.90 +      ;;0xE1
    1.91 +      ;;0xD1
    1.92 +      ;;0xC1
    1.93 +      
    1.94 +      0xFB ;; re-enable interrupts
    1.95 +      
    1.96 +      0xC3
    1.97 +      return-low
    1.98 +      return-high ])))
    1.99 +
   1.100 +
   1.101 +(defn test-basic-writer []
   1.102 +  (-> (read-state "bootstrap-init")
   1.103 +      (set-memory pc-item-list-start 50)
   1.104 +      (set-memory-range
   1.105 +       map-function-address-start
   1.106 +       (reverse (disect-bytes-2 (inc pc-item-list-start))))
   1.107 +      (set-memory-range
   1.108 +       (inc pc-item-list-start)
   1.109 +       (basic-writer 0xD162 10 0x5F0C))))
   1.110 +
   1.111 +(defn debug-basic-writer []
   1.112 +  (PC! (test-basic-writer) (inc pc-item-list-start)))
   1.113 +
   1.114 +(defn d-ticks [state n]
   1.115 +  (reduce (fn [state _] (d-tick state))
   1.116 +          state (range n)))
   1.117 +
   1.118 +(defn d-print [state message]
   1.119 +  (println message) state)
   1.120 +
   1.121 +(defn dddd
   1.122 +  []
   1.123 +  (-> (debug-basic-writer)
   1.124 +      (d-ticks 20)
   1.125 +      (set-memory 0xFF00 0xFF)
   1.126 +      (d-print "============== second cycle")
   1.127 +      (d-ticks 14)
   1.128 +      (d-print "============== end")
   1.129 +      (d-ticks 20)))
   1.130 +
   1.131 +  
   1.132 \ No newline at end of file