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
|
;****************************************************************************
; NoteOnFM* [events $00~$07]
; Does a "note on" for a FM channel
;----------------------------------------------------------------------------
; input a .... FM channel (bottom 3 bits)
; input c .... current bank
; input hl ... current address
;----------------------------------------------------------------------------
; breaks: af, b
;****************************************************************************
NoteOnFMSFX:
call NoteOnFM ; We're just a wrapper
jp ProcessSFXRun ; End of subroutine
NoteOnFMBGM:
ld b, a
PollPCM
push hl
ld a, b
and $07 ; Check if channel is free
ld hl, RAM_Locked
add l
ld l, a
ld a, (hl)
pop hl
or a
jp nz, ProcessBGMSkip1 ; Don't play if locked
ld a, b
call NoteOnFM ; We're just a wrapper
jp ProcessBGMRun ; End of subroutine
NoteOnFM:
and $07 ; Get channel ID
ld (ix+0), $28 ; Note off
ld (ix+1), a
ld b, a
ex af, af'
PollPCM
ld a, b ; Determine which port to write
and $04
rrca
ld iyl, a
PollPCM
call GetParam ; Get note
PollPCM
ex af, af'
push de
push hl
ld d, a
and $03 ; Index of first frequency register
add $A4
ld e, a
PollPCM
ld h, FMFreqTable>>8 ; Get address of note
ld a, b
and $1F
add FMFreqTable&$FF
ld l, a
PollPCM
ld a, b ; Set new frequency
and $E0
rrca
rrca
ld b, a
ld a, (hl)
or b
ld (iy+0), e
ld (iy+1), a
PollPCM
ld a, e
sub 4
ld e, a
dec l
ld a, (hl)
ld (iy+0), e
ld (iy+1), a
PollPCM
ld a, d ; Note on!
or $F0
ld (ix+0), $28
ld (ix+1), a
pop hl
pop de
ret ; End of subroutine
;****************************************************************************
; NoteOffFM [events $10~$17]
; Does a "note off" for a FM channel
;----------------------------------------------------------------------------
; input a .... FM channel (bottom 3 bits)
; input c .... current bank
; input hl ... current address
;----------------------------------------------------------------------------
; breaks: af, b
;****************************************************************************
NoteOffFMSFX:
call NoteOffFM ; We're just a wrapper
jp ProcessSFXRun ; End of subroutine
NoteOffFMBGM:
ld b, a
PollPCM
ld a, b
push hl
and $07 ; Check if channel is free
ld hl, RAM_Locked
add l
ld l, a
ld a, (hl)
pop hl
or a
jp nz, ProcessBGMSkip ; Don't stop if locked
ld a, b
call NoteOffFM ; We're just a wrapper
jp ProcessBGMRun ; End of subroutine
NoteOffFM:
and $07 ; Get channel ID
ld (ix+0), $28 ; Note off
ld (ix+1), a
ret ; End of subroutine
;****************************************************************************
; SetFMNote* [events $30~$37]
; Sets the note of a FM channel without "note on"
;----------------------------------------------------------------------------
; input a .... FM channel (bottom 3 bits)
; input c .... current bank
; input hl ... current address
;----------------------------------------------------------------------------
; breaks: af, b
;****************************************************************************
SetNoteFMSFX:
call SetNoteFM ; We're just a wrapper
jp ProcessSFXRun ; End of subroutine
SetNoteFMBGM:
ld b, a
PollPCM
ld a, b
push hl
and $07 ; Check if channel is free
ld hl, RAM_Locked
add l
ld l, a
ld a, (hl)
pop hl
or a
jp nz, ProcessBGMSkip2 ; Don't change if locked
ld a, b
call SetNoteFM ; We're just a wrapper
jp ProcessBGMRun ; End of subroutine
SetNoteFM:
push af
ld b, a
PollPCM
ld a, b ; Determine which port to write
and $04
rrca
ld iyl, a
PollPCM
call GetParam ; Get high byte
PollPCM
pop af
push de
and $07
ld d, a
and $03 ; Index of first frequency register
add $A4
ld e, a
PollPCM
ld (iy+0), e ; Load high byte
ld (iy+1), b
PollPCM ; Load low byte
call GetParam
PollPCM
ld a, e
sub 4
ld e, a
ld (iy+0), e
ld (iy+1), b
PollPCM
pop de
ret ; End of subroutine
;****************************************************************************
; LoadFM* [events $40~$47]
; Loads a FM instrument
;----------------------------------------------------------------------------
; input a .... FM channel (bottom 3 bits)
; input c .... current bank
; input hl ... current address
;----------------------------------------------------------------------------
; breaks: af, b
;****************************************************************************
LoadFMSFX:
call LoadFMEvent ; We're just a wrapper
jp ProcessSFXRun ; End of subroutine
LoadFMBGM:
and $07 ; Get channel ID
ld b, a
PollPCM
push de
push bc
ld a, b
ld de, RAM_BGMFMInstr ; Store instrument ID
add e
ld e, a
PollPCM
call GetParam
PollPCM
ld a, b
ld (de), a
ld a, e ; Reset volume
add 8
ld e, a
xor a
ld (de), a
ld e, c
pop bc
ld c, e
pop de
PollPCM
push hl ; Check if channel is free
ld a, b
ld h, RAM_Locked>>8
add RAM_Locked&$FF
ld l, a
ld a, (hl)
pop hl
or a
jp nz, ProcessBGMSkip ; Don't load if locked
PollPCM
ld a, b ; Get back instrument ID
push hl
ld hl, RAM_BGMFMInstr
add l
ld l, a
ld b, (hl)
pop hl
ex af, af'
PollPCM
ex af, af'
call LoadFMDirect ; We're just a wrapper
jp ProcessBGMRun ; End of subroutine
LoadFMEvent:
and $07 ; Get channel ID
ex af, af'
PollPCM
call GetParam ; Get instrument ID
PollPCM
ex af, af'
LoadFMDirect:
push af
and $04 ; Determine which port to write
rrca
ld iyl, a
PollPCM
pop af
push bc
push de
push hl
ld h, RAM_PointerList>>8 ; Get instrument address
ld l, b
ld d, (hl)
inc h
ld e, (hl)
inc h
ld c, (hl)
ex de, hl
push af
PollPCM
ld b, 28/4 ; Load FM instrument into our scratch buffer,
ld de, RAM_Scratch ; so we don't conflict with PCM bank
.getinstr: ; switch (which would slow down A LOT)
ld a, b
ex af, af'
call GetParam
ex de, hl
ld (hl), b
ex de, hl
inc e
call GetParam
ex de, hl
ld (hl), b
ex de, hl
inc e
call GetParam
ex de, hl
ld (hl), b
ex de, hl
inc e
call GetParam
ex de, hl
ld (hl), b
ex de, hl
inc e
ex af, af'
ld b, a
djnz .getinstr
call GetParam
ex de, hl
ld (hl), b
ex de, hl
PollPCM
pop af ; Kill ADSR
ld b, a
call KillFM
ld a, b
ld de, RAM_FMData ; Get address of FM data
and $07
add e
ld e, a
push af
and $03 ; Register index for operator
add $B0
ld hl, RAM_Scratch ; Get address of buffer with the instrument
; data
ex af, af'
PollPCM ; Write operator
ex af, af'
ld (iy+0), a
ld b, (hl)
inc l
ld (iy+1), b
ex af, af' ; Store $B0 register in FM data buffer
ld a, b
ld (de), a
ld a, e
add 8
ld e, a
PollPCM
ex af, af'
sub $B0-$30
ld b, 7*4 ; Load operator registers into the YM2612
.loadfmloop:
ld (iy+0), a
ld c, (hl)
ld (iy+1), c
add 4
inc l
djnz .loadfmloop
PollPCM
ld a, l ; Store operator TLs (needed to set the
sub 24 ; volume properly when it changes)
ld l, a
ld b, 4
.loadfmsavetl:
ld a, (hl)
ld (de), a
ld a, e
add 8
ld e, a
inc l
djnz .loadfmsavetl
PollPCM
pop af
pop hl
pop de
pop bc
ld b, 0
jp SetFMVolLoad
;****************************************************************************
; SetFMVol*
; Sets the volume of a FM channel
;****************************************************************************
SetFMVolSFX:
call SetFMVolSFXEvent ; We're just a wrapper
jp ProcessSFXRun ; End of subroutine
SetFMVolBGM:
and $07
ld b, a
PollPCM
push de
push bc
ld a, b
ld d, RAM_BGMFMVol>>8 ; Store BGM volume
add RAM_BGMFMVol&$FF
ld e, a
PollPCM
call GetParam
PollPCM
ex de, hl
ld (hl), b
ex de, hl
ld e, c
pop bc
ld c, e
pop de
PollPCM
push hl
ld a, b ; Check if channel is free
ld hl, RAM_Locked
add l
ld l, a
ld a, (hl)
pop hl
or a
jp nz, ProcessBGMSkip ; Don't change if locked
PollPCM
ld a, b
push hl
ld hl, RAM_BGMFMVol
add l
ld l, a
ld b, (hl)
pop hl
push af
PollPCM
pop af
call SetFMVolLoad ; We're just a wrapper
jp ProcessBGMRun ; End of subroutine
SetFMVolSFXEvent:
push af
PollPCM
call GetParam ; Get new volume
PollPCM
pop af
SetFMVolLoad:
push bc
push de
push hl
and $07 ; Get channel ID
push af ; Store new FM volume
ld h, RAM_FMVol>>8
add RAM_FMVol&$FF
ld l, a
ld (hl), b
pop af
push af
ld h, RAM_FMData>>8 ; Get address of FM data
add RAM_FMData&$FF
ld l, a
ex af, af'
PollPCM
ex af, af'
and $07 ; Get global volume
or $E0
ld d, $1F
ld e, a
ex af, af'
ld a, (de)
ld d, a
PollPCM
ex af, af'
and $04 ; Determine which port to write
rrca
ld iyl, a
PollPCM
pop af
and $03 ; Index of first volume register
add $40
ld c, a
ld a, (hl) ; Get algorithm
and $07
ld e, a
PollPCM
ld a, l
add 8
ld l, a
ld a, e ; Process operator #1
cp $07
jr c, .noop1
ld (iy+0), c
ld a, (hl)
add d
jp m, .tooquiet1
add b
jp p, .notooquiet1
.tooquiet1:
ld a, $7F
.notooquiet1:
ld (iy+1), a
.noop1:
ld a, c
add 4
ld c, a
PollPCM
ld a, l
add 8
ld l, a
ld a, e ; Process operator #2
cp $05
jr c, .noop2
ld (iy+0), c
ld a, (hl)
add d
jp m, .tooquiet2
add b
jp p, .notooquiet2
.tooquiet2:
ld a, $7F
.notooquiet2:
ld (iy+1), a
.noop2:
ld a, c
add 4
ld c, a
PollPCM
ld a, l
add 8
ld l, a
ld a, e ; Process operator #3
cp $04
jr c, .noop3
ld (iy+0), c
ld a, (hl)
add d
jp m, .tooquiet3
add b
jp p, .notooquiet3
.tooquiet3:
ld a, $7F
.notooquiet3:
ld (iy+1), a
.noop3:
ld a, c
add 4
ld c, a
PollPCM
ld a, l
add 8
ld l, a
ld (iy+0), c
ld a, (hl) ; Process operator #4
add d
jp m, .tooquiet4
add b
jp p, .notooquiet4
.tooquiet4:
ld a, $7F
.notooquiet4:
ld (iy+1), a
PollPCM
pop hl
pop de
pop bc
ret ; End of subroutine
;****************************************************************************
; SetFMParam* [events $F0-$F7]
; Sets the different parameters of a FM channel
;----------------------------------------------------------------------------
; input c .... current bank
; input hl ... current address
;----------------------------------------------------------------------------
; output b .... value
; output c .... new bank
; output hl ... new address
;----------------------------------------------------------------------------
; breaks: af, b
;****************************************************************************
SetFMParamSFX:
ld b, a
PollPCM
ld a, b ; Determine which port to write
and $04
rrca
ld iyl, a
ld a, b
ex af, af'
PollPCM
call GetParam ; Get parameters
PollPCM
ex af, af'
and $03 ; Get channel ID
add $B4
ld (iy+0), a ; Set new parameters
ld (iy+1), b
jp ProcessSFXRun ; End of subroutine
SetFMParamBGM:
ld b, a
PollPCM
ld a, b ; Determine which port to write
and $04
rrca
ld iyl, a
ld a, b
ex af, af'
PollPCM
call GetParam ; Get parameters
PollPCM
ex af, af'
push hl
and $07 ; Store parameters
ld h, RAM_BGMFMPan>>8
add RAM_BGMFMPan&$FF
ld l, a
ld (hl), b
ex af, af'
PollPCM
ex af, af'
and $07 ; Check if channel is free
add RAM_Locked&$FF
ld l, a
ex af, af'
ld a, (hl)
or a
pop hl
jp nz, ProcessBGMRun ; Don't modify if locked
PollPCM
ex af, af'
and $03 ; Get channel ID
add $B4
ld (iy+0), a ; Set new parameters
ld (iy+1), b
jp ProcessBGMRun ; End of subroutine
;****************************************************************************
; SetFMReg* [events $F8-$F9]
; Changes a FM register
;----------------------------------------------------------------------------
; input a .... YM2612 register bank (0 or 1, in bit 0)
; input c .... current bank
; input hl ... current address
;----------------------------------------------------------------------------
; output b .... value
; output c .... new bank
; output hl ... new address
;----------------------------------------------------------------------------
; breaks: af, b
;****************************************************************************
SetFMRegSFX:
call SetFMReg ; We're just a wrapper
jp ProcessSFXRun
SetFMRegBGM:
call SetFMReg ; We're just a wrapper
jp ProcessBGMRun
;----------------------------------------------------------------------------
SetFMReg:
and $01 ; Get port address
add a
ld iyl, a
PollPCM ; Get parameters
call GetParam
push bc
PollPCM
call GetParam
PollPCM
ld a, b
pop bc
ld (iy+0), b ; Write register
ld (iy+1), a
ret ; End of subroutine
;****************************************************************************
; LockChannelFM [events $E0-$E7]
; Locks a FM channel
;----------------------------------------------------------------------------
; input a ... FM channel (0..7, in bottom 3 bits)
; input c .... current bank
; input hl ... current address
;----------------------------------------------------------------------------
; output b .... value
; output c .... new bank
; output hl ... new address
;----------------------------------------------------------------------------
; breaks: af, iy
;****************************************************************************
LockChannelFM:
and $07
ld b, a
PollPCM
push hl
ld h, RAM_Locked>>8 ; Get address of channel to lock
ld a, b
add RAM_Locked&$FF
ld l, a
ld (hl), $01 ; Lock channel
pop hl
PollPCM
ld a, b ; Determine which port to write
and $04
rrca
ld iyl, a
ld a, b ; Reset FM parameters
and $03
add a
add a
add $B4
ld (iy+0), a
ld (iy+1), $C0
jp ProcessSFXRun ; End of subroutine
;****************************************************************************
; KillFM
; Kills a FM channel
;----------------------------------------------------------------------------
; input a ... FM channel (0..7, in bottom 3 bits)
;----------------------------------------------------------------------------
; breaks: af, c, iy
;****************************************************************************
KillFM:
and $07 ; Get channel ID
push af
push de
push hl
ld c, a ; Determine to which port to write
and $04
rrca
ld iyl, a
ld a, c
and $03 ; Load dummy FM instrument
add $40
ld c, 6
ld hl, DummyFMInstr
.loaddummy:
ex af, af'
PollPCM
ex af, af'
ld e, (hl)
ld (iy+0), a
ld (iy+1), e
add 4
ex af, af'
PollPCM
ex af, af'
ld (iy+0), a
ld (iy+1), e
add 4
ex af, af'
PollPCM
ex af, af'
ld (iy+0), a
ld (iy+1), e
add 4
ex af, af'
PollPCM
ex af, af'
ld (iy+0), a
ld (iy+1), e
add 4
inc l
dec c
jp nz, .loaddummy
pop hl
pop de
PollPCM
pop af
ld c, a ; Cause the ADSR to reset
or $F0
ld (ix+0), $28
ld (ix+1), c
ld (ix+0), $28
ld (ix+1), a
ld (ix+0), $28
ld (ix+1), c
PollPCM
ret ; End of subroutine
;****************************************************************************
; RestoreFM
; Restores a FM channel (from BGM information)
;----------------------------------------------------------------------------
; input b ... FM channel (0..7)
;----------------------------------------------------------------------------
; breaks: af, hl
;****************************************************************************
RestoreFM:
ld a, b ; Restore BGM FM instrument
ld h, RAM_BGMFMInstr>>8
add RAM_BGMFMInstr&$FF
ld l, a
push bc
ld a, b
ld b, (hl)
call LoadFMDirect
pop bc
PollPCM
push bc
ld a, l ; Restore BGM FM volume
add 8
ld l, a
ld a, b
ld b, (hl)
call SetFMVolLoad
pop bc
ld a, l ; Restore BGM FM panning
add 8
ld l, a
ld a, b
and $03
add $B4
ld (iy+0), a
ld a, (hl)
ld (iy+1), a
PollPCM
ret ; End of subroutine
|