Here you are the crazy code I found in Just Breed.
I understant pretty much the begining, but the end becomes very crazy. It uses the MMC5 multiplier once, but scince it multiplies by $40, it would be pretty simple to write a similar code for a normal mapper. I just didn't understand the actual algorithm behind that.
Note that you enter with $4e=octave, and output with $4a = low 8 bits of period, $4b = high 3 bits of period. You enter with note in A, that is between $0 and $b (between C and B of the upper octave, for each semitune respectively).
Code:
GetPitch tay ;Y=Note; X=Channel
lda SndFineTuneBufferL,x
clc
adc SndPitchSlideBufferL,x
sta $4a
lda SndFineTuneBufferH,x
adc SndPitchSlideBuffer,x
sta $4b ;Setup fine tune (detune+pitch slide index)
lda VibratoPitchValue,x
sec
sbc #$80
bmi _vibNeg
clc
adc $4a
sta $4a ;Add vibrato index to fine tune
lda #$0
adc $4b
jmp _vibPos
;--------------------
_vibNeg clc
adc $4a
sta $4a ;Same as above, but decrease MSB if overflow instead of increase it
lda $4b
sbc #$0
_vibPos sta $4b
bmi $a8c8 ;Main detune calculated. Is that thing negative ?
lda $4a
asl a
rol $4b
asl a
rol $4b ;Bit 6 and 7 of LSB goes in LSB (?)
lsr a
lsr a
sta $4a
jmp lbl_c8e5
;--------------------
lbl_a8c8: lda $4a
asl a
rol $4b ;Same as above
asl a
rol $4b
lda $4a
and #$3f ;Check if the LSB is zero (apart of bit 6 and 7)
beq lbl_a8da
inc $4b ;If not, set the bits 6 and 7, and increase MSB
ora #$c0
lbl_a8da: sta $4a
bpl lbl_a8e5 ;The N flag is set by the lda $4a above or the inc $4b above, cant say much here
clc
adc #$40 ;Would that clear bit 6 and 7 ??
sta $4a
dec $4b ;$4b equals main detune (note) and $4a fine detune
lbl_a8e5: tya
clc ;Y = Note
adc $4b ;Check if the detune could go one octave below
bpl lbl_a8fa
dec $4e ;If so decrease octave
clc
adc #$c ;And correct the note in A
bpl lbl_a90c ;Check if the detune is strong enough to get below a second octave
dec $4e
clc
adc #$c ;If so correct it
jmp lbl_c90c
;--------------------
lbl_a8fa: cmp #$c
bcc lbl_a90c ;Check if the detune could go one octave above
inc $4e
sec
sbc #$c
cmp #$c
bcc lbl_a90c ;Check for a second octave up
inc $4e
sec
sbc #$c ;Now A equals the correct note with the correct octave
lbl_a90c: sta $5206
sta $4f
lda #$40 ;Multiply note by $40
sta $5205
lda $4a
bmi $a92a ;Check for the note before it's octave correction (?)
clc
adc $5205
lbl_a91e: sta $4a ;Result is detune in bits 0-5 and note in bits 6-7
lda $5206
adc #$0
sta $4b ;High bits of the note are here
jmp lbl_c937
;--------------------
lbl_a92a: clc
adc $5205 ;??
sta $4a
lda $5206
sbc #$0 ;??
lbl_a935: sta $4b
lda $4a
clc
adc #$ab
sta $4a
lda $4b ;Add main pitch to the pointer $c9ab
adc #$c9
sta $4b ;$c9ab+$23a=
ldy #$0
ldy ($4a),y ;Load low tune
sta $4a
ldy $4f
lda $c99f,y ;Load high tune in function of the note
lbl_a94f: sta $4b
lbl_a951: lda $c993,y
beq lbl_a95c ;?
lda $4a
bpl lbl_a95c
dec $4b ;Decrease low pitch if high pitch is negative
lbl_a95c: lda $4b
ldy $4e
beq lbl_a989 ;If octave is zero, the thing is completed
bmi $a98a
cpy #$5
bcs lbl_a973 ;Go elsewhere if octave above 5 (for very high notes)
lbl_a968: lsr a
ror $4a ;Divide the period by 2 for each octave
dey
bne lbl_a968
sta $4b
jmp lbl_c989 ;Done !!
;--------------------
lbl_a973: sty $50 ;Octave higher than 5 (probably used for sound effects)
lda #$8
sec
sbc $50 ;8-Octave = a number between 1 and 3
tay
lda $4a
lbl_a97d: asl a
rol $4b ;This will multiply the period by 2 for each octave
dey ;In other word, the highest notes become the lowest (?)
bne lbl_a97d
lda $4b
sty $4b ;0 overrides the high pitch
sta $4a ;And the low pitch overrides the high pitch
lbl_a989: rts
;--------------------
lbl_a98a: asl $4a ;If octave is negative, multiply period by 2
rol $4b
lda #$0 ;And re-set octave to zero
sta $4e
rts