-
Notifications
You must be signed in to change notification settings - Fork 1
/
vgm_player.asm
914 lines (757 loc) · 24.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
; ****************************************************************************
; * 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_TIMERS to initialize TIMER0 and TIMER1.
; * Chips supported at this time are:
; * - SN76489 (PSG)
; * - YM2612 (OPN2)
; * - YM2151 (OPM)
; * - YM262 (OPL3)
; * - YM3812 (OPL2)
; ****************************************************************************
; Important 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
COMMAND = $7F ; 1 byte
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
DATA_STREAM_CNT = $7D ; 2 byte
DATA_STREAM_TBL = $8000 ; each entry is 4 bytes
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
increment_long_addr CURRENT_POSITION
JMP VGM_WRITE_REGISTER
SKIP_TWO_BYTES
.as
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
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
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 COMMAND
CMP #$A0
BEQ AY_COMMAND
JMP SKIP_TWO_BYTES
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 #$F
SEC
SBC [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
AND #$F
ORA #$90
STA PSG_BASE_ADDRESS
JMP VGM_WRITE_REGISTER
AY_R11
CMP #9
BNE AY_R12
LDA #$F
SEC
SBC [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
AND #$F
ORA #$B0
STA PSG_BASE_ADDRESS
JMP VGM_WRITE_REGISTER
AY_R12
CMP #10
BNE AY_R15
LDA #$F
SEC
SBC [CURRENT_POSITION]
increment_long_addr CURRENT_POSITION
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
; add the start offset
setal
LDA #0
STA WAIT_CNTR
LDA SONG_START + 2
STA CURRENT_POSITION + 2
CLC
LDY #VGM_OFFSET
LDA [SONG_START],Y
ADC #VGM_OFFSET
ADC SONG_START
STA CURRENT_POSITION
BCC VSP_DONE
INC CURRENT_POSITION + 2
VSP_DONE
setas
RTS
VGM_SET_LOOP_POINTERS
.as
; add the start offset
setal
LDA #0
STA WAIT_CNTR
CLC
LDY #LOOP_OFFSET
LDA [SONG_START],Y
BEQ NO_LOOP_INFO ; if this is zero, assume that the upper word is also 0
ADC #LOOP_OFFSET ; add the current position
STA ADDER_A
INY
INY
LDA [SONG_START],Y
STA ADDER_A + 2
LDA SONG_START
STA ADDER_B
LDA SONG_START + 2
STA ADDER_B + 2
LDA ADDER_R
STA CURRENT_POSITION
LDA ADDER_R + 2
STA CURRENT_POSITION + 2
BRA VSL_DONE
NO_LOOP_INFO
LDY #VGM_OFFSET
LDA [SONG_START],Y
ADC #VGM_OFFSET
ADC SONG_START
STA CURRENT_POSITION
LDA SONG_START + 2
STA CURRENT_POSITION + 2
BCC VSL_DONE
INC CURRENT_POSITION + 2
VSL_DONE
setas
RTS
VGM_INIT_TIMERS
.as
LDA #$44
STA TIMER0_CMP_L
STA TIMER1_CMP_L
LDA #1
STA TIMER0_CMP_M
STA TIMER1_CMP_M
LDA #0
STA TIMER0_CMP_H
STA TIMER1_CMP_H
LDA #0 ; set timer0 charge to 0
STA TIMER0_CHARGE_L
STA TIMER0_CHARGE_M
STA TIMER0_CHARGE_H
STA TIMER1_CHARGE_L
STA TIMER1_CHARGE_M
STA TIMER1_CHARGE_H
LDA #TMR0_CMP_RECLR ; count up from "CHARGE" value to TIMER_CMP
STA TIMER0_CMP_REG
STA TIMER1_CMP_REG
LDA #(TMR0_EN | TMR0_UPDWN | TMR0_SCLR)
STA TIMER0_CTRL_REG
STA TIMER1_CTRL_REG
RTS
; *******************************************************************************
; * Read a data block - 67 66 tt ss ss ss ss
; *******************************************************************************
READ_DATA_BLOCK
.as
LDA [CURRENT_POSITION] ; should be 66
;CMP #$66 ; what happens if it's not 66?
increment_long_addr CURRENT_POSITION
LDA [CURRENT_POSITION] ; should be the type - I expect $C0
PHA
increment_long_addr CURRENT_POSITION
; read the size of the data stream - and compute the end of stream position
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 CURRENT_POSITION
STA ADDER_B
LDA CURRENT_POSITION + 2
STA ADDER_B + 2
; continue reading the file here
LDA ADDER_R
STA CURRENT_POSITION
LDA ADDER_R + 2
STA CURRENT_POSITION + 2
setas
PLA
BEQ UNCOMPRESSED
CMP #$C0
BNE UNKNOWN_DATA_BLOCK
UNCOMPRESSED
setal
LDA DATA_STREAM_CNT ; multiply by 4
ASL A
ASL A
TAX
LDA ADDER_B
STA DATA_STREAM_TBL,X
LDA ADDER_B + 2
STA DATA_STREAM_TBL,X + 2
INC DATA_STREAM_CNT
setas
UNKNOWN_DATA_BLOCK
RTS