-
Notifications
You must be signed in to change notification settings - Fork 1
/
ggsound.asm
2107 lines (1721 loc) · 48 KB
/
ggsound.asm
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;
; ggsound.asm
;
; by Derek Andrews <[email protected]>
;
; Distributed under Unlicense (see UNLICENSE.txt)
;
;
;Expects sound_param_byte_0 to contain desired region (SOUND_REGION_NTSC or SOUND_REGION_PAL)
;Expects sound_param_word_0 to contain song list address.
;Expects sound_param_word_1 to contain sfx list address.
;Expects sound_param_word_2 to contain envelopes list address.
;If FEATURE_DPCM is defined, then
;Expects sound_param_word_3 to contain dpcm sample address.
sound_initialize:
lda #1
sta sound_disable_update
lda sound_param_byte_0
sta sound_region
lda sound_param_word_0
sta song_list_address
lda sound_param_word_0+1
sta song_list_address+1
lda sound_param_word_1
sta sfx_list_address
lda sound_param_word_1+1
sta sfx_list_address+1
;Get volume envelopes address.
ldy #0
lda (sound_param_word_2),y
sta base_address_volume_envelopes
iny
lda (sound_param_word_2),y
sta base_address_volume_envelopes+1
ifdef FEATURE_ARPEGGIOS
;Get arpeggio envelopes address.
iny
lda (sound_param_word_2),y
sta base_address_arpeggio_envelopes
iny
lda (sound_param_word_2),y
sta base_address_arpeggio_envelopes+1
endif
;Get pitch envelopes address.
iny
lda (sound_param_word_2),y
sta base_address_pitch_envelopes
iny
lda (sound_param_word_2),y
sta base_address_pitch_envelopes+1
;Get duty envelopes address.
iny
lda (sound_param_word_2),y
sta base_address_duty_envelopes
iny
lda (sound_param_word_2),y
sta base_address_duty_envelopes+1
ifdef FEATURE_DPCM
;Get dpcm samples list.
ldy #0
lda (sound_param_word_3),y
sta base_address_dpcm_sample_table
iny
lda (sound_param_word_3),y
sta base_address_dpcm_sample_table+1
;Get dpcm note to sample index table.
iny
lda (sound_param_word_3),y
sta base_address_dpcm_note_to_sample_index
iny
lda (sound_param_word_3),y
sta base_address_dpcm_note_to_sample_index+1
;Get dpcm note to sample length table.
iny
lda (sound_param_word_3),y
sta base_address_dpcm_note_to_sample_length
iny
lda (sound_param_word_3),y
sta base_address_dpcm_note_to_sample_length+1
;Get dpcm note to loop and pitch index table.
iny
lda (sound_param_word_3),y
sta base_address_dpcm_note_to_loop_pitch_index
iny
lda (sound_param_word_3),y
sta base_address_dpcm_note_to_loop_pitch_index+1
endif
lda sound_region
cmp #SOUND_REGION_PAL
beq @pal
@nstc:
lda #<(ntsc_note_table_lo)
sta base_address_note_table_lo
lda #>(ntsc_note_table_lo)
sta base_address_note_table_lo+1
lda #<(ntsc_note_table_hi)
sta base_address_note_table_hi
lda #>(ntsc_note_table_hi)
sta base_address_note_table_hi+1
jmp @done
@pal:
lda #<(pal_note_table_lo)
sta base_address_note_table_lo
lda #>(pal_note_table_lo)
sta base_address_note_table_lo+1
lda #<(pal_note_table_hi)
sta base_address_note_table_hi
lda #>(pal_note_table_hi)
sta base_address_note_table_hi+1
@done:
;Enable square 1, square 2, triangle and noise.
lda #%00001111
sta $4015
lda #0
sta apu_data_ready
ifdef FEATURE_DPCM
lda #DPCM_STATE_NOP
sta apu_dpcm_state
endif
jsr sound_initialize_apu_buffer
;Make sure all streams are killed.
jsr sound_stop
dec sound_disable_update
rts
;Kill all active streams and halt sound.
sound_stop:
;Save x.
txa
pha
inc sound_disable_update
;Kill all streams.
ldx #(MAX_STREAMS-1)
-
lda #0
sta stream_flags,x
dex
bpl -
jsr sound_initialize_apu_buffer
dec sound_disable_update
;Restore x.
pla
tax
rts
;Updates all playing streams, if actve. Streams 0 through MAX_MUSIC_STREAMS-1
;are assumed to be music streams. The last two streams, are assumed to be sound
;effect streams. When these are playing, their channel control registers are
;copied overtop what the corresponding music streams had written, so the sound
;effect streams essentially take over while they are playing. When the sound
;effect streams are finished, they signify their corresponding music stream
;(via the TRM callback) to silence themselves until the next note to avoid
;ugly volume envelope transitions. DPCM is handled within this framework by
;a state machine that handles sound effect priority.
sound_update:
;Save regs.
txa
pha
;Signal apu data not ready.
lda #0
sta apu_data_ready
;First copy all music streams.
ldx #0
@song_stream_register_copy_loop:
;Load whether this stream is active.
lda stream_flags,x
and #STREAM_ACTIVE_TEST
beq @song_stream_not_active
;Update the stream.
jsr stream_update
;Load channel number.
lda stream_channel,x
;Multiply by four to get location within apu_register_sets.
asl
asl
tay
;Copy the registers over.
lda stream_channel_register_1,x
sta apu_register_sets,y
lda stream_channel_register_2,x
sta apu_register_sets+1,y
lda stream_channel_register_3,x
sta apu_register_sets+2,y
lda stream_channel_register_4,x
sta apu_register_sets+3,y
@song_stream_not_active:
inx
cpx #MAX_MUSIC_STREAMS
bne @song_stream_register_copy_loop
do_not_update_music:
ldx #soundeffect_one
@sfx_stream_register_copy_loop:
;Load whether this stream is active.
lda stream_flags,x
and #STREAM_ACTIVE_TEST
beq @sfx_stream_not_active
;Update the stream.
jsr stream_update
;Load channel number
lda stream_channel,x
;Multiply by four to get location within apu_register_sets.
asl
asl
tay
;Copy the registers over.
lda stream_channel_register_1,x
sta apu_register_sets,y
lda stream_channel_register_2,x
sta apu_register_sets+1,y
lda stream_channel_register_3,x
sta apu_register_sets+2,y
lda stream_channel_register_4,x
sta apu_register_sets+3,y
@sfx_stream_not_active:
inx
cpx #MAX_STREAMS
bne @sfx_stream_register_copy_loop
;Signial apu data ready.
lda #1
sta apu_data_ready
;Restore regs.
pla
tax
rts
;Note table borrowed from periods.s provided by FamiTracker's NSF driver.
ntsc_note_table_lo:
.dl $07F1, $077F, $0713, $06AD, $064D, $05F3, $059D, $054C, $0500, $04B8, $0474, $0434
.dl $03F8, $03BF, $0389, $0356, $0326, $02F9, $02CE, $02A6, $0280, $025C, $023A, $021A
.dl $01FB, $01DF, $01C4, $01AB, $0193, $017C, $0167, $0152, $013F, $012D, $011C, $010C
.dl $00FD, $00EF, $00E1, $00D5, $00C9, $00BD, $00B3, $00A9, $009F, $0096, $008E, $0086
.dl $007E, $0077, $0070, $006A, $0064, $005E, $0059, $0054, $004F, $004B, $0046, $0042
.dl $003F, $003B, $0038, $0034, $0031, $002F, $002C, $0029, $0027, $0025, $0023, $0021
.dl $001F, $001D, $001B, $001A, $0018, $0017, $0015, $0014, $0013, $0012, $0011, $0010
.dl $000F, $000E, $000D
ntsc_note_table_hi:
.dh $07F1, $077F, $0713, $06AD, $064D, $05F3, $059D, $054C, $0500, $04B8, $0474, $0434
.dh $03F8, $03BF, $0389, $0356, $0326, $02F9, $02CE, $02A6, $0280, $025C, $023A, $021A
.dh $01FB, $01DF, $01C4, $01AB, $0193, $017C, $0167, $0152, $013F, $012D, $011C, $010C
.dh $00FD, $00EF, $00E1, $00D5, $00C9, $00BD, $00B3, $00A9, $009F, $0096, $008E, $0086
.dh $007E, $0077, $0070, $006A, $0064, $005E, $0059, $0054, $004F, $004B, $0046, $0042
.dh $003F, $003B, $0038, $0034, $0031, $002F, $002C, $0029, $0027, $0025, $0023, $0021
.dh $001F, $001D, $001B, $001A, $0018, $0017, $0015, $0014, $0013, $0012, $0011, $0010
.dh $000F, $000E, $000D
pal_note_table_lo:
.dl $0760, $06F6, $0692, $0634, $05DB, $0586, $0537, $04EC, $04A5, $0462, $0423, $03E8
.dl $03B0, $037B, $0349, $0319, $02ED, $02C3, $029B, $0275, $0252, $0231, $0211, $01F3
.dl $01D7, $01BD, $01A4, $018C, $0176, $0161, $014D, $013A, $0129, $0118, $0108, $00F9
.dl $00EB, $00DE, $00D1, $00C6, $00BA, $00B0, $00A6, $009D, $0094, $008B, $0084, $007C
.dl $0075, $006E, $0068, $0062, $005D, $0057, $0052, $004E, $0049, $0045, $0041, $003E
.dl $003A, $0037, $0034, $0031, $002E, $002B, $0029, $0026, $0024, $0022, $0020, $001E
.dl $001D, $001B, $0019, $0018, $0016, $0015, $0014, $0013, $0012, $0011, $0010, $000F
.dl $000E, $000D, $000C
pal_note_table_hi:
.dh $0760, $06F6, $0692, $0634, $05DB, $0586, $0537, $04EC, $04A5, $0462, $0423, $03E8
.dh $03B0, $037B, $0349, $0319, $02ED, $02C3, $029B, $0275, $0252, $0231, $0211, $01F3
.dh $01D7, $01BD, $01A4, $018C, $0176, $0161, $014D, $013A, $0129, $0118, $0108, $00F9
.dh $00EB, $00DE, $00D1, $00C6, $00BA, $00B0, $00A6, $009D, $0094, $008B, $0084, $007C
.dh $0075, $006E, $0068, $0062, $005D, $0057, $0052, $004E, $0049, $0045, $0041, $003E
.dh $003A, $0037, $0034, $0031, $002E, $002B, $0029, $0026, $0024, $0022, $0020, $001E
.dh $001D, $001B, $0019, $0018, $0016, $0015, $0014, $0013, $0012, $0011, $0010, $000F
.dh $000E, $000D, $000C
channel_callback_table_lo:
.dl (square_1_play_note)
.dl (square_2_play_note)
.dl (triangle_play_note)
.dl (noise_play_note)
ifdef FEATURE_DPCM
.dl (dpcm_play_note)
endif
channel_callback_table_hi:
.dh (square_1_play_note)
.dh (square_2_play_note)
.dh (triangle_play_note)
.dh (noise_play_note)
ifdef FEATURE_DPCM
.dh (dpcm_play_note)
endif
stream_callback_table_lo:
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_s)
.dl (stream_set_length_lo)
.dl (stream_set_length_hi)
.dl (stream_set_volume_envelope)
.dl (stream_set_pitch_envelope)
.dl (stream_set_duty_envelope)
.dl (stream_goto)
.dl (stream_call)
.dl (stream_return)
.dl (stream_terminate)
ifdef FEATURE_ARPEGGIOS
.dl (stream_set_arpeggio_envelope)
endif
stream_callback_table_hi:
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_s)
.dh (stream_set_length_lo)
.dh (stream_set_length_hi)
.dh (stream_set_volume_envelope)
.dh (stream_set_pitch_envelope)
.dh (stream_set_duty_envelope)
.dh (stream_goto)
.dh (stream_call)
.dh (stream_return)
.dh (stream_terminate)
ifdef FEATURE_ARPEGGIOS
.dh (stream_set_arpeggio_envelope)
endif
;****************************************************************
;These callbacks are all note playback and only execute once per
;frame.
;****************************************************************
square_1_play_note:
;Set negate flag for sweep unit.
lda #$08
sta stream_channel_register_2,x
ifdef FEATURE_ARPEGGIOS
;Load arpeggio index.
lda stream_arpeggio_index,x
asl a
tay
;Load arpeggio address.
lda (base_address_arpeggio_envelopes),y
sta sound_local_word_0
iny
lda (base_address_arpeggio_envelopes),y
sta sound_local_word_0+1
ldy stream_arpeggio_offset,x
lda (sound_local_word_0),y
cmp #ENV_STOP
beq @arpeggio_stop
cmp #ENV_LOOP
beq @arpeggio_loop
@arpeggio_play:
;We're changing notes.
lda stream_flags,x
and #STREAM_PITCH_LOADED_CLEAR
sta stream_flags,x
;Load the current arpeggio value and add it to current note.
clc
lda (sound_local_word_0),y
adc stream_byte
tay
;Advance arpeggio offset.
inc stream_arpeggio_offset,x
jmp @arpeggio_done
@arpeggio_stop:
;Just load the current note.
ldy stream_byte
jmp @arpeggio_done
@arpeggio_loop:
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_arpeggio_offset,x
tay
;We're changing notes.
lda stream_flags,x
and #STREAM_PITCH_LOADED_CLEAR
sta stream_flags,x
;Load the current arpeggio value and add it to current note.
clc
lda (sound_local_word_0),y
adc stream_byte
tay
;Advance arpeggio offset.
inc stream_arpeggio_offset,x
@arpeggio_done:
else
ldy stream_byte
endif
;Skip loading note pitch if already loaded, to allow envelopes
;to modify the pitch.
lda stream_flags,x
and #STREAM_PITCH_LOADED_TEST
bne @pitch_already_loaded
lda stream_flags,x
ora #STREAM_PITCH_LOADED_SET
sta stream_flags,x
;Load low byte of note.
lda (base_address_note_table_lo),y
;Store in low 8 bits of pitch.
sta stream_channel_register_3,x
;Load high byte of note.
lda (base_address_note_table_hi),y
sta stream_channel_register_4,x
@pitch_already_loaded:
lda stream_flags,x
and #STREAM_SILENCE_TEST
bne @silence_until_note
@note_not_silenced:
;Load volume index.
lda stream_volume_index,x
asl
tay
;Load volume address.
lda (base_address_volume_envelopes),y
sta sound_local_word_0
iny
lda (base_address_volume_envelopes),y
sta sound_local_word_0+1
;Load volume offset.
ldy stream_volume_offset,x
;Load volume value for this frame, branch if opcode.
lda (sound_local_word_0),y
cmp #ENV_STOP
beq @volume_stop
cmp #ENV_LOOP
bne @skip_volume_loop
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_volume_offset,x
tay
@skip_volume_loop:
;Initialize channel control register with envelope decay and
;length counter disabled but preserving current duty cycle.
lda stream_channel_register_1,x
and #%11000000
ora #%00110000
;Load current volume value.
ora (sound_local_word_0),y
sta stream_channel_register_1,x
inc stream_volume_offset,x
@volume_stop:
jmp @done
@silence_until_note:
lda stream_channel_register_1,x
and #%11000000
ora #%00110000
sta stream_channel_register_1,x
@done:
;Load pitch index.
lda stream_pitch_index,x
asl
tay
;Load pitch address.
lda (base_address_pitch_envelopes),y
sta sound_local_word_0
iny
lda (base_address_pitch_envelopes),y
sta sound_local_word_0+1
;Load pitch offset.
ldy stream_pitch_offset,x
;Load pitch value.
lda (sound_local_word_0),y
cmp #ENV_STOP
beq @pitch_stop
cmp #ENV_LOOP
bne @skip_pitch_loop
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_pitch_offset,x
tay
@skip_pitch_loop:
;Test sign.
lda (sound_local_word_0),y
bmi @pitch_delta_negative
@pitch_delta_positive:
clc
lda stream_channel_register_3,x
adc (sound_local_word_0),y
sta stream_channel_register_3,x
lda stream_channel_register_4,x
adc #0
sta stream_channel_register_4,x
jmp @pitch_delta_test_done
@pitch_delta_negative:
clc
lda stream_channel_register_3,x
adc (sound_local_word_0),y
sta stream_channel_register_3,x
lda stream_channel_register_4,x
adc #$ff
sta stream_channel_register_4,x
@pitch_delta_test_done:
;Move pitch offset along.
inc stream_pitch_offset,x
@pitch_stop:
@duty_code:
;Load duty index.
lda stream_duty_index,x
asl
tay
;Load duty address.
lda (base_address_duty_envelopes),y
sta sound_local_word_0
iny
lda (base_address_duty_envelopes),y
sta sound_local_word_0+1
;Load duty offset.
ldy stream_duty_offset,x
;Load duty value for this frame, but hard code flags and duty for now.
lda (sound_local_word_0),y
cmp #DUTY_ENV_STOP
beq @duty_stop
cmp #DUTY_ENV_LOOP
bne @skip_duty_loop
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_duty_offset,x
tay
@skip_duty_loop:
;Or the duty value into the register.
lda stream_channel_register_1,x
and #%00111111
ora (sound_local_word_0),y
sta stream_channel_register_1,x
;Move duty offset along.
inc stream_duty_offset,x
@duty_stop:
rts
square_2_play_note = square_1_play_note
triangle_play_note:
;Load note index
ldy stream_byte
ifdef FEATURE_ARPEGGIOS
;Load arpeggio index.
lda stream_arpeggio_index,x
asl a
tay
;Load arpeggio address.
lda (base_address_arpeggio_envelopes),y
sta sound_local_word_0
iny
lda (base_address_arpeggio_envelopes),y
sta sound_local_word_0+1
ldy stream_arpeggio_offset,x
lda (sound_local_word_0),y
cmp #ENV_STOP
beq @arpeggio_stop
cmp #ENV_LOOP
beq @arpeggio_loop
@arpeggio_play:
;We're changing notes.
lda stream_flags,x
and #STREAM_PITCH_LOADED_CLEAR
sta stream_flags,x
;Load the current arpeggio value and add it to current note.
clc
lda (sound_local_word_0),y
adc stream_byte
tay
;Advance arpeggio offset.
inc stream_arpeggio_offset,x
jmp @arpeggio_done
@arpeggio_stop:
;Just load the current note.
ldy stream_byte
jmp @arpeggio_done
@arpeggio_loop:
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_arpeggio_offset,x
tay
;We're changing notes.
lda stream_flags,x
and #STREAM_PITCH_LOADED_CLEAR
sta stream_flags,x
;Load the current arpeggio value and add it to current note.
clc
lda (sound_local_word_0),y
adc stream_byte
tay
;Advance arpeggio offset.
inc stream_arpeggio_offset,x
@arpeggio_done:
else
ldy stream_byte
endif
;Skip loading note pitch if already loaded, to allow envelopes
;to modify the pitch.
lda stream_flags,x
and #STREAM_PITCH_LOADED_TEST
bne @pitch_already_loaded
lda stream_flags,x
ora #STREAM_PITCH_LOADED_SET
sta stream_flags,x
;Load low byte of note.
lda (base_address_note_table_lo),y
;Store in low 8 bits of pitch.
sta stream_channel_register_3,x
;Load high byte of note.
lda (base_address_note_table_hi),y
sta stream_channel_register_4,x
@pitch_already_loaded:
;Load volume index.
lda stream_volume_index,x
asl
tay
;Load volume address.
lda (base_address_volume_envelopes),y
sta sound_local_word_0
iny
lda (base_address_volume_envelopes),y
sta sound_local_word_0+1
;Load volume offset.
ldy stream_volume_offset,x
;Load volume value for this frame, but hard code flags and duty for now.
lda (sound_local_word_0),y
cmp #ENV_STOP
beq @volume_stop
cmp #ENV_LOOP
bne @skip_volume_loop
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_volume_offset,x
tay
@skip_volume_loop:
lda #%10000000
ora (sound_local_word_0),y
sta stream_channel_register_1,x
inc stream_volume_offset,x
@volume_stop:
;Load pitch index.
lda stream_pitch_index,x
asl
tay
;Load pitch address.
lda (base_address_pitch_envelopes),y
sta sound_local_word_0
iny
lda (base_address_pitch_envelopes),y
sta sound_local_word_0+1
;Load pitch offset.
ldy stream_pitch_offset,x
;Load pitch value.
lda (sound_local_word_0),y
cmp #ENV_STOP
beq @pitch_stop
cmp #ENV_LOOP
bne @skip_pitch_loop
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_pitch_offset,x
tay
@skip_pitch_loop:
;Test sign.
lda (sound_local_word_0),y
bmi @pitch_delta_negative
@pitch_delta_positive:
clc
lda stream_channel_register_3,x
adc (sound_local_word_0),y
sta stream_channel_register_3,x
lda stream_channel_register_4,x
adc #0
sta stream_channel_register_4,x
jmp @pitch_delta_test_done
@pitch_delta_negative:
clc
lda stream_channel_register_3,x
adc (sound_local_word_0),y
sta stream_channel_register_3,x
lda stream_channel_register_4,x
adc #$ff
sta stream_channel_register_4,x
@pitch_delta_test_done:
;Move pitch offset along.
inc stream_pitch_offset,x
@pitch_stop:
rts
noise_play_note:
;Load note index.
lda stream_byte
and #%01111111
sta sound_local_byte_0
;Note index is actually the "sound type" for noise channel.
lda stream_channel_register_3,x
and #%10000000
ora sound_local_byte_0
sta stream_channel_register_3,x
;Load volume index.
lda stream_volume_index,x
asl
tay
;Load volume address.
lda (base_address_volume_envelopes),y
sta sound_local_word_0
iny
lda (base_address_volume_envelopes),y
sta sound_local_word_0+1
;Load volume offset.
ldy stream_volume_offset,x
;Load volume value for this frame, hard code disable flags.
lda (sound_local_word_0),y
cmp #ENV_STOP
beq @volume_stop
cmp #ENV_LOOP
bne @skip_volume_loop
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_volume_offset,x
tay
@skip_volume_loop:
lda #%00110000
ora (sound_local_word_0),y
sta stream_channel_register_1,x
;Move volume offset along.
inc stream_volume_offset,x
@volume_stop:
@duty_code:
;Load duty index.
lda stream_duty_index,x
asl
tay
;Load duty address.
lda (base_address_duty_envelopes),y
sta sound_local_word_0
iny
lda (base_address_duty_envelopes),y
sta sound_local_word_0+1
;Load duty offset.
ldy stream_duty_offset,x
;Load duty value for this frame, but hard code flags and duty for now.
lda (sound_local_word_0),y
cmp #DUTY_ENV_STOP
beq @duty_stop
cmp #DUTY_ENV_LOOP
bne @skip_duty_loop
;We hit a loop opcode, advance envelope index and load loop point.
iny
lda (sound_local_word_0),y
sta stream_duty_offset,x
tay
@skip_duty_loop:
;We only care about bit 6 for noise, and we want it in bit 7 position.
lda (sound_local_word_0),y
asl
sta sound_local_byte_0
lda stream_channel_register_3,x
and #%01111111
ora sound_local_byte_0
sta stream_channel_register_3,x
;Move duty offset along.
inc stream_duty_offset,x
@duty_stop:
rts
ifdef FEATURE_DPCM
dpcm_play_note:
;Determine if silence until note is set.
lda stream_flags,x
and #STREAM_SILENCE_TEST
bne @note_already_played
;Load note index.
ldy stream_byte
;Get sample index.
lda (base_address_dpcm_note_to_sample_index),y
bmi @no_sample
;This sample index looks up into base_address_dpcm_sample_table.
tay
lda (base_address_dpcm_sample_table),y
sta stream_channel_register_3,x
;Get loop and pitch from dpcm_note_to_loop_pitch_index table.
ldy stream_byte
lda (base_address_dpcm_note_to_loop_pitch_index),y
sta stream_channel_register_1,x
;Get sample length.
lda (base_address_dpcm_note_to_sample_length),y
sta stream_channel_register_4,x
;Upload the dpcm data if sfx commands are not overriding.
lda apu_dpcm_state
cmp #DPCM_STATE_WAIT
beq @skip_update_apu_dpcm_state
cmp #DPCM_STATE_UPLOAD_THEN_WAIT
beq @skip_update_apu_dpcm_state
lda #DPCM_STATE_UPLOAD
sta apu_dpcm_state
@skip_update_apu_dpcm_state:
lda stream_flags,x
ora #STREAM_SILENCE_SET
sta stream_flags,x
@no_sample:
@note_already_played:
rts
endif
;****************************************************************
;These callbacks are all stream control and execute in sequence
;until exhausted.
;****************************************************************
stream_set_volume_envelope:
advance_stream_read_address
;Load byte at read address.
lda stream_read_address_lo,x
sta sound_local_word_0
lda stream_read_address_hi,x
sta sound_local_word_0+1
ldy #0
lda (sound_local_word_0),y
sta stream_volume_index,x
lda #0
sta stream_volume_offset,x
rts
ifdef FEATURE_ARPEGGIOS
stream_set_arpeggio_envelope:
advance_stream_read_address