diff clojure/com/aurellem/run/bootstrap_0.clj @ 343:652c5abee892

constructing exact item-pc layout for item-writer.
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Apr 2012 08:27:55 -0500
parents 4e0b64ea7b66
children 9366539d29b6
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/bootstrap_0.clj	Sun Apr 08 06:11:29 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/bootstrap_0.clj	Sun Apr 08 08:27:55 2012 -0500
     1.3 @@ -1030,7 +1030,6 @@
     1.4         (inc pc-item-list-start)
     1.5         (item-writer 0xD162 201 0xD162))))
     1.6  
     1.7 -
     1.8  (defn item-writer-state []
     1.9    (read-state "item-writer"))
    1.10  
    1.11 @@ -1038,9 +1037,158 @@
    1.12    (let [orig (item-writer-state)]
    1.13      (-> orig
    1.14      (print-listing 0xD162 (+ 0xD162 20))
    1.15 -    (run-moves (reduce
    1.16 -                concat [[:a] [:b]]
    1.17 -                (repeat 5 [[:a :b :start :select] []])))
    1.18 +    (run-moves (reduce concat
    1.19 +                (repeat 10 [[:a :b :start :select] []])))
    1.20      ((fn [_] (println "===========") _))
    1.21      (print-listing 0xD162 (+ 0xD162 20)))))
    1.22 -             
    1.23 \ No newline at end of file
    1.24 +
    1.25 +(defn pc-item-writer-program
    1.26 +  []
    1.27 +  (let [limit 201
    1.28 +        [target-high target-low] (disect-bytes-2 0xD162)]
    1.29 +    (flatten
    1.30 +     [[0x00  ;; (item-hack) set increment stack pointer no-op
    1.31 +       0x1E  ;; load limit into E
    1.32 +       limit
    1.33 +       0x3F  ;; (item-hack) set carry flag no-op
    1.34 +
    1.35 +       ;; load 2 into C.
    1.36 +       0x0E   ;; C == 1 means input-first nybble
    1.37 +       0x04   ;; C == 0 means input-second nybble
    1.38 +
    1.39 +       0x21 ;; load target into HL
    1.40 +       target-low
    1.41 +       target-high
    1.42 +       0x37 ;; (item-hack) set carry flag no-op
    1.43 +
    1.44 +       0x2F ;; (item-hack) cpl A
    1.45 +       0x2F ;; (item-hack) cpl A --together a spacer no-op
    1.46 +       
    1.47 +       0x00 ;; (item-hack) no-op
    1.48 +       0xF3 ;; disable interrupts
    1.49 +       ;; Input Section
    1.50 +
    1.51 +       0x3E ;; load 0x20 into A, to measure buttons
    1.52 +       0x10 
    1.53 +
    1.54 +       0x00 ;; (item-hack) no-op
    1.55 +       0xE0 ;; load A into [FF00]
    1.56 +       0x00
    1.57 +
    1.58 +       0xF0 ;; load 0xFF00 into A to get
    1.59 +       0x00 ;; button presses
    1.60 +       
    1.61 +       0xE6
    1.62 +       0x0F ;; select bottom four bits of A
    1.63 +       0x37 ;; (item-hack) set carry flag no-op
    1.64 +
    1.65 +       0x00 ;; (item-hack) no-op
    1.66 +       0xB8 ;; see if input is different (CP A B)
    1.67 +
    1.68 +       0x00 ;; (item-hack) (INC SP)
    1.69 +       0x28 ;; repeat above steps if input is not different
    1.70 +       ;; (jump relative backwards if B != A)
    1.71 +       0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)
    1.72 +
    1.73 +       0x47 ;; load A into B
    1.74 +       
    1.75 +       0x0D ;; dec C
    1.76 +       0x37 ;; (item-hack) set-carry flag
    1.77 +       ;; branch based on C:
    1.78 +       0x20 ;; JR NZ
    1.79 +       23 ;; skip "input second nybble" and "jump to target" below
    1.80 +       
    1.81 +       ;; input second nybble
    1.82 +
    1.83 +       0x0C ;; inc C
    1.84 +       0x0C ;; inc C
    1.85 +
    1.86 +       0x00 ;; (item-hack) no-op
    1.87 +       0xE6 ;; select bottom bits
    1.88 +       0x0F
    1.89 +       0x37 ;; (item-hack) set-carry flag no-op
    1.90 +
    1.91 +       0x00 ;; (item-hack) no-op
    1.92 +       0xB2 ;; (OR A D) -> A
    1.93 +
    1.94 +       0x22 ;; (do (A -> (HL)) (INC HL))
    1.95 +
    1.96 +       0x1D ;; (DEC E)
    1.97 +
    1.98 +       0x00 ;; (item-hack) 
    1.99 +       0x20 ;; jump back to input section if not done
   1.100 +       0xDA ;; literal -36 == TM 18 (counter)
   1.101 +       0x01 ;; (item-hack) set BC to literal (no-op)
   1.102 +
   1.103 +       ;; jump to target
   1.104 +       0x00  ;; (item-hack) these two bytes can be anything.
   1.105 +       0x01 
   1.106 +
   1.107 +       0x00   ;; (item-hack) no-op
   1.108 +       0xBF   ;; (CP A A) ensures Z
   1.109 +       
   1.110 +       0xCA   ;; (item-hack) jump if Z
   1.111 +       target-low
   1.112 +       target-high
   1.113 +       0x01   ;; (item-hack) will never be reached.
   1.114 +       
   1.115 +       ;; input first nybble
   1.116 +       0x00
   1.117 +       0xCB
   1.118 +       0x37  ;; swap nybbles on A
   1.119 +
   1.120 +       0x57  ;; A -> D
   1.121 +
   1.122 +       0x37  ;; (item-hack) set carry flag no-op
   1.123 +       0x18  ;; relative jump backwards
   1.124 +       0xCD  ;; literal -51 == TM05; go back to input section
   1.125 +       0x01  ;; (item-hack) will never reach this instruction
   1.126 +
   1.127 +       ]
   1.128 +      (repeat 8 [0xFF 0x01])
   1.129 +
   1.130 +      [;; jump to actual program
   1.131 +       0x00
   1.132 +       0x37  ;; (item-hack) set carry flag no-op
   1.133 +
   1.134 +       0x00
   1.135 +       0x21
   1.136 +
   1.137 +       0x3A
   1.138 +       0xD5
   1.139 +
   1.140 +       ;; 0x00
   1.141 +       ;; 0x44 ;; H -> B
   1.142 +
   1.143 +       ;; 0x00
   1.144 +       ;; 0x7D ;; L -> A
   1.145 +       
   1.146 +       ;; 0x00
   1.147 +       ;; 0x7C ;; A -> H
   1.148 +
   1.149 +       ;; 0x00
   1.150 +       ;; 0x68 ;; B -> L
   1.151 +       
   1.152 +       0x00
   1.153 +       0xE9 ;; jump to (HL)
   1.154 +       ]])))
   1.155 +
   1.156 +
   1.157 +(defn test-pc-item-writer []
   1.158 +    (-> (read-state "bootstrap-init")
   1.159 +      (set-memory pc-item-list-start 50)
   1.160 +      (set-memory-range
   1.161 +       map-function-address-start
   1.162 +       [0x8B 0xD5])
   1.163 +      (set-memory-range
   1.164 +       (inc pc-item-list-start)
   1.165 +       (pc-item-writer-program))))
   1.166 +
   1.167 +(defn test-pc-item-writer-2 []
   1.168 +  (let [orig (read-state "pc-item-writer")]
   1.169 +    (-> orig
   1.170 +    (print-listing 0xD162 (+ 0xD162 20))
   1.171 +    (run-moves (reduce concat
   1.172 +                (repeat 10 [[:a :b :start :select] []])))
   1.173 +    ((fn [_] (println "===========") _))
   1.174 +    (print-listing 0xD162 (+ 0xD162 20)))))