aboutsummaryrefslogtreecommitdiff
path: root/src-z80/player/psg.z80
blob: 892931a6d0fc3c189a697b7ce8e84c62c20bb8ad (plain)
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
;****************************************************************************
; UpdatePSG
; Updates PSG output
;****************************************************************************

UpdatePSG:
    ld hl, RAM_PSGData+48       ; PSG envelope data of *last* channel
    ld b, 3                     ; Go through all channels
.loop:
    push bc
    xor a
    ld iyl, a

    ld a, (hl)                  ; Get channel volume
    or a
    jp m, .noskip
    ld b, $0F
    inc l
    jp .skip
.noskip:
    and $7F
    ld b, a

    inc l                       ; Add global volume
    ld a, (hl)
    add b
    ld b, a

    PollPCM
    push bc

    inc l                       ; Get current address of envelope
    ld c, (hl)
    inc l
    ld e, (hl)
    inc l
    ld d, (hl)
    ex de, hl

.readenv:
    PollPCM
    call GetParam               ; Get next byte
    PollPCM

    ld a, b
    cp $FE                      ; Set loop point?
    jp z, .envsetloop
    cp $FF                      ; Loop envelope?
    jp z, .envloop

    ld iyl, b                   ; Keep byte safe somewhere...
    PollPCM

    ex de, hl                   ; Store new address
    ld (hl), d
    dec l
    ld (hl), e
    dec l
    ld (hl), c
    dec l

    pop bc
    PollPCM

    db $FD,$7D ; ld a, iyl      ; Mix envelope with volume
    and $0F
    add b
    ld b, a

    cp $10                      ; Check for overflow
    jr c, .notmute
    ld b, $0F
.notmute:

.skip:
    PollPCM
    
    ld a, b                     ; Set PSG channel volume
    rlca   
    rlca
    rlca
    pop bc
    or b
    rrca
    rrca
    rrca
    or $90
    ld ($7F11), a
    
    PollPCM
    push bc
    
    ld a, l                     ; Get current semitone
    add 11-1
    ld l, a
    ld b, (hl)
    sub 11-1
    ld l, a
    
    ld a, b                     ; Oh, don't tocuh it?
    inc a
    jr z, .notone
    
    PollPCM
    
    db $FD,$7D ; ld a, iyl      ; Get semitone shift
    and $F0
    rrca
    rrca
    rrca
    rrca
    ex de, hl
    ld h, PSGShiftTable>>8
    add PSGShiftTable&$FF
    ld l, a
    ld c, (hl)
    ex de, hl
    
    PollPCM
    
    ld a, b                     ; Compute resulting semitone
    add c
    
    ex de, hl                   ; Get corresponding frequency
    ld h, PSGFreqTable>>8
    ld l, a
    ld c, (hl)
    inc l
    ld b, (hl)
    
    PollPCM
    
    ld a, e                     ; Get PSG channel
    and $30
    add a
    or c
    
    ld hl, $7F11                ; Set new frequency
    ld (hl), a
    ld (hl), b
    ex de, hl
    
    PollPCM
.notone:
    pop bc

    ld a, l                     ; Go for next channel
    sub 16+1
    ld l, a
    PollPCM
    dec b
    jp p, .loop

    jp DoTick_PSGSkip           ; End of subroutine

.envsetloop:
    PollPCM
    inc e                       ; Where we store the loop point

    ex de, hl                   ; Store loop point
    ld (hl), c
    inc l
    ld (hl), e
    inc l
    ld (hl), d
    ex de, hl

    dec e                       ; Back to where we were...
    dec e
    dec e
    jp .readenv                 ; Go for next byte

.envloop:
    PollPCM
    inc e                       ; Where we store the loop point

    ex de, hl                   ; Retrieve loop point
    ld c, (hl)
    inc l
    ld e, (hl)
    inc l
    ld d, (hl)
    ex de, hl

    dec e                       ; Back to where we were...
    dec e
    dec e
    jp .readenv                 ; Go for next byte

