-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWave_Player_50Hz.s
More file actions
792 lines (682 loc) · 12.7 KB
/
Wave_Player_50Hz.s
File metadata and controls
792 lines (682 loc) · 12.7 KB
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
;Wave_Player_50Hz.s - Play Compiled tune for V1.1 in 50Hz IRQ
;Encompass 1.1 changes..
; Cover pattern commands 4,5,6 only
; 4 Tempo
; 5 Pitchbend
; 6 Trigger Out to BFFF
; Handle VRST Relative Volume changes
; 0 Silence Volume and EG on Channel
; 1 Decrement Volume
; 2 Increment Volume
; Handle expanded Patterns up to 35
#define VIA_PORTB $0300
#define VIA_PCR $030C
#define VIA_PORTA $030F
#define SYS_IRQPATCH $24A
#define MUSICBASEADDRESS $7700
#define MUSICTRIGGERLOC $BFFF
.zero
*=$00
pattern .dsb 2 ;00-01 Current Pattern Row Address
effect
ornament .dsb 2 ;02-03 Current Ornament/Effect base Address
header .dsb 2 ;04-05 Music Header Location
pat .dsb 2 ;06-07 Pattern Address Table
eat .dsb 2 ;08-09 Effect Address Table
oat .dsb 2 ;0A-0B Ornament Address Table
.text
*=$B000
;Ensure this is kept page alligned
InitMusic
;Patch IRQ Vector
sei
;twi999 nop
; jmp twi999
lda #$4C
sta SYS_IRQPATCH
lda #<MusicIRQ
sta SYS_IRQPATCH+1
lda #>MusicIRQ
sta SYS_IRQPATCH+2
lda #<MUSICBASEADDRESS
sta header
lda #>MUSICBASEADDRESS
sta header+1
lda #11
sta ListIndex
;Set up pat
ldy #2
lda (header),y
clc
adc header
sta pat
iny
lda (header),y
adc header+1
sta pat+1
;Set up eat
ldy #4
lda (header),y
clc
adc header
sta eat
iny
lda (header),y
adc header+1
sta eat+1
;Set up oat
ldy #6
lda (header),y
clc
adc header
sta oat
iny
lda (header),y
adc header+1
sta oat+1
jsr ProcList
lda #%11000000
sta MusicActivity
;Set up Music Tempo as 1 so that first irq call pattern
lda #01
sta MusicTempoCount
cli
rts
MusicIRQ
sta RegA+1
stx RegX+1
sty RegY+1
jsr ProcMusic
jsr SendAY
RegA lda #00
RegX ldx #00
RegY ldy #00
rti
SendAY ldx #13
.(
loop1 lda ayw_Bank,x
cmp ayr_Bank,x
beq skip1
sta ayr_Bank,x
ldy ayRealRegister,x
sty VIA_PORTA
ldy #$FF
sty VIA_PCR
ldy #$DD
sty VIA_PCR
sta VIA_PORTA
lda #$FD
sta VIA_PCR
sty VIA_PCR
skip1 dex
bpl loop1
.)
rts
ProcMusic
dec MusicTempoCount
bne ProcSound
ldy #1
lda (header),y
sta MusicTempoCount
jsr ProcPattern
ProcSound
ldx #2
.(
loop1 lda MusicActivity
and NoiseBit,x ;B3 B4 B5
cmp #1
lda PatternNote,x
bcc skip1
;ProcOrnament
ldy OrnamentID,x
lda header
clc
adc (oat),y
sta ornament
lda header+1
iny
adc (oat),y
sta ornament+1
loop2 ldy OrnamentIndex,x
lda (ornament),y
bne skip2
ldy #00
lda (ornament),y
bmi skip6
sta OrnamentIndex,x
jmp loop2
skip6 lda MusicActivity
and NoiseMask,x
sta MusicActivity
lda #00
skip2 clc
adc PatternNote,x
and #127
inc OrnamentIndex,x
;Convert to Pitch
skip1 tay
lda PitchTableLo,y
sta IntermediatePitchLo
lda PitchTableHi,y
sta IntermediatePitchHi
;Check Effect
lda MusicActivity
and EffectBit,x
beq skip3
;Process Effect
ldy EffectID,x
lda header
clc
adc (eat),y
sta effect
lda header+1
iny
adc (eat),y
sta effect+1
ldy EffectIndex,x
loop3 lda (effect),y
and #%11100000
lsr
sta vector1+1
lda (effect),y
iny
and #31
adc #255-15
sec
vector1 jsr pmeLoopOrEnd
bcs loop3
tya
sta EffectIndex,x
skip3 ;Transfer Intermediate Pitch to AY Registers
lda IntermediatePitchLo
sta ayw_PitchLo,x
lda IntermediatePitchHi
sta ayw_PitchHi,x
;Progress to next Effect/Ornament
dex
bmi skip4
jmp loop1
skip4 rts
.)
ProcPattern
lda PatternRestCount
.(
beq skip4
dec PatternRestCount
jmp skip3
skip4 lda #00
sta BarFlag
sta ChannelID
sta NewActivity
loop1 ldy #00
lda (pattern),y
inc pattern
bne skip2
inc pattern+1
skip2 ldx #12
loop2 cmp PatternRangeThreshhold-1,x
bcs skip1
dex
bne loop2
sec
skip1 sbc PatternRangeThreshhold-1,x
ldy PatternRangeCommandLo,x
sty vector1+1
ldy PatternRangeCommandHi,x
sty vector1+2
clc
vector1 jsr $dead
bcc loop1
skip3 dec PatternRow
.)
beq ProcList
lda BarFlag
beq AvoidList
ProcList ;Get next List PatternID
ldy ListIndex ;Offset by $0B
lda (header),y
;Check for End
.(
bmi skip1
;Calculate address of Pattern
asl
tay
lda header
adc (pat),y
sta pattern
lda header+1
iny
adc (pat),y
sta pattern+1
;Progress List Index
inc ListIndex
;Check for forced end
bmi EndMusic
;Reset Pattern Stuff
lda #64
sta PatternRow
lda #00
sta PatternRestCount
rts
skip1 ;Check for loop
ldy #10
lda (header),y
bmi EndMusic
sta ListIndex
jmp ProcList
EndMusic ;Terminate Music
.)
TerminateMusic
lda #00
sta ayw_Volume
sta ayw_Volume+1
sta ayw_Volume+2
sta MusicActivity
jsr SendAY
lda #$40
sta SYS_IRQPATCH
AvoidList rts
PatternRangeThreshhold
;The above loop searches and exits on 0 defaulting to Sample so we can save
;a massive one byte here!
; .byt 0 ;00 prcSample
.byt 6 ;01 prcEGPeriod
.byt 69 ;02 prcCycle
.byt 72 ;03 prcNoise
.byt 103 ;04 prcNote
.byt 165 ;05 prcRest
.byt 166 ;06 prcBar
.byt 167 ;07 prcVolume
.byt 171 ;08 prcEffect
.byt 186 ;09 prcOrnament
.byt 201 ;10 prcCommand
.byt 208 ;11 prcLongRowRest
.byt 209 ;12 prcShortRowRest
PatternRangeCommandLo
.byt <prcSample
.byt <prcEGPeriod
.byt <prcCycle
.byt <prcNoise
.byt <prcNote
.byt <prcRest
.byt <prcBar
.byt <prcVolume
.byt <prcEffect
.byt <prcOrnament
.byt <prcCommand
.byt <prcLongRowRest ;(Second byte holds period)
.byt <prcShortRowRest
prcCycle tax
lda CycleCode,x
sta ayw_Cycle
lda #128
sta ayr_Cycle
rts
prcNote ldx ChannelID
adc #11
sta PatternNote,x
;If Note specified always default to Noise Off, Tone On
lda ayw_Status
and ToneMask,x
ora NoiseBit,x
sta ayw_Status
inc ChannelID
lda ChannelID
cmp #3 ;If end of Row then indicate with Carry
;If effect/Ornament not set and note then reset in musicactivity
lda NewActivity
and EffectBit,x
beq NoteDisableEffect
lda NewActivity
and OrnamentBit,x
beq NoteDisableOrnament
rts
prcRest ldx ChannelID
inx
stx ChannelID
cpx #3
rts
NoteDisableEffect
lda MusicActivity
and ToneMask,x
sta MusicActivity
;If no effect (And EG not set) ensure Pattern Volume gets into AY
lda EGActiveFlag,x
.(
bne skip1
lda PatternVolume,x
sta ayw_Volume,x
skip1 rts
.)
NoteDisableOrnament
lda MusicActivity
and NoiseMask,x
sta MusicActivity
rts
prcVolume
tax
lda RealVolume,x
ldx ChannelID
sta PatternVolume,x
rts
prcEffect
ldx ChannelID
;Shift left so it can directly index eat
asl
sta EffectID,x
lda #01
sta EffectIndex,x
lda MusicActivity
ora ToneBit,x
sta MusicActivity
sta NewActivity
rts
prcOrnament
ldx ChannelID
;Shift left so it can directly index oat
asl
sta OrnamentID,x
lda #01
sta OrnamentIndex,x
lda MusicActivity
ora NoiseBit,x
sta MusicActivity
sta NewActivity
rts
prcCommand
tax
ldy #00
lda (pattern),y
inc pattern
.(
bne skip1
inc pattern+1
skip1 ldy CommandVectorLo,x
.)
sty vector1+1
ldy CommandVectorHi,x
sty vector1+2
vector1 jmp $DEAD
prcLongRowRest ;(Second byte holds period)
ldy #00
lda (pattern),y
inc pattern
bne prcShortRowRest
inc pattern+1
prcShortRowRest
sta PatternRestCount
sec
rts
CommandVectorLo
.byt <prcCom_DoNothing
.byt <prcCom_DoNothing
.byt <prcCom_DoNothing
.byt <prcCom_Tempo
.byt <prcCom_Pitchbend
.byt <prcCom_TriggerOut
.byt <prcCom_DoNothing
CommandVectorHi
.byt >prcCom_DoNothing
.byt >prcCom_DoNothing
.byt >prcCom_DoNothing
.byt >prcCom_Tempo
.byt >prcCom_Pitchbend
.byt >prcCom_TriggerOut
.byt >prcCom_DoNothing
prcCom_Tempo
sta MusicTempoCount
;Also store into compiled header
ldy #01
sta (header),y
prcCom_DoNothing
rts
prcCom_TriggerOut
sta MUSICTRIGGERLOC
rts
prcCom_Pitchbend
;Fetch Parameter
ldy #00
lda (pattern),y
inc pattern
.(
bne skip1
inc pattern+1
skip1
.)
;Set up Pitchbend for channel
pha
lsr
lsr
sta pmStepRefer,x
;Shift to steps of 4 (4 bytes per shift calc)
eor #7
asl
asl
sta pbBranchStep,x
pla
and #3
sta pbDelayRefer,x
;Writing 0 ensures the next pitchbend step will set up the next semitone interpolation
lda #00
sta pmStepCount,x
sta pbDelayCount,x
;At this point the new pattern note will have been set.
;However since we are wanting to bend to this note we must set it to the previous note..
ldy pmPatternNote,x
lda pbOldPatternNote,x
sta pmPatternNote,x
;And use the current pattern note as the destination note to reach
tya
sta pbDestinationNote,x
;Compare destination with current note
cmp pmPatternNote,x
.(
beq skip1
;Place result into the pbFlag ensuring its value is always more than zero
lda #2
ror
sta pbFlag,x
skip1 rts
.)
PatternNote
.dsb 3,0
PatternVolume
.dsb 3,0
EffectID
.dsb 3,0
EffectIndex
.dsb 3,0
ayw_Bank
ayw_PitchLo .byt 0,0,0
ayw_PitchHi .byt 0,0,0
ayw_Noise .byt 0
ayw_Status .byt %01111000
ayw_Volume .byt 0,0,0
ayw_EGPeriod .byt 0,0
ayw_Cycle .byt 0
ayr_Bank
ayr_PitchLo .byt 128,128,128
ayr_PitchHi .byt 128,128,128
ayr_Noise .byt 128
ayr_Status .byt 128
ayr_Volume .byt 128,128,128
ayr_EGPeriod .byt 128,128
ayr_Cycle .byt 128
;These routines(pme) have calculated low addresses
;So are page alligned and spaced 16 bytes apart.
;To save space the gaps hold other subroutines,
;music vars and tables
;PageAllignedEffectCommands
; .dsb 256-(*&255)
pmeLoopOrEnd ;XX00
ldy #00 ;2
lda (effect),y ;2
bpl pmeLoop ;2
jmp EndEffect ;3
pmeLoop tay ;1
rts ;1
prcEGPeriod
sta ayw_EGPeriod
rts
MusicTempoCount .byt 0
pmeNoiseOff ;XX10
lda ayw_Status ;3
ora NoiseBit,x ;3
sta ayw_Status ;3
rts ;1
prcBar inc BarFlag
sec
rts
nop
pmeEGOff ;XX20
lda ayw_Volume,x ;3
and #15 ;2
sta ayw_Volume,x ;3
lda #00 ;2
sta EGActiveFlag,x ;3
rts ;1
NewActivity .byt 0
PatternRestCount .byt 0
pmeToneOff ;XX30
lda ayw_Status ;3
ora ToneBit,x ;3
sta ayw_Status ;3
rts ;1
prcNoise
sta ayw_Noise
rts
ChannelID .byt 0
ListIndex .byt 0
pmeVolumeOFS ;XX40
clc
adc PatternVolume,x ;3
cmp #16 ;2
bcs EndEffect ;2
sta PatternVolume,x ;3
sta ayw_Volume,x ;3
rts ;1
MusicActivity .byt 0
pmeNoiseOFS ;XX50
clc
adc ayw_Noise ;3
and #31 ;2
sta ayw_Noise ;3
jsr NoiseOn ;3
rts ;1
EffectBit
ToneBit
.byt %00000001
.byt %00000010
.byt %00000100
pmeEGPerOFS ;XX60
clc
adc ayw_EGPeriod ;3
sta ayw_EGPeriod ;3
jmp EGOn ;3
EffectMask
ToneMask
.byt %11111110
.byt %11111101
.byt %11111011
OrnamentBit
NoiseBit
.byt %00001000
.byt %00010000
.byt %00100000
pmePitchOFS ;XX70
.(
;A pitch offset is always relative to the Intermediate Pitch
sta Temp01
clc
adc IntermediatePitchLo
sta IntermediatePitchLo
bit Temp01
bmi skip2
bcc skip1
inc IntermediatePitchHi
jmp skip1
skip2 bcs skip1
dec IntermediatePitchHi
skip1 lda ayw_Status
.)
and ToneMask,x
sta ayw_Status
sec
rts
EndEffect lda MusicActivity
and EffectMask,x
sta MusicActivity
clc
rts
ayRealRegister
.byt 0,2,4,1,3,5,6,7,8,9,10,11,12,13
NoiseOn lda ayw_Status
and NoiseMask,x
sta ayw_Status
rts
EGOn lda ayw_Volume,x
ora #16
sta ayw_Volume,x
sta EGActiveFlag,x
clc
rts
OrnamentMask
NoiseMask
.byt %11110111
.byt %11101111
.byt %11011111
OrnamentID
.dsb 3,0
OrnamentIndex
.dsb 3,0
RealVolume
.byt 0,4,8,15
EGActiveFlag
.byt 0,0,0
PatternRow .byt 0
IntermediatePitchLo .byt 0
IntermediatePitchHi .byt 0
CycleCode
.byt 8,10,0
BarFlag .byt 0
Temp01 .byt 0
PatternRangeCommandHi
.byt >prcSample
.byt >prcEGPeriod
.byt >prcCycle
.byt >prcNoise
.byt >prcNote
.byt >prcRest
.byt >prcBar
.byt >prcVolume
.byt >prcEffect
.byt >prcOrnament
.byt >prcCommand
.byt >prcLongRowRest ;(Second byte holds period)
.byt >prcShortRowRest
PitchTableLo
.byt <3822,<3606,<3404,<3214,<3032,<2862,<2702,<2550,<2406,<2272,<2144,<2024
.byt <1911,<1803,<1702,<1607,<1516,<1431,<1351,<1275,<1203,<1136,<1072,<1012
.byt <955,<901,<851,<803,<758,<715,<675,<637,<601,<568,<536,<506
.byt <477,<450,<425,<401,<379,<357,<337,<318,<300,<284,<268,<253
.byt <238,<225,<212,<200,<189,<178,<168,<159,<150,<142,<134,<126
.byt <119,<112,<106,<100,<94,<89,<84,<79,<75,<71,<67,<63
.byt <59,<56,<53,<50,<47,<44,<42,<39,<37,<35,<33,<31
.byt <29,<28,<26,<25,<23,<22,<21,<19,<18,<17,<16,<15
.byt <14,<14,<13,<12,<11,<11,<10,<9,<9,<8,<8,<7
.byt 7,7,6,6,5,5,5,4,4,4,4,3
.byt 3,3,3,3,2,2,2,2
PitchTableHi
.byt >3822,>3606,>3404,>3214,>3032,>2862,>2702,>2550,>2406,>2272,>2144,>2024
.byt >1911,>1803,>1702,>1607,>1516,>1431,>1351,>1275,>1203,>1136,>1072,>1012
.byt >955,>901,>851,>803,>758,>715,>675,>637,>601,>568,>536,>506
.byt >477,>450,>425,>401,>379,>357,>337,>318,>300,>284,>268,>253
.byt >238,>225,>212,>200,>189,>178,>168,>159,>150,>142,>134,>126
.byt >119,>112,>106,>100,>94,>89,>84,>79,>75,>71,>67,>63
.byt >59,>56,>53,>50,>47,>44,>42,>39,>37,>35,>33,>31
.byt >29,>28,>26,>25,>23,>22,>21,>19,>18,>17,>16,>15
.byt >14,>14,>13,>12,>11,>11,>10,>9,>9,>8,>8,>7
.byt 0,0,0,0,0,0,0,0,0,0,0,0
.byt 0,0,0,0,0,0,0,0
EndOfPlayer
.byt 0