Skip to content

Commit d1a8ac1

Browse files
committed
refactor(nsfdrv): Remove unnecessary init to 0
- fix: Fix multichip FDS padjumps
1 parent abc7c53 commit d1a8ac1

6 files changed

Lines changed: 82 additions & 107 deletions

File tree

Source/drivers/FTDRV init variables.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ last updated: 2026-06-11
44

55
---
66

7+
Note that all these variables pertain to the initialization routine on driver
8+
versions 2.16 and below.
9+
710
## variables init by `ft_music_init`
811

912
- var_Linear_Counter `#$FF`
@@ -21,9 +24,9 @@ last updated: 2026-06-11
2124
- var_ch_TimerPeriodLo `#$00`
2225
- var_ch_TimerPeriodHi `#$00`
2326
- var_ch_Trigger `#$00`
24-
- var_ch_VibratoPos `#$00` (old vibrato mode)
25-
- var_ch_VibratoPos `#$48` (new vibrato mode)
26-
- var_ch_Note `#$80`
27+
- var_ch_VibratoPos `#$00` (new vibrato mode)
28+
- var_ch_VibratoPos `#$48` (old vibrato mode)
29+
- var_ch_Note `#$80` (DPCM)
2730

2831
### chip-specific init routines
2932

Source/drivers/asm/build/build_engine.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ def build(chip: str):
6868
# headerless NSF kernel binary not supported
6969
if HEADERLESS:
7070
print("debug: NSF kernel binary not supported")
71-
os.remove(f"out_{chip}{hdrless}.lst")
72-
os.remove(f"c0_{chip}{hdrless}.bin")
73-
os.remove(f"c1_{chip}{hdrless}.bin")
74-
os.remove(f"driver_{chip}{hdrless}.o")
71+
if not DEBUG:
72+
os.remove(f"out_{chip}{hdrless}.lst")
73+
os.remove(f"c0_{chip}{hdrless}.bin")
74+
os.remove(f"c1_{chip}{hdrless}.bin")
75+
os.remove(f"driver_{chip}{hdrless}.o")
7576
return
7677

7778
adr = {}
@@ -90,7 +91,7 @@ def build(chip: str):
9091
label = resolvelabel(label)
9192
pos[label] = hexpos
9293
adr[hexpos] = label
93-
94+
9495
# search for label pointers to relocate
9596
relocsearch = re.search(r"(......).....(..)(.*[><])([\w]*).*;; Reloc$", line)
9697
if relocsearch is not None:

Source/drivers/asm/driver.s

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -501,36 +501,46 @@ PLAY:
501501

502502
USE_PADJMP = 1 ; disable if you don't need FDS write protection
503503

504+
;;
505+
; define a pad of nops, for areas that don't get executed
506+
; pads from startpad to endpad, inclusive
507+
.macro padnop startpad, endpad, condition
508+
.local @padcount
509+
@padcount = endpad-startpad+1
510+
.if condition && USE_PADJMP
511+
.assert * = LOAD+((startpad-$8000) & $FFFF), warning, .sprintf("padding does not start at $%04X", startpad)
512+
.repeat (@padcount)
513+
nop
514+
.endrep
515+
.assert * = LOAD+((endpad-$8000) & $FFFF)+1, warning, .sprintf("padding does not end after $%04X", endpad)
516+
.endif
517+
.endmacro
518+
504519
;;
505520
; manually define a jump command before padding as an optimization
521+
; pads from startpad to endpad, inclusive, and also including the jmp command
506522
.macro jmppad jump, startpad, endpad, condition
507523
; count: bytes in total that the padding takes; must be more than 3
508524
; count = (endpad - startpad)
509-
.local @padcount
510-
@padcount = endpad-startpad-2
511-
.assert (endpad-startpad > 0), warning, .sprintf("padding byte size %d must be bigger than 3", @padcount)
512525
.if condition && USE_PADJMP
513-
.assert * = LOAD+((startpad-$8000) & $FFFF), warning, .sprintf("padding does not start at $%04X", startpad)
526+
.assert * = LOAD+((startpad-$8000) & $FFFF), warning, .sprintf("padding does not start at $%04X", startpad)
514527
jump
515-
.assert * = LOAD+((startpad+3-$8000) & $FFFF), warning, "jump command is not 3 bytes long"
516-
.repeat (@padcount)
517-
nop
518-
.endrep
519-
.assert * = LOAD+((endpad-$8000) & $FFFF)+1, warning, .sprintf("padding does not end after $%04X", endpad)
528+
.assert * = LOAD+((startpad+3-$8000) & $FFFF), warning, "jump command is not 3 bytes long"
529+
padnop (startpad+3), endpad, condition
530+
.assert * = LOAD+((endpad-$8000) & $FFFF)+1, warning, .sprintf("padding does not end after $%04X", endpad)
520531
.else
521532
jump
522533
.endif
523534
.endmacro
524535

