Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 931 lines (812 sloc) 29.92 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
;;;; This is a graphics demo. It horizontally scrolls a portion of
;;;; some artwork by Mark Ryden, in various colors, with some sinebobs
;;;; (?) and a wavy raster effect on top. The art is converted from a
;;;; GIF file to an 8 KB character ROM during assembly. The image is
;;;; large enough that we need some timed code to switch pattern table
;;;; banks mid-frame. It also incorporates a simple music player,
;;;; with the music described in a simple embedded DSL that compiles
;;;; down to a sequence of register writes to drive the 2A03 sound
;;;; hardware.

;;;; It targets a basic NROM board (32KB program, 8KB character,
;;;; vertical mirroring). I've tested it on an EPROM cart made from a
;;;; Gyromite board.

;;;; I'm neither a demo coder nor particularly experienced at 6502
;;;; assembly language, so expect nothing clever here.

;;;; (Somewhat embarrassingly, this demo reveals a bug in my own NES
;;;; emulator which I've yet to fix, where the frame timing is
;;;; apparently off by one scanline. Oops.)

(defpackage :dollhouse-demo
  (:use :common-lisp :6502 :6502-modes :asm6502 :asm6502-utility :asm6502-nes))

(in-package :dollhouse-demo)

(defvar *path* #.*compile-file-pathname*)

;;;; --- Music language ---

(defun register (address value) (list value address))
(defun nop-write () (register #x0D 0)) ; Dummy write to unused register.

(defun pad-list (list padding desired-length)
  (assert (<= (length list) desired-length))
  (append list (loop repeat (- desired-length (length list)) collect padding)))

(defun pad-frame (frame)
  (pad-list frame (nop-write) 16))

(defun segment (length list)
  (if (< (length list) length)
      (pad-list list nil length)
      (subseq list 0 length)))

(defun translate-freq (seqlen lbits freq)
  (let ((fbits (round (/ +ntsc-clock-rate+ seqlen freq))))
   (values (ldb (byte 8 0) fbits)
           (logior (ldb (byte 3 8) fbits)
                   (ash lbits 3)))))

(defun noteon (chan lbits freq)
  (multiple-value-bind (base seqlen)
      (ecase chan
        (0 (values 0 8))
        (1 (values 4 8))
        (2 (values 8 32)))
    (multiple-value-bind (v2 v3) (translate-freq seqlen lbits freq)
      (list
       (register (+ 2 base) v2)
       (register (+ 3 base) v3)))))

(defun translate-length (length)
  "Find closest match to load the length counter."
  (first
   (first
    (sort
     (copy-list
      '((0 #x0A) (1 #xFE)
        (2 #x14) (3 #x02)
        (4 #x28) (5 #x04)
        (6 #x50) (7 #x06)
        (8 #xA0) (9 #x08)
        (10 #x3C) (11 #x0A)
        (12 #x0E) (13 #x0C)
        (14 #x1A) (15 #x0E)
        (16 #x0C) (17 #x10)
        (18 #x18) (19 #x12)
        (20 #x30) (21 #x14)
        (22 #x60) (23 #x16)
        (24 #xC0) (25 #x18)
        (26 #x48) (27 #x1A)
        (28 #x10) (29 #x1C)
        (30 #x20) (31 #x1E)))
     #'<
     :key (lambda (p) (abs (- (second p) length)))))))

(defun cfg (channel &key (duty 2) (vol 15) (env t) (loop nil))
  (list
   (list (register (* channel 4)
                   (logior (ash duty 6)
                           (if env 0 #x10)
                           (if loop #x20 0)
                           vol)))))

(defun note (channel length freq &key (d length) cfg)
  (check-type channel (integer 0 1))
  (segment length
           (para
            (and cfg (apply 'cfg channel cfg))
            (list
             (noteon channel (translate-length d) freq)))))

(defun silence-channel (channel)
  (ecase channel
    (0 (note 0 1 1 :d 0 :cfg '(:vol 0 :loop t :env nil)))
    (1 (note 1 1 1 :d 0 :cfg '(:vol 0 :loop t :env nil)))))

(defun tri (length freq &key (d length))
  (check-type d (integer 0 31))
  (segment length
           (list
            (list* (register #x8 (* d 4))
                   (noteon 2 1 freq)))))

(defun noise (length duration period &key short loop (env t) (vol 15))
  (check-type duration (integer 0 31))
  (check-type vol (integer 0 15))
  (check-type period (integer 0 15))
  (segment length
    (list
     (list
      (register #xC (logior (if loop #x20 0)
                            (if env 0 #x10)
                            vol))
      (register #xE (logior (if short #x80 0)
                            period))
      (register #xF (ash (translate-length duration) 3))))))

(defun para (&rest args)
  (apply #'mapcar #'append (mapcar (lambda (x) (pad-list x nil (reduce #'max args :key #'length))) args)))

(defun measure (&rest args)
  (segment 128 (apply 'para args)))

;;; These look familiar:
(defun seq (&rest args)
  (apply #'concatenate 'list args))

(defun repeat (n &rest args)
  (apply #'seq (mapcan #'copy-list (loop repeat n collect args))))

(defun rst (length) (segment length nil))

(defun et (&rest args)
  (* 276.0 (expt 2 (/ (apply '+ args) 12))))

(defun kick (length)
  (noise length 8 15 :vol 1))

(defun snare (length &optional (variation 0))
  (noise length 8 (+ 10 variation) :vol 1))

(defun hat (length &optional (variation 0))
  (noise length 4 (+ variation 1) :vol 1))

(defun thump (length &optional (pitch (et -24)))
  (segment
   length
   (seq (tri 1 (* pitch 1))
        (tri 1 (* pitch 4/3))
        (tri 1 (* pitch 2/3))
        (tri 1 (* pitch 1/2)))))

(defun shaker (length volume)
  (assert (>= length 2))
  (segment
   length
   (seq
    (noise 1 1 1 :env nil :loop t :vol volume)
    (noise 1 1 1 :env nil :vol 0))))

(defun eltmod (i seq) (elt seq (mod i (length seq))))
(defun clamp (x min max) (max (min x max) min))

(defun volramp (&optional (start 15) (rate -1/10))
  (lambda (time)
   (clamp (round (+ start (* time rate)))
          0
          15)))

(defun shimmer (&optional (time-shift -4) (phase-offset 0))
  (lambda (time) (mod (+ phase-offset (ash time time-shift)) 4)))

(defun arpeggio (channel length chord &key
                 (rate 3)
                 (d rate)
                 (env nil)
                 (loop t)
                 (mute nil)
                 (volume (volramp))
                 (duty (shimmer)))
  (segment length
           (para
            (loop for time below length by rate
                  for index upfrom 0
                  append (note channel rate (eltmod index chord)
                               :d d
                               :cfg (list :duty (funcall duty time)
                                          :vol (funcall volume time)
                                          :env env
                                          :loop loop)))
            (seq
             (rst (1- length))
             (and mute (silence-channel channel))))))

(defun fat-arp (length chord &rest args)
  (para
   (apply #'arpeggio 0 length (apply #'chord (- (first chord) 0.06) (rest chord))
          :duty (shimmer -2) args)
   (apply #'arpeggio 1 length (apply #'chord (+ (first chord) 0.06) (rest chord))
          :duty (shimmer -2 2) args)))

(defun funky-arp (&rest args)
  (fat-arp (* 8 (length args)) (list* 0.0 args)
           :d 15 :rate 8 :env t :loop nil :volume (constantly 1) :mute t))

(defun chord (root &rest notes)
  (mapcar (lambda (note) (et root note)) notes))

;;;; *********************************************************************
;;;; ( You are now about to witness the strength of street knowledge. ;)

(let* ((global (make-instance 'basic-context :address #x8000))
       (*context* global)
       (num-wavy-lines (* 3 26)) ; Must be multiple of 3 !
       (log2-wavy-period 5)

       ;; Music player:
       (mfr-addr #x40)
       (mfr-get (indi mfr-addr))
       (mptr #x42)
       (mptr-msb (zp (1+ mptr)))
       (mptr-lsb (zp mptr))
       (log2-song-length 4) ; Base 2 log of song length.
       (write-patterns (make-hash-table :test 'equal))
       (music-sequence '())

       ;; Variables (graphics):
       (sprite-table #x0300)
       (table-x #x0200)
       (table-y #x0240)
       (countdown (zp #x58))
       (wave (zp #x59))
       (wt-lsb (zp #x60))
       (wt-msb (zp #x61))
       (wt-get (indi #x60))
       (fill-count (zp #x91))
       (scroll-x (zp #x92))
       (phase (zp #x93))
       (ntaddr (zp #x95))
       (top-ntaddr (zp #x97))
       (vblank-flag (zp #x96))) ; Set by NMI handler.

  ;; Step music playback. Advances MPTR.
  (procedure player-step

    ;; Transfer *MPTR to MFR and play this frame.
    (ldy (imm 0)) ; LSB of new music frame pointer
    (lda (indi mptr))
    (sta (zp mfr-addr))
    (iny) ; MSB of new music frame pointer
    (lda (indi mptr))
    (sta (zp (1+ mfr-addr)))
    (jsr 'player-write) ; Play frame from MFR.

    ;; Advance music pointer
    (lda mptr-lsb) ; 16-bit addition: MPTR = (MPTR+2) mod song_len
    (clc)
    (adc (imm 2))
    (sta mptr-lsb)
    (lda mptr-msb)
    (adc (imm
          (delay nil ((offset (msb (label 'music-start))))
            (- 256 offset))))
    (anda (imm (1- (expt 2 log2-song-length))))
    (clc)
    (adc (imm (delay nil ((offset (msb (label 'music-start))))
                offset)))
    (sta mptr-msb)

    (rts))

  ;; Reset sound hardware and initialize music player.
  (procedure player-init
    (poke 0 #x4015) ; Silence all channels.
    (poke #x40 #x4017) ; IRQ off, 4-step.
    (ldx (imm #xF)) ; Zero the registers
    (lda (imm 0))
    (as/until :negative
      (sta (abx #x4000))
      (dex))
    (poke 0 #x4011) ; Hit the DMC DAC, for good measure.
    (poke #x0F #x4015) ; Enable square, triangle, noise.

    (pokeword (label 'music-start) mptr) ; Set initial playback pointer.
    (rts))

  (procedure reset ; ------------------------------------
    (sei)
    (cld)
    (poke #b00010000 +ppu-cr1+) ; NMI off during init.
    (poke #b00000000 +ppu-cr2+) ; Do turn the screen off too..
    (ldx (imm #xFF)) ; Set stack pointer
    (txs)

    ;; Init sound hardware
    (poke 0 #x4015) ; Silence all channels
    (poke #x40 #x4017) ; Disable IRQ !!

    (as/until :negative (bita (mem +ppu-status+))) ; PPU warmup interval
    (as/until :negative (bita (mem +ppu-status+))) ; (two frames)

    ;; Paranoid initialization. Had some intermittent quicks on real
    ;; hardware, as though the nametables weren't fully initialized,
    ;; so I've inserted a bunch of paranoid vblank syncs, and call the
    ;; init routine multiple times just to be sure.
    (lda (imm 3))
    (sta countdown)
    (as/until :zero
      (jsr 'init-nametable)
      (jsr 'init-attr)
      (jsr 'init-palette)
      (dec countdown))

    (jsr 'player-init) ; Initialize music player

    (lda (imm 64)) ; Initialize scroll variables
    (sta scroll-x)
    (lda (imm 0))
    (sta ntaddr)
    (sta wave)
    (lda (imm 159))
    (sta phase)

    (jsr 'initialize-sprites)

    (jsr 'update-sprites)

    (lda (imm 0)) ; Don't forget to reset this.
    (sta vblank-flag)

    (align 256 #xEA) ; Pad with NOPs to next page
    (poke #b10001000 +ppu-cr1+) ; Enable NMI

    (with-label mainloop
      (macrolet ((repeat (times &body body)
                   `(progn
                      (lda countdown)
                      (pha)
                      (lda (imm ,(if (eql times 256)
                                     0
                                     times)))
                      (sta countdown)
                      (as/until :zero
                        ,@(if (and (= 1 (length body))
                                   (symbolp (first body)))
                              `((jsr ',(first body)))
                              body)
                        (dec countdown))
                      (pla)
                      (sta countdown)))
                 (scroll-panels ()
                   `(poke 0 phase))
                 (stop-panels ()
                   `(poke 159 phase)))

        ;; Show them the circle.
        (repeat 192 linearly)
        ;; Introduce the split, gently.
        (repeat 3
          (scroll-panels)
          (repeat 128 split-by-4)
          (repeat 128 linearly))
        ;; Compare/contrast:
        (repeat 2
         (scroll-panels)
         (repeat 128
           (jsr 'spin-apart)
           (jsr 'linearly))
         (repeat 128 linearly)
         (scroll-panels)
         (repeat 128 split-by-4)
         (repeat 128 linearly))
        (repeat 64 split-by-4)
        (repeat 96 linearly)
        ;; Change it up.
        (repeat 256 spin-apart)
        (repeat 96 linearly)
        ;; A little more intense..
        (repeat 5
         (repeat 24 split-by-4)
         (repeat 96 linearly))
        (repeat 88 split-by-4)
        ;; Slow down..
        (repeat 256
          (scroll-panels)
          (jsr 'split-by-4)
          (jsr 'spin-apart))
        (stop-panels)
        ;; Rest and repeat. Come a little unglued.
        (repeat 256 spin-apart)
        (repeat 253 spin-apart)

      (jmp (mem 'mainloop)))))

  ;;; Do register writes for this frame of music. Set MFR to the
  ;;; set of writes for this frame (16*2 bytes).
  (procedure player-write
    (ldy (imm #x1F))
    (as/until :negative
     (lda mfr-get)
     (tax)
     (dey)
     (lda mfr-get)
     (sta (abx #x4000))
     (dey))
    (rts))

  (procedure init-nametable
    ;; Fill nametable.
    (as/until :negative (bita (mem +ppu-status+)))
    (ppuaddr #x2000)
    (jsr 'fill-nametable)
    (as/until :negative (bita (mem +ppu-status+)))
    (ppuaddr #x2400)
    (jsr 'fill-nametable)
    (rts))

  (procedure init-attr
    ;; Fill attribute table.
    (as/until :negative (bita (mem +ppu-status+)))
    (ppuaddr #x23C0)
    (ldy (imm 0))
    (lda (imm #b01010101))
    (jsr 'fill-attributes)
    (as/until :negative (bita (mem +ppu-status+)))
    (ppuaddr #x27C0)
    (ldy (imm #b10101010))
    (lda (imm #b11111111))
    (jsr 'fill-attributes)
    (rts))

  (procedure init-palette
    ;; Program palette.
    (ppuaddr #x3F00)
    (dolist (color '(#x3F #x2D #x3D #x30 #x3F #x03 #x13 #x23
                     #x3F #x2D #x3D #x30 #x3F #x05 #x15 #x25
                     #x3F #x3F #x27 #x37))
      (poke color +vram-io+))
    (rts))

  (align 256)
  (procedure framestep
    (jsr 'wait-for-vblank)
    (lda (mem +ppu-status+)) ; Reset PPU address latch.
    (poke 0 +spr-addr+) ; Reset sprite address!
    (lda (imm (msb sprite-table))) ; Sprite DMA transfer.
    (sta (mem +sprite-dma+))

    (lda (mem +ppu-status+)) ; Reset address latch, to be safe.
    (ldx phase) ; 'phase' steps through rate-pattern
    (lda scroll-x) ; Scroll horizontally..
    (clc) ; Update scroll-x by rate-pattern
    (adc (abx 'rate-pattern)) ; Add. Use the carry-out below!
    (sta scroll-x)
    (lda (abx 'rate-transition)) ; Update phase via transition function.
    (sta phase)
    (lda ntaddr) ; Carry into ntaddr
    (adc (imm 0)) ; ** Carry in from ADC above. **
    (anda (imm #b00000001)) ; Carry toggles $2000/$2400
    ;; (*) Subtle: I had to bum an instruction out of the scanline kernel, so
    ;; I store this with bit 4 inverted, as the bottom half of the screen
    ;; requires.
    (eor (imm #b10011000)) ; NMI on, invert BG Pattern address! (*)
    (sta ntaddr)

    ;; This got a little messy. Arithmetic for the bottom half of the screen
    ;; is biased by 128, because it's easier than screwing with the overflow
    ;; flag (the wave offsets are signed). Must adjust the top half to match.
    ;; There's also the issue of the gap between the screen split and the wave
    ;; effect, which also needs the correct value from here ("top-ntaddr").
    (lda scroll-x)
    (clc)
    (adc (imm 128))
    (sta (mem +vram-scroll+))
    (lda ntaddr) ; Carry into NT address.
    (adc (imm 0)) ; (okay if it carries again)
    (eor (imm #b00010000)) ; Flip pattern table address.
    (sta top-ntaddr) ; Reuse this after the split.
    (sta (mem +ppu-cr1+))

    (poke #b00011110 +ppu-cr2+) ; BG and sprites on.
    (lda (imm 0))
    (sta (mem +vram-scroll+))

    (jsr 'update-sprites)

    ;; Switch pattern tables mid-frame:
    (emit-delay (+ (* 114 107) 22))
    (lda top-ntaddr)
    (eor (imm #b10010000)) ; Invert pattern bank.
    (sta (mem +ppu-cr1+))

    ;; Run the music
    (jsr 'player-step)

    ;; Leftover scanlines before wave effect starts
    (emit-delay (round (+ (* 113.66 9) 110)))

    ;; Wavy effect
    (flet ((kernel ()
             ;; Assumes Y counts down each line..
             (poke #b00011110 +ppu-cr2+) ; To visually calibrate timing.
             (clc)
             (lda wt-get) ; Get wave offset
             (adc scroll-x) ; Add to BG scroll position
             (ldx (imm 0))
             (sta (mem +vram-scroll+))
             (stx (mem +vram-scroll+))
             (lda ntaddr) ; Carry in. Effectively, scroll-x and
             (adc (imm 0)) ; ntaddr.0 form a 9-bit field.
             (sta (mem +ppu-cr1+))
             (poke #b00011110 +ppu-cr2+)))

      ;; Setup for wave effect.. load table pointer into zero page.
      (inc wave) ; Increment wave phase
      (lda wave)
      (anda (imm (1- (expt 2 log2-wavy-period)))) ; Modulo cycle length..
      (lsr)
      (clc)
      (adc (imm (msb (label 'wave-offsets)))) ; Calculate table MSB
      (sta wt-msb)
      (lda wave)
      (lsr)
      (lda (imm 0)) ; Rotate into MSB of LSB (...)
      (ror) ; (each table is 128 bytes)
      (adc (imm (lsb (label 'wave-offsets))))
      (sta wt-lsb)

      (emit-delay 47) ; Realign with hblank
      (bita (zp 0))
      (nop)

      (macrolet ((reporting-timing (&body body)
                   `(print (list :kernel-cycles
                                 (counting-cycles ,@body)))))

        (ldy (imm num-wavy-lines)) ; Decremented within scanline kernel.

        (loop repeat (/ num-wavy-lines 3) do
              (reporting-timing (kernel) (dey))
              (loop repeat 14 do (inc (zp 0)))
              (reporting-timing (kernel) (dey))
              (nop)
              (loop repeat 14 do (inc (zp 0)))
              (reporting-timing (kernel) (dey))
              (loop repeat 14 do (inc (zp 0))))))

    ;; Shut the screen off. Need a few extra scanlines here to update
    ;; the sprite tables, and if I didn't shut it off, you'd notice
    ;; the background stopped waving.
    (loop repeat 10 do (nop)) ; Realign with hblank.. again.
    ;; Actually, leave the sprites on, otherwise they'll be disabled
    ;; during the DMA transfer, which is unwise.
    (poke #x10 +ppu-cr2+)

    (rts))

  ;; This table controls the scrolling.
  (align 256)
  (with-label rate-pattern
    (let* ((tr '(1 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0 1))
           (pattern (subseq
                     (append (loop repeat (- 128 (reduce '+ tr))
                                   collect 1)
                             tr
                             (loop repeat 256 collect 0))
                     0 256)))
      (assert (= 256 (length pattern)))
      (assert (= 128 (reduce #'+ pattern)))
      (map nil 'db pattern)))
  (with-label rate-transition
    (loop for i from 0 below 160 do (db (1+ i)))
    (db 160))

  (procedure fill-nametable
    (lda (imm 30))
    (sta fill-count)
    (ldx (imm 0))
    (as/until :zero
      (txa)
      (ldy (imm 16))
      (as/until :zero
        (stx (mem +vram-io+))
        (inx)
        (dey))
      (tax) ; ..and once more.
      (ldy (imm 16))
      (as/until :zero
        (stx (mem +vram-io+))
        (inx)
        (dey))
      (dec fill-count)))

  (procedure fill-attributes
    (ldx (imm 8))
    (as/until :zero
      (dotimes (x 4) (sty (mem +vram-io+)))
      (dotimes (x 4) (sta (mem +vram-io+)))
      (dex))
    (rts))

  (procedure initialize-sprites
    ;; Initialize OAM mirror.
    (ldx (imm 0))
    (as/until :zero
      (poke 96 (abx sprite-table)) ; Y coordinate
      (inx)
      (txa) ; Alternate character #s
      (lsr)
      (lsr) ;
      (ora (imm #xFE))
      (sta (abx sprite-table)) ; Sprite index
      (inx)
      (poke 0 (abx sprite-table)) ; Attirubtes
      (inx)
      (poke 64 (abx sprite-table)) ; X coordinate
      (inx))
    ;; Initialize X/Y phase variables
    (ldx (imm 63))
    (lda (imm 0))
    (as/until :negative
      (sta (abx table-x))
      (sta (abx table-y))
      (clc)
      (adc (imm 4))
      (dex))
    (rts))

  (procedure linearly
    (jsr 'framestep)
    (ldx (imm 63))
    (as/until :negative
      (print
       (counting-cycles
        (inc (abx table-x))
        (inc (abx table-y))
        (dotimes (i 7) (nop))
        (dex))))
    (rts))

  (procedure spin-apart
    (jsr 'framestep)
    (ldx (imm 63))
    (as/until :negative
      (print
       (counting-cycles
        (inc (abx table-x))
        (txa)
        (anda (imm 1))
        (clc)
        (adc (abx table-x))
        (sta (abx table-x))
        (dotimes (i 3) (nop))
        (dex))))
    (rts))

  (procedure split-by-4
    (jsr 'framestep)
    (ldx (imm 63))
    (as/until :negative
      (print
       (counting-cycles
         (inc (abx table-y))
         (txa)
         (anda (imm 16))
         (lsr)
         (lsr)
         (lsr)
         (clc)
         (adc (abx table-x))
         (sta (abx table-x))
         (dex))))
    (rts))

  (procedure wait-for-vblank
    (lda (imm 0))
    (sta vblank-flag)
    (as/until :not-zero (lda vblank-flag))
    (lda (imm 0))
    (sta vblank-flag)
    (rts))

  (procedure brk-handler (rti))

  (procedure vblank-handler
    (inc vblank-flag)
    (rti))

  (align 256)
  (procedure update-sprites
    (ldx (imm 63))
    (as/until :negative
      (ldy (abx table-x)) ; Push sin[table_x[X]]
      (lda (aby 'sine-table))
      (pha)

      (lda (abx table-y)) ; Push sin[table_x[X]+64]
      (clc)
      (adc (imm 64))
      (tay)
      (lda (aby 'sine-table))
      (pha)

      (txa) ; A = X * 4
      (asl)
      (asl)
      (tay) ; Y = X * 4
      (pla) ; Pop cosine
      (clc)
      (adc (imm -14)) ; Offset Y coordinate
      (sta (aby sprite-table)) ; Store in sprite Y coordinate
      (pla) ; Pop sine
      (sta (aby (+ 3 sprite-table))) ; Store in sprite X coordinate
      (dex)) ; Decrement sprite index
    (rts))

  (align 256)
  (with-label sine-table
    (loop for i from 0 below 256
          do (db (round (+ 124 (* 99 (sin (* 2 pi i 1/256))))))))

  (align 256)
  (with-label wave-offsets
    (loop with nframes = (expt 2 log2-wavy-period)
          for frame from (1- nframes) downto 0 do
      (align 128)
      (emit
       ;; The wave loop counts from num-wavy-lines to 1. So, one extra here.
       (loop for line from num-wavy-lines downto 0
             with amp = 2
             collect (mod (round ( + 128 ; bias to fix carry
                                     (* amp (/ (expt line 1.1) 80)
                                        (sin (* 2 pi (/ (+ (* 0.07 (expt line 1.75)) frame)
                                                        nframes))))))
                          256)))))

  (format t "~&Code ends at at ~X~%" *origin*)

  ;; ************************************************************
  ;; MUSIC

  (labels
      ((emit-frame (frame)
         (unless (<= (length frame) 16)
           (error "Too many writes! ~X" (mapcar 'second frame)))
         (setf frame (pad-frame frame))
         (unless (gethash frame write-patterns)
           (setf (gethash frame write-patterns) *origin*)
           ;; Reverse order, because player scans backward!
           (dolist (pair (reverse frame)) (apply 'db pair)))
         (push (gethash frame write-patterns) music-sequence))

       (song (frames)
         ;; music-test.lisp has a sad tale to share.
         (map nil #'emit-frame frames))

       ;; Song elements:

       (phrase-aaab (a b) (seq a a a b))

       (four-on-the-floor () (repeat 4 (thump 32 (et -24))))

       (rhythm (fn notes &optional (transpose 0))
         (seq
          (funcall fn 32 (et transpose (nth 0 notes)))
          (funcall fn 24 (et transpose (nth 1 notes)))
          (funcall fn 16 (et transpose (nth 2 notes)))
          (funcall fn 24 (et transpose (nth 3 notes)))
          (funcall fn 32 (et transpose (nth 4 notes)))))

       (swagger ()
         (seq
          (kick 16)
          (hat 8)
          (hat 8)
          (snare 16)
          (hat 8)
          (hat 8 4)))

       (stagger ()
         (seq
          (hat 8)
          (kick 8)
          (hat 8)
          (hat 8)
          (snare 16)
          (rst 16)))

       (jagger ()
         (seq
          (shaker 8 15)
          (shaker 8 4)
          (shaker 8 8)
          (shaker 8 12)
          (shaker 8 15)
          (shaker 8 5)
          (shaker 8 11)
          (shaker 8 14)))

       (intro-beat ()
         (measure
          (four-on-the-floor)
          (seq
           (swagger)
           (swagger))))

       (intro-fill-1 ()
         (measure
          (four-on-the-floor)
          (seq (swagger)
               (stagger))))

       (intro-fill-2 ()
         (measure
          (four-on-the-floor)
          (seq (jagger)
               (jagger)))))

    (align 32)

    (song
     (seq
      ;; Smooth section
      (seq
       (para
        (phrase-aaab
         (intro-beat)
         (intro-fill-1))
        (seq
         (measure (fat-arp 128 '(0.00 0 3 7 11) :rate 4 :volume (volramp 8 -1/22)))
         (measure (fat-arp 128 '(0.00 0 2 5 8 ) :rate 4 :volume (volramp 8 -1/22)))
         (measure (fat-arp 128 '(0.00 -2 7 8 12 ) :rate 4 :volume (volramp 9 -1/20)))
         (measure (fat-arp 128 '(0.00 -1 2 3 7 ) :rate 4 :volume (volramp 10 -1/18)))))

       (para
        (phrase-aaab
         (intro-beat)
         (intro-fill-2))
        (seq
         (measure (arpeggio 0 128 (chord -0.02 0 3 7 11) :rate 4 :volume (volramp 11 -1/16))
                  (arpeggio 1 128 (chord 0.02 12 0 3 14 7 11) :rate 3 :volume (volramp 11 -1/13)))
         (measure (arpeggio 0 128 (chord -0.02 0 2 5 8) :rate 4 :volume (volramp 12 -1/14))
                  (arpeggio 1 128 (chord 0.02 2 5 8 12 15) :rate 3 :volume (volramp 12 -1/14)))
         (measure (arpeggio 0 128 (chord -0.02 -2 7 8 12) :rate 4 :volume (volramp 13 -1/12))
                  (arpeggio 1 128 (chord 0.02 5 15 8 12 15 17) :rate 3 :volume (volramp 13 -1/12)))
         (measure (arpeggio 0 128 (chord -0.02 -1 2 3 7) :rate 4 :volume (volramp 15 -1/10) :mute t)
                  (arpeggio 1 128 (chord 0.02 3 7 14 11 19 14) :rate 4 :volume (volramp 15 -1/10) :mute t)))))

      ;; Funky section
      (seq
       (para
        (phrase-aaab
         (measure
          (rhythm #'thump '(0 0 0 0 7) -12)
          (seq (swagger) (stagger)))
         (measure
          (seq (stagger) (stagger))
          (four-on-the-floor)))
        (seq
         (measure
          (funky-arp 0 12 0 3 11 14 7 17 0 17 12 19 15 17 10 15))
         (measure
          (para
           (note 0 32 (et 0.026) :cfg '(:duty 1 :env t :vol 2 :loop nil))
           (note 1 32 (et -0.026) :cfg '(:duty 3 :env t :vol 2 :loop nil))))
         (measure
          (funky-arp 5 0 5 7 8 11 12 0 12 17 15 17 15 14 12 8 7))
         (measure
          (funky-arp 7 0 7 11 12 3 17 19 20 24 23 20 0 19 17 20))))
       (para
        (phrase-aaab
         (measure
          (seq (swagger) (stagger))
          (rhythm #'thump '(0 3 3 -2 0) -12))
         (measure
          (seq (stagger) (jagger))))
        (seq
         (measure
          (fat-arp 128 '(0.0 0 12 0 3 11 14 7 17 0 17 12 19 15 17 20 19)
                   :d 6 :rate 8 :env t :loop nil :volume (constantly 3) :mute nil))
         (measure
          (seq
           (para
            (note 0 32 (et 0.026 24) :cfg '(:duty 1 :env t :vol 2 :loop nil))
            (note 1 32 (et -0.026 24) :cfg '(:duty 3 :env t :vol 0 :loop nil)))
           (para
            (note 0 32 (et 0.026 24) :cfg '(:duty 1 :env t :vol 2 :loop nil))
            (note 1 32 (et -0.026 12) :cfg '(:duty 3 :env t :vol 2 :loop nil)))))
         (measure
          (fat-arp 128 '(5.0 0 3 7 0 3 7 0 3 8 0 3 7 0 3 8) :rate 4 :d 3 :volume (volramp 10 -1/18)))
         (measure
          (fat-arp 128 '(0.0 2 5 8 11 2 5 8 14) :rate 4 :d 3 :volume (volramp 9 -1/18))))))))

    (align 256)
    (with-label music-start
      (unless (= (length music-sequence) (* 128 (expt 2 log2-song-length)))
        (error "Song length is ~:D, should be ~:D" (length music-sequence) (* 128 (expt 2 log2-song-length))))
      (print (list :num-unique (length (remove-duplicates music-sequence))))
      ;; Write the pointer table:
      (mapcar #'dw (reverse music-sequence))))

  ;; ************************************************************

  (format t "~&Empty space begins at ~X~%" *origin*)

  ;; Interrupt vectors:
  (advance-to +nmi-vector+)
  (dw (label 'vblank-handler))
  (dw (label 'reset))
  (dw (label 'brk-handler))

  ;; Create output file:
  (write-ines "/tmp/dollhouse.nes"
              (link global)
              :mirror-mode :vertical
              :chr (ichr:encode-chr
                    (ichr:read-gif
                     (merge-pathnames "ryden.gif" *path*)))))
Something went wrong with that request. Please try again.