-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundSystem.asm
3849 lines (3313 loc) · 93 KB
/
SoundSystem.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
INCLUDE "SoundSystemNotes.inc"
INCLUDE "SoundSystem.def"
INCLUDE "SoundSystem.inc"
; tabs=8,hard
;***************************************************************************************************************************
;* default behaviors
;***************************************************************************************************************************
; force support for color gameboy-specific roms to be disabled if not user-specified
IF !DEF(SOUNDSYSTEM_GBC_COMPATIBLE)
SOUNDSYSTEM_GBC_COMPATIBLE EQU 0
ENDC
; force support for banking if not user-specified
IF !DEF(SOUNDSYSTEM_ROM_BANKING)
SOUNDSYSTEM_ROM_BANKING EQU 1
ENDC
; force support for large roms to be disabled if not user-specified
IF !DEF(SOUNDSYSTEM_LARGE_ROM)
SOUNDSYSTEM_LARGE_ROM EQU 0
ENDC
; force the code to reside in bank 0 if not user-specified
IF !DEF(SOUNDSYSTEM_CODE_BANK)
SOUNDSYSTEM_CODE_BANK EQU 0
ENDC
; force the variables to reside in wram bank 0 if not user-specified
IF !DEF(SOUNDSYSTEM_WRAM_BANK)
SOUNDSYSTEM_WRAM_BANK EQU 0
ENDC
; force the sfx to be enabled if not user-specified
if !DEF(SOUNDSYSTEM_ENABLE_SFX)
SOUNDSYSTEM_ENABLE_SFX EQU 1
ENDC
; force the vu meters to be disabled if not user-specified
if !DEF(SOUNDSYSTEM_ENABLE_VUM)
SOUNDSYSTEM_ENABLE_VUM EQU 0
ENDC
; force certain settings if the rom is not specific to color gameboy
IF (SOUNDSYSTEM_GBC_COMPATIBLE == 0)
PURGE SOUNDSYSTEM_WRAM_BANK
SOUNDSYSTEM_WRAM_BANK EQU 0
ENDC
; do some sanity checking
IF (SOUNDSYSTEM_GBC_COMPATIBLE != 0)
ASSERT(SOUNDSYSTEM_WRAM_BANK < 8)
; force boolean
PURGE SOUNDSYSTEM_GBC_COMPATIBLE
SOUNDSYSTEM_GBC_COMPATIBLE EQU 1
ENDC
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ASSERT(SOUNDSYSTEM_ROM_BANKING != 0)
ASSERT(SOUNDSYSTEM_CODE_BANK < 512)
; force boolean
PURGE SOUNDSYSTEM_LARGE_ROM
SOUNDSYSTEM_LARGE_ROM EQU 1
ENDC
IF (SOUNDSYSTEM_ENABLE_SFX != 0)
; force boolean
PURGE SOUNDSYSTEM_ENABLE_SFX
SOUNDSYSTEM_ENABLE_SFX EQU 1
ENDC
IF (SOUNDSYSTEM_ENABLE_VUM != 0)
; force boolean
PURGE SOUNDSYSTEM_ENABLE_VUM
SOUNDSYSTEM_ENABLE_VUM EQU 1
ENDC
sizeof_BANK_VAR = 1+SOUNDSYSTEM_LARGE_ROM ; the size, in bytes, of the bank variables
; display the configuration
PRINTLN "SoundSystem Configuration:"
IF (SOUNDSYSTEM_GBC_COMPATIBLE == 0)
PRINTLN " GBC Only: no"
ELSE
PRINTLN " GBC Only: YES"
ENDC
IF (SOUNDSYSTEM_LARGE_ROM == 0)
PRINTLN " Large ROM: no"
ELSE
PRINTLN " Large ROM: YES"
ENDC
PRINTLN " Code Bank: {SOUNDSYSTEM_CODE_BANK}"
PRINTLN " WRAM Bank: {SOUNDSYSTEM_WRAM_BANK}"
IF (SOUNDSYSTEM_ROM_BANKING == 0)
PRINTLN " ROM Banking: disabled"
ELSE
PRINTLN " ROM Banking: ENABLED"
ENDC
IF (SOUNDSYSTEM_ENABLE_SFX == 0)
PRINTLN " SFX: disabled"
ELSE
PRINTLN " SFX: ENABLED"
ENDC
IF (SOUNDSYSTEM_ENABLE_VUM == 0)
PRINTLN " VU Meters: disabled"
ELSE
PRINTLN " VU Meters: ENABLED"
ENDC
;***************************************************************************************************************************
;* hardware registers
;***************************************************************************************************************************
rROMB0 EQU $2000 ; $2000->$2FFF
rROMB1 EQU $3000 ; $3000->$3FFF - If more than 256 ROM banks are present.
rSVBK EQU $FF70
rAUD1SWEEP EQU $FF10
rAUD1LEN EQU $FF11
rAUD1ENV EQU $FF12
rAUD1LOW EQU $FF13
rAUD1HIGH EQU $FF14
rAUD2LEN EQU $FF16
rAUD2ENV EQU $FF17
rAUD2LOW EQU $FF18
rAUD2HIGH EQU $FF19
rAUD3ENA EQU $FF1A
rAUD3LEN EQU $FF1B
rAUD3LEVEL EQU $FF1C
rAUD3LOW EQU $FF1D
rAUD3HIGH EQU $FF1E
rAUD4LEN EQU $FF20
rAUD4ENV EQU $FF21
rAUD4POLY EQU $FF22
rAUD4GO EQU $FF23
rAUDVOL EQU $FF24
rAUDTERM EQU $FF25
rAUDENA EQU $FF26
_AUD3WAVERAM EQU $FF30 ; $FF30->$FF3F
; values for rAUD1LEN, rAUD2LEN
AUDLEN_DUTY_75 EQU %11000000 ; 75%
AUDLEN_DUTY_50 EQU %10000000 ; 50%
AUDLEN_DUTY_25 EQU %01000000 ; 25%
AUDLEN_DUTY_12_5 EQU %00000000 ; 12.5%
AUDLEN_LENGTHMASK EQU %00111111
; values for rAUD1HIGH, rAUD2HIGH, rAUD3HIGH
AUDHIGH_RESTART EQU %10000000
AUDHIGH_LENGTH_ON EQU %01000000
AUDHIGH_LENGTH_OFF EQU %00000000
; values for rAUD3ENA
AUD3ENA_ON EQU %10000000
; values for rAUDVOL
AUDVOL_VIN_LEFT EQU %10000000 ; SO2
AUDVOL_VIN_RIGHT EQU %00001000 ; SO1
; values for rAUDTERM
; SO2
AUDTERM_4_LEFT EQU %10000000
AUDTERM_3_LEFT EQU %01000000
AUDTERM_2_LEFT EQU %00100000
AUDTERM_1_LEFT EQU %00010000
; SO1
AUDTERM_4_RIGHT EQU %00001000
AUDTERM_3_RIGHT EQU %00000100
AUDTERM_2_RIGHT EQU %00000010
AUDTERM_1_RIGHT EQU %00000001
AUDTERM_ALL EQU $FF ; shorthand instead of ORing all the EQUs together
;***************************************************************************************************************************
;* supported music commands
;***************************************************************************************************************************
RSSET 0
MUSIC_CMD_ENDOFFRAME RB 1
MUSIC_CMD_PLAYINSTNOTE RB 1
MUSIC_CMD_PLAYINST RB 1
MUSIC_CMD_SETVOLUME RB 1
MUSIC_CMD_VIBRATO_ON RB 1
MUSIC_CMD_EFFECT_OFF RB 1
MUSIC_CMD_SYNCFLAG RB 1
MUSIC_CMD_ENDOFPATTERN RB 1
MUSIC_CMD_GOTOORDER RB 1
MUSIC_CMD_ENDOFSONG RB 1
MUSIC_CMD_SETSPEED RB 1
MUSIC_CMD_ENDOFFRAME1X RB 1
MUSIC_CMD_ENDOFFRAME2X RB 1
MUSIC_CMD_ENDOFFRAME3X RB 1
MUSIC_CMD_ENDOFFRAME4X RB 1
MUSIC_CMD_PITCHUP_ON RB 1
MUSIC_CMD_PITCHDOWN_ON RB 1
MUSIC_CMD_TRIPLENOTE_ON RB 1
MUSIC_CMD_EXTRA RB 1
;***************************************************************************************************************************
;* supported music effects
;***************************************************************************************************************************
RSRESET
MUSIC_FX_NONE RB 1
MUSIC_FX_VIB1 RB 1
MUSIC_FX_VIB2 RB 1
MUSIC_FX_TRIPLEFREQ1 RB 1
MUSIC_FX_TRIPLEFREQ2 RB 1
MUSIC_FX_TRIPLEFREQ3 RB 1
MUSIC_FX_PITCHUP RB 1
MUSIC_FX_PITCHDOWN RB 1
;***************************************************************************************************************************
;* supported instrument commands
;***************************************************************************************************************************
RSRESET
; common commands
MUSIC_INSTCMD_X_FRAMEEND RB 1
MUSIC_INSTCMD_X_START RB 1
MUSIC_INSTCMD_X_END RB 1
MUSIC_INSTCMD_X_ENVELOPE RB 1
MUSIC_INSTCMD_X_STARTFREQ RB 1
MUSIC_INSTCMD_X_ENVELOPEVOL RB 1
MUSIC_INSTCMD_X_STARTENVVOLFREQ RB 1
MUSIC_INSTCMD_X_PANMID RB 1
MUSIC_INSTCMD_X_PANRIGHT RB 1
MUSIC_INSTCMD_X_PANLEFT RB 1
; count of common instrument commands
MUSIC_INSTCMD_COMMONCOUNT RB 0
; specific commands
; channels 1 and 2
RSSET MUSIC_INSTCMD_COMMONCOUNT
MUSIC_INSTCMD_12_PULSELEN RB 1
MUSIC_INSTCMD_1_SWEEP RB 1
; channel 3
RSSET MUSIC_INSTCMD_COMMONCOUNT
MUSIC_INSTCMD_3_WAVE RB 1
MUSIC_INSTCMD_3_LEN RB 1
; channel 4
RSSET MUSIC_INSTCMD_COMMONCOUNT
MUSIC_INSTCMD_4_POLYLOAD RB 1
MUSIC_INSTCMD_4_LEN RB 1
;***************************************************************************************************************************
;* wSoundFXLock bit definitions
;***************************************************************************************************************************
SFXLOCKF_4_LEFT EQU AUDTERM_4_LEFT
SFXLOCKF_3_LEFT EQU AUDTERM_3_LEFT
SFXLOCKF_2_LEFT EQU AUDTERM_2_LEFT
SFXLOCKF_1_LEFT EQU AUDTERM_1_LEFT
SFXLOCKF_4_RIGHT EQU AUDTERM_4_RIGHT
SFXLOCKF_3_RIGHT EQU AUDTERM_3_RIGHT
SFXLOCKF_2_RIGHT EQU AUDTERM_2_RIGHT
SFXLOCKF_1_RIGHT EQU AUDTERM_1_RIGHT
SFXLOCKB_CHANNEL4 EQU 3
SFXLOCKB_CHANNEL3 EQU 2
SFXLOCKB_CHANNEL2 EQU 1
SFXLOCKB_CHANNEL1 EQU 0
;***************************************************************************************************************************
;* work ram
;***************************************************************************************************************************
IF (SOUNDSYSTEM_WRAM_BANK == 0)
SECTION "SoundSystem Variables",WRAM0,ALIGN[7]
ELSE
SECTION "SoundSystem Variables",WRAMX,BANK[SOUNDSYSTEM_WRAM_BANK],ALIGN[7]
ENDC
wMusicSyncData:: DS 1 ; arbitrary value set by the song to sync visual effects with bg music
; soundfx variables
wSoundFXLock: DS 1 ; bitfield (see above), 1 = Music, 0 = SFX Locked
wSoundFXTable: DS 2 ; table of soundfx pointers
IF (SOUNDSYSTEM_ROM_BANKING != 0)
wSoundFXBank: DS sizeof_BANK_VAR ; bank of soundfxs
ENDC
wSoundFXStart: DS 4 ; sound fx to start
wSoundFXNote: DS 1 ; sound fx's start note
; music/sfx shared variables
wMusicSFXPanning: DS 1
wMusicSFXInstPause1: DS 1 ; frames left before instrument/soundfx update for channel 1
wMusicSFXInstPause2: DS 1 ; frames left before instrument/soundfx update for channel 2
wMusicSFXInstPause3: DS 1 ; frames left before instrument/soundfx update for channel 3
wMusicSFXInstPause4: DS 1 ; frames left before instrument/soundfx update for channel 4
wMusicSFXInstPtr1: DS 2 ; pointer to playing instrument/soundfx for channel 1
wMusicSFXInstPtr2: DS 2 ; pointer to playing instrument/soundfx for channel 2
wMusicSFXInstPtr3: DS 2 ; pointer to playing instrument/soundfx for channel 3
wMusicSFXInstPtr4: DS 2 ; pointer to playing instrument/soundfx for channel 4
IF (SOUNDSYSTEM_ROM_BANKING != 0)
wMusicSFXInstBank1: DS sizeof_BANK_VAR ; bank of active instrument for channel 1
wMusicSFXInstBank2: DS sizeof_BANK_VAR ; bank of active instrument for channel 2
wMusicSFXInstBank3: DS sizeof_BANK_VAR ; bank of active instrument for channel 3
wMusicSFXInstBank4: DS sizeof_BANK_VAR ; bank of active instrument for channel 4
ENDC
wMusicSFXInstChnl3WaveID: DS 1 ; current waveid loaded, IDs of 255 in instruments will load, whatever the value here
wMusicSFXInstChnl3Lock: DS 1 ; 0 = no lock, 1 = external lock
; music variables
wMusicPlayState:: DS 1 ; current music playback state, 0 = stopped, 1 = playing
wMusicNextFrame: DS 1 ; number of frames until the next music commands
wMusicCommandPtr: DS 2 ; position of playing music
IF (SOUNDSYSTEM_ROM_BANKING != 0)
wMusicCommandBank: DS sizeof_BANK_VAR ; bank of playing music
ENDC
wMusicOrderPtr: DS 2 ; position of pattern order list (list of pointers to start of patterns)
IF (SOUNDSYSTEM_ROM_BANKING != 0)
wMusicOrderBank: DS sizeof_BANK_VAR ; bank of order list
ENDC
wMusicInstrumentTable: DS 2 ; table of instrument pointers
IF (SOUNDSYSTEM_ROM_BANKING != 0)
wMusicInstrumentBank: DS sizeof_BANK_VAR ; bank of instruments
ENDC
; miscellaneous variables
wChannelMusicFreq1: DS 2 ; GB frequency of channel 1 for music backup
wChannelMusicFreq2: DS 2 ; GB frequency of channel 2 for music backup
wChannelMusicFreq3: DS 2 ; GB frequency of channel 3 for music backup
wChannelMusicFreq4: DS 2 ; GB frequency of channel 4 for music backup
wChannelMusicNote1: DS 1 ; note of channel 1 for music backup
wChannelMusicNote2: DS 1 ; note of channel 2 for music backup
wChannelMusicNote3: DS 1 ; note of channel 3 for music backup
wChannelMusicNote4: DS 1 ; note of channel 4 for music backup
wChannelFreq1: DS 2 ; GB frequency of channel 1
wChannelFreq2: DS 2 ; GB frequency of channel 2
wChannelFreq3: DS 2 ; GB frequency of channel 3
wChannelFreq4: DS 2 ; GB frequency of channel 4
wChannelVol1: DS 1 ; volumes of channel 1, byte[4:VOL,4:xxxx]
wChannelVol2: DS 1 ; volumes of channel 2, byte[4:VOL,4:xxxx]
wChannelVol3: DS 1 ; volumes of channel 3, byte[4:VOL,4:xxxx]
wChannelVol4: DS 1 ; volumes of channel 4, byte[4:VOL,4:xxxx]
wMusicSpeed: DS 1 ; speed
; effect variables
wChannelMusicEffect1: DS 1 ; active effect for channel 1, 0 = none
wChannelMusicEffect2: DS 1 ; active effect for channel 2, 0 = none
wChannelMusicEffect3: DS 1 ; active effect for channel 3, 0 = none
wChannelMusicEffect4: DS 1 ; active effect for channel 4, 0 = none
wChannelMusicFXParamA1: DS 2 ; effect parameters for channel 1
wChannelMusicFXParamA2: DS 2 ; effect parameters for channel 2
wChannelMusicFXParamA3: DS 2 ; effect parameters for channel 3
wChannelMusicFXParamA4: DS 2 ; effect parameters for channel 4
wChannelMusicFXParamB1: DS 2 ; effect parameters for channel 1
wChannelMusicFXParamB2: DS 2 ; effect parameters for channel 2
wChannelMusicFXParamB3: DS 2 ; effect parameters for channel 3
wChannelMusicFXParamB4: DS 2 ; effect parameters for channel 4
wTemp: DS 2 ; temporary storage for player calcs
IF (SOUNDSYSTEM_ENABLE_VUM)
wVUMeter1:: DS 1 ; vu meter data for channel 1
wVUMeter2:: DS 1 ; vu meter data for channel 2
wVUMeter3:: DS 1 ; vu meter data for channel 3
wVUMeter4:: DS 1 ; vu meter data for channel 4
ENDC
;***************************************************************************************************************************
;* Identification
;***************************************************************************************************************************
IF (SOUNDSYSTEM_CODE_BANK == 0)
SECTION "SoundSystem_Identity",ROM0
ELSE
SECTION "SoundSystem_Identity",ROMX,BANK[SOUNDSYSTEM_CODE_BANK]
ENDC
SoundSystem_Version::
DB "SoundSystem v20.249",$00
SoundSystem_Author::
DB "Code: S. Hockenhull",$00
;***************************************************************************************************************************
;* SoundSystem_Init
;***************************************************************************************************************************
IF (SOUNDSYSTEM_CODE_BANK == 0)
SECTION "SoundSystem_Init",ROM0
ELSE
SECTION "SoundSystem_Init",ROMX,BANK[SOUNDSYSTEM_CODE_BANK]
ENDC
SoundSystem_Init::
IF (SOUNDSYSTEM_WRAM_BANK != 0)
ld a,SOUNDSYSTEM_WRAM_BANK
ldh [rSVBK],a
ENDC
; set all channel samples to 'stop'
ld hl,wMusicSFXInstPtr1
ld e,4
.instptrloop:
ld a,LOW(Music_InstrumentEnd)
ld [hl+],a
ld a,HIGH(Music_InstrumentEnd)
ld [hl+],a
dec e
jr nz,.instptrloop
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; set all channel banks to be the bank with the stop instrument
ld hl,wMusicSFXInstBank1
ld e,4
IF (SOUNDSYSTEM_LARGE_ROM)
.instbankloop:
ld a,LOW(BANK(Music_InstrumentEnd))
ld [hl+],a
ld a,HIGH(BANK(Music_InstrumentEnd))
ld [hl+],a
dec e
jr nz,.instbankloop
ELSE
ld a,BANK(Music_InstrumentEnd)
.instbankloop:
ld [hl+],a
dec e
jr nz,.instbankloop
ENDC
ENDC
; set all channel volumes to 8
ld a,$80
ld hl,wChannelVol1
REPT 4
ld [hl+],a
ENDR
; set all channel sfxs to unused (etc.)
ld a,$FF
ld hl,wSoundFXStart
REPT 4
ld [hl+],a
ENDR
ld [wSoundFXLock],a
ld [wMusicSFXPanning],a
ld [wMusicSFXInstChnl3WaveID],a
; clear all channel music effects
xor a
ld hl,wChannelMusicEffect1
REPT 4
ld [hl+],a
ENDR
ld [wMusicSFXInstChnl3Lock],a
; clear all sfx pause timers
ld hl,wMusicSFXInstPause1
REPT 4
ld [hl+],a
ENDR
; clear all channel music frequencies
ld hl,wChannelMusicFreq1
REPT 8
ld [hl+],a
ENDR
IF (SOUNDSYSTEM_ENABLE_VUM)
; clear all vu meter values
ld hl,wVUMeter1
REPT 4
ld [hl+],a
ENDR
ENDC
; enable sound
ld a,AUD3ENA_ON
ldh [rAUDENA],a
; channel 1
xor a
ldh [rAUD1SWEEP],a
; all channels off
call Music_Pause
; general
ld a,(AUDVOL_VIN_LEFT|AUDVOL_VIN_RIGHT) ^ $FF ; same as ~(), but ~ here triggers a false warning
ldh [rAUDVOL],a
ld a,AUDTERM_ALL
ldh [rAUDTERM],a
ret
; dummy instrument to init/clear instrument pointers
Music_InstrumentEnd:
DB MUSIC_INSTCMD_X_END
;***************************************************************************************************************************
;* SoundSystem_Process
;***************************************************************************************************************************
IF (SOUNDSYSTEM_CODE_BANK == 0)
SECTION "SoundSystem_Process",ROM0
ELSE
SECTION "SoundSystem_Process",ROMX,BANK[SOUNDSYSTEM_CODE_BANK]
ENDC
SoundSystem_Process::
IF (SOUNDSYSTEM_WRAM_BANK != 0)
ld a,SOUNDSYSTEM_WRAM_BANK
ldh [rSVBK],a
ENDC
IF (SOUNDSYSTEM_ENABLE_SFX)
; sfx start process
ld hl,wSoundFXStart
ld c,4
.multisfx:
ld a,[hl]
push hl
push bc
xor $FF
jp z,.nonewsfx
ld b,a ; save
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; change the rom bank
ld a,[wSoundFXBank]
ld [rROMB0],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wSoundFXBank+1]
ld [rROMB1],a
ENDC
ENDC
; lock & update SFX
ld a,b ; restore
cpl
; calculate table plus index address
ld b,a ;save
ld a,[wSoundFXTable]
ld e,a
ld a,[wSoundFXTable+1]
ld d,a
ld a,b ;restore
ld b,0
add a
rl b
add a
rl b
add a ; 4 words
rl b
add e
ld l,a
ld a,0 ; can't xor a here becuase of the adc
adc d
add b
ld h,a
push hl
ld a,[wSoundFXNote]
add a
ld l,a
ld h,HIGH(FrequencyTable)
ASSERT LOW(FrequencyTable) == 0
ld a,[hl+]
ld [wTemp],a
ld a,[hl]
ld [wTemp+1],a ; store note freq
pop hl
; update wSoundFXLock
ld a,[wSoundFXLock]
ld d,a
; load channel 1
ld a,[hl+]
ld c,a
ld a,[hl+]
ld b,a
or c
jr z,.nosfxchnl1
ld a,c
ld [wMusicSFXInstPtr1],a
ld a,b
ld [wMusicSFXInstPtr1+1],a
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; update the rom bank
ld a,[wSoundFXBank]
ld [wMusicSFXInstBank1],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wSoundFXBank+1]
ld [wMusicSFXInstBank1+1],a
ENDC
ENDC
ld a,[wTemp]
ld [wChannelFreq1],a
ld a,[wTemp+1]
ld [wChannelFreq1+1],a
ld a,d
and ~(SFXLOCKF_1_LEFT|SFXLOCKF_1_RIGHT)
ld d,a
ld a,1 ; set counter to immediate start
ld [wMusicSFXInstPause1],a
.nosfxchnl1:
; load channel 2
ld a,[hl+]
ld c,a
ld a,[hl+]
ld b,a
or c
jr z,.nosfxchnl2
ld a,c
ld [wMusicSFXInstPtr2],a
ld a,b
ld [wMusicSFXInstPtr2+1],a
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; update the rom bank
ld a,[wSoundFXBank]
ld [wMusicSFXInstBank2],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wSoundFXBank+1]
ld [wMusicSFXInstBank2+1],a
ENDC
ENDC
ld a,[wTemp]
ld [wChannelFreq2],a
ld a,[wTemp+1]
ld [wChannelFreq2+1],a
ld a,d
and ~(SFXLOCKF_2_LEFT|SFXLOCKF_2_RIGHT)
ld d,a
ld a,1 ; set counter to immediate start
ld [wMusicSFXInstPause2],a
.nosfxchnl2:
; load channel 3
ld a,[hl+]
ld c,a
ld a,[hl+]
ld b,a
or c
jr z,.nosfxchnl3
ld a,[wMusicSFXInstChnl3Lock]
or a
jr nz,.nosfxchnl3
ld a,c
ld [wMusicSFXInstPtr3],a
ld a,b
ld [wMusicSFXInstPtr3+1],a
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; update the rom bank
ld a,[wSoundFXBank]
ld [wMusicSFXInstBank3],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wSoundFXBank+1]
ld [wMusicSFXInstBank3+1],a
ENDC
ENDC
ld a,[wTemp]
ld [wChannelFreq3],a
ld a,[wTemp+1]
ld [wChannelFreq3+1],a
ld a,d
and ~(SFXLOCKF_3_LEFT|SFXLOCKF_3_RIGHT)
ld d,a
ld a,1 ; set counter to immediate start
ld [wMusicSFXInstPause3],a
.nosfxchnl3:
; load channel 4
ld a,[hl+]
ld c,a
ld a,[hl+]
ld b,a
or c
jr z,.nosfxchnl4
ld a,c
ld [wMusicSFXInstPtr4],a
ld a,b
ld [wMusicSFXInstPtr4+1],a
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; update the rom bank
ld a,[wSoundFXBank]
ld [wMusicSFXInstBank4],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wSoundFXBank+1]
ld [wMusicSFXInstBank4+1],a
ENDC
ENDC
ld a,d
and (SFXLOCKF_4_LEFT|SFXLOCKF_4_RIGHT) ^ $FF ; same as ~(), but ~ here triggers a false warning
ld d,a
ld a,1 ; set counter to immediate start
ld [wMusicSFXInstPause4],a
.nosfxchnl4:
pop bc
pop hl
; update wSoundFXLock
ld a,d
ld [wSoundFXLock],a
; de-flag sfx start
ld a,$FF
ld [hl+],a
dec c
jp nz,.multisfx
jr .newsfxdone
.nonewsfx:
add sp,4
.newsfxdone:
ENDC
;-------------------------------
; instruments and SFX process
;-------------------------------
; channel 1
ld hl,wMusicSFXInstPause1
dec [hl]
jr nz,SSFP_Inst1UpdateDone
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; change the rom bank
ld a,[wMusicSFXInstBank1]
ld [rROMB0],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wMusicSFXInstBank1+1]
ld [rROMB1],a
ENDC
ENDC
ld hl,wMusicSFXInstPtr1
ld a,[hl+]
ld d,[hl]
ld e,a
jp SSFP_Inst1Update
SSFP_Inst1UpdateFrameEnd:
; save back
ld hl,wMusicSFXInstPtr1
ld a,e
ld [hl+],a
ld [hl],d
SSFP_Inst1UpdateDone:
;-------------------------------
; channel 2
ld hl,wMusicSFXInstPause2
dec [hl]
jr nz,SSFP_Inst2UpdateDone
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; change the rom bank
ld a,[wMusicSFXInstBank2]
ld [rROMB0],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wMusicSFXInstBank2+1]
ld [rROMB1],a
ENDC
ENDC
ld hl,wMusicSFXInstPtr2
ld a,[hl+]
ld d,[hl]
ld e,a
jp SSFP_Inst2Update
SSFP_Inst2UpdateFrameEnd:
; save back
ld hl,wMusicSFXInstPtr2
ld a,e
ld [hl+],a
ld [hl],d
SSFP_Inst2UpdateDone:
;-------------------------------
; channel 3
ld hl,wMusicSFXInstPause3
dec [hl]
jr nz,SSFP_Inst3UpdateDone
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; change the rom bank
ld a,[wMusicSFXInstBank3]
ld [rROMB0],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wMusicSFXInstBank3+1]
ld [rROMB1],a
ENDC
ENDC
ld hl,wMusicSFXInstPtr3
ld a,[hl+]
ld d,[hl]
ld e,a
jp SSFP_Inst3Update
SSFP_Inst3UpdateFrameEnd:
; save back
ld hl,wMusicSFXInstPtr3
ld a,e
ld [hl+],a
ld [hl],d
SSFP_Inst3UpdateDone:
;-------------------------------
; channel 4
ld hl,wMusicSFXInstPause4
dec [hl]
jr nz,SSFP_Inst4UpdateDone
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; change the rom bank
ld a,[wMusicSFXInstBank4]
ld [rROMB0],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wMusicSFXInstBank4+1]
ld [rROMB1],a
ENDC
ENDC
ld hl,wMusicSFXInstPtr4
ld a,[hl+]
ld d,[hl]
ld e,a
jp SSFP_Inst4Update
SSFP_Inst4UpdateFrameEnd:
; save back
ld hl,wMusicSFXInstPtr4
ld a,e
ld [hl+],a
ld [hl],d
SSFP_Inst4UpdateDone:
;-------------------------------
; process music
ld a,[wMusicPlayState]
or a ; is music playing?
ret z ; no, exit early (nothing to do)
;-------------------------------
; update music effects
;-------------------------------
; channel 1
ld a,[wChannelMusicEffect1]
or a ; is channel 1 playing music fx?
jr z,SSFP_MusicFX_Done1 ; no, skip to the next channel
; check if sound effect active (no music fx then)
ld b,a
ld a,[wSoundFXLock]
bit SFXLOCKB_CHANNEL1,a ; is channel 1 playing fx?
jr z,SSFP_MusicFX_Done1 ; no, skip to the next channel
; call the fx handler
ld a,b
ld hl,SSFP_MusicFX_JumpTable1
add a
add l
ld l,a
ld a,[hl+]
ld h,[hl]
ld l,a
jp hl
SSFP_MusicFX_Done1: ; some handlers return here
;-------------------------------
; channel 2
ld a,[wChannelMusicEffect2]
or a ; is channel 2 playing music fx?
jr z,SSFP_MusicFX_Done2 ; no, skip to the next channel
; check if sound effect active (no music fx then)
ld b,a
ld a,[wSoundFXLock]
bit SFXLOCKB_CHANNEL2,a ; is channel 2 playing fx?
jr z,SSFP_MusicFX_Done2 ; no, skip to the next channel
; call the fx handler
ld a,b
ld hl,SSFP_MusicFX_JumpTable2
add a
add l
ld l,a
ld a,[hl+]
ld h,[hl]
ld l,a
jp hl
SSFP_MusicFX_Done2: ; some handlers return here
;-------------------------------
; channel 3
ld a,[wChannelMusicEffect3]
or a ; is channel 3 playing music fx?
jr z,SSFP_MusicFX_Done3 ; no, skip to the next channel
; check if sound effect active (no music fx then)
ld b,a
ld a,[wSoundFXLock]
bit SFXLOCKB_CHANNEL3,a ; is channel 3 playing fx?
jr z,SSFP_MusicFX_Done3 ; no, skip to the next channel
; call the fx handler
ld a,b
ld hl,SSFP_MusicFX_JumpTable3
add a
add l
ld l,a
ld a,[hl+]
ld h,[hl]
ld l,a
jp hl
SSFP_MusicFX_Done3: ; some handlers return here
;-------------------------------
; update music
; determine if music should update this frame
ld a,[wMusicNextFrame]
dec a
ld [wMusicNextFrame],a
ret nz ; no update needed
IF (SOUNDSYSTEM_ROM_BANKING != 0)
; change the rom bank
ld a,[wMusicCommandBank]
ld [rROMB0],a
IF (SOUNDSYSTEM_LARGE_ROM != 0)
ld a,[wMusicCommandBank+1]
ld [rROMB1],a
ENDC
ENDC
; put the music command handler in de
ld hl,wMusicCommandPtr
ld a,[hl+]
ld e,a
ld d,[hl]
;-------------------------------
SSFP_MusicUpdate: ; some handlers return here
ld a,[de]
inc de
ld hl,SSFP_Music_JumpTable
add a
add l
ld l,a
ld a,[hl+]
ld h,[hl]
ld l,a
jp hl
;-------------------------------
SSFP_MusicUpdateFrameEnd: ; some handlers return here
; update the ptr for next time
ld hl,wMusicCommandPtr
ld a,e
ld [hl+],a
ld [hl],d
ret
;***************************************************************************************************************************
;* Music_PrepareInst
;***************************************************************************************************************************
IF (SOUNDSYSTEM_CODE_BANK == 0)
SECTION "SoundSystem_Music_PrepareInst",ROM0
ELSE