# HG changeset patch
# User Robert McIntyre <rlm@mit.edu>
# Date 1346346555 18000
# Node ID 9068685e7d96a1bc65845f3ca15c323f363c2a56
# Parent  b69a3dba80454da2997deaa60ffc05fcb22eafc5
moduralized main-bootstrap-program

diff -r b69a3dba8045 -r 9068685e7d96 clojure/com/aurellem/gb/rlm_assembly.clj
--- a/clojure/com/aurellem/gb/rlm_assembly.clj	Thu Aug 30 11:19:52 2012 -0500
+++ b/clojure/com/aurellem/gb/rlm_assembly.clj	Thu Aug 30 12:09:15 2012 -0500
@@ -355,14 +355,13 @@
             (partial not= symbol)
             sequence))))
 
-(defn main-bootstrap-program
-  ([] (main-bootstrap-program pokemon-list-start))
+(defn bootstrap-state-machine
   ([start-address]
      ;; Register Use:
      
      ;; ED non-volitale scratch
      
-     ;; A  user-input
+     ;; A  user-input (A MUST contain user-input for this to work!) 
      ;; HL target-address
      ;; B  bytes-to-write
      ;; C  non-volatile scratch
@@ -376,10 +375,7 @@
 
      ;; multi-action-modes
      ;; WRITE               0x47 ;; A->B
-
-     (let [init [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B
-           header (concat (frame-metronome) (read-user-input))
-           
+     (let [
            input
            [0xC1  ;; pop BC so it's not volatile
 
@@ -416,30 +412,26 @@
             0x3F ;; ;; prevent repeated nybbles
             0x54 ;;
             0x5D ;; HL->DE  \
-                 ;;          | This mess is here to do
+            ;;          | This mess is here to do
             0x12 ;; A->(DE)  | 0x22 (LDI (HL), A) without
-                 ;;         /  any repeating nybbles
+            ;;         /  any repeating nybbles
             0x05 ;; DEC bytes-to-write (B)
 
             0x23 ;; inc HL 
-            
-            0x18
-            :to-beginning] 
-
+            ]
+        
            symbols
            {:to-be-executed-address
             (reverse
              (disect-bytes-2
               (+ start-address
-                 (count header)
-                 (count init)
                  (symbol-index :to-be-executed input))))
             :to-be-executed 0x3F} ;; clear carry flag no-op
 
            program** (flatten
                       (replace
                        symbols
-                       (concat init header input output)))
+                       (concat input output)))
            
            resolve-internal-jumps
            {:output-start []
@@ -454,17 +446,36 @@
            
            resolve-external-jumps
            {:to-jump
-            (- (- (symbol-index :to-beginning program*)
-                  (symbol-index :to-jump program*)) 2)
-               
-            :to-beginning
-            (->signed-8-bit
-             (+ (count init) -1
-                (- (symbol-index :to-beginning program*))))}
-
+            (- (- (count program*)
+                  (symbol-index :to-jump program*)) 1)}
            program
            (replace resolve-external-jumps program*)]
        program)))
+     
+
+(defn main-bootstrap-program
+  ([] (main-bootstrap-program pokemon-list-start))
+  ([start-address]
+     (let [init [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B
+           header (concat (frame-metronome) (read-user-input))
+           state-machine-start-address
+           (+ start-address (count init) (count header))
+           state-machine
+           (bootstrap-state-machine state-machine-start-address)
+
+           return-to-header
+           (flatten 
+            [0x18
+             (->signed-8-bit
+              (- (count init)
+                 2 ;; this command length
+                 3 ;; I have no idea why we need a 3 here
+                   ;; need to investigate.
+                 (count header)
+                 (count state-machine)))])]
+                 
+       (concat init header state-machine return-to-header))))
+
 
 
 (defn no-consecutive-repeats? [seq]
diff -r b69a3dba8045 -r 9068685e7d96 clojure/com/aurellem/run/adv_choreo.clj
--- a/clojure/com/aurellem/run/adv_choreo.clj	Thu Aug 30 11:19:52 2012 -0500
+++ b/clojure/com/aurellem/run/adv_choreo.clj	Thu Aug 30 12:09:15 2012 -0500
@@ -92,4 +92,19 @@
      (->> script
           (transfer-control main-program-base-address)
           (do-nothing 1800))))
-     
\ No newline at end of file
+
+
+;; possible screen writing programs
+
+;; (program needs to stop executing at some point)
+;; maybe have total length counter or something?
+
+;; automatic counter that reads from program-start and clears the
+;; screen every 360 (* 18 20) gliphs
+
+;; advantages -- very simple and low bandwidth
+;; disadvantages -- hard to align counter
+
+;; implementation -- refactor main-bootstrap-program to provide a
+;; state-machine code-section which can be recombined into another
+;; program.