annotate clojure/com/aurellem/run/adv_choreo.clj @ 570:a6dcb6236fbc

clear-screen complete
author Robert McIntyre <rlm@mit.edu>
date Sat, 01 Sep 2012 03:36:33 -0500
parents 3fcc395b76ef
children c6b01ff2f1e5
rev   line source
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@554 46 ;;pinkie-pie-mark
rlm@554 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@554 78 (infinite-loop)
rlm@554 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@553 90 ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F"]))
rlm@553 91
rlm@554 92 (defn glyph-init-program
rlm@553 93 [start-address]
rlm@553 94 (let [zero-glyph (image->gb-image (glyphs 0))
rlm@553 95
rlm@553 96 ;; write same pallet information to all pallettes
rlm@553 97 A (flatten
rlm@554 98 [(write-byte LCD-control-register 0x00);; disable LCD protection
rlm@553 99 (set-palettes bg-palette-select bg-palette-data
rlm@553 100 (repeat 8 (first (:palettes zero-glyph))))
rlm@553 101 (select-LCD-bank 0)
rlm@553 102 (write-byte SCX-register 0)
rlm@553 103 (write-byte SCY-register 0)])
rlm@553 104 B (flatten
rlm@553 105 [(write-data
rlm@553 106 (+ start-address (count A))
rlm@553 107 character-data-address
rlm@553 108 (flatten
rlm@553 109 (map (comp gb-tile->bytes first :tiles image->gb-image)
rlm@554 110 glyphs)))
rlm@553 111
rlm@554 112
rlm@554 113 (write-byte
rlm@554 114 LCD-control-register
rlm@554 115 (Integer/parseInt
rlm@554 116 (str
rlm@554 117 "1" ;; LCDC on/off
rlm@554 118 "0" ;; Window code area
rlm@554 119 "0" ;; Windowing on?
rlm@554 120 "1" ;; BG tile base (1 = 0x8000)
rlm@554 121 "0" ;; BG-1 or BG-2 ?
rlm@554 122 "0" ;; OBJ-block composition
rlm@554 123 "0" ;; OBJ-on flag
rlm@554 124 "1") ;; no-effect
rlm@554 125 2))])]
rlm@554 126 (concat A B )))
rlm@553 127
rlm@558 128
rlm@560 129
rlm@553 130 (defn glyph-display-program
rlm@553 131 [start-address
rlm@562 132 max-glyphs]
rlm@556 133 (let [data-start (+ 2 start-address)
rlm@562 134 [max-glyphs-high max-glyphs-low]
rlm@562 135 (disect-bytes-2 max-glyphs)
rlm@557 136 load-data
rlm@557 137 (flatten
rlm@557 138 [;; data region
rlm@557 139 0x18
rlm@562 140 2
rlm@562 141 0 0 ;; current num of glyphs-rendered
rlm@557 142
rlm@557 143 ;; save all registers
rlm@557 144 0xC5 0xD5 0xE5 0xF5
rlm@557 145
rlm@557 146 ;; load data from data region into registers
rlm@562 147
rlm@562 148 0xF5 ;; push A
rlm@562 149 0x21 ;; begin data load
rlm@558 150 (reverse (disect-bytes-2 data-start))
rlm@562 151
rlm@562 152 0x2A 0x47 ;; glyphs-rendered -> BC
rlm@562 153 0x2A 0x4F
rlm@557 154
rlm@562 155 0x16 max-glyphs-high ;; load max-glyphs
rlm@562 156 0x1E max-glyphs-low ;; into DE
rlm@557 157 ])
rlm@557 158
rlm@560 159 handle-glyph-count*
rlm@560 160 (flatten
rlm@562 161 [;; if glyphs-rendered = max-glyph count, go directly
rlm@562 162 ;; to stack-cleanup
rlm@562 163
rlm@562 164 0x47 0xBA ;; compare B to D
rlm@562 165 0x20 ;; skip next section B != D
rlm@566 166 8 ;; this is equal to the number of instructions in the next
rlm@562 167 ;; indented region!
rlm@562 168
rlm@562 169 0x79 0xBB ;; compare C to E
rlm@562 170 0x20 ;; JR NZ, skip if C != E
rlm@566 171 4
rlm@562 172 0xF1 ;; pop AF for stack maintainance!
rlm@562 173 0x18
rlm@566 174 :stack-cleanup-low
rlm@566 175 :stack-cleanup-high
rlm@560 176 ])
rlm@560 177
rlm@562 178 display-glyph
rlm@562 179
rlm@568 180 (let [init*
rlm@563 181 (flatten
rlm@563 182 [0xF1 ;; pop A, now A is equal to key input
rlm@563 183 ;; BC is current number of glyphs rendered.
rlm@563 184 ;; each glyph is two characters, and the screen can hold up
rlm@563 185 ;; to 360 characters. Thus, if the current glyphs is a
rlm@563 186 ;; multiple of 180, the screen must be refreshed.
rlm@562 187
rlm@563 188 ;; DE contains max-glyphs and HL will be overwritten next
rlm@563 189 ;; section, so both are free to use here.
rlm@565 190 (repeat 100 0)
rlm@565 191 ;; Reset HL to initial value
rlm@565 192 0x21
rlm@565 193 (reverse (disect-bytes-2 data-start))
rlm@567 194 ;; load row and column into DE
rlm@567 195 0x23 0x23 ;; HL += 2
rlm@567 196 0x2A 0x57 ;; row -> D
rlm@567 197 0x2A 0x5F ;; column -> E
rlm@567 198
rlm@568 199 ;; clear screen if we are at 0,0
rlm@568 200 0x57 0xB3 ;; D->A, OR E A ==> (= D E 0)
rlm@570 201 ;;0x20 ;; skip clear-screen if D and E are not both zero
rlm@570 202 ;;:clear-screen-length
rlm@562 203
rlm@563 204 ])
rlm@563 205 clear-screen
rlm@568 206 (flatten
rlm@569 207 [;; save all registers
rlm@569 208 0xC5 0xD5 0xE5 0xF5
rlm@569 209
rlm@570 210 (select-LCD-bank 0)
rlm@570 211 ;; write 0x00 to memory locations
rlm@570 212 ;; 0x9800 to 0x9A34
rlm@570 213 0x21
rlm@570 214 0x00 0x98 ;; load 0x9800 into HL
rlm@570 215
rlm@570 216
rlm@570 217 0x16 3 ;; 3 -> D
rlm@570 218 0x1E 190 ;; 188 -> E
rlm@570 219
rlm@570 220 0x3E 0 ;; 0-> A
rlm@570 221
rlm@570 222 ;; begin of do-while loop
rlm@570 223 0x22 ;; load 0 to 0x9800
rlm@570 224 0x1D ;; dec E
rlm@570 225 0x20
rlm@570 226 (->signed-8-bit -4)
rlm@570 227 0x15 ;; dec D
rlm@570 228 0x1E 190 ;; 188 -> E
rlm@570 229 0x20
rlm@570 230 (->signed-8-bit -8)
rlm@570 231 ;; end of do-while-loop
rlm@570 232
rlm@570 233
rlm@569 234 ;; restore all registers
rlm@569 235 0xF1 0xE1 0xD1 0xC1
rlm@569 236 ])
rlm@563 237
rlm@568 238 init (replace
rlm@568 239 {:clear-screen-length (count clear-screen)} init*)
rlm@568 240 ]
rlm@568 241
rlm@563 242 (concat init clear-screen))
rlm@563 243
rlm@562 244
rlm@562 245
rlm@557 246 cleanup
rlm@557 247 ;; restore all registers
rlm@562 248 (flatten
rlm@562 249 [0x03 ;; (inc glyphs-rednered) -> glyphs-rendered
rlm@558 250
rlm@558 251 ;; Reset HL to initial value
rlm@558 252 0x21
rlm@558 253 (reverse (disect-bytes-2 data-start))
rlm@569 254 0x78 0x22 ;; BC -> save glyphs-rendered
rlm@569 255 0x79 0x22 ;;
rlm@558 256
rlm@569 257 0x7A 0x22 ;; D -> rows
rlm@569 258 0x7B 0x22 ;; E -> columns
rlm@558 259 ])
rlm@558 260
rlm@558 261 stack-cleanup
rlm@557 262 [0xF1 0xE1 0xD1 0xC1]
rlm@566 263
rlm@566 264 [stack-cleanup-high
rlm@566 265 stack-cleanup-low]
rlm@566 266 (disect-bytes-2 (+ start-address (count load-data)
rlm@566 267 (count handle-glyph-count*)
rlm@566 268 (count cleanup)
rlm@566 269 (count display-glyph)))
rlm@566 270
rlm@560 271 handle-glyph-count
rlm@566 272 (replace {:stack-cleanup-high stack-cleanup-high
rlm@566 273 :stack-cleanup-low stack-cleanup-low}
rlm@561 274 handle-glyph-count*)]
rlm@565 275 (println (+ (count display-glyph) (count cleanup)))
rlm@558 276 (concat load-data
rlm@562 277 handle-glyph-count
rlm@558 278 display-glyph
rlm@558 279 cleanup stack-cleanup)))
rlm@556 280
rlm@556 281 (def main-program-base-address 0xC000)
rlm@553 282
rlm@553 283 (defn glyph-bootstrap-program
rlm@553 284 [start-address delay-count total-glyph-count]
rlm@553 285 (let [init [0xAF 0x4F 0x47] ;; 0->A; 0->C; 0->B
rlm@554 286 header (concat (frame-metronome) (read-user-input))
rlm@553 287
rlm@553 288 glyph-display (glyph-display-program
rlm@559 289 (+ (count init)
rlm@562 290 (count header)
rlm@553 291 start-address)
rlm@562 292 2000)
rlm@558 293 ;;(- (count (program-data 0)) 100))
rlm@553 294
rlm@553 295 state-machine-start-address
rlm@553 296 (+ start-address (count init) (count header) (count glyph-display))
rlm@553 297 state-machine
rlm@553 298 (bootstrap-state-machine state-machine-start-address)
rlm@553 299
rlm@553 300 return-to-header
rlm@553 301 (flatten
rlm@564 302 [0xC3
rlm@564 303 (reverse (disect-bytes-2
rlm@564 304 (+ (count init) start-address)))])]
rlm@562 305 (concat init header glyph-display state-machine return-to-header)))
rlm@553 306
rlm@556 307
rlm@551 308
rlm@558 309 (defn-memo begin-glyph-bootstrap
rlm@554 310 ([] (begin-glyph-bootstrap (launch-main-bootstrap-program)))
rlm@554 311 ([script]
rlm@554 312 (let [glyph-init (glyph-init-program relocated-bootstrap-start)
rlm@554 313 main-glyph-start (+ relocated-bootstrap-start
rlm@554 314 (count glyph-init))
rlm@554 315 glyph-program (glyph-bootstrap-program
rlm@554 316 main-glyph-start 0 0)]
rlm@554 317 (->> script
rlm@554 318 (do-nothing 2)
rlm@554 319 ;; begin glyph program
rlm@554 320 (write-RAM 0xFF1A [0 0 0]) ;; silence remnant music
rlm@554 321
rlm@554 322 (write-RAM
rlm@554 323 relocated-bootstrap-start
rlm@554 324 (concat glyph-init glyph-program))
rlm@554 325 (transfer-control relocated-bootstrap-start)
rlm@555 326 (do-nothing 1)
rlm@553 327
rlm@554 328 ))))
rlm@553 329
rlm@551 330 (defn write-all-program-data
rlm@554 331 ([] (write-all-program-data (begin-glyph-bootstrap)))
rlm@551 332 ([script]
rlm@551 333 (let [base-address main-program-base-address]
rlm@551 334 (->> script
rlm@551 335 (write-RAM base-address (program-data base-address))))))
rlm@551 336
rlm@551 337 (defn activate-program
rlm@551 338 ([] (activate-program (write-all-program-data)))
rlm@551 339 ([script]
rlm@551 340 (->> script
rlm@551 341 (transfer-control main-program-base-address)
rlm@554 342 ;;(do-nothing 1800)
rlm@554 343 (do-nothing 50)
rlm@554 344 )))
rlm@552 345
rlm@552 346
rlm@552 347 ;; possible screen writing programs
rlm@552 348
rlm@552 349 ;; (program needs to stop executing at some point)
rlm@552 350 ;; maybe have total length counter or something?
rlm@552 351
rlm@552 352 ;; automatic counter that reads from program-start and clears the
rlm@552 353 ;; screen every 360 (* 18 20) gliphs
rlm@552 354
rlm@552 355 ;; advantages -- very simple and low bandwidth
rlm@552 356 ;; disadvantages -- hard to align counter
rlm@552 357
rlm@552 358 ;; implementation -- refactor main-bootstrap-program to provide a
rlm@552 359 ;; state-machine code-section which can be recombined into another
rlm@552 360 ;; program.