525536
;;
526537
; pads with NOPs and jmps to end of padding
527-
; @param startpad: start of register area to be padded with for assert, including the jmp command
528-
; @param endpad: end of register area to be padded with for assert
538+
; pads from startpad to endpad, inclusive, and also including the jmp command
529539
.macro padjmp startpad, endpad, condition
530540
.if condition && USE_PADJMP
531-
.local end
532-
jmppad {jmp end}, startpad, endpad, condition
533-
end:
541+
.local @end
542+
jmppad {jmp @end}, startpad, endpad, condition
543+
@end:
534544
.endif
535545
.endmacro
536546

Source/drivers/asm/effects.s

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,9 @@ ft_load_slide:
160160
@Done:
161161
jsr ft_limit_note
162162
sta var_ch_Note, x
163-
164-
; FDS scratch write padding: guard $9000-9003
165-
padjmp $8FFC, $9003, .defined(USE_ALL) && .defined(PACKAGE)
166-
167163
sta var_ch_EchoBuffer, x ;;; ;; ;
168164
jsr ft_translate_freq_only
169165
lda var_ch_TimerPeriodLo, x
170-
171-
; FDS scratch write padding: guard $9010
172-
padjmp $900D, $9010, .defined(USE_ALL) && .defined(PACKAGE)
173-
174166
sta var_ch_PortaToLo, x
175167
lda var_ch_TimerPeriodHi, x
176168
sta var_ch_PortaToHi, x
@@ -187,10 +179,6 @@ ft_load_slide:
187179
pla
188180
sta var_ch_TimerPeriodLo, x
189181
; change mode to sliding
190-
191-
; FDS scratch write padding: guard $9030
192-
padjmp $902D, $9030, .defined(USE_ALL) && .defined(PACKAGE)
193-
194182
clc
195183
lda var_ch_Effect, x
196184
adc #01
@@ -203,10 +191,6 @@ ft_load_slide:
203191
lda #EFF_SLIDE_DOWN
204192
jmp :++
205193
: lda #EFF_SLIDE_UP
206-
207-
; FDS scratch write padding: guard $9000-$9003
208-
padjmp $8FFB, $9003, .defined(USE_ALL) && (.not .defined(PACKAGE))
209-
210194
:
211195
.endif
212196
sta var_ch_Effect, x
@@ -215,10 +199,6 @@ ft_load_slide:
215199
lda ft_channel_type, x ;;; ;; ;
216200
.if .defined(USE_N163)
217201
cpx #CHAN_N163
218-
219-
; FDS scratch write padding: guard $9010
220-
padjmp $900C, $9010, .defined(USE_ALL) && (.not .defined(PACKAGE))
221-
222202
beq @Invert
223203
.endif
224204
cpx #CHAN_NOI
@@ -229,20 +209,33 @@ ft_load_slide:
229209
beq :+
230210
lda #EFF_SLIDE_UP
231211
sta var_ch_Effect, x
232-
: lda #EFF_SLIDE_DOWN
212+
213+
; FDS scratch write padding: guard $9000-9003
214+
padjmp $8FFB, $9003, .defined(USE_ALL) && .defined(PACKAGE)
215+
216+
: lda #EFF_SLIDE_DOWN
233217
sta var_ch_Effect, x
234218
:
235219

236-
; FDS scratch write padding
237-
jmppad {jmp ft_jump_to_effect}, $9028, $9030, .defined(USE_ALL) && (.not .defined(PACKAGE))
220+
221+
; FDS scratch write padding: guard $9000-$9003
222+
jmppad {jmp ft_jump_to_effect}, $8FF8, $9003, .defined(USE_ALL) && !.defined(PACKAGE)
238223

