@@ -5,6 +5,32 @@ include "hardware.inc" |
| 5 |
include "constants.inc" |
5 |
include "constants.inc" |
| 6 |
include "debug_macros.inc" |
6 |
include "debug_macros.inc" |
| 7 |
|
7 |
|
|
|
8 |
add_a_to_r16: MACRO |
|
|
9 |
add a, \2 |
|
|
10 |
ld \2, a |
|
|
11 |
adc a, \1 |
|
|
12 |
sub \2 |
|
|
13 |
ld \1, a |
|
|
14 |
ENDM |
|
|
15 |
|
|
|
16 |
;; TODO: See if there's a way to shave off a byte. |
|
|
17 |
sub_from_r16: MACRO ;; (high, low, value) |
|
|
18 |
ld a, \2 |
|
|
19 |
sub \3 |
|
|
20 |
ld \2, a |
|
|
21 |
ld a, \1 |
|
|
22 |
sbc a, 0 |
|
|
23 |
ld \1, a |
|
|
24 |
ENDM |
|
|
25 |
|
|
|
26 |
add_a_to_hl: MACRO |
|
|
27 |
add_a_to_r16 h, l |
|
|
28 |
ENDM |
|
|
29 |
|
|
|
30 |
add_a_to_de: MACRO |
|
|
31 |
add_a_to_r16 d, e |
|
|
32 |
ENDM |
|
|
33 |
|
| 8 |
; Constants |
34 |
; Constants |
| 9 |
STACK_SIZE EQU 20 |
35 |
STACK_SIZE EQU 20 |
| 10 |
;; Stack starts at $FFFE |
36 |
;; Stack starts at $FFFE |
@@ -97,7 +123,7 @@ SECTION "Song Data", ROM0 |
| 97 |
;; order_cnt is the number of orders times 2 |
123 |
;; order_cnt is the number of orders times 2 |
| 98 |
order_cnt: db 2 |
124 |
order_cnt: db 2 |
| 99 |
order1: dw empty ;; unreal ;; unreal;, lunawaves, lunawaves, unreal |
125 |
order1: dw empty ;; unreal ;; unreal;, lunawaves, lunawaves, unreal |
| 100 |
order2: dw unreal ;; empty, unreal, unreal, lunawaves |
126 |
order2: dw slideup ;; empty, unreal, unreal, lunawaves |
| 101 |
order3: dw empty, lunawaves, lunawaves, unreal |
127 |
order3: dw empty, lunawaves, lunawaves, unreal |
| 102 |
order4: dw empty, lunawaves, lunawaves, unreal |
128 |
order4: dw empty, lunawaves, lunawaves, unreal |
| 103 |
|
129 |
|
@@ -117,9 +143,9 @@ rept 16 |
| 117 |
db 90, 0, 0 |
143 |
db 90, 0, 0 |
| 118 |
endr |
144 |
endr |
| 119 |
slideup: |
145 |
slideup: |
| 120 |
dn C3, 00, $000 |
146 |
dn C4, 00, $000 |
| 121 |
rept 63 |
147 |
rept 63 |
| 122 |
dn 90, 00, 332 |
148 |
dn 90, 00, $101 |
| 123 |
endr |
149 |
endr |
| 124 |
|
150 |
|
| 125 |
SECTION "Playback variables", WRAM0 |
151 |
SECTION "Playback variables", WRAM0 |
@@ -130,15 +156,16 @@ pattern4: dw |
| 130 |
|
156 |
|
| 131 |
current_order: dw |
157 |
current_order: dw |
| 132 |
|
158 |
|
| 133 |
;; Reset to this once done screwing with channel values |
159 |
;; Size of a channel in bytes |
| 134 |
old_stack_pointer: dw |
160 |
CHANNEL_SIZE EQU 4 |
|
|
161 |
;; Amount to be shifted in order to skip a channel. |
|
|
162 |
CHANNEL_SIZE_EXPONENT EQU 2 |
| 135 |
|
163 |
|
| 136 |
channels: |
164 |
channels: |
| 137 |
|
165 |
|
| 138 |
;;;;;;;;;;; |
166 |
;;;;;;;;;;; |
| 139 |
;;Channel 1 |
167 |
;;Channel 1 |
| 140 |
;;;;;;;;;;; |
168 |
;;;;;;;;;;; |
| 141 |
jumpback1: dw |
|
|
| 142 |
channel1: |
169 |
channel1: |
| 143 |
channel_period1: dw |
170 |
channel_period1: dw |
| 144 |
toneporta_target1: dw |
171 |
toneporta_target1: dw |
@@ -146,7 +173,6 @@ toneporta_target1: dw |
| 146 |
;;;;;;;;;;; |
173 |
;;;;;;;;;;; |
| 147 |
;;Channel 2 |
174 |
;;Channel 2 |
| 148 |
;;;;;;;;;;; |
175 |
;;;;;;;;;;; |
| 149 |
jumpback2: dw |
|
|
| 150 |
channel2: |
176 |
channel2: |
| 151 |
channel_period2: dw |
177 |
channel_period2: dw |
| 152 |
toneporta_target2: dw |
178 |
toneporta_target2: dw |
@@ -154,7 +180,6 @@ toneporta_target2: dw |
| 154 |
;;;;;;;;;;; |
180 |
;;;;;;;;;;; |
| 155 |
;;Channel 3 |
181 |
;;Channel 3 |
| 156 |
;;;;;;;;;;; |
182 |
;;;;;;;;;;; |
| 157 |
jumpback3: dw |
|
|
| 158 |
channel3: |
183 |
channel3: |
| 159 |
channel_period3: dw |
184 |
channel_period3: dw |
| 160 |
toneporta_target3: dw |
185 |
toneporta_target3: dw |
@@ -162,7 +187,6 @@ toneporta_target3: dw |
| 162 |
;;;;;;;;;;; |
187 |
;;;;;;;;;;; |
| 163 |
;;Channel 4 |
188 |
;;Channel 4 |
| 164 |
;;;;;;;;;;; |
189 |
;;;;;;;;;;; |
| 165 |
jumpback4: dw |
|
|
| 166 |
channel4: |
190 |
channel4: |
| 167 |
channel_period4: dw |
191 |
channel_period4: dw |
| 168 |
toneporta_target4: dw |
192 |
toneporta_target4: dw |
@@ -232,18 +256,6 @@ _addr = _addr + 1 |
| 232 |
ld a, $77 |
256 |
ld a, $77 |
| 233 |
ldh [rAUDVOL], a |
257 |
ldh [rAUDVOL], a |
| 234 |
|
258 |
|
| 235 |
;; Setup jumpback addresses for the stack hack |
|
|
| 236 |
load_jumpbacks: MACRO |
|
|
| 237 |
ld a, LOW(\2) |
|
|
| 238 |
ld [\1], a |
|
|
| 239 |
ld a, HIGH(\2) |
|
|
| 240 |
ld [\1+1], a |
|
|
| 241 |
ENDM |
|
|
| 242 |
load_jumpbacks jumpback1, after_effect1 |
|
|
| 243 |
load_jumpbacks jumpback2, after_effect2 |
|
|
| 244 |
load_jumpbacks jumpback3, after_effect3 |
|
|
| 245 |
load_jumpbacks jumpback4, after_effect4 |
|
|
| 246 |
|
|
|
| 247 |
ld c, 0 ;; Current order index |
259 |
ld c, 0 ;; Current order index |
| 248 |
call _refresh_patterns |
260 |
call _refresh_patterns |
| 249 |
|
261 |
|
@@ -337,8 +349,7 @@ ENDM |
| 337 |
_update_channel: |
349 |
_update_channel: |
| 338 |
;; Call with: |
350 |
;; Call with: |
| 339 |
;; Channel in B |
351 |
;; Channel in B |
| 340 |
;; Note tone in HL |
352 |
;; Note tone in DE |
| 341 |
;; Volume in D |
|
|
| 342 |
|
353 |
|
| 343 |
;; TODO: Turn this into a jump table? Or find some other way to optimize |
354 |
;; TODO: Turn this into a jump table? Or find some other way to optimize |
| 344 |
ld a, b |
355 |
ld a, b |
@@ -353,15 +364,15 @@ _update_channel: |
| 353 |
jp z, _update_channel4 |
364 |
jp z, _update_channel4 |
| 354 |
|
365 |
|
| 355 |
_update_channel1: |
366 |
_update_channel1: |
| 356 |
ld a, l |
367 |
ld a, e |
| 357 |
ldh [rAUD1LOW], a |
368 |
ldh [rAUD1LOW], a |
| 358 |
ld a, h |
369 |
ld a, d |
| 359 |
ldh [rAUD1HIGH], a |
370 |
ldh [rAUD1HIGH], a |
| 360 |
ret |
371 |
ret |
| 361 |
_update_channel2: |
372 |
_update_channel2: |
| 362 |
ld a, l |
373 |
ld a, e |
| 363 |
ldh [rAUD2LOW], a |
374 |
ldh [rAUD2LOW], a |
| 364 |
ld a, h |
375 |
ld a, d |
| 365 |
ldh [rAUD2HIGH], a |
376 |
ldh [rAUD2HIGH], a |
| 366 |
ret |
377 |
ret |
| 367 |
_update_channel3: |
378 |
_update_channel3: |
@@ -477,11 +488,11 @@ _playnote4: |
| 477 |
|
488 |
|
| 478 |
ret |
489 |
ret |
| 479 |
|
490 |
|
| 480 |
cphl: MACRO |
491 |
; cphl: MACRO |
| 481 |
or a |
492 |
; or a |
| 482 |
sbc hl, \1 |
493 |
; sbc hl, \1 |
| 483 |
add hl, \1 |
494 |
; add hl, \1 |
| 484 |
ENDM |
495 |
; ENDM |
| 485 |
|
496 |
|
| 486 |
loadShort: MACRO |
497 |
loadShort: MACRO |
| 487 |
ld a, [\1] |
498 |
ld a, [\1] |
@@ -523,9 +534,9 @@ _doeffect: |
| 523 |
.jump: |
534 |
.jump: |
| 524 |
;; Jump table for effect |
535 |
;; Jump table for effect |
| 525 |
jp fx_no_op ;fx_arpeggio ;0xy |
536 |
jp fx_no_op ;fx_arpeggio ;0xy |
| 526 |
jp fx_no_op ;fx_porta_up ;1xy |
537 |
jp fx_porta_up ;1xy |
| 527 |
jp fx_no_op ;fx_porta_down ;2xy |
538 |
jp fx_porta_down ;2xy |
| 528 |
jp fx_toneporta ;fx_no_op ;3xy |
539 |
jp fx_toneporta ;3xy |
| 529 |
jp fx_no_op ;fx_vibrato ;4xy |
540 |
jp fx_no_op ;fx_vibrato ;4xy |
| 530 |
jp fx_no_op ;fx_toneporta_volslide ;5xy |
541 |
jp fx_no_op ;fx_toneporta_volslide ;5xy |
| 531 |
jp fx_no_op ;fx_vibrato_volslide ;6xy |
542 |
jp fx_no_op ;fx_vibrato_volslide ;6xy |
@@ -539,13 +550,69 @@ _doeffect: |
| 539 |
jp fx_no_op ;fx_note_cut ;Exy |
550 |
jp fx_no_op ;fx_note_cut ;Exy |
| 540 |
jp fx_no_op ;fx_set_speed ;Fxy |
551 |
jp fx_no_op ;fx_set_speed ;Fxy |
| 541 |
|
552 |
|
|
|
553 |
setup_channel_pointer: |
|
|
554 |
;; Call with: |
|
|
555 |
;; Channel value in B |
|
|
556 |
;; Returns value in HL |
|
|
557 |
|
|
|
558 |
ld a, b |
|
|
559 |
REPT CHANNEL_SIZE_EXPONENT |
|
|
560 |
sla a |
|
|
561 |
ENDR |
|
|
562 |
ld hl, channels |
|
|
563 |
add_a_to_hl |
|
|
564 |
ret |
|
|
565 |
|
| 542 |
fx_no_op: |
566 |
fx_no_op: |
| 543 |
dec sp |
|
|
| 544 |
dec sp |
|
|
| 545 |
dec sp |
|
|
| 546 |
dec sp |
|
|
| 547 |
ret |
567 |
ret |
| 548 |
|
568 |
|
|
|
569 |
fx_porta_up: |
|
|
570 |
ld b, e ;; so we have access to DE |
|
|
571 |
;; B: channel |
|
|
572 |
;; C: effect parameters |
|
|
573 |
|
|
|
574 |
;; free registers: A, D, E, H, L |
|
|
575 |
|
|
|
576 |
call setup_channel_pointer |
|
|
577 |
|
|
|
578 |
ld a, [hl+] |
|
|
579 |
ld e, a |
|
|
580 |
ld d, [hl] |
|
|
581 |
|
|
|
582 |
ld a, c |
|
|
583 |
add_a_to_de |
|
|
584 |
|
|
|
585 |
ld a, d |
|
|
586 |
ld [hl-], a |
|
|
587 |
ld [hl], e |
|
|
588 |
|
|
|
589 |
jp _update_channel |
|
|
590 |
|
|
|
591 |
;; TODO: Maybe merge with fx_porta_up, since they're so similar? Would need |
|
|
592 |
;; to find a way to compare against effect code. |
|
|
593 |
fx_porta_down: |
|
|
594 |
ld b, e ;; so we have access to DE |
|
|
595 |
;; B: channel |
|
|
596 |
;; C: effect parameters |
|
|
597 |
|
|
|
598 |
;; free registers: A, D, E, H, L |
|
|
599 |
|
|
|
600 |
call setup_channel_pointer |
|
|
601 |
|
|
|
602 |
ld a, [hl+] |
|
|
603 |
ld e, a |
|
|
604 |
ld d, [hl] |
|
|
605 |
|
|
|
606 |
ld a, c |
|
|
607 |
sub_from_r16 d, e, c |
|
|
608 |
|
|
|
609 |
ld a, d |
|
|
610 |
ld [hl-], a |
|
|
611 |
ld [hl], e |
|
|
612 |
|
|
|
613 |
jp _update_channel |
|
|
614 |
|
|
|
615 |
|
| 549 |
fx_toneporta: |
616 |
fx_toneporta: |
| 550 |
ld b, e ;; so we have access to DE |
617 |
ld b, e ;; so we have access to DE |
| 551 |
;; B: channel |
618 |
;; B: channel |
@@ -553,87 +620,80 @@ fx_toneporta: |
| 553 |
|
620 |
|
| 554 |
;; free registers: A, D, E, H, L |
621 |
;; free registers: A, D, E, H, L |
| 555 |
|
622 |
|
| 556 |
pop hl ; Load channel_period |
623 |
;; TOOD: Optimize. This usage of the stack isn't great but IMO it's |
| 557 |
pop de ; Load toneporta_target |
624 |
;; better than using the stack hack which forces all effects to screw with |
|
|
625 |
;; pushing and popping. Most effects won't need to do this. |
| 558 |
|
626 |
|
| 559 |
; ld a, [channel_period2] |
627 |
call setup_channel_pointer |
| 560 |
; ld l, a |
628 |
push hl |
| 561 |
; ld a, [channel_period2+1] |
|
|
| 562 |
; ld h, a |
|
|
| 563 |
|
629 |
|
| 564 |
; ld a, [toneporta_target2] |
630 |
ld a, [hl+] |
| 565 |
; ld e, a |
631 |
ld e, a |
| 566 |
; ld a, [toneporta_target2+1] |
632 |
ld a, [hl+] |
| 567 |
; ld d, a |
633 |
ld d, a |
|
|
634 |
|
|
|
635 |
ld a, [hl+] |
|
|
636 |
ld h, [hl] |
|
|
637 |
ld l, a |
| 568 |
|
638 |
|
| 569 |
;; Comparing which direction to move the current value |
639 |
;; Comparing which direction to move the current value |
| 570 |
;; TODO: Optimize this!!!! |
640 |
;; TODO: Optimize this!!!! |
| 571 |
|
641 |
|
| 572 |
;; Compare high byte |
642 |
;; Compare high byte |
| 573 |
ld a, d |
643 |
ld a, h |
| 574 |
cp h |
644 |
|
|
|
645 |
cp d |
| 575 |
jp c, .subtract ; target is less than the current period |
646 |
jp c, .subtract ; target is less than the current period |
| 576 |
jp z, .high_byte_same |
647 |
jp z, .high_byte_same |
| 577 |
jp .add |
648 |
jp .add |
| 578 |
.high_byte_same: |
649 |
.high_byte_same: |
| 579 |
ld a, e |
650 |
ld a, l |
| 580 |
cp l |
651 |
cp e |
| 581 |
jp c, .subtract ; the target is less than the current period |
652 |
jp c, .subtract ; the target is less than the current period |
| 582 |
jp z, .done ; both nibbles are the same so no portamento |
653 |
jp z, .done ; both nibbles are the same so no portamento |
| 583 |
.add: |
654 |
.add: |
| 584 |
ld a, c |
655 |
ld a, c |
| 585 |
add a, l |
656 |
add a, e |
| 586 |
ld l, a |
657 |
ld e, a |
| 587 |
adc a, h |
658 |
adc a, d |
| 588 |
sub l |
659 |
sub e |
| 589 |
ld h, a |
660 |
ld d, a |
| 590 |
|
661 |
|
| 591 |
ld a, d |
662 |
ld a, h |
| 592 |
cp h |
663 |
cp d |
| 593 |
jp c, .set_exact |
664 |
jp c, .set_exact |
| 594 |
ld a, e |
665 |
ld a, l |
| 595 |
cp l |
666 |
cp e |
| 596 |
jp c, .set_exact |
667 |
jp c, .set_exact |
| 597 |
|
668 |
|
| 598 |
jp .done |
669 |
jp .done |
| 599 |
|
670 |
|
| 600 |
.subtract: |
671 |
.subtract: |
| 601 |
ld a, l |
672 |
ld a, e |
| 602 |
sub c |
673 |
sub c |
| 603 |
ld l, a |
674 |
ld e, a |
| 604 |
|
675 |
|
| 605 |
ld a, h |
676 |
ld a, d |
| 606 |
sbc a, 0 |
677 |
sbc a, 0 |
| 607 |
ld h, a |
678 |
ld d, a |
| 608 |
|
679 |
|
| 609 |
ld a, d |
680 |
ld a, h |
| 610 |
cp h |
681 |
cp d |
| 611 |
jp nc, .set_exact |
682 |
jp nc, .set_exact |
| 612 |
ld a, e |
683 |
ld a, l |
| 613 |
cp l |
684 |
cp e |
| 614 |
jp nc, .set_exact |
685 |
jp nc, .set_exact |
| 615 |
|
686 |
|
| 616 |
jp .done |
687 |
jp .done |
| 617 |
|
688 |
|
| 618 |
.set_exact: |
689 |
.set_exact: |
| 619 |
ld h, d |
690 |
ld d, h |
| 620 |
ld l, e |
691 |
ld e, l |
| 621 |
.done: |
692 |
.done: |
| 622 |
push de |
693 |
pop hl |
| 623 |
|
694 |
ld a, e |
| 624 |
; ;; Write new channel period |
695 |
ld [hl+], a |
| 625 |
push hl |
696 |
ld [hl], d |
| 626 |
|
|
|
| 627 |
dec sp |
|
|
| 628 |
dec sp |
|
|
| 629 |
dec sp |
|
|
| 630 |
dec sp |
|
|
| 631 |
|
|
|
| 632 |
; ld a, l |
|
|
| 633 |
; ld [channel_period2], a |
|
|
| 634 |
|
|
|
| 635 |
; ld a, h |
|
|
| 636 |
; ld [channel_period2+1], a |
|
|
| 637 |
|
697 |
|
| 638 |
jp _update_channel |
698 |
jp _update_channel |
| 639 |
|
699 |
|
@@ -675,43 +735,26 @@ _dosound: |
| 675 |
jp process_tick |
735 |
jp process_tick |
| 676 |
|
736 |
|
| 677 |
.process_effects: |
737 |
.process_effects: |
| 678 |
|
|
|
| 679 |
restore_stack_pointer: MACRO |
|
|
| 680 |
ld hl, old_stack_pointer+1 |
|
|
| 681 |
ld a, [hl-] |
|
|
| 682 |
ld l, [hl] |
|
|
| 683 |
ld h, a |
|
|
| 684 |
ld sp, hl |
|
|
| 685 |
ENDM |
|
|
| 686 |
|
|
|
| 687 |
;; Only do effects if not on tick zero |
738 |
;; Only do effects if not on tick zero |
| 688 |
ld [old_stack_pointer], sp |
|
|
| 689 |
|
739 |
|
| 690 |
loadShort pattern1, d, e |
740 |
loadShort pattern1, d, e |
| 691 |
call _lookup_note |
741 |
call _lookup_note |
| 692 |
ld e, 0 |
742 |
ld e, 0 |
| 693 |
ld sp, channel1 |
743 |
call _doeffect |
| 694 |
jp _doeffect |
|
|
| 695 |
|
744 |
|
| 696 |
after_effect1: |
745 |
after_effect1: |
| 697 |
restore_stack_pointer |
|
|
| 698 |
|
|
|
| 699 |
loadShort pattern2, d, e |
746 |
loadShort pattern2, d, e |
| 700 |
call _lookup_note |
747 |
call _lookup_note |
| 701 |
ld e, 1 |
748 |
ld e, 1 |
| 702 |
ld sp, channel2 |
749 |
call _doeffect |
| 703 |
jp _doeffect |
|
|
| 704 |
|
750 |
|
| 705 |
after_effect2: |
751 |
after_effect2: |
| 706 |
restore_stack_pointer |
|
|
| 707 |
|
|
|
| 708 |
; loadShort pattern3, d, e |
752 |
; loadShort pattern3, d, e |
| 709 |
; call _lookup_note |
753 |
; call _lookup_note |
| 710 |
; ld e, 2 |
754 |
; ld e, 2 |
| 711 |
; call _doeffect |
755 |
; call _doeffect |
| 712 |
|
756 |
|
| 713 |
after_effect3: |
757 |
after_effect3: |
| 714 |
|
|
|
| 715 |
; loadShort pattern4, d, e |
758 |
; loadShort pattern4, d, e |
| 716 |
; call _lookup_note |
759 |
; call _lookup_note |
| 717 |
; ld e, 3 |
760 |
; ld e, 3 |