Prev: C0CF Up: Map Next: C11C
C0DA: 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
PlayNote C0DA AND %00111111 Keep the note number.
C0DC JR Z,PlayRest Jump to make a rest if the note number is zero.
Look up the delays and the number of cycles.
C0DE LD L,A Set HL to the note number.
C0DF LD H,$00
C0E1 ADD HL,HL Double the number.
C0E2 CALL AddAToHL ...and add the note number to give three times it, using AddAToHL.
C0E5 LD BC,$C132 Add the address of the table of notes.
C0E8 ADD HL,BC
C0E9 LD B,(HL) Fetch the first delay.
C0EA INC HL Move on.
C0EB LD C,(HL) Fetch the second delay.
C0EC INC HL Move on.
C0ED LD L,(HL) Fetch the number of cycles of the shortest note.
C0EE LD H,$00 Set H to 00.
Bits 6 and 7 are the length, less one.
C0F0 LD A,(DE) Fetch the note again.
C0F1 RLCA Move bits 6 and 7 to bits 0 and 1.
C0F2 RLCA
C0F3 AND %00000011 Keep bits 0 and 1.
C0F5 INC A Add one.
C0F6 PUSH DE Save the address of the note.
C0F7 LD E,L Copy the number of cycles of the shortest note into DE.
C0F8 LD D,H
Multiply the number of cycles by the length.
PlayMusic_Process_Multiply C0F9 DEC A Count down the length.
C0FA JR Z,PlayMusic_Process_Finished Jump to PlayMusic_Process_Finished if the length has been used up.
C0FC ADD HL,DE Otherwise add on the number of cycles of the shortest note again...
C0FD JR PlayMusic_Process_Multiply Jump to PlayMusic_Process_Multiply.
PlayMusic_Process_Finished C0FF POP DE Restore the note address from the stack.
Each pass makes one cycle of the note.
PlayMusic_Process_Cycle C100 PUSH BC Stash the delays on the stack.
C101 XOR A Set A to 00.
C102 OUT ($FE),A Turn the speaker off.
PlayMusic_Process_LowWait C104 DJNZ PlayMusic_Process_LowWait Wait for the time of the low half of a cycle...
Count down the rest of the delay.
C106 DEC C Count down the second delay.
C107 JR NZ,PlayMusic_Process_LowWait Jump to PlayMusic_Process_LowWait until it has finished.
C109 POP BC Restore the delays from the stack.
C10A PUSH BC Stash the delays on the stack.
C10B LD A,$10 Set bit 4.
C10D OUT ($FE),A Turn the speaker on.
PlayMusic_Process_HighWait C10F DJNZ PlayMusic_Process_HighWait Wait for the time of the high half of a cycle...
Count down the rest of the delay.
C111 DEC C Count down the second delay.
C112 JR NZ,PlayMusic_Process_HighWait Jump to PlayMusic_Process_HighWait until it has finished.
C114 POP BC Restore the delays from the stack.
Count down the number of cycles, and loop back until it reaches zero.
C115 DEC HL Count down the cycles.
C116 LD A,H Jump to PlayMusic_Process_Cycle until all of the cycles have been played.
C117 OR L
C118 JR NZ,PlayMusic_Process_Cycle
C11A INC DE Move on to the next note.
C11B RET Return.
View the equivalent code in;
Prev: C0CF Up: Map Next: C11C