view clojure/com/aurellem/run/adv_choreo.clj @ 553:0901694725f0

saving progress for the night.
author Robert McIntyre <rlm@mit.edu>
date Thu, 30 Aug 2012 14:11:11 -0500
parents 9068685e7d96
children 37daf1acb212
line wrap: on
line source
1 ;;;; "Advanced Choreography" -- this is the final video for this project.
3 (ns com.aurellem.run.adv-choreo
4 (:use (com.aurellem.gb saves gb-driver util constants
5 items vbm characters money
6 rlm-assembly))
7 (:use (com.aurellem.run util music title save-corruption
8 bootstrap-0 bootstrap-1 image
9 ram-display final-cut basic-choreo))
10 (:require clojure.string)
11 (:import java.awt.image.BufferedImage)
12 (:import (javax.imageio ImageWriteParam IIOImage ImageIO))
13 (:import [com.aurellem.gb.gb_driver SaveState])
14 (:import java.io.File))
18 ;; Use the gameboy's screen to display the new programming
19 ;; instead of a side window. This will make it look much
20 ;; cooler and create a terminal-like effect as the game is
21 ;; being reprogramed. To do this, use a fixed data entry
22 ;; region in ram, and run a program that translates this
23 ;; region into the screen. Every time this data entry region
24 ;; is full, run a program that copies the data to the
25 ;; appropriate region in memory. This will cost ~15 seconds
26 ;; at the beginning to set up, and then should have minimal
27 ;; overhead (~5%) for the rest of the data transfer, but
28 ;; will have a good psychological effect for the viewer
29 ;; since he can see that something is actually happening in
30 ;; the game.
33 ;; Symbol size and type.
35 ;; use fonts from zophar's domain:
36 ;; http://www.zophar.net/utilities/fonts/8x8-font-archive.html
38 ;; Green font on black background for matrix look.
41 (defn program-data [base-address]
42 (let [image-program
43 (display-image-kernel
44 base-address
45 pinkie-pie-mark)
47 music-base-address (+ (count image-program) base-address)
49 initial-music-data
50 (midi-bytes pony-csv 0 0 0 0)
52 data-lengths
53 (map (comp count :data)
54 [(:kernel initial-music-data)
55 (:voice-1 initial-music-data)
56 (:voice-2 initial-music-data)]);; noise not needed
57 addresses
58 (map (partial + music-base-address) (reductions + 0 data-lengths))
60 final-music-data
61 (apply (partial midi-bytes pony-csv) addresses)
63 music-program
64 (concat
65 (:data (:kernel final-music-data))
66 (:data (:voice-1 final-music-data))
67 (:data (:voice-2 final-music-data))
68 (:data (:noise final-music-data)))]
70 (concat
71 image-program ;; image program falls through to music program
72 music-program)))
77 (def glyphs
78 "The sixteen 8x8 glyphs which make up the \"terminal\" font."
79 (mapv #(ImageIO/read
80 (File. user-home (str "proj/vba-clojure/font/" % ".png")))
81 ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F"]))
83 (defn glyphs-init-program
84 [start-address]
85 (let [zero-glyph (image->gb-image (glyphs 0))
87 ;; write same pallet information to all pallettes
88 A (flatten
89 [(write-byte LCD-control-register 0x00)
90 (set-palettes bg-palette-select bg-palette-data
91 (repeat 8 (first (:palettes zero-glyph))))
92 (select-LCD-bank 0)
93 (write-byte SCX-register 0)
94 (write-byte SCY-register 0)])
95 B (flatten
96 [(write-data
97 (+ start-address (count A))
98 character-data-address
99 (flatten
100 (map (comp gb-tile->bytes first :tiles image->gb-image)
101 glyphs)))])]
102 (concat A B)))
105 (defn glyph-display-program
106 [start-address
107 delay-count
108 total-glyph-count]
109 (glyphs-init-program start-address) ;; ONLY for testing!
110 )
113 (defn glyph-bootstrap-program
114 [start-address delay-count total-glyph-count]
115 (let [init [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B
116 header (concat (frame-metronome) (read-user-input))
118 glyph-display (glyph-display-program
119 (+ (count init) (count header)
120 start-address)
121 0 0) ;; ONLY FOR TESTING
123 state-machine-start-address
124 (+ start-address (count init) (count header) (count glyph-display))
125 state-machine
126 (bootstrap-state-machine state-machine-start-address)
128 return-to-header
129 (flatten
130 [0x18
131 (->signed-8-bit
132 (- (count init)
133 2 ;; this command length
134 3 ;; I have no idea why we need a 3 here
135 ;; need to investigate.
136 (count glyph-display)
137 (count header)
138 (count state-machine)))])]
140 (concat init glyph-display header state-machine return-to-header)))
145 (def main-program-base-address 0xC000)
149 ;; RLM want to transfer control here --- this is where I left off.
150 (defn begin-glyph-bootstrap
151 ([] (begin-glyph-bootstrap (relocate-main-bootstrap)))
152 ([script]
153 (let [glyph-program (glyph-bootstrap-program
154 0xDF00 0 0)
160 (defn write-all-program-data
161 ([] (write-all-program-data (relocate-main-bootstrap)))
162 ([script]
163 (let [base-address main-program-base-address]
164 (->> script
165 (write-RAM 0xFF1A [0 0 0]) ;; silence remnant music
166 (write-RAM base-address (program-data base-address))))))
168 (defn activate-program
169 ([] (activate-program (write-all-program-data)))
170 ([script]
171 (->> script
172 (transfer-control main-program-base-address)
173 (do-nothing 1800))))
176 ;; possible screen writing programs
178 ;; (program needs to stop executing at some point)
179 ;; maybe have total length counter or something?
181 ;; automatic counter that reads from program-start and clears the
182 ;; screen every 360 (* 18 20) gliphs
184 ;; advantages -- very simple and low bandwidth
185 ;; disadvantages -- hard to align counter
187 ;; implementation -- refactor main-bootstrap-program to provide a
188 ;; state-machine code-section which can be recombined into another
189 ;; program.