239224
; see CChannelHandler::CalculatePeriod()
225+
226+
; FDS scratch write padding: guard $9010
227+
padnop $900C, $9010, .defined(USE_ALL) && .defined(PACKAGE)
228+
240229
ft_calc_period:
241230

242231
; Load period
243232
lda var_ch_TimerPeriodLo, x
244233
sta var_ch_PeriodCalcLo, x
245234
lda var_ch_TimerPeriodHi, x
235+
236+
; FDS scratch write padding: guard $9010
237+
padjmp $900D, $9010, .defined(USE_ALL) && !.defined(PACKAGE)
238+
246239
sta var_ch_PeriodCalcHi, x
247240

248241
.if .defined(USE_VRC7)
@@ -254,9 +247,17 @@ ft_calc_period:
254247
lda ft_channel_type, x
255248
cmp #CHAN_VRC7
256249
bne :+
250+
251+
; FDS scratch write padding: guard $9030
252+
padjmp $902B, $9030, .defined(USE_ALL) && .defined(PACKAGE)
253+
257254
lsr var_ch_PeriodCalcHi, x
258255
ror var_ch_PeriodCalcLo, x
259256
lsr var_ch_PeriodCalcHi, x
257+
258+
; FDS scratch write padding: guard $9030
259+
padjmp $902B, $9030, .defined(USE_ALL) && !.defined(PACKAGE)
260+
260261
ror var_ch_PeriodCalcLo, x
261262
:
262263
.endif

Source/drivers/asm/init.s

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ft_music_init:
1111
txa
1212
pha ; region
1313

14+
; initialize all variables to #$00
1415
lda #$00
1516
; init variables in ZP
1617
ldx #ZP_END
@@ -57,6 +58,7 @@ ft_music_init:
5758
jsr ft_load_song
5859

5960
; Kill APU registers
61+
lda #$00
6062
ldx #$0B
6163
@LoadRegs:
6264
sta $4000, x
@@ -88,52 +90,30 @@ ft_music_init:
8890
sta var_ch_DPCM_EffPitch
8991
sta var_ch_DPCMDAC
9092

91-
; Reset some variables for the wave channels
92-
ldx #$00 ;;; ;; ;
93-
:
94-
95-
cpx #(CH_COUNT_2A03 + CH_COUNT_MMC5)
96-
bpl :+
93+
; Reset channels with length counter
9794
lda #$08
98-
sta var_ch_LengthCounter, x ;;; ;; ;
99-
: lda #$00
100-
sta var_ch_NoteRelease, x ;;; ;; ;
101-
sta var_ch_Transpose, x ;;; ;; ;
102-
sta var_ch_NoteCut, x
103-
sta var_ch_Effect, x
104-
sta var_ch_EffParam, x
105-
sta var_ch_PortaToLo, x
106-
sta var_ch_PortaToHi, x
107-
sta var_ch_TimerPeriodLo, x
108-
sta var_ch_TimerPeriodHi, x
109-
sta var_ch_Trigger, x ;;; ;; ;
110-
inx
111-
cpx #WAVE_CHANS
112-
bne :-- ; ;; ;;;
95+
ldx #(CH_COUNT_2A03 + CH_COUNT_MMC5)
96+
:
97+
dex
98+
sta var_ch_LengthCounter, x
99+
bne :- ; ;; ;;;
113100

114101
.if .defined(USE_OLDVIBRATO) ;;; ;; ;
115102
lda var_SongFlags
116103
and #FLAG_OLDVIBRATO
117-
beq :+
104+
bne @SkipVibratoInit
118105
lda #48
119-
:
120-
.endif
121106
ldx #$00
122107
: sta var_ch_VibratoPos, x
123108
inx
124109
cpx #WAVE_CHANS
125110
bne :-
126-
.if .defined(USE_OLDVIBRATO) ;;; ;; ;
127-
lda #$00
111+
@SkipVibratoInit:
128112
.endif
129113

130114
; DPCM
131115
.if .defined(USE_DPCM)
132-
sta var_ch_NoteCut + DPCM_OFFSET
133-
sta var_ch_NoteRelease + DPCM_OFFSET ;;; ;; ;
134-
.if .defined(USE_ALL)
135-
ldx #EFF_CHANS
136-
.elseif .defined(USE_N163)
116+
.if .defined(USE_N163) && .not .defined(USE_ALL)
137117
ldx var_EffChannels
138118
.else
139119
ldx #EFF_CHANS
@@ -306,8 +286,8 @@ ft_load_song:
306286
; Load the song
307287
jsr ft_load_track
308288

