-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.s
3036 lines (2969 loc) · 42.5 KB
/
util.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
.segment "util"
.autoimport on
.importzp sp, sreg, regsave, regbank
.importzp tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
.macpack longbranch
; ----------------------------------
; exports
; ----------------------------------
.EXPORT func_minikey, func_init_sid, func_prepare_adsrs, func_prepare_sid
.EXPORT func_decode_note, func_music_loop_iteration, func_update_decoded_music
.EXPORT func_music_loop_preparation, func_prepare_waveform_control_registers
.EXPORT func_prepare_song, func_calculate_note_frequency
.EXPORT _prepare_song, _draw_sprintf, _dstr, _draw_text
.export _screen_loc
.export _rel_loc
.export _gtmpw
.export _gtmpw2
.export _gtmpw3
.export _gtmpw4
.export _a
.export _b
.export _gk
.export _gtmp
.export _num_repairs
.export _snd_trigger, _snd_idx, _snd_delay, _sky_idx
; .EXPORT _fq, _dr, _oc, _nt, _pnm, _delay_cnt, _use_bug, _k, _v1, _v2, _v3
; ----------------------------------
; variables
; ----------------------------------
_screen_loc:
.res 2,$00
_rel_loc:
.res 2,$00
_gtmpw:
.res 2,$00
_gtmpw2:
.res 2,$00
_gtmpw3:
.res 2,$00
_gtmpw4:
.res 2,$00
_a:
.res 1,$00
_b:
.res 1,$00
_gk:
.res 1,$00
_gtmp:
.res 1,$00
_num_repairs:
.res 1,$00
_dstr:
.res 40,$00
_snd_trigger:
.byte $00
_snd_idx:
.byte $00
_snd_delay:
.byte $00
_sky_idx:
.word $0000
; define frequency table
_fq:
.word 34334, 36376, 38539, 40830 ; C, C#, D, D#
.word 43258, 45830, 48556, 51443 ; E, F, F#, G
.word 54502, 57743, 61176, 64814 ; G#, A, A#, B
_dr: .word 0 ; duration of currently decoded note-data
_oc: .word 0 ; octave of currently decoded note-data
_nt: .word 0 ; note-in-octave of currently decoded note-data
_pnm: .res 2, $00 ; pointer to current nm
_delay_cnt:
.byte $03
_use_bug:
.byte $01
_k:
.res 1,$00
_v1:
.res 2,$00
_v2:
.res 2,$00
_v3:
.res 2,$00
_autogateflag:
.word $0001
.word $0001
.word $0001
_sub_idx_v1:
.word $0000
.res 10,$00
_sub_idx_v2:
.word $0000
.res 10,$00
_sub_idx_v3:
.word $0000
.res 10,$00
_psub_idxs:
.addr _sub_idx_v1
.addr _sub_idx_v2
.addr _sub_idx_v3
_psub_idx:
.res 2,$00
_pstart_idx:
.res 2,$00
_start_idx:
.word $FFFF
.word $FFFF
.word $FFFF
_ph:
.res 2,$00
_pl:
.res 2,$00
_pc:
.res 2,$00
_h:
.res 3,$00
_l:
.res 3,$00
_c:
.res 3,$00
_pcaller_idx:
.res 2,$00
_caller_idx:
.word $FFFF
.res 4,$00
_v:
.byte $11
.byte $41
.byte $81
_marker_pos:
.word $0000
_repeat_to_marker_pos:
.word $0000
_repeat_to_marker_count:
.word $0000
_repeat_to_beginning_pos:
.word $0000
_rptcnt:
.word $0000
_nm:
.word $0000
.res 4,$00
_pwa:
.res 2,$00
_pwb:
.res 2,$00
_wa:
.byte $00
.res 2,$00
_wb:
.byte $00
.res 2,$00
_fr:
.dword $00000000
_hf:
.res 2,$00
_lf:
.res 2,$00
_bar_idx:
.byte $00
_music_idx:
.word $0000
_music_t:
.byte $00
_j:
.res 2,$00
_bar_length:
.byte $48
_cmd:
.res 2,$00
_data:
.res 2,$00
_pidx:
.res 2,$00
_idx:
.word $0000
.res 4,$00
_premain:
.res 2,$00
_remain:
.byte $00
.res 2,$00
_found_note:
.byte $00
_within_sub:
.byte $00
_ppvoice:
.res 2,$00
_pvoice:
.res 2,$00
_pptr_voice:
.res 2,$00
_ptr_voice:
.res 6,$00
_pvoices:
.res 6,$00
_pval:
.res 2,$00
_sid_offset:
.byte $00
.byte $07
.byte $0E
; ----------------------------------
; functions
; ----------------------------------
func_decode_note:
;
; dr = *pnm >> 7;
;
lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
ldy #$01
lda (ptr1),y
tax
dey
lda (ptr1),y
jsr asrax4
jsr asrax3
sta _dr
stx _dr+1
;
; oc = (*pnm >> 4) & 0x07;
;
lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
iny
lda (ptr1),y
tax
dey
lda (ptr1),y
jsr asrax4
ldx #$00
and #$07
sta _oc
stx _oc+1
;
; nt = *pnm & 0x0F;
;
lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
lda (ptr1),y
and #$0F
sta _nt
stx _nt+1
;
; }
;
rts
; ---------------------------------------------------------------
; void __near__ calculate_note_frequency (void)
; ---------------------------------------------------------------
func_calculate_note_frequency:
.proc _calculate_note_frequency: near
;
; fr = fq[nt];
;
lda _nt
ldx _nt+1
stx tmp1
asl a
rol tmp1
clc
adc #<(_fq)
sta ptr1
lda tmp1
adc #>(_fq)
sta ptr1+1
ldy #$01
lda (ptr1),y
sta _fr+1
dey
lda (ptr1),y
sta _fr
sty _fr+2
sty _fr+3
;
; if (oc != 7)
;
lda _oc+1
bne L005D
lda _oc
cmp #$07
beq L0034
;
; for (j = 6; j >= oc; j--)
;
L005D: ldx #$00
lda #$06
L005F: sta _j
stx _j+1
lda _j
cmp _oc
lda _j+1
sbc _oc+1
bvs L005C
eor #$80
L005C: bpl L0034
;
; fr = fr >> 1;
;
lda _fr+3
sta sreg+1
lda _fr+2
sta sreg
ldx _fr+1
lda _fr
jsr shreax1
sta _fr
stx _fr+1
ldy sreg
sty _fr+2
ldy sreg+1
sty _fr+3
;
; for (j = 6; j >= oc; j--)
;
lda _j
ldx _j+1
sec
sbc #$01
bcs L005F
dex
jmp L005F
;
; hf = fr >> 8;
;
L0034: lda _fr+2
sta sreg
ldx _fr+1
stx _hf
ldx sreg
stx _hf+1
;
; lf = fr & 0xff;
;
lda #$00
sta _lf+1
lda _fr
sta _lf
;
; }
;
rts
.endproc
; ---------------------------------------------------------------
; void __near__ prepare_song (__near__ int *, __near__ int *, __near__ int *, unsigned char, unsigned char, unsigned char)
; ---------------------------------------------------------------
func_prepare_song:
.proc _prepare_song: near
;
; {
;
jsr pusha
;
; v1 = pv1;
;
ldy #$08
lda (sp),y
sta _v1+1
dey
lda (sp),y
sta _v1
;
; v2 = pv2;
;
dey
lda (sp),y
sta _v2+1
dey
lda (sp),y
sta _v2
;
; v3 = pv3;
;
dey
lda (sp),y
sta _v3+1
dey
lda (sp),y
sta _v3
;
; bar_length = p_bar_length;
;
dey
lda (sp),y
sta _bar_length
;
; delay_cnt = p_delay_cnt;
;
dey
lda (sp),y
sta _delay_cnt
;
; use_bug = p_use_bug;
;
dey
lda (sp),y
sta _use_bug
;
; }
;
ldy #$09
jmp addysp
.endproc
; ---------------------------------------------------------------
; void __near__ prepare_waveform_control_registers (void)
; ---------------------------------------------------------------
func_prepare_waveform_control_registers:
.proc _prepare_waveform_control_registers: near
;
; *pwa = v[k]; // set the waveform control to proper voice
;
lda _pwa+1
sta ptr1+1
lda _pwa
sta ptr1
ldy _k
lda _v,y
ldy #$00
sta (ptr1),y
;
; *pwb = *pwa - 1; // turn the gate-bit (bit0) of the voice's control-register off (releases the note from the sustain)
;
lda _pwb+1
sta sreg+1
lda _pwb
sta sreg
lda _pwa+1
sta ptr1+1
lda _pwa
sta ptr1
lda (ptr1),y
sec
sbc #$01
sta (sreg),y
;
; if (*pnm < 0) // if encoded note value is negative, this equates to silence, so set waveform controls to 0.
;
lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
iny
lda (ptr1),y
tax
cpx #$80
bcc L0058
;
; *pnm = -*pnm; // invert it, and what remains equates to a duration value
;
lda _pnm+1
sta sreg+1
lda _pnm
sta sreg
lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
lda (ptr1),y
tax
dey
lda (ptr1),y
jsr negax
sta (sreg),y
iny
txa
sta (sreg),y
;
; *pwa = 0;
;
lda _pwa+1
sta ptr1+1
lda _pwa
sta ptr1
lda #$00
dey
sta (ptr1),y
;
; *pwb = 0;
;
lda _pwb+1
sta ptr1+1
lda _pwb
sta ptr1
tya
sta (ptr1),y
;
; }
;
L0058: rts
.endproc
; ---------------------------------------------------------------
; void __near__ music_loop_preparation (void)
; ---------------------------------------------------------------
func_music_loop_preparation:
.proc _music_loop_preparation: near
;
; rptcnt = repeat_to_marker_count;
;
lda _repeat_to_marker_count+1
sta _rptcnt+1
lda _repeat_to_marker_count
sta _rptcnt
;
; start_idx[0] = -1;
;
lda #$FF
sta _start_idx
sta _start_idx+1
;
; start_idx[1] = -1;
;
sta _start_idx+2
sta _start_idx+2+1
;
; start_idx[2] = -1;
;
sta _start_idx+4
sta _start_idx+4+1
;
; caller_idx[0] = -1;
;
sta _caller_idx
sta _caller_idx+1
;
; caller_idx[1] = -1;
;
sta _caller_idx+2
sta _caller_idx+2+1
;
; caller_idx[2] = -1;
;
sta _caller_idx+4
sta _caller_idx+4+1
;
; bar_idx = 0;
;
lda #$00
sta _bar_idx
;
; music_idx = 0;
;
tax
sta _music_idx
sta _music_idx+1
;
; music_t = 0;
;
sta _music_t
;
; marker_pos = 0;
;
sta _marker_pos
sta _marker_pos+1
;
; repeat_to_marker_pos = 0;
;
sta _repeat_to_marker_pos
sta _repeat_to_marker_pos+1
;
; repeat_to_marker_count = 0;
;
sta _repeat_to_marker_count
sta _repeat_to_marker_count+1
;
; repeat_to_beginning_pos = 0;
;
sta _repeat_to_beginning_pos
sta _repeat_to_beginning_pos+1
;
; rptcnt = 0;
;
sta _rptcnt
sta _rptcnt+1
;
; idx[0] = 0;
;
sta _idx
sta _idx+1
;
; idx[1] = 0;
;
sta _idx+2
sta _idx+2+1
;
; idx[2] = 0;
;
sta _idx+4
sta _idx+4+1
;
; remain[0] = 0;
;
sta _remain
;
; remain[1] = 0;
;
sta _remain+1
;
; remain[2] = 0;
;
sta _remain+2
;
; found_note = 0;
;
sta _found_note
;
; within_sub = 0;
;
sta _within_sub
;
; autogateflag[0] = 1;
;
lda #$01
sta _autogateflag
stx _autogateflag+1
;
; autogateflag[1] = 1;
;
sta _autogateflag+2
stx _autogateflag+2+1
;
; autogateflag[2] = 1;
;
sta _autogateflag+4
stx _autogateflag+4+1
;
; v[0] = C_TRIANGLE + C_GATE;
;
lda #$11
sta _v
;
; v[1] = C_PULSE + C_GATE;
;
lda #$41
sta _v+1
;
; v[2] = C_RANDOM + C_GATE;
;
lda #$81
sta _v+2
;
; pvoices[0] = v1;
;
lda _v1+1
sta _pvoices+1
lda _v1
sta _pvoices
;
; pvoices[1] = v2;
;
lda _v2+1
sta _pvoices+2+1
lda _v2
sta _pvoices+2
;
; pvoices[2] = v3;
;
lda _v3+1
sta _pvoices+4+1
lda _v3
sta _pvoices+4
;
; ptr_voice[0] = v1;
;
lda _v1+1
sta _ptr_voice+1
lda _v1
sta _ptr_voice
;
; ptr_voice[1] = v2;
;
lda _v2+1
sta _ptr_voice+2+1
lda _v2
sta _ptr_voice+2
;
; ptr_voice[2] = v3;
;
lda _v3+1
sta _ptr_voice+4+1
lda _v3
sta _ptr_voice+4
;
; Poke(_SID_+10, 8); // (0xd40a) Set high pulse width for voice 2
;
lda #$08
sta $D40A
;
; Poke(_SID_+22, 128); // (0xd416) Set high frequency for filter cutoff
;
lda #$80
sta $D416
;
; }
;
rts
.endproc
; ---------------------------------------------------------------
; int __near__ update_decoded_music (void)
; ---------------------------------------------------------------
func_update_decoded_music:
.proc _update_decoded_music: near
;
; pidx = idx;
;
lda #>(_idx)
sta _pidx+1
lda #<(_idx)
sta _pidx
;
; premain = remain;
;
lda #>(_remain)
sta _premain+1
lda #<(_remain)
sta _premain
;
; pnm = nm;
;
lda #>(_nm)
sta _pnm+1
lda #<(_nm)
sta _pnm
;
; pstart_idx = start_idx;
;
lda #>(_start_idx)
sta _pstart_idx+1
lda #<(_start_idx)
sta _pstart_idx
;
; pcaller_idx = caller_idx;
;
lda #>(_caller_idx)
sta _pcaller_idx+1
lda #<(_caller_idx)
sta _pcaller_idx
;
; ppvoice = pvoices;
;
lda #>(_pvoices)
sta _ppvoice+1
lda #<(_pvoices)
sta _ppvoice
;
; pptr_voice = ptr_voice;
;
lda #>(_ptr_voice)
sta _pptr_voice+1
lda #<(_ptr_voice)
sta _pptr_voice
;
; psub_idx = psub_idxs;
;
lda #>(_psub_idxs)
sta _psub_idx+1
lda #<(_psub_idxs)
sta _psub_idx
;
; for (k = 0; k <= 2; k++)
;
lda #$00
sta _k
L01EA: lda _k
cmp #$03
jcs L0080
;
; found_note = 0;
;
lda #$00
sta _found_note
;
; within_sub = 0;
;
sta _within_sub
;
; pvoice = *ppvoice;
;
lda _ppvoice+1
sta ptr1+1
lda _ppvoice
sta ptr1
ldy #$01
lda (ptr1),y
sta _pvoice+1
dey
lda (ptr1),y
sta _pvoice
;
; pval = *pptr_voice;
;
lda _pptr_voice+1
sta ptr1+1
lda _pptr_voice
sta ptr1
iny
lda (ptr1),y
sta _pval+1
dey
lda (ptr1),y
sta _pval
;
; if (*premain == 0)
;
lda _premain+1
sta ptr1+1
lda _premain
sta ptr1
lda (ptr1),y
jne L0092
;
; while (!found_note)
;
jmp L0156
;
; *pnm = *pval;
;
L0091: lda _pnm
ldx _pnm+1
jsr pushax
lda _pval+1
sta ptr1+1
lda _pval
sta ptr1
ldy #$01
lda (ptr1),y
tax
dey
lda (ptr1),y
jsr staxspidx
;
; if (*pnm < 0) // is this a rest note?
;
lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
iny
lda (ptr1),y
tax
cpx #$80
bcc L0097
;
; if (!within_sub)
;
lda _within_sub
jne L0157
;
; found_note = 1;
;
sty _found_note
;
; if (*pstart_idx == -1)
;
lda _pstart_idx+1
sta ptr1+1
lda _pstart_idx
sta ptr1
lda (ptr1),y
tax
dey
lda (ptr1),y
cpx #$FF
jne L0092
cmp #$FF
jne L0092
;
; *pstart_idx = (int)pval;
;
lda _pstart_idx+1
sta ptr1+1
lda _pstart_idx
sta ptr1
lda _pval
sta (ptr1),y
iny
lda _pval+1
sta (ptr1),y
;
; break;
;
jmp L0092
;
; else if (*pnm == REPEAT_TO_BEGINNING)
;
L0097: lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
lda (ptr1),y
tax
dey
lda (ptr1),y
cpx #$40
bne L00A5
cmp #$00
bne L00A5
;
; pval = (int*)*pstart_idx; // back to start pointer of track
;
lda _pstart_idx+1
sta ptr1+1
lda _pstart_idx
sta ptr1
iny
lda (ptr1),y
sta _pval+1
dey
lda (ptr1),y
sta _pval
;
; *premain = 0;
;
lda _premain+1
sta ptr1+1
lda _premain
sta ptr1
tya
sta (ptr1),y
;
; else if (*pnm & SPECIAL_TOKEN)
;
jmp L0156
L00A5: lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
iny
lda (ptr1),y
and #$20
jeq L00AD
;
; cmd = *pnm & 0x1F;
;
lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
dey
lda (ptr1),y
ldx #$00
and #$1F
sta _cmd
stx _cmd+1
;
; data = (*pnm >> 5) & 0xff;
;
lda _pnm+1
sta ptr1+1
lda _pnm
sta ptr1
iny
lda (ptr1),y
tax
dey
lda (ptr1),y
jsr asrax4
jsr asrax1
ldx #$00
sta _data
stx _data+1
;
; pval++;