-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusic.s
1059 lines (1032 loc) · 19 KB
/
music.s
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
; Lizard Music Engine
; Copyright Brad Smith 2019
; http://lizardnes.com
;
; music and sound
;
.feature force_range
.macpack longbranch
.export music_tick
.export music_init
.export player_pal
.exportzp player_next_sound
.exportzp player_next_music
.exportzp player_pause
.export player_current_music
.segment "MUSIC"
.include "output/data_music.inc"
.segment "ZEROPAGE"
temp_ptrn: .res 2 ; not required to persist outside of music_init/music_tick
; zero-page for convenience
player_next_sound: .res 2
player_next_music: .res 1
player_pause: .res 1
; zero-page for indirection
pointer_pattern_table: .res 2
pointer_order: .res 2
pointer_pattern: .res 8
pointer_sfx_pattern: .res 4
.segment "RAM"
player_pal: .res 1
;player_next_sound: .res 2
;player_next_music: .res 1
;player_pause: .res 1
player_current_music: .res 1
player_speed: .res 1
player_pattern_length: .res 1
player_row: .res 1
player_row_sub: .res 1
player_order_frame: .res 1
player_row_skip: .res 4
player_macro_pos: .res 16
player_note: .res 4
player_vol: .res 4
player_halt: .res 4
player_pitch_low: .res 4
player_pitch_high: .res 4
player_sfx_on: .res 2
player_sfx_pos: .res 2
player_sfx_skip: .res 2
player_vol_out: .res 4
player_freq_out_low: .res 4
player_freq_out_high: .res 4
player_duty_out: .res 4
player_apu_high: .res 4
;pointer_pattern_table: .res 2
;pointer_order: .res 2
;pointer_pattern: .res 8
pointer_macro: .res 32
;pointer_sfx_pattern: .res 4
.segment "CODE"
music_init:
;
; initialize the APU
;
; disable length counters, set volume to 0
lda #%00110000
sta $4000
sta $4004
sta $400C
; init triangle halted
lda #%10000000
sta $4008
; enable channels
lda #%00001111
sta $4015
; disable and minimize sweep
lda #%01111111
sta $4001
sta $4005
; zero low freq
lda #0
sta $4002
sta $4006
sta $400A
sta $400E
sta $4011 ; reset ZXX
; zero high freq and reload length counters
lda #%00001000
sta $4003
sta $4007
sta $400B
sta $400F
; flag to reset high freq registers
lda #$FF
sta player_apu_high+0
sta player_apu_high+1
sta player_apu_high+2
sta player_apu_high+3
;
; initialize to song 0
;
lda #0
jsr load_music
rts
music_tick:
; if new music assigned, load it now
lda player_next_music
cmp player_current_music
beq :+
jsr load_music
:
; if new sfx assigned, load it now
lda player_next_sound+0
beq :+
ldx #0
jsr load_sfx
:
lda player_next_sound+1
beq :+
ldx #1
jsr load_sfx
:
; if paused, mute the APU and skip the update
lda player_pause
beq :+
lda #0
sta $4015
; high freq registers need to reset when 4015 is renabled
lda #$FF
sta player_apu_high+0
sta player_apu_high+1
sta player_apu_high+2
sta player_apu_high+3
rts
:
;
; tick pattern
;
@pal_repeat: ; for PAL support, repeat every 5th frame
lda player_row_sub
jne @tick_pattern_end
inc player_row
ldx #0
@channel_loop:
lda player_row_skip, X
beq :+
dec player_row_skip, X
jmp @next_channel
:
txa
pha ; push X
asl
tax ; X *= 2
@read_loop:
; X = channel * 2
; top of stack = channel
lda (pointer_pattern+0, X) ; A = command
inc pointer_pattern+0, X
bne :+
inc pointer_pattern+1, X
:
; if A < 0x80 skip and end row
cmp #$80
bcs :+
tay
pla
tax
tya
sta player_row_skip, X
jmp @next_channel
:
; if A == 0x80 halt and end row
bne :+
pla
tax
lda #1
sta player_halt, X
jmp @next_channel
:
; A >= 0x81
; if A < 0xE0 play note and end row
cmp #$E0
bcs :+
sec
sbc #$81
tay ; Y = note (A - 0x81)
pla
tax
tya
sta player_note, X
lda #0
sta player_halt, X
sta player_pitch_low, X
sta player_pitch_high, X
sta player_macro_pos+ 0, X
sta player_macro_pos+ 4, X
sta player_macro_pos+ 8, X
sta player_macro_pos+12, X
jmp @next_channel
:
; A >= 0xE0
; if A < 0xF0 volume
cmp #$F0
bcs :+
and #$0F ; volume = A & 0x0F
tay
pla
tax
tya
sta player_vol, X
txa
pha ; put X back on stack for next loop
asl
tax ; put X*2 back
jmp @read_loop
:
; if A == 0xF0 set instrument
bne :++
lda #0
sta temp_ptrn+1
; let A = instrument
lda (pointer_pattern+0, X)
inc pointer_pattern+0, X
bne :+
inc pointer_pattern+1, X
:
; A *= 4 (store 2 high bits in temp_ptrn+1)
asl
rol temp_ptrn+1
asl
rol temp_ptrn+1
clc
adc #<data_music_instrument
sta temp_ptrn+0
lda #>data_music_instrument
adc temp_ptrn+1
sta temp_ptrn+1
; temp_ptrn = data_music_instrument + (4 * instrument)
; load 4 macros
ldy #0
lda (temp_ptrn), Y
tay
lda data_music_macro_low, Y
sta pointer_macro+0+ 0, X
lda data_music_macro_high, Y
sta pointer_macro+1+ 0, X
ldy #1
lda (temp_ptrn), Y
tay
lda data_music_macro_low, Y
sta pointer_macro+0+ 8, X
lda data_music_macro_high, Y
sta pointer_macro+1+ 8, X
ldy #2
lda (temp_ptrn), Y
tay
lda data_music_macro_low, Y
sta pointer_macro+0+16, X
lda data_music_macro_high, Y
sta pointer_macro+1+16, X
ldy #3
lda (temp_ptrn), Y
tay
lda data_music_macro_low, Y
sta pointer_macro+0+24, X
lda data_music_macro_high, Y
sta pointer_macro+1+24, X
; reset the macro positions
pla
pha ; put X back on stack for next loop
tax
lda #0
sta player_macro_pos+ 0, X
sta player_macro_pos+ 4, X
sta player_macro_pos+ 8, X
sta player_macro_pos+12, X
txa
asl
tax ; put back X*2 for next loop
jmp @read_loop
:
; if A == F1 BXX
cmp #$F1
bne :++
lda (pointer_pattern+0, X)
inc pointer_pattern+0, X
bne :+
inc pointer_pattern+1, X
:
; A = (A*4)-4
sec
sbc #1
asl
asl
sta player_order_frame
; advance to end of pattern
lda player_pattern_length
sta player_row
jmp @read_loop
:
; if A == F2 D00
cmp #$F2
bne :+
; advance to end of pattern
lda player_pattern_length
sta player_row
jmp @read_loop
:
; if A == F3 FXX
cmp #$F3
bne :++
lda (pointer_pattern+0, X)
inc pointer_pattern+0, X
bne :+
inc pointer_pattern+1, X
:
sta player_speed
jmp @read_loop
:
; else this is an unimplemented command
inc pointer_pattern+0, X
bne :+
inc pointer_pattern+1, X
:
jmp @read_loop
; next channel
@next_channel:
; X = channel
inx
cpx #4
jne @channel_loop
; if player_row >= player_pattern_length
; advance order
lda player_row
cmp player_pattern_length
bcc @advance_order_end
lda #0
sta player_row
lda player_order_frame
clc
adc #4
sta player_order_frame
tay ; y = order+0
ldx #0
: ; for i=0;i<4;++i (X = i*2)
tya
pha ; push order+i
lda (pointer_order), Y
asl
tay ; Y = order frame * 2
lda (pointer_pattern_table), Y
sta pointer_pattern+0, X
iny
lda (pointer_pattern_table), Y
sta pointer_pattern+1, X
; pull order+i
pla
tay
iny ; ++i
; next channel
inx
inx
cpx #8
bne :-
lda #0
ldx #0
:
sta player_row_skip, X
inx
cpx #4
bne :-
@advance_order_end:
lda player_speed
sta player_row_sub
@tick_pattern_end:
dec player_row_sub
; adjust for pal (tick twice every 5th frame)
lda player_pal ; 0 = NTSC, else PAL
beq :++
cmp #1 ; 1 = repeat this frame
bne :+
lda #6
sta player_pal
jmp @pal_repeat
:
dec player_pal
:
;
; tick sfx
;
ldy #0
lda player_sfx_on+0
beq @sfx0_done
lda player_sfx_skip+0
beq :+
dec player_sfx_skip+0
jmp @sfx0_done
:
@sfx0_read_loop:
lda (pointer_sfx_pattern+0), Y
inc pointer_sfx_pattern+0
bne :+
inc pointer_sfx_pattern+1
:
; if A < 0x80 skip
cmp #$80
bcs :+
sta player_sfx_skip+0
jmp @sfx0_done
:
; if A == 0x80 halt
bne :+
lda #0
sta player_sfx_on+0
.ifndef SFX_NO_HALT
lda #1
sta player_halt+0
.endif
jmp @sfx0_done
:
; A >= 0x81
; if A < 0xE0 note
cmp #$E0
bcs :+
sec
sbc #$81
tax
lda data_music_tuning_low, X
sta player_freq_out_low+0
lda data_music_tuning_high, X
sta player_freq_out_high+0
jmp @sfx0_done
:
; A >= 0xE0
; if A < 0xF0 volume
cmp #$F0
bcs :+
and #$0F
sta player_vol_out+0
jmp @sfx0_read_loop
:
; A >= 0xF0 duty
and #$03
sta player_duty_out+0
jmp @sfx0_read_loop
; end of sfx0_read_loop
@sfx0_done:
lda player_sfx_on+1
beq @sfx1_done
lda player_sfx_skip+1
beq :+
dec player_sfx_skip+1
jmp @sfx1_done
:
@sfx1_read_loop:
lda (pointer_sfx_pattern+2), Y
inc pointer_sfx_pattern+2
bne :+
inc pointer_sfx_pattern+3
:
; if A < 0x80 skip
cmp #$80
bcs :+
sta player_sfx_skip+1
jmp @sfx1_done
:
; if A == 0x80 halt
bne :+
lda #0
sta player_sfx_on+1
.ifndef SFX_NO_HALT
lda #1
sta player_halt+3
.endif
jmp @sfx1_done
:
; A >= 0x81
; if A < 0xE0 note
cmp #$E0
bcs :+
eor #$FF ; A = $0F - (A - $81)
sec
adc #($0F+$81)
sta player_freq_out_low+3
jmp @sfx1_done
:
; A >= 0xE0
; if A < 0xF0 volume
cmp #$F0
bcs :+
and #$0F
sta player_vol_out+3
jmp @sfx1_read_loop
:
; A >= 0xF0 duty
and #$01
sta player_duty_out+3
jmp @sfx1_read_loop
; end of sfx1_read_loop
@sfx1_done:
;
; tick macros
;
; channel 0 square 1
lda player_sfx_on+0
jne @channel_0_sfx_override
; volume
lda player_halt+0
bne :+
lda #0
jsr tick_macro
asl
asl
asl
asl
ora player_vol+0
tax
lda data_music_multiply, X
jmp :++
:
lda #0
jsr tick_macro
lda #0
:
sta player_vol_out+0
; arp
lda #4
jsr tick_macro
clc
adc player_note+0
cmp #96
bcc :+
lda #95
:
tax
lda data_music_tuning_low, X
sta player_freq_out_low+0
lda data_music_tuning_high, X
sta player_freq_out_high+0
; pitch
lda #8
jsr tick_macro
cmp #0
bmi :+
clc
adc player_pitch_low+0
sta player_pitch_low+0
lda player_pitch_high+0
adc #0
sta player_pitch_high+0
jmp :++
:
clc
adc player_pitch_low+0
sta player_pitch_low+0
lda player_pitch_high+0
adc #$FF
sta player_pitch_high+0
:
lda player_freq_out_low+0
clc
adc player_pitch_low+0
sta player_freq_out_low+0
lda player_freq_out_high+0
adc player_pitch_high+0
and #$07
sta player_freq_out_high+0
; duty
lda #12
jsr tick_macro
and #$03
sta player_duty_out+0
jmp @tick_channel_1
@channel_0_sfx_override:
lda #0
jsr tick_macro
lda #4
jsr tick_macro
lda #8
jsr tick_macro
; pitch needs update even when SFX playing
cmp #0
bmi :+
clc
adc player_pitch_low+0
sta player_pitch_low+0
lda player_pitch_high+0
adc #0
sta player_pitch_high+0
jmp :++
:
clc
adc player_pitch_low+0
sta player_pitch_low+0
lda player_pitch_high+0
adc #$FF
sta player_pitch_high+0
:
lda #12
jsr tick_macro
; channel 1 square 2
@tick_channel_1:
; volume
lda player_halt+1
bne :+
lda #1
jsr tick_macro
asl
asl
asl
asl
ora player_vol+1
tax
lda data_music_multiply, X
jmp :++
:
lda #1
jsr tick_macro
lda #0
:
sta player_vol_out+1
; arp
lda #5
jsr tick_macro
clc
adc player_note+1
cmp #96
bcc :+
lda #95
:
tax
lda data_music_tuning_low, X
sta player_freq_out_low+1
lda data_music_tuning_high, X
sta player_freq_out_high+1
; pitch
lda #9
jsr tick_macro
cmp #0
bmi :+
clc
adc player_pitch_low+1
sta player_pitch_low+1
lda player_pitch_high+1
adc #0
sta player_pitch_high+1
jmp :++
:
clc
adc player_pitch_low+1
sta player_pitch_low+1
lda player_pitch_high+1
adc #$FF
sta player_pitch_high+1
:
lda player_freq_out_low+1
clc
adc player_pitch_low+1
sta player_freq_out_low+1
lda player_freq_out_high+1
adc player_pitch_high+1
and #$07
sta player_freq_out_high+1
; duty
lda #13
jsr tick_macro
and #$03
sta player_duty_out+1
; channel 2 triangle
@tick_channel_2:
; volume
lda player_halt+2
bne :+
lda #2
jsr tick_macro
asl
asl
asl
asl
ora player_vol+2
tax
lda data_music_multiply, X
jmp :++
:
lda #2
jsr tick_macro
lda #0
:
sta player_vol_out+2
; arp
lda #6
jsr tick_macro
clc
adc player_note+2
cmp #96
bcc :+
lda #95
:
tax
lda data_music_tuning_low, X
sta player_freq_out_low+2
lda data_music_tuning_high, X
sta player_freq_out_high+2
; pitch
lda #10
jsr tick_macro
cmp #0
bmi :+
clc
adc player_pitch_low+2
sta player_pitch_low+2
lda player_pitch_high+2
adc #0
sta player_pitch_high+2
jmp :++
:
clc
adc player_pitch_low+2
sta player_pitch_low+2
lda player_pitch_high+2
adc #$FF
sta player_pitch_high+2
:
lda player_freq_out_low+2
clc
adc player_pitch_low+2
sta player_freq_out_low+2
lda player_freq_out_high+2
adc player_pitch_high+2
and #$07
sta player_freq_out_high+2
; duty
;lda #14
;jsr tick_macro
; channel 3 noise
@tick_channel_3:
lda player_sfx_on+1
bne @channel_3_sfx_override
; volume
lda player_halt+3
bne :+
lda #3
jsr tick_macro
asl
asl
asl
asl
ora player_vol+3
tax
lda data_music_multiply, X
jmp :++
:
lda #3
jsr tick_macro
lda #0
:
sta player_vol_out+3
; arp
lda #7
jsr tick_macro
clc
adc player_note+3
cmp #96
bcc :+
lda #95
:
eor #$FF ; A = $0F - A
sec
adc #$0F
and #$0F ; A &= $0F
sta player_freq_out_low+3
; pitch
;lda #11
;jsr tick_macro
; duty
lda #15
jsr tick_macro
and #$01
sta player_duty_out+3
jmp @tick_channel_end
@channel_3_sfx_override:
lda #3
jsr tick_macro
lda #7
jsr tick_macro
;lda #11
;jsr tick_macro
lda #15
jsr tick_macro
@tick_channel_end:
;
; write to APU
;
lda #%00001111
sta $4015 ; unmute if muted
lda #%11000000
sta $4017 ; tick the frame counter
; channel 0
lda player_duty_out+0
asl
asl
asl
asl
asl
asl
ora #%00110000
ora player_vol_out+0
sta $4000
lda player_freq_out_low+0
sta $4002
lda player_freq_out_high+0
ora #%00001000
cmp player_apu_high+0
beq :+
sta $4003
sta player_apu_high+0
:
; channel 1
lda player_duty_out+1
asl
asl
asl
asl
asl
asl
ora #%00110000
ora player_vol_out+1
sta $4004
lda player_freq_out_low+1
sta $4006
lda player_freq_out_high+1
ora #%00001000
cmp player_apu_high+1
beq :+
sta $4007
sta player_apu_high+1
:
; channel 2
lda player_vol_out+2
beq :+
lda #%11111111
jmp :++
:
lda #%10000000
:
sta $4008
lda player_freq_out_low+2
sta $400A
lda player_freq_out_high+2
ora #%00001000
cmp player_apu_high+2
beq :+
sta $400B
sta player_apu_high+2
:
; channel 3
lda player_vol_out+3
ora #%00110000
sta $400C
lda player_duty_out+3
ror
ror
ora player_freq_out_low+3
sta $400E
lda #%00001000
cmp player_apu_high+3
beq :+
sta $400F
sta player_apu_high+3
:
rts
tick_macro:
pha
asl
tax
lda pointer_macro+0, X
sta temp_ptrn+0
lda pointer_macro+1, X
sta temp_ptrn+1
pla
tax
lda player_macro_pos, X
tay
@macro_loop:
lda (temp_ptrn), Y
iny
cmp #<LOOP
bne :+
lda (temp_ptrn), Y
tay
jmp @macro_loop
:
pha
tya
sta player_macro_pos, X
pla
rts
load_music:
; store current music, put in x as index
sta player_current_music
tax
; setup speed, pattern length, order pointer
lda data_music_speed, x
sta player_speed
lda data_music_pattern_length, x
sta player_pattern_length
lda data_music_order_low, x
sta pointer_order+0
lda data_music_order_high, x
sta pointer_order+1
; x *= 2
txa
asl
tax
; load pattern table pointer
lda data_music_pattern+0, x
sta pointer_pattern_table+0
lda data_music_pattern+1, x
sta pointer_pattern_table+1
; initialize the pattern reader
lda #0
sta player_row
sta player_row_sub
sta player_order_frame
; load first pattern and initialize channels
ldx #0
:
lda #0
sta player_row_skip, x
lda #15
sta player_vol, x
lda #1
sta player_halt, x
; fetch order 0
txa
pha ; store X temporarily
tay ; copy X to Y
asl
tax ; X *= 2
lda (pointer_order), y
asl
tay
lda (pointer_pattern_table), y
sta pointer_pattern, x
iny
lda (pointer_pattern_table), y
sta pointer_pattern+1, x
; restore and increment x
pla
tax
inx
cpx #4
bne :-
; clear all macros (point to macro 0)
lda data_music_macro_low
ldy data_music_macro_high
ldx #0
:
sta pointer_macro+0, x
tya
sta pointer_macro+1, x
lda data_music_macro_low
inx
inx
cpx #32
bne :-
lda #0
ldx #0
:
sta player_macro_pos, x
inx
cpx #16
bne :-
rts
load_sfx:
tay
lda #0
sta player_next_sound, x