;****************************************************************************
; NoteOnPSG*
; Does a "note on" for a PSG channel
;****************************************************************************

NoteOnPSGSFX:
    call NoteOnPSG              ; We're just a wrapper
    jp ProcessSFXRun            ; End of subroutine

NoteOnPSGBGM:
    ld b, a
    PollPCM
    ld a, b

    push hl
    and $03                     ; Check if channel is free
    ld hl, RAM_Locked+8
    add l
    ld l, a
    ld a, (hl)
    pop hl
    or a
    jp nz, ProcessBGMSkip1      ; Don't play if locked

    ld a, b
    call NoteOnPSG              ; We're just a wrapper
    jp ProcessBGMRun            ; End of subroutine

NoteOnPSG:
    and $03
    ld b, a
    ;push af
    ex af, af'
    PollPCM

    push hl                     ; Set channel volume
    ld h, RAM_PSGData>>8
    ld a, b
    rrca
    rrca
    rrca
    rrca
    ld l, a
    ld a, (hl)
    or $80
    ld (hl), a

    PollPCM
    push de

    inc l                       ; Now we'll reset the envelope address...
    inc l
    ld d, h
    ld a, l
    add 6
    ld e, a

    PollPCM

    ld a, (de)                  ; Reset envelope address
    ld (hl), a
    inc l
    inc e
    ld a, (de)
    ld (hl), a
    inc l
    inc e
    ld a, (de)
    ld (hl), a

    ld a, e
    pop de
    pop hl

    ex af, af'
    PollPCM
    call GetParam               ; Get note
    PollPCM
    ex af, af'

SetSemitonePSG:
    push hl                     ; Store new semitone
    inc a
    ld h, RAM_PSGData>>8
    ld l, a
    ld (hl), b
    pop hl

    ret                         ; End of subroutine

;****************************************************************************
; NoteOnNoise*
; Does a "note on" for the noise PSG channel
;****************************************************************************

NoteOnNoiseSFX:
    call NoteOnNoise            ; We're just a wrapper
    jp ProcessSFXRun            ; End of subroutine

NoteOnNoiseBGM:
    ld a, (RAM_Locked+11)       ; Check if channel is free
    or a
    jp nz, ProcessBGMSkip1      ; Don't play if locked

    call NoteOnNoise            ; We're just a wrapper
    jp ProcessBGMRun            ; End of subroutine

NoteOnNoise:
    PollPCM
    push hl

    ld hl, RAM_PSGData+48       ; Set channel volume
    ld a, (hl)
    or $80
    ld (hl), a

    PollPCM
    push de

    inc l                       ; Now we'll reset the envelope address...
    inc l
    ld d, h
    ld a, l
    add 6
    ld e, a

    PollPCM

    ld a, (de)                  ; Reset envelope address
    ld (hl), a
    inc l
    inc e
    ld a, (de)
    ld (hl), a
    inc l
    inc e
    ld a, (de)
    ld (hl), a

    pop de
    pop hl

SetNoteNoise:
    PollPCM
    call GetParam               ; Get noise type
    PollPCM

    ld a, $E0                   ; Set new noise type
    or b
    ld ($7F11), a

    ret                         ; End of subroutine

;****************************************************************************
; NoteOffPSG*
; Does a "note off" for a PSG channel
;****************************************************************************

NoteOffPSGSFX:
    call NoteOffPSG             ; We're just a wrapper
    jp ProcessSFXRun            ; End of subroutine

NoteOffPSGBGM:
    ld b, a
    PollPCM
    ld a, b

    push hl
    and $03                     ; Check if channel is free
    ld hl, RAM_Locked+8
    add l
    ld l, a
    ld a, (hl)
    pop hl
    or a
    jp nz, ProcessBGMRun        ; Don't stop if locked

    ld a, b
    call NoteOffPSG             ; We're just a wrapper
    jp ProcessBGMRun            ; End of subroutine

