diff clojure/com/aurellem/gb/rlm_assembly.clj @ 552:9068685e7d96

moduralized main-bootstrap-program
author Robert McIntyre <rlm@mit.edu>
date Thu, 30 Aug 2012 12:09:15 -0500
parents 21b8b3350b20
children 96ee9d72aeb9
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb/rlm_assembly.clj	Thu Aug 30 11:19:52 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/rlm_assembly.clj	Thu Aug 30 12:09:15 2012 -0500
     1.3 @@ -355,14 +355,13 @@
     1.4              (partial not= symbol)
     1.5              sequence))))
     1.6  
     1.7 -(defn main-bootstrap-program
     1.8 -  ([] (main-bootstrap-program pokemon-list-start))
     1.9 +(defn bootstrap-state-machine
    1.10    ([start-address]
    1.11       ;; Register Use:
    1.12       
    1.13       ;; ED non-volitale scratch
    1.14       
    1.15 -     ;; A  user-input
    1.16 +     ;; A  user-input (A MUST contain user-input for this to work!) 
    1.17       ;; HL target-address
    1.18       ;; B  bytes-to-write
    1.19       ;; C  non-volatile scratch
    1.20 @@ -376,10 +375,7 @@
    1.21  
    1.22       ;; multi-action-modes
    1.23       ;; WRITE               0x47 ;; A->B
    1.24 -
    1.25 -     (let [init [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B
    1.26 -           header (concat (frame-metronome) (read-user-input))
    1.27 -           
    1.28 +     (let [
    1.29             input
    1.30             [0xC1  ;; pop BC so it's not volatile
    1.31  
    1.32 @@ -416,30 +412,26 @@
    1.33              0x3F ;; ;; prevent repeated nybbles
    1.34              0x54 ;;
    1.35              0x5D ;; HL->DE  \
    1.36 -                 ;;          | This mess is here to do
    1.37 +            ;;          | This mess is here to do
    1.38              0x12 ;; A->(DE)  | 0x22 (LDI (HL), A) without
    1.39 -                 ;;         /  any repeating nybbles
    1.40 +            ;;         /  any repeating nybbles
    1.41              0x05 ;; DEC bytes-to-write (B)
    1.42  
    1.43              0x23 ;; inc HL 
    1.44 -            
    1.45 -            0x18
    1.46 -            :to-beginning] 
    1.47 -
    1.48 +            ]
    1.49 +        
    1.50             symbols
    1.51             {:to-be-executed-address
    1.52              (reverse
    1.53               (disect-bytes-2
    1.54                (+ start-address
    1.55 -                 (count header)
    1.56 -                 (count init)
    1.57                   (symbol-index :to-be-executed input))))
    1.58              :to-be-executed 0x3F} ;; clear carry flag no-op
    1.59  
    1.60             program** (flatten
    1.61                        (replace
    1.62                         symbols
    1.63 -                       (concat init header input output)))
    1.64 +                       (concat input output)))
    1.65             
    1.66             resolve-internal-jumps
    1.67             {:output-start []
    1.68 @@ -454,17 +446,36 @@
    1.69             
    1.70             resolve-external-jumps
    1.71             {:to-jump
    1.72 -            (- (- (symbol-index :to-beginning program*)
    1.73 -                  (symbol-index :to-jump program*)) 2)
    1.74 -               
    1.75 -            :to-beginning
    1.76 -            (->signed-8-bit
    1.77 -             (+ (count init) -1
    1.78 -                (- (symbol-index :to-beginning program*))))}
    1.79 -
    1.80 +            (- (- (count program*)
    1.81 +                  (symbol-index :to-jump program*)) 1)}
    1.82             program
    1.83             (replace resolve-external-jumps program*)]
    1.84         program)))
    1.85 +     
    1.86 +
    1.87 +(defn main-bootstrap-program
    1.88 +  ([] (main-bootstrap-program pokemon-list-start))
    1.89 +  ([start-address]
    1.90 +     (let [init [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B
    1.91 +           header (concat (frame-metronome) (read-user-input))
    1.92 +           state-machine-start-address
    1.93 +           (+ start-address (count init) (count header))
    1.94 +           state-machine
    1.95 +           (bootstrap-state-machine state-machine-start-address)
    1.96 +
    1.97 +           return-to-header
    1.98 +           (flatten 
    1.99 +            [0x18
   1.100 +             (->signed-8-bit
   1.101 +              (- (count init)
   1.102 +                 2 ;; this command length
   1.103 +                 3 ;; I have no idea why we need a 3 here
   1.104 +                   ;; need to investigate.
   1.105 +                 (count header)
   1.106 +                 (count state-machine)))])]
   1.107 +                 
   1.108 +       (concat init header state-machine return-to-header))))
   1.109 +
   1.110  
   1.111  
   1.112  (defn no-consecutive-repeats? [seq]