Prev: 39593 Up: Map Next: 39692
39604: Play Note
Used by the routines at PlayMusic_WaitKey and PlayMusic.
Plays a note of a tune, or a rest if the note number is zero. Bits 0 to 5 select the entry in NoteTable that holds the two delays of a cycle and the number of cycles. Bits 6 and 7 are the length less one, which multiplies the number of cycles, so the note is one to four times as long.
Input
A The note
DE Address of the note
Output
DE Address of the next note
Look up the delays and the number of cycles.
PlayNote 39604 AND %00111111 Keep the note number.
39606 JR Z,PlayNote_Rest Jump to PlayNote_Rest if the note is a rest.
39608 LD L,A Set HL to the note number.
39609 LD H,0
39611 ADD HL,HL Double the number.
Add the address of the table of notes at NoteTable.
39612 CALL AddAToHL Call AddAToHL.
39615 LD BC,39692 Point BC at NoteTable.
39618 ADD HL,BC Find the entry for the note.
39619 LD B,(HL) Fetch the first delay.
39620 INC HL Move on.
39621 LD C,(HL) Fetch the second delay.
39622 INC HL Move on.
39623 LD L,(HL) Fetch the number of cycles.
39624 LD H,0 Set H to 0.
Bits 6 and 7 are the length, less one.
39626 LD A,(DE) Fetch the note again.
39627 RLCA Move bits 6 and 7 to bits 0 and 1.
39628 RLCA
39629 AND %00000011 Keep bits 0 and 1.
39631 INC A Add one.
39632 PUSH DE Save the address of the note.
39633 LD E,L Copy the number of cycles to DE.
39634 LD D,H
Multiply the number of cycles by the length.
PlayNote_Multiply 39635 DEC A Count down the length.
39636 JR Z,PlayNote_Play Jump to PlayNote_Play if it is zero.
39638 ADD HL,DE Add the number of cycles again.
39639 JR PlayNote_Multiply Jump back to PlayNote_Multiply.
PlayNote_Play 39641 POP DE Restore the note address from the stack.
Each pass makes one cycle of the note.
PlayNote_Cycle 39642 PUSH BC Stash the delays on the stack.
39643 XOR A Set A to 0.
39644 OUT (254),A Send it to the speaker.
PlayNote_Low 39646 DJNZ PlayNote_Low Wait for the first half of the cycle.
39648 DEC C Count down the second delay.
39649 JR NZ,PlayNote_Low Jump back to PlayNote_Low until none remain.
39651 POP BC Restore the delays from the stack.
39652 PUSH BC Stash the delays on the stack.
39653 LD A,16 Set bit 4.
39655 OUT (254),A Send it to the speaker.
PlayNote_High 39657 DJNZ PlayNote_High Wait for the second half of the cycle.
39659 DEC C Count down the second delay.
39660 JR NZ,PlayNote_High Jump back to PlayNote_High until none remain.
39662 POP BC Restore the delays from the stack.
39663 DEC HL Decrease the number of cycles.
39664 LD A,H Combine the high and low bytes.
39665 OR L
39666 JR NZ,PlayNote_Cycle Jump back to PlayNote_Cycle until the note is over.
39668 INC DE Move on to the next note.
39669 RET Return.
A rest is a silence of the same length as the other notes.
PlayNote_Rest 39670 LD A,(DE) Fetch the note byte.
39671 INC DE Move on to the next note.
39672 RLCA Move the top two bits to the bottom.
39673 RLCA
39674 AND %00000011 Keep them.
39676 INC A Add one to get a multiplier.
39677 LD L,A Use it as a counter.
PlayNote_RestLoop 39678 LD BC,17163 Set the delay.
PlayNote_Loop 39681 PUSH BC Save the delay.
PlayNote_Delay 39682 DEC BC Count down.
39683 LD A,B Combine the high and low bytes.
39684 OR C
39685 JR NZ,PlayNote_Delay Jump back to PlayNote_Delay until the delay is over.
39687 POP BC Restore the delay.
39688 DEC L Decrease the multiplier by one.
39689 JR NZ,PlayNote_Loop Jump back to PlayNote_Loop until none remain.
39691 RET Return.
Prev: 39593 Up: Map Next: 39692