NoteOffPSG:
    and $03
    ld b, a
    PollPCM

    push hl                     ; Mark channel as not playing
    ld h, RAM_PSGData>>8
    ld a, b
    rrca
    rrca
    rrca
    rrca
    ld l, a
    ld a, (hl)
    and $7F
    ld (hl), a
    
    PollPCM
    
    ld a, l                     ; Don't waste time with semitone shifting
    add 11
    ld l, a
    ld a, $FF
    ld (hl), a
    pop hl

    ret                         ; End of subroutine

;****************************************************************************
; SetPSGVol*
; Sets the volume of a PSG channel
;****************************************************************************

SetPSGVolSFX:
    and $03                     ; Get channel ID

    ex af, af'
    PollPCM
    call GetParam               ; Get volume
    PollPCM
    ex af, af'

    push hl
    ld h, RAM_PSGData>>8        ; Set new volume
    rrca
    rrca
    rrca
    rrca
    ld l, a
    ld a, (hl)
    and $80
    or b
    ld (hl), a
    pop hl

    jp ProcessSFXRun            ; End of subroutine

SetPSGVolBGM:
    and $03                     ; Get channel ID

    ex af, af'
    PollPCM
    call GetParam               ; Get volume
    PollPCM
    ex af, af'

    push de
    push hl

    push af
    ld de, RAM_Locked+8         ; Check if channel is locked
    add e                         ; Keep results for later
    ld e, a
    ld a, (de)
    ld e, a

    PollPCM
    pop af

    ld h, RAM_PSGData>>8        ; Store new volume
    rrca
    rrca
    rrca
    rrca
    add 15
    ld l, a
    ld (hl), b

    PollPCM

    ld a, e                     ; Is channel locked?
    or a
    jr nz, .nosetvol

    ld a, l                     ; Set new volume
    sub 15
    ld l, a
    ld a, (hl)
    and $80
    or b
    ld (hl), a

    PollPCM

.nosetvol:
    pop hl
    pop de

    jp ProcessBGMRun            ; End of subroutine

;****************************************************************************
; LoadPSG*
; Loads a PSG instrument
;****************************************************************************

LoadPSGSFX:
    and $03                     ; Get channel number

    ex af, af'
    PollPCM
    call GetParam               ; Get instrument ID
    PollPCM
    ex af, af'

    push de
    push hl

    ld d, RAM_PointerList>>8    ; Get position in pointer list
    ld e, b

    ld h, RAM_PSGData>>8        ; Where to store address
    rrca
    rrca
    rrca
    rrca
    add 8+2+(RAM_PSGData&$FF)
    ld l, a

    PollPCM

    ld a, (de)                  ; Store PSG envelope start address
    ld (hl), a
    inc d
    dec l
    ld a, (de)
    ld (hl), a
    inc d
    dec l
    ld a, (de)
    ld (hl), a
    
    PollPCM
    
    ld a, l                     ; Reset volume
    sub 8
    ld l, a
    ld (hl), $00

    pop hl
    pop de
    jp ProcessSFXRun            ; End of subroutine

;----------------------------------------------------------------------------

LoadPSGBGM:
    and $03                     ; Get channel number

    ex af, af'
    PollPCM
    call GetParam               ; Get instrument ID
    PollPCM
    ex af, af'

    push de
    push hl

    ld d, RAM_PointerList>>8    ; Get position in pointer list
    ld e, b

    ld hl, RAM_Locked+8         ; Get if channel is locked
    push af
    add l
    ld l, a
    pop af
    ld b, (hl)

    ld h, RAM_PSGData>>8        ; Where to store BGM instrument data
    rrca
    rrca
    rrca
    rrca
    add 15
    ld l, a

    PollPCM
    
    ld (hl), a                  ; Reset volume for BGM
    dec l

    ld a, (de)                  ; Store PSG envelope address for BGM
    ld (hl), a
    inc d
    dec l
    ld a, (de)
    ld (hl), a
    inc d
    dec l
    ld a, (de)
    ld (hl), a

    PollPCM

    ld a, b                     ; Don't set PSG envelope if locked
    or a
    jp z, .noloadlocked
    pop hl
    pop de
    jp ProcessBGMRun
