-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.s
622 lines (578 loc) · 11.9 KB
/
init.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
;
; ft_sound_init
;
; Initializes the player and song number
; a = song number
; x = ntsc/pal
;
ft_music_init:
asl a
jsr ft_load_song
; Kill APU registers
lda #$00
ldx #$0B
@LoadRegs:
sta $4000, x
dex
bne @LoadRegs
ldx #$06
@LoadRegs2:
sta $400D, x
dex
bne @LoadRegs2
lda #$30 ; noise is special
sta $400C
lda #$0F
sta $4015 ; APU control
lda #$08
sta $4001
sta $4005
lda #$80 ;;; ;; ;
sta $4017
lda #$00
sta $4017 ; ;; ;;; Disable frame IRQs
lda #$FF ;;; ;; ; Reset triangle linear counter
sta var_Linear_Counter
.if .defined(CHANNEL_CONTROL)
sta var_Channels ; Enable all channels
.endif
sta var_ch_DPCM_EffPitch
sta var_ch_DPCMDAC
; Reset some variables for the wave channels
ldx #$00 ;;; ;; ;
:
cpx #(CH_COUNT_2A03 + CH_COUNT_MMC5)
bpl :+
lda #$08
sta var_ch_LengthCounter, x ;;; ;; ;
: lda #$00
sta var_ch_NoteRelease, x ;;; ;; ;
sta var_ch_Transpose, x ;;; ;; ;
sta var_ch_NoteCut, x
sta var_ch_Effect, x
sta var_ch_EffParam, x
sta var_ch_PortaToLo, x
sta var_ch_PortaToHi, x
sta var_ch_TimerPeriodLo, x
sta var_ch_TimerPeriodHi, x
sta var_ch_Trigger, x ;;; ;; ;
inx
cpx #WAVE_CHANS
bne :-- ; ;; ;;;
.if .defined(USE_OLDVIBRATO) ;;; ;; ;
lda var_SongFlags
and #FLAG_OLDVIBRATO
beq :+
lda #48
:
.endif
ldx #$00
: sta var_ch_VibratoPos, x
inx
cpx #WAVE_CHANS
bne :-
.if .defined(USE_OLDVIBRATO) ;;; ;; ;
lda #$00
.endif
; DPCM
.if .defined(USE_DPCM)
sta var_ch_NoteCut + DPCM_OFFSET
sta var_ch_NoteRelease + DPCM_OFFSET ;;; ;; ;
.if .defined(USE_ALL)
ldx #EFF_CHANS
.elseif .defined(USE_N163)
ldx var_EffChannels
.else
ldx #EFF_CHANS
.endif
lda #$80
sta var_ch_Note, x
.endif
.if .defined(USE_VRC6)
lda #$00
sta $9003
.endif
.if .defined(USE_MMC5)
lda #$03
sta $5015 ; Enable channels
.endif
.if .defined(USE_N163)
jsr ft_init_n163
.endif
.if .defined(USE_VRC7)
jsr ft_init_vrc7
.endif
.if .defined(USE_FDS)
jsr ft_init_fds
.endif
.if .defined(USE_S5B) ;;; ;; ;
jsr ft_init_s5b
.endif ; ;; ;;;
rts
;
; Prepare the player for a song
;
; NSF music data header:
;
; - Song list, 2 bytes
; - Instrument list, 2 bytes
; - DPCM instrument list, 2 bytes
; - DPCM sample list, 2 bytes
; - Flags, 1 byte
; - Pointer to wave tables, 2 bytes, if FDS is enabled
; - NTSC speed divider
; - PAL speed divider
;
ft_load_song:
pha
; Get the header
lda ft_music_addr
sta var_Temp_Pointer
lda ft_music_addr + 1
sta var_Temp_Pointer + 1
; Read the header and store in RAM
ldy #$00
@LoadAddresses:
.if .defined(RELOCATE_MUSIC)
clc
lda (var_Temp_Pointer), y
adc ft_music_addr
sta var_Song_list, y
iny
lda (var_Temp_Pointer), y ; Song list offset, high addr
adc ft_music_addr + 1
sta var_Song_list, y
.else
lda (var_Temp_Pointer), y
sta var_Song_list, y
iny
lda (var_Temp_Pointer), y ; Song list offset, high addr
sta var_Song_list, y
.endif
iny
cpy #$0A ;;; ;; ; 5 items, now including groove table
bne @LoadAddresses
lda (var_Temp_Pointer), y ; Flags, 1 byte
sta var_SongFlags
iny
.if .defined(USE_FDS)
; Load FDS wave table pointer
.if .defined(RELOCATE_MUSIC)
clc
lda (var_Temp_Pointer), y
adc ft_music_addr
sta var_Wavetables
iny
lda (var_Temp_Pointer), y
adc ft_music_addr + 1
sta var_Wavetables + 1
.else
lda (var_Temp_Pointer), y
sta var_Wavetables
iny
lda (var_Temp_Pointer), y
sta var_Wavetables + 1
.endif
iny
.endif
.if .defined(PAL_PERIOD_TABLE) ;;; ;; ;
cpx #$01 ; PAL / NTSC flag
beq @LoadPAL
.endif ; ;; ;;;
.if .defined(NTSC_PERIOD_TABLE)
; Load NTSC speed divider and frequency table
lda (var_Temp_Pointer), y
iny
sta var_Tempo_Dec
lda (var_Temp_Pointer), y
iny
sta var_Tempo_Dec + 1
jsr ft_load_ntsc_table ;;; ;; ;
.if .defined(USE_N163)
iny
iny
.endif
.endif
.if .defined(PAL_PERIOD_TABLE)
jmp @LoadDone
@LoadPAL:
; Load PAL speed divider and frequency table
iny
iny
lda (var_Temp_Pointer), y
iny
sta var_Tempo_Dec
lda (var_Temp_Pointer), y
iny
sta var_Tempo_Dec + 1
lda var_PlayerFlags ;;; ;; ;
ora #FLAG_USEPAL
sta var_PlayerFlags
jsr ft_load_pal_table ; ;; ;;;
.endif
@LoadDone:
clc
.if .defined(USE_N163)
; N163 channel count
lda (var_Temp_Pointer), y
iny
sta var_NamcoChannels
adc #(CH_COUNT_2A03 + CH_COUNT_VRC6 + CH_COUNT_MMC5 + CH_COUNT_FDS + CH_COUNT_VRC7 + CH_COUNT_S5B)
.else
lda #(CH_COUNT_2A03 + CH_COUNT_VRC6 + CH_COUNT_MMC5 + CH_COUNT_FDS + CH_COUNT_VRC7 + CH_COUNT_S5B)
.endif
sta var_EffChannels
adc #$01
sta var_AllChannels
.if .defined(USE_N163)
ldx var_NamcoChannels
dex
txa
asl a
asl a
asl a
asl a
sta var_NamcoChannelsReg
.endif
pla
tay
; Load the song
jsr ft_load_track
; Clear variables to zero
; Important!
ldx #$01
stx var_PlayerFlags ; Player flags, bit 0 = playing
dex
@ClearChannels2: ; This clears the first four channels
lda #$7F
sta var_ch_VolColumn, x
sta var_ch_VolDefault, x ;;; ;; ;
lda #$80
sta var_ch_FinePitch, x
lda #$00
;
;lda #$00
sta var_ch_VibratoSpeed, x
sta var_ch_TremoloSpeed, x
sta var_ch_Effect, x
sta var_ch_VolSlide, x
sta var_ch_NoteDelay, x
sta var_ch_ArpeggioCycle, x
;
sta var_ch_Note, x
inx
cpx #EFF_CHANS ;;; ;; ;
bne @ClearChannels2
ldx #$FF
stx var_ch_PrevFreqHigh ; Set prev freq to FF for Sq1 & 2
stx var_ch_PrevFreqHigh + 1
.if .defined(USE_DPCM)
lda #$00
sta var_ch_DPCM_Offset
.endif
.if .defined(USE_MMC5)
stx var_ch_PrevFreqHighMMC5
stx var_ch_PrevFreqHighMMC5 + 1
.endif
inx ; Jump to the first frame
stx var_Current_Frame
jsr ft_load_frame
jsr ft_calculate_speed
;jsr ft_restore_speed
lda #$00
sta var_Tempo_Accum
sta var_Tempo_Accum + 1
rts
;
; Load the track number in A
;
; Track headers:
;
; - Frame list address, 2 bytes
; - Number of frames, 1 byte
; - Pattern length, 1 byte
; - Speed, 1 byte
; - Tempo, 1 byte
;
ft_load_track:
; Load track header address
lda var_Song_list
sta var_Temp16
lda var_Song_list + 1
sta var_Temp16 + 1
; Get the real address, song number * 2 will be in Y here
.if .defined(RELOCATE_MUSIC)
clc
lda (var_Temp16), y
adc ft_music_addr
sta var_Temp_Pointer
iny
lda (var_Temp16), y
adc ft_music_addr + 1
sta var_Temp_Pointer + 1
.else
lda (var_Temp16), y
sta var_Temp_Pointer
iny
lda (var_Temp16), y
sta var_Temp_Pointer + 1
.endif
; Read header
lda #$00
tay
.if .defined(RELOCATE_MUSIC)
clc
lda (var_Temp_Pointer), y ; Frame offset, low addr
adc ft_music_addr
sta var_Frame_List
iny
lda (var_Temp_Pointer), y ; Frame offset, high addr
adc ft_music_addr + 1
sta var_Frame_List + 1
.else
lda (var_Temp_Pointer), y ; Frame offset, low addr
sta var_Frame_List
iny
lda (var_Temp_Pointer), y ; Frame offset, high addr
sta var_Frame_List + 1
.endif
iny ; Y == 02
@ReadLoop:
lda (var_Temp_Pointer), y ; Frame count
sta var_Frame_Count - 2, y
iny
cpy #$08 ;;; ;; ; Groove
bne @ReadLoop
rts
;
; Load the frame in A for all channels
;
ft_load_frame:
.if .defined(USE_BANKSWITCH)
pha ; Frame bank
lda var_InitialBank
beq :+
jsr ft_bankswitch
: pla
.endif
; Get the entry in the frame list
asl a ; Multiply by two
clc ; And add the frame list addr to get
adc var_Frame_List ; the pattern list addr
sta var_Temp16
lda #$00
tay
tax
adc var_Frame_List + 1
sta var_Temp16 + 1
lda var_Current_Frame ;;; ;; ;
bpl :+
inc var_Temp16 + 1
: ; ;; ;;;
; Get the entry in the pattern list
.if .defined(RELOCATE_MUSIC)
clc
lda (var_Temp16), y
adc ft_music_addr
sta var_Temp_Pointer
iny
lda (var_Temp16), y
adc ft_music_addr + 1
sta var_Temp_Pointer + 1
.else
lda (var_Temp16), y
sta var_Temp_Pointer
iny
lda (var_Temp16), y
sta var_Temp_Pointer + 1
.endif
; Iterate through the channels, x = channel
ldy #$00 ; Y = address
stx var_Pattern_Pos
@LoadPatternAddr:
CH_LOOP_START @LoadPatternAddrEpilog
.if .defined(RELOCATE_MUSIC)
clc
lda (var_Temp_Pointer), y ; Load the pattern address for the channel
adc ft_music_addr
sta var_ch_PatternAddrLo, x
iny
lda (var_Temp_Pointer), y ; Pattern address, high byte
adc ft_music_addr + 1
sta var_ch_PatternAddrHi, x
iny
.else
lda (var_Temp_Pointer), y ; Load the pattern address for the channel
sta var_ch_PatternAddrLo, x
iny
lda (var_Temp_Pointer), y ; Pattern address, high byte
sta var_ch_PatternAddrHi, x
iny
.endif
lda #$00
sta var_ch_NoteDelay, x
sta var_ch_Delay, x
; sta var_ch_LoopCounter, x
lda #$FF
sta var_ch_DefaultDelay, x
@LoadPatternAddrEpilog:
CH_LOOP_END @LoadPatternAddr
; Bankswitch values
.if .defined(USE_BANKSWITCH)
lda var_SongFlags ; Check bankswitch flag
and #FLAG_BANKSWITCH
beq @SkipBankValues ; Skip if no bankswitch info is stored
ldx #$00
@LoadBankValues:
CH_LOOP_START @LoadBankValuesEpilog
lda (var_Temp_Pointer), y ; Pattern bank number
sta var_ch_Bank, x
iny
@LoadBankValuesEpilog:
CH_LOOP_END @LoadBankValues
@SkipBankValues:
.endif
lda #$00
sta var_Jump
sta var_Skip
.if .defined(ENABLE_ROW_SKIP)
lda var_SkipTo
bne ft_SkipToRow
rts
;
; Skip to a certain row, this is NOT recommended in songs when CPU time is critical!!
;
;;; ;; ; various optimizations
ft_SkipToRow:
sta var_Pattern_Pos
ldx #$00 ; x = channel
@ChannelLoop:
CH_LOOP_START @ChannelLoopEpilog
.if .defined(USE_BANKSWITCH) ;;; ;; ; perform bankswitching
lda var_ch_Bank, x
beq :+
jsr ft_bankswitch
:
.endif ; ;; ;;;
lda #$FF
sta var_Temp3
lda var_Pattern_Pos
sta var_Temp2 ; Restore row count
lda #$00
sta var_ch_NoteDelay, x
tay
lda var_ch_PatternAddrLo, x
sta var_Temp_Pattern
lda var_ch_PatternAddrHi, x
sta var_Temp_Pattern + 1
@RowLoop:
lda var_ch_NoteDelay, x ; First check if in the middle of a row delay
beq @NoRowDelay
lda var_Temp2
sec
sbc var_ch_NoteDelay, x
bcc :+
pha
lda #$00
sta var_ch_NoteDelay, x
pla
sta var_Temp2
bne @NoRowDelay
beq @Finished
: lda var_ch_NoteDelay, x
sec
sbc var_Temp2
sta var_ch_NoteDelay, x
lda #$00
sta var_Temp2
jmp @Finished
@EffectDispatch: ;;; ;; ;
jsr @Effect
@NoRowDelay:
; Read a row
lda (var_Temp_Pattern), y
bmi @EffectDispatch
lda var_ch_DefaultDelay, x
cmp #$FF
bne :+
iny
lda (var_Temp_Pattern), y
: iny
sta var_ch_NoteDelay, x ; Store default delay
; Save the new address
tya
bpl :+ ; defer as many additions as possible
clc
adc var_Temp_Pattern
sta var_Temp_Pattern
lda #$00
tay
adc var_Temp_Pattern + 1
sta var_Temp_Pattern + 1
:
dec var_Temp2 ; Next row
bne @RowLoop
@Finished:
clc
tya
adc var_Temp_Pattern
sta var_ch_PatternAddrLo, x
lda #$00
adc var_Temp_Pattern + 1
sta var_ch_PatternAddrHi, x
lda var_Temp3
bmi :+
jsr ft_load_instrument
:
@ChannelLoopEpilog:
CH_LOOP_END @ChannelLoop
lda #$00
sta var_SkipTo
rts
@Effect:
cmp #$80
beq @LoadInstCmd
cmp #$84
beq @EffectDuration
cmp #$86
beq @EffectNoDuration
cmp #$F0 ; See if volume
bcs @OneByteCommand
cmp #$E0 ; See if a quick instrument command
bcs @LoadInst
; cmp #$8E
; beq @OneByteCommand
cmp #$94
beq @OneByteCommand
cmp #$A4
beq @OneByteCommand
iny ; Command takes two bytes
@OneByteCommand: ; Command takes one byte
iny
rts ; A new command or note is immediately following
@EffectDuration:
iny
lda (var_Temp_Pattern), y
iny
sta var_ch_DefaultDelay, x
rts
@EffectNoDuration:
iny
lda #$FF
sta var_ch_DefaultDelay, x
rts
@LoadInstCmd: ; mult-byte
iny
lda (var_Temp_Pattern), y
iny
sta var_Temp3
rts
@LoadInst: ; single byte
iny
and #$0F
asl a
sta var_Temp3
rts ;;; ;; ; var_ch_NoteDelay remains unaltered
.else ; ENABLE_ROW_SKIP
rts
.endif ; ENABLE_ROW_SKIP