-
Notifications
You must be signed in to change notification settings - Fork 1
/
vgm-player.asm
1225 lines (1019 loc) · 32.5 KB
/
vgm-player.asm
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
; ****************************************************************************
; * Video Game Music Player
; * Author Daniel Tremblay
; * Code written for the C256 Foenix retro computer
; * Permission is granted to reuse this code to create your own games
; * for the C256 Foenix.
; * Copyright Daniel Tremblay 2020
; * This code is provided without warranty.
; * Please attribute credits to Daniel Tremblay if you reuse.
; ****************************************************************************
; * To play VGM files in your games, include this file first.
; * Next, in your interrupt handler, enable timer0.
; * In the TIMER0 interrupt handler, call the VGM_WRITE_REGISTER subroutine.
; * In your game code, set the SONG_START to the beginning of your VGM file.
; * Call VGM_SET_SONG_POINTERS, this will initialize the other register.
; * Finally, call VGM_INIT_TIMER0 to initialize TIMER0.
; * Chips supported at this time are:
; * - SN76489 (PSG)
; * - YM2612 (OPN2)
; * - YM2151 (OPM)
; * - YM262 (OPL3)
; * - YM3812 (OPL2)
; ****************************************************************************
.cpu "65816"
.include "macros_inc.asm"
.include "base.asm"
.include "bank_00_inc.asm"
.include "interrupt_def.asm"
.include "keyboard_def.asm"
.include "vicky_def.asm"
.include "kernel_inc.asm"
.include "timer_def.asm"
.include "math_def.asm"
OPM_BASE_ADDRESS = $AFF000
PSG_BASE_ADDRESS = $AFF100
OPN2_BASE_ADDRESS = $AFF200
OPL3_BASE_ADRESS = $AFE600
; Important VGM file offsets
VGM_VERSION = $8 ; 32-bits
SN_CLOCK = $C ; 32-bits
LOOP_OFFSET = $1C ; 32-bits
YM_OFFSET = $2C ; 32-bits
OPM_CLOCK = $30 ; 32-bits
VGM_OFFSET = $34 ; 32-bits
; VGM Registers
MIN_VERSION = $77 ; 1 byte
DISPLAY_OFFSET = $78 ; 2 bytes
MSG_PTR = $7A ; 3 bytes
DATA_STREAM_CNT = $7D ; 2 byte
COMMAND = $7F ; 1 byte
SONG_START = $80 ; 4 bytes
CURRENT_POSITION = $84 ; 4 bytes
WAIT_CNTR = $88 ; 2 bytes
PCM_OFFSET = $8A ; 4 bytes
AY_3_8910_A = $90 ; 2 bytes
AY_3_8910_B = $92 ; 2 bytes
AY_3_8910_C = $94 ; 2 bytes
AY_3_8910_N = $96 ; 2 bytes
AY_BASE_AMPL = $98 ; 1 byte
DATA_STREAM_TBL = $8000 ; each entry is 4 bytes
* = $160000
VGM_START
.as
.xs
setas
setxl
JSL CLRSCREEN
; disable the cursor
LDA #0
STA VKY_TXT_CURSOR_CTRL_REG
LDX #0
STX DATA_STREAM_CNT
LDX #$A000
STX DISPLAY_OFFSET
LDA #`RESET_MSG
STA MSG_PTR+2
; load the music
LDA #`VGM_FILE
STA CURRENT_POSITION + 2
STA SONG_START + 2
setal
LDA #<>VGM_FILE
STA SONG_START
setas
; first three bytes should be VGM - if not, maybe it's compressed?
LDA #0
STA COMMAND
JSR CHECK_VGM_FILE
LDA COMMAND ; if the command is still 0, it's a vgm file
BNE INVALID_FILE
JSR VGM_SET_SONG_POINTERS
JSR VGM_INIT_TIMER0
LDA #$FF
STA @lINT_EDGE_REG0
STA @lINT_EDGE_REG1
STA @lINT_EDGE_REG2
STA @lINT_EDGE_REG3
LDA #~( FNX0_INT02_TMR0 ) ; | FNX0_INT00_SOF) ; uncomment this to debug in the IDE
STA @lINT_MASK_REG0
LDA #$FF
STA @lINT_MASK_REG1
STA @lINT_MASK_REG2
STA @lINT_MASK_REG3
JSR VGM_WRITE_REGISTER ; the initial load of register should set the timerA
CLI
; loop, waiting for interrupts
LOOP
BRA LOOP
INVALID_FILE
setal
LDA #<>INVALID_FILE_MSG
STA MSG_PTR
setas
JSR DISPLAY_MSG
BRA LOOP
RESET_MSG .text 'Restarting song:',0
LOOPING_MSG .text 'Looping song:',0
DATA_BLOCK_MSG .text 'Reading Data Block:',0
INVALID_FILE_MSG .text 'Invalid file type:', 0
UNK_CMD1_MSG .text 'Unknown 1-Byte Command:',0
UNK_CMD2_MSG .text 'Unknown 2-Byte Command:',0
UNK_CMD3_MSG .text 'Unknown 3-Byte Command:',0
UNK_CMD4_MSG .text 'Unknown 4-Byte Command:',0
HEX_VALUES .text '0123456789ABCDEF'
; *******************************************************************
; * First three bytes of the file must be VGM
; *******************************************************************
CHECK_VGM_FILE
.as
LDY #0
LDA [SONG_START],Y
INY
CMP #'V'
BNE CHECK_FAILED
LDA [SONG_START],Y
INY
CMP #'g'
BNE CHECK_FAILED
LDA [SONG_START],Y
INY
CMP #'m'
BNE CHECK_FAILED
; get the file version
LDY #8
LDA [SONG_START],Y
STA MIN_VERSION
RTS
CHECK_FAILED
LDA #1
STA COMMAND
RTS
DISPLAY_MSG
.as
PHY
LDX DISPLAY_OFFSET
LDY #0
LDA #0
XBA
DISPLAY_NEXT
LDA #$2D
STA $AF2000,X
LDA [MSG_PTR],Y
STA $AF0000,X
; write the color for the characters - green
INX
INY
CMP #0
BNE DISPLAY_NEXT
LDA #$2D
STA $AF2000,X
LDA COMMAND
AND #$F0
LSR A
LSR A
LSR A
LSR A
TXY
TAX
LDA HEX_VALUES,X
TYX
STA $AF0000,X
INX
LDA #$2D
STA $AF2000,X
LDA COMMAND
AND #$F
TXY
TAX
LDA HEX_VALUES,X
TYX
STA $AF0000,X
setal
LDA DISPLAY_OFFSET
CLC
ADC #$80
STA DISPLAY_OFFSET
setas
XBA
CMP #$BF
BLT DISPLAY_DONE
XBA
BNE FIRST_COL
LDX #$A020 ; create a second column
STX DISPLAY_OFFSET
BRA DISPLAY_DONE
FIRST_COL
LDX #$A000
STX DISPLAY_OFFSET
JSL CLRSCREEN
DISPLAY_DONE
PLY
RTS
increment_long_addr .macro
setal
INC \1
BNE increment_done
INC \1 + 2
increment_done
setas
.endm
; *******************************************************************
; * Interrupt driven sub-routine.
; *******************************************************************
VGM_WRITE_REGISTER
.as
LDX WAIT_CNTR
CPX #0
BEQ READ_COMMAND
DEX
STX WAIT_CNTR
RTS
READ_COMMAND
LDA #0
XBA
LDA [CURRENT_POSITION]
STA COMMAND
increment_long_addr CURRENT_POSITION
AND #$F0
LSR A
LSR A
LSR A
TAX
JMP (VGM_COMMAND_TABLE,X)
VGM_LOOP_DONE
RTS
; *******************************************************************
; * Command Table
; *******************************************************************
VGM_COMMAND_TABLE
.word <>INVALID_COMMAND ;0
.word <>INVALID_COMMAND ;1
.word <>INVALID_COMMAND ;2
.word <>SKIP_BYTE_CMD ;3 - reserved - not implemented
.word <>SKIP_BYTE_CMD ;4 - not implemented
.word <>WRITE_YM_CMD ;5 - YM*
.word <>WAIT_COMMANDS ;6
.word <>WAIT_N_1 ;7
.word <>YM2612_SAMPLE ;8
.word <>DAC_STREAM ;9
.word <>AY8910 ;A - AY8910
.word <>SKIP_TWO_BYTES ;B - not implemented
.word <>SKIP_THREE_BYTES;C - not implemented
.word <>SKIP_THREE_BYTES;D - not implemented
.word <>SEEK_OFFSET ;E - not implemented
.word <>SKIP_FOUR_BYTES ;F - not implemented
INVALID_COMMAND
.as
JMP VGM_WRITE_REGISTER
SKIP_BYTE_CMD
.as
setal
LDA #<>UNK_CMD1_MSG
STA MSG_PTR
setas
JSR DISPLAY_MSG
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
SKIP_TWO_BYTES
.as
setal
LDA #<>UNK_CMD2_MSG
STA MSG_PTR
setas
JSR DISPLAY_MSG
setal
INC CURRENT_POSITION
BNE s2_1
INC CURRENT_POSITION + 2
s2_1
INC CURRENT_POSITION
BNE s2_2
INC CURRENT_POSITION + 2
s2_2
setas
JMP VGM_WRITE_REGISTER
SKIP_THREE_BYTES
.as
setal
LDA #<>UNK_CMD3_MSG
STA MSG_PTR
setas
JSR DISPLAY_MSG
setal
INC CURRENT_POSITION
BNE s3_1
INC CURRENT_POSITION + 2
s3_1
INC CURRENT_POSITION
BNE s3_2
INC CURRENT_POSITION + 2
s3_2
INC CURRENT_POSITION
BNE s3_3
INC CURRENT_POSITION + 2
s3_3
setas
JMP VGM_WRITE_REGISTER
SEEK_OFFSET
.as
LDA COMMAND
CMP #$E0
BNE SKIP_FOUR_BYTES
; read 4 bytes, add them to the databank 0 offset
; and store in the PCM_OFFSET
setal
LDA [CURRENT_POSITION]
STA ADDER_A
increment_long_addr CURRENT_POSITION
increment_long_addr CURRENT_POSITION
setal
LDA [CURRENT_POSITION]
STA ADDER_A + 2
increment_long_addr CURRENT_POSITION
increment_long_addr CURRENT_POSITION
setal
LDA DATA_STREAM_TBL
STA ADDER_B
LDA DATA_STREAM_TBL + 2
STA ADDER_B + 2
LDA ADDER_R
STA PCM_OFFSET
LDA ADDER_R + 2
STA PCM_OFFSET + 2
setas
JMP VGM_LOOP_DONE
SKIP_FOUR_BYTES
.as
setal
LDA #<>UNK_CMD4_MSG
STA MSG_PTR
setas
JSR DISPLAY_MSG
setal
INC CURRENT_POSITION
BNE s4_1
INC CURRENT_POSITION + 2
s4_1
INC CURRENT_POSITION
BNE s4_2
INC CURRENT_POSITION + 2
s4_2
INC CURRENT_POSITION
BNE s4_3
INC CURRENT_POSITION + 2
s4_3
INC CURRENT_POSITION
BNE s4_4
INC CURRENT_POSITION + 2
s4_4
setas
JMP VGM_WRITE_REGISTER
; we need to combine R1 and R0 together before we send
; the data to the SN76489
AY8910
.as
LDA #$F
STA AY_BASE_AMPL
LDA COMMAND
CMP #$A0
BEQ AY_COMMAND
JMP SKIP_TWO_BYTES ; when mixing with the YM2612, the SN76489 is just too load.
AY_COMMAND
; the second byte is the register
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
CMP #0 ; Register 0 fine
BNE AY_R1
LDA AY_3_8910_A
CMP #8
BLT R0_FINE
LDA #$87
STA PSG_BASE_ADDRESS
LDA #$3F
STA PSG_BASE_ADDRESS
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
R0_FINE
XBA
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
setal
LSR A ; drop the LSB
setas
PHA
AND #$F
ORA #$80
STA PSG_BASE_ADDRESS
PLA
setal
LSR A
LSR A
LSR A
LSR A
setas
AND #$3F ; 6 bits
STA PSG_BASE_ADDRESS
JMP VGM_WRITE_REGISTER
AY_R1 CMP #1
BNE AY_R2
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
AND #$F
STA AY_3_8910_A
JMP VGM_WRITE_REGISTER
AY_R2 CMP #2
BNE AY_R3
LDA AY_3_8910_B
CMP #8
BLT R1_FINE
LDA #$A7
STA PSG_BASE_ADDRESS
LDA #$3F
STA PSG_BASE_ADDRESS
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
R1_FINE
XBA
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
setal
LSR A ; drop the LSB
setas
PHA
AND #$F
ORA #$A0
STA PSG_BASE_ADDRESS
PLA
setal
LSR A
LSR A
LSR A
LSR A
setas
AND #$3F ; 6 bits
STA PSG_BASE_ADDRESS
JMP VGM_WRITE_REGISTER
AY_R3 CMP #3
BNE AY_R4
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
AND #$F
STA AY_3_8910_B
JMP VGM_WRITE_REGISTER
AY_R4 CMP #4
BNE AY_R5
LDA AY_3_8910_C
CMP #8
BLT R2_FINE
LDA #$C7
STA PSG_BASE_ADDRESS
LDA #$3F
STA PSG_BASE_ADDRESS
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
R2_FINE
XBA
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
setal
LSR A ; drop the LSB
setas
PHA
AND #$F
ORA #$C0
STA PSG_BASE_ADDRESS
PLA
setal
LSR A
LSR A
LSR A
LSR A
setas
AND #$3F ; 6 bits
STA PSG_BASE_ADDRESS
JMP VGM_WRITE_REGISTER
AY_R5 CMP #5
BNE AY_R10
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
AND #$F
STA AY_3_8910_C
JMP VGM_WRITE_REGISTER
AY_R10
CMP #8
BNE AY_R11
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
EOR AY_BASE_AMPL
AND #$F
ORA #$90
STA PSG_BASE_ADDRESS
JMP VGM_WRITE_REGISTER
AY_R11
CMP #9
BNE AY_R12
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
EOR AY_BASE_AMPL
AND #$F
ORA #$B0
STA PSG_BASE_ADDRESS
JMP VGM_WRITE_REGISTER
AY_R12
CMP #10
BNE AY_R15
LDA [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
EOR AY_BASE_AMPL
AND #$F
ORA #$D0
STA PSG_BASE_ADDRESS
JMP VGM_WRITE_REGISTER
AY_R15
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
; *******************************************************************
; * YM Commands
; *******************************************************************
WRITE_YM_CMD
.as
LDA COMMAND
CMP #$50
BNE CHK_YM2413
LDA [CURRENT_POSITION]
STA PSG_BASE_ADDRESS
increment_long_addr CURRENT_POSITION
JMP VGM_LOOP_DONE ; for some reason, this chip needs more time between writes
CHK_YM2413
CMP #$51
BNE CHK_YM2612_P0
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPL3_BASE_ADRESS,X
;STA @lOPN2_BASE_ADDRESS,X ; this probably won't work
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM2612_P0
CMP #$52
BNE CHK_YM2612_P1
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPN2_BASE_ADDRESS,X
increment_long_addr CURRENT_POSITION
JMP VGM_LOOP_DONE ; for some reason, this chip needs more time between writes
CHK_YM2612_P1
CMP #$53
BNE CHK_YM2151
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPN2_BASE_ADDRESS + $100,X
increment_long_addr CURRENT_POSITION
JMP VGM_LOOP_DONE ; for some reason, this chip needs more time between writes
CHK_YM2151
CMP #$54
BNE CHK_YM2203
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPM_BASE_ADDRESS,X
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM2203
CMP #$55
BNE CHK_YM2608_P0
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
;STA @lOPM_BASE_ADDRESS,X
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM2608_P0
CMP #$56
BNE CHK_YM2608_P1
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
CMP #$10 ; if the register is 0 to $1F, process as SSG
BGE YM2608_FM
JMP AY8910
YM2608_FM
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPN2_BASE_ADDRESS,X
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM2608_P1
CMP #$57
BNE CHK_YM2610_P0
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPN2_BASE_ADDRESS,X
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM2610_P0
CMP #$58
BNE CHK_YM2610_P1
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
CMP #$10 ; if the register is 0 to $1F, process as SSG
BGE YM2610_FM
JMP AY8910
YM2610_FM
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPN2_BASE_ADDRESS,X
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM2610_P1
CMP #$59
BNE CHK_YM3812
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPN2_BASE_ADDRESS + $100,X
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM3812
CMP #$5A
BNE CHK_YM262_P0
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPL3_BASE_ADRESS,X
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM262_P0
CMP #$5E
BNE CHK_YM262_P1
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPL3_BASE_ADRESS,X
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
CHK_YM262_P1
CMP #$5F
BNE YM_DONE
; the second byte is the register
LDA #0
XBA
LDA [CURRENT_POSITION]
TAX
increment_long_addr CURRENT_POSITION
; the third byte is the value to write in the register
LDA [CURRENT_POSITION]
STA @lOPL3_BASE_ADRESS+ $100,X
increment_long_addr CURRENT_POSITION
YM_DONE
JMP VGM_WRITE_REGISTER
; *******************************************************************
; * Wait Commands
; *******************************************************************
WAIT_COMMANDS
.as
LDA COMMAND
CMP #$61
BNE CHK_WAIT_60th
setal
LDA [CURRENT_POSITION]
TAX
STX WAIT_CNTR
setas
increment_long_addr CURRENT_POSITION
increment_long_addr CURRENT_POSITION
JMP VGM_LOOP_DONE
CHK_WAIT_60th
CMP #$62
BNE CHK_WAIT_50th
LDX #$2df
STX WAIT_CNTR
JMP VGM_LOOP_DONE
CHK_WAIT_50th
CMP #$63
BNE CHK_END_SONG
LDX #$372
STX WAIT_CNTR
JMP VGM_LOOP_DONE
CHK_END_SONG
CMP #$66 ; end of song
BNE CHK_DATA_BLOCK
JSR VGM_SET_LOOP_POINTERS
JMP VGM_LOOP_DONE
CHK_DATA_BLOCK
CMP #$67
BNE DONE_WAIT
JSR READ_DATA_BLOCK
DONE_WAIT
JMP VGM_LOOP_DONE
; *******************************************************************
; * Wait N+1 Commands
; *******************************************************************
WAIT_N_1
.as
LDA #0
XBA
LDA COMMAND
AND #$F
TAX
INX ; $7n where we wait n+1
STX WAIT_CNTR
JMP VGM_LOOP_DONE
; *******************************************************************
; * Play Samples and wait N
; *******************************************************************
YM2612_SAMPLE
.as
; write directly to YM2612 DAC then wait n
; load a value from database
LDA [PCM_OFFSET]
STA OPN2_BASE_ADDRESS + $2A
; increment PCM_OFFSET
setal
LDA PCM_OFFSET
INC A
STA PCM_OFFSET
BCC YMS_WAIT
LDA PCM_OFFSET + 2
INC A
STA PCM_OFFSET + 2
YMS_WAIT
setas
LDA #0
XBA
LDA COMMAND
; this is the wait part
AND #$F
TAX
STX WAIT_CNTR
;CPX #0
;BNE YMS_NOT_ZERO
;RTS
YMS_NOT_ZERO
JMP VGM_WRITE_REGISTER
; *******************************************************************
; * Don't know yet
; *******************************************************************
DAC_STREAM
.as
;JMP VGM_LOOP_DONE
JMP VGM_WRITE_REGISTER
VGM_SET_SONG_POINTERS
.as
setal
LDA #<>RESET_MSG
STA MSG_PTR
setas
JSR DISPLAY_MSG
setal
LDA #0
STA WAIT_CNTR
LDA SONG_START + 2
STA CURRENT_POSITION + 2
setas
LDA MIN_VERSION
CMP #$50
BLT OLD_VERSION
; add the start offset
setal
CLC
LDY #VGM_OFFSET
LDA [SONG_START],Y
ADC #VGM_OFFSET
ADC SONG_START