.noloadlocked:
    PollPCM

    ld d, h                     ; Set PSG envelope
    ld a, l
    sub 12-8
    ld e, a

    ld a, (hl)
    ld (de), a
    inc l
    inc e
    ld a, (hl)
    ld (de), a
    inc l
    inc e
    ld a, (hl)
    ld (de), a

    PollPCM
    
    ld a, l                     ; Reset volume
    sub 8
    ld l, a
    ld (hl), $00

    pop hl
    pop de
    jp ProcessBGMRun            ; End of subroutine

;****************************************************************************
; SetNotePSG*
; Sets the note of a PSG channel without "note on"
;****************************************************************************

SetNotePSGSFX:
    call SetNotePSG             ; We're just a wrapper
    jp ProcessSFXRun            ; End of subroutine

SetNotePSGBGM:
    ld b, a
    PollPCM
    ld a, b

    push hl
    and $0F                     ; Check if channel is free
    ld h, RAM_Locked>>8
    add RAM_Locked&$FF
    ld l, a
    ld a, (hl)
    pop hl
    or a
    jp nz, ProcessBGMSkip2      ; Don't play if locked

    ld a, b
    call SetNotePSG             ; We're just a wrapper
    jp ProcessBGMRun            ; End of subroutine

SetNotePSG:
    and $03                     ; Get channel number

    ex af, af'
    PollPCM
    call GetParam               ; Get first byte
    PollPCM

    ld a, b                     ; Select by semitone?
    add a, a
    jp c, .freqtone
    ex af, af'

    push hl                     ; Mark semitone as not valid
    ld l, a
    ex af, af'
    PollPCM
    ld a, l
    rrca
    rrca
    rrca
    rrca
    add a, 11
    ld l, a
    ld h, RAM_PSGData>>8
    ld (hl), $FF
    pop hl
    PollPCM
    ex af, af'

    push de                     ; PSG port address
    ld de, $7F11

    rrca                        ; Set first frequency byte
    rrca
    rrca
    or b
    or $80
    ld (de), a

    PollPCM
    call GetParam               ; Get second byte
    PollPCM

    ex de, hl
    ld (hl), b                  ; Set second frequency byte
    ex de, hl
    pop de

    ret                         ; End of subroutine

;----------------------------------------------------------------------------

.freqtone:
    ld b, a
    PollPCM
    ex af, af'
    rrca
    rrca
    rrca
    rrca
    add 10
    jp SetSemitonePSG

;****************************************************************************
; SetNoteNoise*
; Sets the note of the noise PSG channel without "note on"
;****************************************************************************

SetNoteNoiseSFX:
    call SetNoteNoise           ; We're just a wrapper
    jp ProcessSFXRun            ; End of subroutine

SetNoteNoiseBGM:
    ld a, (RAM_Locked+11)       ; Check if channel is free
    or a
    jp nz, ProcessBGMSkip1      ; Don't play if locked

    call SetNoteNoise           ; We're just a wrapper
    jp ProcessBGMRun            ; End of subroutine

;****************************************************************************
; LockChannelPSG [events $E8-$EB]
; Locks a PSG channel
;****************************************************************************

LockChannelPSG:
    and $03
    ld b, a
    PollPCM
    push hl

    ld h, RAM_Locked>>8         ; Get address of channel to lock
    ld a, b
    add (RAM_Locked&$FF)+8
    ld l, a

    ld (hl), $01                ; Lock channel
    
    PollPCM
    
    ld a, b                     ; Stop channel
    rrca
    rrca
    rrca
    rrca
    ld l, a
    ld h, RAM_PSGData>>8
    ld (hl), $00

    pop hl
    jp ProcessSFXRun            ; End of subroutine