Skip to content

Commit

Permalink
Implement linear pitch period limit
Browse files Browse the repository at this point in the history
- Correct linear pitch calculation sequence
  • Loading branch information
Gumball2415 committed Jul 20, 2024
1 parent 4cc1a5e commit bdf33eb
Show file tree
Hide file tree
Showing 17 changed files with 3,016 additions and 2,974 deletions.
21 changes: 0 additions & 21 deletions Source/drivers/asm/apu.s
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,6 @@ ft_update_2a03:
sta $4017
rts
@Play:
.if .defined(USE_LINEARPITCH) ;;; ;; ;
lda var_SongFlags
and #FLAG_LINEARPITCH
beq @End
.if .defined(PAL_PERIOD_TABLE)
lda var_SongFlags
and #FLAG_USEPAL
bne :+
jsr ft_load_ntsc_table
jmp @TableLoaded
: jsr ft_load_pal_table
.else
jsr ft_load_ntsc_table
.endif
@TableLoaded:
ldx #APU_OFFSET
jsr ft_linear_fetch_pitch
jsr ft_linear_fetch_pitch
jsr ft_linear_fetch_pitch
@End:
.endif ; ;; ;;;
ldx #$00
ldy #$00
; ==============================================================================
Expand Down
24 changes: 21 additions & 3 deletions Source/drivers/asm/effects.s
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ ft_load_slide:
: ;rts
jmp ft_jump_to_effect

; see CChannelHandler::CalculatePeriod()
ft_calc_period:

; Load period
Expand Down Expand Up @@ -314,7 +315,26 @@ ft_calc_period:
sta var_ch_PeriodCalcLo, x
sta var_ch_PeriodCalcHi, x
@Skip:


jsr ft_vibrato
jsr ft_tremolo

.if .defined(USE_LINEARPITCH) ;;; ;; ;
; apply linear pitch
lda var_SongFlags
and #FLAG_LINEARPITCH
beq :+
; these channels don't use linear pitch adjustments
lda ft_channel_type, x
cmp #CHAN_NOI
beq :+
cmp #CHAN_DPCM
beq :+
jsr ft_load_freq_table
jsr ft_linear_fetch_pitch
:
.endif ; ;; ;;;

; apply frequency multiplication
lda var_ch_Harmonic, x
beq @MaxPeriod ; K00 results in lowest possible frequency
Expand Down Expand Up @@ -382,8 +402,6 @@ ft_calc_period:
jsr @CopyPeriodToCarrier
.endif
@JumpHarmonicEnd:
jsr ft_vibrato
jsr ft_tremolo
rts

@MaxPeriod:
Expand Down
9 changes: 0 additions & 9 deletions Source/drivers/asm/fds.s
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ ft_update_fds:
sta $4080
rts
@Play:
.if .defined(USE_LINEARPITCH) ;;; ;; ;
lda var_SongFlags
and #FLAG_LINEARPITCH
beq :+
jsr ft_load_fds_table
ldx #FDS_OFFSET
jsr ft_linear_fetch_pitch
:
.endif ; ;; ;;;

lda var_ch_Note + FDS_OFFSET
jeq @KillFDS
Expand Down
57 changes: 57 additions & 0 deletions Source/drivers/asm/instrument.s
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,24 @@ ft_load_instrument_vrc7:
; Make sure the period doesn't exceed max or min
ft_limit_freq:

.if .defined(USE_LINEARPITCH)
; linear pitch mode has different bounds check
lda var_SongFlags
and #FLAG_LINEARPITCH
beq :+
lda ft_channel_type, x
asl
tay
lda ft_limit_linear_pointers, y
sta var_Temp16
iny
lda ft_limit_linear_pointers, y
sta var_Temp16 + 1
ldy #$00
jmp (var_Temp16)
:
.endif

; Jump to the instrument setup routine
lda ft_channel_type, x
asl ;;; ;; ;
Expand Down Expand Up @@ -652,6 +670,7 @@ ft_limit_pointers: ;;; ;; ; 0CC: optimize this
.word ft_limit_period_vrc6 ;;; ;; ; S5B

ft_limit_period_no:
ft_limit_linear_period_no:
rts

; 2A03: period is between 0 to $7FF
Expand Down Expand Up @@ -690,6 +709,44 @@ ft_limit_period_vrc6:
sta var_ch_TimerPeriodHi, x
rts

ft_limit_linear_pointers: ;;; ;; ; 0CC: optimize this
.word ft_limit_linear_period_2a03 ; 2A03
.word ft_limit_linear_period_2a03 ; 2A03
.word ft_limit_linear_period_no ; 2A03 noise
.word ft_limit_linear_period_no ; 2A03 dpcm
.word ft_limit_linear_period_vrc6 ; VRC6
.word ft_limit_linear_period_vrc6 ; VRC6
.word ft_limit_linear_period_no ; VRC7
.word ft_limit_linear_period_vrc6 ; FDS
.word ft_limit_linear_period_2a03 ; MMC5
.word ft_limit_linear_period_no ; N163
.word ft_limit_linear_period_vrc6 ;;; ;; ; S5B

