rlm@550
|
1 ;;;; "Advanced Choreography" -- this is the final video for this project.
|
rlm@550
|
2
|
rlm@550
|
3 (ns com.aurellem.run.adv-choreo
|
rlm@550
|
4 (:use (com.aurellem.gb saves gb-driver util constants
|
rlm@550
|
5 items vbm characters money
|
rlm@550
|
6 rlm-assembly))
|
rlm@550
|
7 (:use (com.aurellem.run util music title save-corruption
|
rlm@550
|
8 bootstrap-0 bootstrap-1 image
|
rlm@550
|
9 ram-display final-cut basic-choreo))
|
rlm@550
|
10 (:require clojure.string)
|
rlm@553
|
11 (:import java.awt.image.BufferedImage)
|
rlm@553
|
12 (:import (javax.imageio ImageWriteParam IIOImage ImageIO))
|
rlm@550
|
13 (:import [com.aurellem.gb.gb_driver SaveState])
|
rlm@550
|
14 (:import java.io.File))
|
rlm@550
|
15
|
rlm@550
|
16
|
rlm@550
|
17
|
rlm@550
|
18 ;; Use the gameboy's screen to display the new programming
|
rlm@550
|
19 ;; instead of a side window. This will make it look much
|
rlm@550
|
20 ;; cooler and create a terminal-like effect as the game is
|
rlm@550
|
21 ;; being reprogramed. To do this, use a fixed data entry
|
rlm@550
|
22 ;; region in ram, and run a program that translates this
|
rlm@550
|
23 ;; region into the screen. Every time this data entry region
|
rlm@550
|
24 ;; is full, run a program that copies the data to the
|
rlm@550
|
25 ;; appropriate region in memory. This will cost ~15 seconds
|
rlm@550
|
26 ;; at the beginning to set up, and then should have minimal
|
rlm@550
|
27 ;; overhead (~5%) for the rest of the data transfer, but
|
rlm@550
|
28 ;; will have a good psychological effect for the viewer
|
rlm@550
|
29 ;; since he can see that something is actually happening in
|
rlm@550
|
30 ;; the game.
|
rlm@550
|
31
|
rlm@550
|
32
|
rlm@551
|
33 ;; Symbol size and type.
|
rlm@551
|
34
|
rlm@551
|
35 ;; use fonts from zophar's domain:
|
rlm@551
|
36 ;; http://www.zophar.net/utilities/fonts/8x8-font-archive.html
|
rlm@551
|
37
|
rlm@551
|
38 ;; Green font on black background for matrix look.
|
rlm@551
|
39
|
rlm@551
|
40
|
rlm@551
|
41 (defn program-data [base-address]
|
rlm@551
|
42 (let [image-program
|
rlm@551
|
43 (display-image-kernel
|
rlm@551
|
44 base-address
|
rlm@554
|
45
|
rlm@576
|
46 ;;pinkie-pie-mark
|
rlm@576
|
47 test-image-color
|
rlm@554
|
48
|
rlm@554
|
49 )
|
rlm@554
|
50
|
rlm@551
|
51
|
rlm@551
|
52 music-base-address (+ (count image-program) base-address)
|
rlm@551
|
53
|
rlm@551
|
54 initial-music-data
|
rlm@551
|
55 (midi-bytes pony-csv 0 0 0 0)
|
rlm@551
|
56
|
rlm@551
|
57 data-lengths
|
rlm@551
|
58 (map (comp count :data)
|
rlm@551
|
59 [(:kernel initial-music-data)
|
rlm@551
|
60 (:voice-1 initial-music-data)
|
rlm@551
|
61 (:voice-2 initial-music-data)]);; noise not needed
|
rlm@551
|
62 addresses
|
rlm@551
|
63 (map (partial + music-base-address) (reductions + 0 data-lengths))
|
rlm@551
|
64
|
rlm@551
|
65 final-music-data
|
rlm@551
|
66 (apply (partial midi-bytes pony-csv) addresses)
|
rlm@551
|
67
|
rlm@551
|
68 music-program
|
rlm@551
|
69 (concat
|
rlm@551
|
70 (:data (:kernel final-music-data))
|
rlm@551
|
71 (:data (:voice-1 final-music-data))
|
rlm@551
|
72 (:data (:voice-2 final-music-data))
|
rlm@551
|
73 (:data (:noise final-music-data)))]
|
rlm@551
|
74
|
rlm@551
|
75 (concat
|
rlm@551
|
76 image-program ;; image program falls through to music program
|
rlm@554
|
77
|
rlm@576
|
78 (infinite-loop)
|
rlm@576
|
79 ;;music-program
|
rlm@554
|
80
|
rlm@554
|
81 )))
|
rlm@551
|
82
|
rlm@551
|
83
|
rlm@551
|
84
|
rlm@553
|
85
|
rlm@553
|
86 (def glyphs
|
rlm@553
|
87 "The sixteen 8x8 glyphs which make up the \"terminal\" font."
|
rlm@553
|
88 (mapv #(ImageIO/read
|
rlm@553
|
89 (File. user-home (str "proj/vba-clojure/font/" % ".png")))
|
rlm@588
|
90 ["0" "1" "2" "3" "4" "5" "6" "7" "8"
|
rlm@588
|
91 "9" "A" "B" "C" "D" "E" "F" "_"]))
|
rlm@588
|
92
|
rlm@553
|
93
|
rlm@554
|
94 (defn glyph-init-program
|
rlm@553
|
95 [start-address]
|
rlm@553
|
96 (let [zero-glyph (image->gb-image (glyphs 0))
|
rlm@553
|
97
|
rlm@553
|
98 ;; write same pallet information to all pallettes
|
rlm@553
|
99 A (flatten
|
rlm@554
|
100 [(write-byte LCD-control-register 0x00);; disable LCD protection
|
rlm@553
|
101 (set-palettes bg-palette-select bg-palette-data
|
rlm@553
|
102 (repeat 8 (first (:palettes zero-glyph))))
|
rlm@553
|
103 (select-LCD-bank 0)
|
rlm@553
|
104 (write-byte SCX-register 0)
|
rlm@553
|
105 (write-byte SCY-register 0)])
|
rlm@553
|
106 B (flatten
|
rlm@553
|
107 [(write-data
|
rlm@553
|
108 (+ start-address (count A))
|
rlm@553
|
109 character-data-address
|
rlm@553
|
110 (flatten
|
rlm@553
|
111 (map (comp gb-tile->bytes first :tiles image->gb-image)
|
rlm@554
|
112 glyphs)))
|
rlm@553
|
113
|
rlm@554
|
114
|
rlm@554
|
115 (write-byte
|
rlm@554
|
116 LCD-control-register
|
rlm@554
|
117 (Integer/parseInt
|
rlm@554
|
118 (str
|
rlm@554
|
119 "1" ;; LCDC on/off
|
rlm@554
|
120 "0" ;; Window code area
|
rlm@554
|
121 "0" ;; Windowing on?
|
rlm@554
|
122 "1" ;; BG tile base (1 = 0x8000)
|
rlm@554
|
123 "0" ;; BG-1 or BG-2 ?
|
rlm@554
|
124 "0" ;; OBJ-block composition
|
rlm@554
|
125 "0" ;; OBJ-on flag
|
rlm@554
|
126 "1") ;; no-effect
|
rlm@554
|
127 2))])]
|
rlm@554
|
128 (concat A B )))
|
rlm@553
|
129
|
rlm@558
|
130
|
rlm@560
|
131
|
rlm@553
|
132 (defn glyph-display-program
|
rlm@577
|
133 [start-address]
|
rlm@556
|
134 (let [data-start (+ 2 start-address)
|
rlm@557
|
135 load-data
|
rlm@557
|
136 (flatten
|
rlm@557
|
137 [;; data region
|
rlm@557
|
138 0x18
|
rlm@584
|
139 2
|
rlm@584
|
140 0 0;; current row and column
|
rlm@557
|
141 ;; save all registers
|
rlm@557
|
142 0xC5 0xD5 0xE5 0xF5
|
rlm@557
|
143
|
rlm@557
|
144 ;; load data from data region into registers
|
rlm@562
|
145
|
rlm@577
|
146 0xF5 ;; push A, which contains current glyph
|
rlm@577
|
147
|
rlm@577
|
148 0x21
|
rlm@558
|
149 (reverse (disect-bytes-2 data-start))
|
rlm@577
|
150 ;; load row and column into DE
|
rlm@577
|
151 0x2A 0x57 ;; row -> D
|
rlm@577
|
152 0x2A 0x5F ;; column -> E
|
rlm@557
|
153 ])
|
rlm@557
|
154
|
rlm@560
|
155
|
rlm@562
|
156 display-glyph
|
rlm@568
|
157 (let [init*
|
rlm@563
|
158 (flatten
|
rlm@577
|
159 [(repeat 100 0)
|
rlm@565
|
160 ;; Reset HL to initial value
|
rlm@567
|
161
|
rlm@568
|
162 ;; clear screen if we are at 0,0
|
rlm@585
|
163 0x7A 0xB3 ;; D->A, OR E A ==> (= D E 0)
|
rlm@571
|
164 0x20 ;; skip clear-screen if D and E are not both zero
|
rlm@572
|
165 :clear-screen-length])
|
rlm@562
|
166
|
rlm@563
|
167 clear-screen
|
rlm@568
|
168 (flatten
|
rlm@569
|
169 [;; save all registers
|
rlm@569
|
170 0xC5 0xD5 0xE5 0xF5
|
rlm@569
|
171
|
rlm@570
|
172 (select-LCD-bank 0)
|
rlm@570
|
173 ;; write 0x00 to memory locations
|
rlm@570
|
174 ;; 0x9800 to 0x9A34
|
rlm@570
|
175 0x21
|
rlm@570
|
176 0x00 0x98 ;; load 0x9800 into HL
|
rlm@570
|
177
|
rlm@570
|
178 0x16 3 ;; 3 -> D
|
rlm@570
|
179 0x1E 190 ;; 188 -> E
|
rlm@570
|
180
|
rlm@584
|
181 ;; Empty space Character ID
|
rlm@584
|
182 0x3E 16 ;; 0-> A
|
rlm@570
|
183
|
rlm@570
|
184 ;; begin of do-while loop
|
rlm@584
|
185 0x22 ;; load A to 0x9800
|
rlm@570
|
186 0x1D ;; dec E
|
rlm@570
|
187 0x20
|
rlm@570
|
188 (->signed-8-bit -4)
|
rlm@570
|
189 0x15 ;; dec D
|
rlm@570
|
190 0x1E 190 ;; 188 -> E
|
rlm@570
|
191 0x20
|
rlm@570
|
192 (->signed-8-bit -8)
|
rlm@570
|
193 ;; end of do-while-loop
|
rlm@572
|
194
|
rlm@569
|
195 ;; restore all registers
|
rlm@572
|
196 0xF1 0xE1 0xD1 0xC1])
|
rlm@572
|
197
|
rlm@584
|
198
|
rlm@573
|
199 increment-row-column
|
rlm@573
|
200 [;; D contains row and E contains column
|
rlm@573
|
201
|
rlm@573
|
202 ;; every time column (E) reaches 20, set
|
rlm@573
|
203 ;; column to 0 and increment row
|
rlm@573
|
204 0x1C ;; inc E
|
rlm@573
|
205 0x3E 20 0xBB ;; compare E to 20
|
rlm@578
|
206 0x20
|
rlm@579
|
207 3
|
rlm@587
|
208 0x14 ;; increment row
|
rlm@587
|
209 0x1E 0 ;; set column to 0
|
rlm@578
|
210
|
rlm@579
|
211
|
rlm@587
|
212 ;; if row==18, set row to 0
|
rlm@579
|
213 0x3E 18
|
rlm@579
|
214 0xBA
|
rlm@579
|
215 0x20
|
rlm@579
|
216 2
|
rlm@587
|
217 0x16 0]
|
rlm@573
|
218
|
rlm@573
|
219 set-HL-from-row-and-column
|
rlm@581
|
220 (flatten
|
rlm@573
|
221 [;; formula for memory offset is:
|
rlm@577
|
222 ;; (+ 0x9800 (* 32 row) column) ==
|
rlm@587
|
223 0x21 0x00 0x98 ;; load HL with 0x9800
|
rlm@577
|
224
|
rlm@584
|
225 0x06 0
|
rlm@584
|
226 0x4B ;; columns (E) -> BC
|
rlm@584
|
227 0x09 ;; HL += columns
|
rlm@573
|
228
|
rlm@579
|
229
|
rlm@579
|
230 0xAF ;; 0 -> A
|
rlm@579
|
231
|
rlm@579
|
232 0x06 0
|
rlm@579
|
233 0x0E 32 ;; load 32 into BC
|
rlm@579
|
234
|
rlm@581
|
235 0xBA ;; CP A D
|
rlm@583
|
236 0x28 ;; skip this next section if A == D
|
rlm@579
|
237 4
|
rlm@579
|
238 0x09 ;; HL += 32
|
rlm@579
|
239 0x3C
|
rlm@579
|
240 0x18
|
rlm@587
|
241
|
rlm@588
|
242 (->signed-8-bit -7)])
|
rlm@573
|
243
|
rlm@572
|
244 render-glyph
|
rlm@572
|
245 (flatten
|
rlm@577
|
246 [;; Render each nybble of A as a character
|
rlm@577
|
247 ;; there are two characters to a glyph.
|
rlm@584
|
248
|
rlm@577
|
249 set-HL-from-row-and-column
|
rlm@581
|
250
|
rlm@575
|
251 0xF1 ;; pop A, now A is equal to key input
|
rlm@575
|
252 0xF5 ;; save A
|
rlm@575
|
253
|
rlm@575
|
254 0xE6 0xF0 ;; clear second nybble
|
rlm@575
|
255 0xCB 0x37 ;; swap nybbles
|
rlm@579
|
256 0x77 ;; store A in video RAM as a character (pun)
|
rlm@577
|
257 increment-row-column
|
rlm@581
|
258
|
rlm@581
|
259
|
rlm@577
|
260 set-HL-from-row-and-column
|
rlm@581
|
261
|
rlm@575
|
262 0xF1 ;; restore A
|
rlm@575
|
263 0xE6 0x0F ;; select second nybble
|
rlm@579
|
264 0x77 ;; store second nybble as character
|
rlm@588
|
265 increment-row-column])
|
rlm@563
|
266
|
rlm@572
|
267
|
rlm@568
|
268 init (replace
|
rlm@568
|
269 {:clear-screen-length (count clear-screen)} init*)
|
rlm@568
|
270 ]
|
rlm@568
|
271
|
rlm@572
|
272 (concat init clear-screen render-glyph))
|
rlm@563
|
273
|
rlm@557
|
274 cleanup
|
rlm@557
|
275 ;; restore all registers
|
rlm@562
|
276 (flatten
|
rlm@582
|
277 [;; Reset HL to initial data-start value
|
rlm@558
|
278 0x21
|
rlm@558
|
279 (reverse (disect-bytes-2 data-start))
|
rlm@579
|
280 ;;0x23
|
rlm@582
|
281 ;; write variables
|
rlm@579
|
282 0x7A 0x22 ;; D -> rows -> to RAM
|
rlm@569
|
283 0x7B 0x22 ;; E -> columns
|
rlm@558
|
284 ])
|
rlm@558
|
285
|
rlm@558
|
286 stack-cleanup
|
rlm@557
|
287 [0xF1 0xE1 0xD1 0xC1]
|
rlm@576
|
288 ]
|
rlm@558
|
289 (concat load-data
|
rlm@558
|
290 display-glyph
|
rlm@558
|
291 cleanup stack-cleanup)))
|
rlm@556
|
292
|
rlm@556
|
293 (def main-program-base-address 0xC000)
|
rlm@553
|
294
|
rlm@553
|
295 (defn glyph-bootstrap-program
|
rlm@553
|
296 [start-address delay-count total-glyph-count]
|
rlm@553
|
297 (let [init [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B
|
rlm@554
|
298 header (concat (frame-metronome) (read-user-input))
|
rlm@553
|
299
|
rlm@553
|
300 glyph-display (glyph-display-program
|
rlm@559
|
301 (+ (count init)
|
rlm@562
|
302 (count header)
|
rlm@577
|
303 start-address))
|
rlm@558
|
304 ;;(- (count (program-data 0)) 100))
|
rlm@553
|
305
|
rlm@553
|
306 state-machine-start-address
|
rlm@553
|
307 (+ start-address (count init) (count header) (count glyph-display))
|
rlm@553
|
308 state-machine
|
rlm@553
|
309 (bootstrap-state-machine state-machine-start-address)
|
rlm@553
|
310
|
rlm@553
|
311 return-to-header
|
rlm@553
|
312 (flatten
|
rlm@564
|
313 [0xC3
|
rlm@564
|
314 (reverse (disect-bytes-2
|
rlm@564
|
315 (+ (count init) start-address)))])]
|
rlm@562
|
316 (concat init header glyph-display state-machine return-to-header)))
|
rlm@553
|
317
|
rlm@556
|
318
|
rlm@551
|
319
|
rlm@558
|
320 (defn-memo begin-glyph-bootstrap
|
rlm@554
|
321 ([] (begin-glyph-bootstrap (launch-main-bootstrap-program)))
|
rlm@554
|
322 ([script]
|
rlm@554
|
323 (let [glyph-init (glyph-init-program relocated-bootstrap-start)
|
rlm@554
|
324 main-glyph-start (+ relocated-bootstrap-start
|
rlm@554
|
325 (count glyph-init))
|
rlm@554
|
326 glyph-program (glyph-bootstrap-program
|
rlm@554
|
327 main-glyph-start 0 0)]
|
rlm@554
|
328 (->> script
|
rlm@554
|
329 (do-nothing 2)
|
rlm@554
|
330 ;; begin glyph program
|
rlm@554
|
331 (write-RAM 0xFF1A [0 0 0]) ;; silence remnant music
|
rlm@554
|
332
|
rlm@554
|
333 (write-RAM
|
rlm@554
|
334 relocated-bootstrap-start
|
rlm@554
|
335 (concat glyph-init glyph-program))
|
rlm@554
|
336 (transfer-control relocated-bootstrap-start)
|
rlm@555
|
337 (do-nothing 1)
|
rlm@553
|
338
|
rlm@554
|
339 ))))
|
rlm@553
|
340
|
rlm@551
|
341 (defn write-all-program-data
|
rlm@554
|
342 ([] (write-all-program-data (begin-glyph-bootstrap)))
|
rlm@551
|
343 ([script]
|
rlm@551
|
344 (let [base-address main-program-base-address]
|
rlm@551
|
345 (->> script
|
rlm@551
|
346 (write-RAM base-address (program-data base-address))))))
|
rlm@551
|
347
|
rlm@551
|
348 (defn activate-program
|
rlm@551
|
349 ([] (activate-program (write-all-program-data)))
|
rlm@551
|
350 ([script]
|
rlm@551
|
351 (->> script
|
rlm@551
|
352 (transfer-control main-program-base-address)
|
rlm@554
|
353 ;;(do-nothing 1800)
|
rlm@554
|
354 (do-nothing 50)
|
rlm@554
|
355 )))
|
rlm@552
|
356
|
rlm@552
|
357
|
rlm@552
|
358 ;; possible screen writing programs
|
rlm@552
|
359
|
rlm@552
|
360 ;; (program needs to stop executing at some point)
|
rlm@552
|
361 ;; maybe have total length counter or something?
|
rlm@552
|
362
|
rlm@552
|
363 ;; automatic counter that reads from program-start and clears the
|
rlm@552
|
364 ;; screen every 360 (* 18 20) gliphs
|
rlm@552
|
365
|
rlm@552
|
366 ;; advantages -- very simple and low bandwidth
|
rlm@552
|
367 ;; disadvantages -- hard to align counter
|
rlm@552
|
368
|
rlm@552
|
369 ;; implementation -- refactor main-bootstrap-program to provide a
|
rlm@552
|
370 ;; state-machine code-section which can be recombined into another
|
rlm@552
|
371 ;; program.
|