SirCmpwn wrote:
Please please!
And well commented would be nice as well, so I can optimize it and possibly apply the solution in pure Axe.
Here it is; it's a fairly large chunk of code, so take a while to read it. This was originally written by Ben Ryves, with some minor modifications by me. The (bport) is of course the equate for the link port. I can provide you with the table of durations and frequencies for this as well, or you can just look at the mobileTunes readme.
Code: ; Play the tone dur=bc, period = h,l,d,e (4 channel sound - h,l = left speaker, d,e = right speaker)
playTone:
di
xor a
ld (toneMask1+1),a
ld (toneMask2+1),a
inc b
; INIT CHANNEL A
ld a,h
ld (toneAPitch+1),a
or a
jr z,isRestA
ld a,1
jr notRestA
isRestA:
xor a
notRestA:
ld (toneAChange+1),a
; INIT CHANNEL B
ld a,l
ld (toneBPitch+1),a
or a
jr z,isRestB
ld a,1
jr notRestB
isRestB:
xor a
notRestB:
ld (toneBChange+1),a
; INIT CHANNEL C
ld a,d
ld (toneCPitch+1),a
or a
jr z,isRestC
ld a,2
jr notRestC
isRestC:
xor a
notRestC:
ld (toneCChange+1),a
; INIT CHANNEL D
ld a,e
ld (toneDPitch+1),a
or a
jr z,isRestD
ld a,2
jr notRestD
isRestD:
xor a
notRestD:
ld (toneDChange+1),a
toneMaskPreserve:
push bc
toneMask:
and 1
jr z,playPart2
ld a,(toneMask1+1)
jr playPart1
playPart2:
ld a,(toneMask2+1)
playPart1:
out (bPort),a
pitchLoop:
dec h
jr nz,noPitchA
toneMask1:
ld a,0
toneAChange:
xor 0
ld (toneMask1+1),a
toneAPitch:
ld h,0
noPitchA:
dec l
jr nz,noPitchB
toneMask2:
ld a,0
toneBChange:
xor 0
ld (toneMask2+1),a
toneBPitch:
ld l,0
noPitchB:
dec d
jr nz,noPitchC
ld a,(toneMask1+1)
toneCChange:
xor 0
ld (toneMask1+1),a
toneCPitch:
ld d,0
noPitchC:
dec e
jr nz,noPitchD
ld a,(toneMask2+1)
toneDChange:
xor 0
ld (toneMask2+1),a
toneDPitch:
ld e,0
noPitchD:
extendDuration:
ld a,0
dec a
ld (extendDuration+1),a
jr nz,toneMask
ld a,KeyRow_Top
pop bc
out (1),a
in a,(1)
cp dkY
jr nz,notTimeToQuit
pop hl
ret
notTimeToQuit:
dec c
jp nz,toneMaskPreserve
dec b
jp nz,toneMaskPreserve
ret