; std::min(std::max(Period, 0), (NOTE_COUNT - 1) << LINEAR_PITCH_AMOUNT);
; even though this is a virtual function,
; nothing seems to override this in linear pitch mode
ft_limit_linear_period_2a03:
ft_limit_linear_period_vrc6:
lda var_ch_TimerPeriodHi, x
bmi @LimitMin
sec
lda var_ch_TimerPeriodLo, x
sbc #<(95<<5)
lda var_ch_TimerPeriodHi, x
sbc #>(95<<5)
bcc @NoLimit
lda #>(95<<5)
sta var_ch_TimerPeriodHi, x
lda #<(95<<5)
sta var_ch_TimerPeriodLo, x
@NoLimit:
rts
@LimitMin:
lda #$00
sta var_ch_TimerPeriodLo, x
sta var_ch_TimerPeriodHi, x
rts

.if 0

lda var_ch_TimerPeriodHi, x
Expand Down
10 changes: 0 additions & 10 deletions Source/drivers/asm/mmc5.s
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ ft_update_mmc5:
sta $5015
rts
:
.if .defined(USE_LINEARPITCH) ;;; ;; ;
lda var_SongFlags
and #FLAG_LINEARPITCH
beq :+
jsr ft_load_ntsc_table
ldx #MMC5_OFFSET
jsr ft_linear_fetch_pitch
jsr ft_linear_fetch_pitch
:
.endif ; ;; ;;;
ldx #$00
@ChannelLoop: ; MMC5 pulse channels
lda var_ch_Note + MMC5_OFFSET, x ; Kill channel if note = off
Expand Down
13 changes: 0 additions & 13 deletions Source/drivers/asm/n163.s
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,6 @@ ft_update_n163:
bne :-
rts
@Play:
.if .defined(USE_LINEARPITCH) ;;; ;; ;
lda var_SongFlags
and #FLAG_LINEARPITCH
beq :++
jsr ft_load_n163_table
lda var_NamcoChannels
sta var_Temp3
ldx #N163_OFFSET
: jsr ft_linear_fetch_pitch
dec var_Temp3
bne :-
:
.endif ; ;; ;;;
; x = channel
ldx #$00
@ChannelLoop:
Expand Down
20 changes: 11 additions & 9 deletions Source/drivers/asm/player.s
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,6 @@ ft_cmd_s5b_noise:
; End of commands
;

.if .defined(USE_N163) || .defined(USE_FDS) || .defined(USE_VRC6) ;;; ;; ;
ft_load_freq_table:
lda ft_channel_type, x
.if .defined(USE_N163)
Expand All @@ -1288,6 +1287,11 @@ ft_load_freq_table:
beq ft_load_saw_table
.endif
; fallthrough

.if .defined(PAL_PERIOD_TABLE)
lda var_SongFlags
and #FLAG_USEPAL
bne ft_load_pal_table
.endif

.if .defined(NTSC_PERIOD_TABLE)
Expand All @@ -1296,8 +1300,8 @@ ft_load_ntsc_table:
sta var_Note_Table
lda #>ft_periods_ntsc ;; Reloc
sta var_Note_Table + 1
.endif
rts
.endif

.if .defined(PAL_PERIOD_TABLE)
ft_load_pal_table:
Expand Down Expand Up @@ -1617,7 +1621,7 @@ ft_linear_prescale:
sta var_Temp
rts

ft_linear_fetch_pitch: ; increments x
ft_linear_fetch_pitch:
jsr ft_linear_prescale
asl var_ch_PeriodCalcHi, x

Expand All @@ -1629,9 +1633,9 @@ ft_linear_fetch_pitch: ; increments x
sta var_ch_PeriodCalcHi, x

cpy #$BF
bcs @Return
bcs :+ ;Return
lda var_Temp
beq @Return
beq :+ ;Return

lda ft_channel_type, x
cmp #CHAN_FDS
Expand Down Expand Up @@ -1660,9 +1664,7 @@ ft_linear_fetch_pitch: ; increments x
lda var_ch_PeriodCalcHi, x
sbc EXT
sta var_ch_PeriodCalcHi, x
@Return:
inx
rts
jmp :+ ;Return
@FrequencyReg:
sec
iny
Expand All @@ -1684,7 +1686,7 @@ ft_linear__final:
lda var_ch_PeriodCalcHi, x
adc EXT
sta var_ch_PeriodCalcHi, x
inx
:
rts

ft_correct_finepitch:
Expand Down
11 changes: 0 additions & 11 deletions Source/drivers/asm/s5b.s
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ ft_update_s5b:
: dex
bpl @UpdateToneMask

.if .defined(USE_LINEARPITCH) ;;; ;; ;
lda var_SongFlags
and #FLAG_LINEARPITCH
beq :+
jsr ft_load_ntsc_table
ldx #S5B_OFFSET
jsr ft_linear_fetch_pitch
jsr ft_linear_fetch_pitch
jsr ft_linear_fetch_pitch
:
.endif ; ;; ;;;
ldx #$00
ldy #$00
@ChannelLoop:
Expand Down
12 changes: 0 additions & 12 deletions Source/drivers/asm/vrc6.s
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ ft_update_vrc6:
sta $B002
rts
:
.if .defined(USE_LINEARPITCH) ;;; ;; ;
lda var_SongFlags
and #FLAG_LINEARPITCH
beq :+
jsr ft_load_ntsc_table
ldx #VRC6_OFFSET
jsr ft_linear_fetch_pitch
jsr ft_linear_fetch_pitch
jsr ft_load_saw_table
jsr ft_linear_fetch_pitch
:
.endif ; ;; ;;;
ldx #$00
txa
sta var_Temp_Pointer
Expand Down
Loading

0 comments on commit bdf33eb

Please sign in to comment.