309-
; Clear variables to zero
310289
; Important!
290+
; Variables should be initialized to 0 in ft_music_init
311291
ldx #$01
312292
stx var_PlayerFlags ; Player flags, bit 0 = playing
313293
dex
@@ -318,20 +298,8 @@ ft_load_song:
318298
lda #$80
319299
sta var_ch_FinePitch, x
320300
sta var_ch_VolSlideTarget, x ;; ;; !!
321-
lda #$00
322-
323-
sta var_ch_VibratoSpeed, x
324-
sta var_ch_TremoloSpeed, x
325-
sta var_ch_Effect, x
326-
sta var_ch_VolSlide, x
327-
sta var_ch_NoteDelay, x
328-
sta var_ch_ArpeggioCycle, x
329-
sta var_ch_PhaseReset, x
330-
sta var_ch_DPCMPhaseReset, x
331-
sta var_ch_Harmonic, x
332-
inc var_ch_Harmonic, x ; default value is 1
333-
;
334-
sta var_ch_Note, x
301+
; all variables initialized to 0, but Kxx buffer should be 1
302+
inc var_ch_Harmonic, x
335303
inx
336304

337305
cpx #EFF_CHANS ;;; ;; ;
@@ -341,10 +309,6 @@ ft_load_song:
341309
stx var_ch_PrevFreqHigh ; Set prev freq to FF for Sq1 & 2
342310
stx var_ch_PrevFreqHigh + 1
343311

344-
.if .defined(USE_DPCM)
345-
lda #$00
346-
sta var_ch_DPCM_Offset
347-
.endif
348312
.if .defined(USE_MMC5)
349313
stx var_ch_PrevFreqHighMMC5
350314
stx var_ch_PrevFreqHighMMC5 + 1
@@ -357,10 +321,6 @@ ft_load_song:
357321
jsr ft_calculate_speed
358322
;jsr ft_restore_speed
359323

360-
lda #$00
361-
sta var_Tempo_Accum
362-
sta var_Tempo_Accum + 1
363-
364324
rts
365325

366326
;

Source/drivers/asm/vrc7.s

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,6 @@ ft_vrc7_get_freq:
374374
lda #$00 ;;; ;; ;
375375
sta ACC ; ;; ;;;
376376
lda var_ch_vrc7_ActiveNote - VRC7_OFFSET, x
377-
378-
; FDS scratch write padding: guard $A000-$A002
379-
padjmp $9FFC, $A002, .defined(USE_ALL) && .defined(PACKAGE)
380-
381377
jsr ft_translate_note_vrc7
382378
tay
383379

@@ -406,10 +402,6 @@ ft_vrc7_get_freq:
406402
@NoPorta:
407403
lda ft_note_table_vrc7_l, y
408404
sta var_ch_TimerPeriodLo, x
409-
410-
; FDS scratch write padding
411-
padjmp $9FFD, $A002, .defined(USE_ALL) && (.not .defined(PACKAGE))
412-
413405
lda ft_note_table_vrc7_h, y
414406
sta var_ch_TimerPeriodHi, x
415407

@@ -429,9 +421,17 @@ ft_vrc7_get_freq_only:
429421
sta ACC ; ;; ;;;
430422
lda var_ch_vrc7_ActiveNote - VRC7_OFFSET, x
431423
jsr ft_translate_note_vrc7
424+
425+
; FDS scratch write padding: guard $A000-$A002
426+
padjmp $9FFD, $A002, .defined(USE_ALL) && .defined(PACKAGE)
427+
432428
tay
433429
lda ft_note_table_vrc7_l, y
434430
sta var_ch_TimerPeriodLo, x
431+
432+
; FDS scratch write padding: guard $A000-$A002
433+
padjmp $9FFB, $A002, .defined(USE_ALL) && !.defined(PACKAGE)
434+
435435
lda ft_note_table_vrc7_h, y
436436
sta var_ch_TimerPeriodHi, x
437437

0 commit comments

Comments
 (0)