Prev: 50774 Up: Map Next: 50873
50785: Play Note
Used by the routines at PlayMusicOnce 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 50785 AND %00111111 Keep bits 0 to 5.
50787 JR Z,PlayNote_Rest Jump to PlayNote_Rest if the note is a rest.
50789 LD L,A Set HL to the note number.
50790 LD H,0
50792 ADD HL,HL Double the number.
50793 CALL AddAToHL Call AddAToHL.
Point HL at the entry for the note.
50796 LD BC,50873 Point BC at NoteTable.
50799 ADD HL,BC Find the entry for the note.
50800 LD B,(HL) Fetch the first delay.
50801 INC HL Move on.
50802 LD C,(HL) Fetch the second delay.
50803 INC HL Move on.
50804 LD L,(HL) Fetch the number of cycles.
50805 LD H,0 Set H to 0.
Bits 6 and 7 are the length, less one.
50807 LD A,(DE) Fetch the note again.
50808 RLCA Move bits 6 and 7 to bits 0 and 1.
50809 RLCA
50810 AND %00000011 Keep bits 0 and 1.
50812 INC A Add one.
50813 PUSH DE Stash the note address on the stack.
50814 LD E,L Copy the number of cycles to DE.
50815 LD D,H
Multiply the number of cycles by the length.
PlayNote_Length 50816 DEC A Count down the length.
50817 JR Z,PlayNote_Start Jump to PlayNote_Start if the length has been used up.
50819 ADD HL,DE Add the number of cycles again.
50820 JR PlayNote_Length Jump to PlayNote_Length.
PlayNote_Start 50822 POP DE Restore the note address from the stack.
Each pass makes one cycle of the note.
PlayNote_Cycle 50823 PUSH BC Stash the delays on the stack.
50824 XOR A Set A to 0.
50825 OUT (254),A Turn the speaker off.
PlayNote_Low 50827 DJNZ PlayNote_Low Wait for the first half of the cycle.
50829 DEC C Count down the second delay.
50830 JR NZ,PlayNote_Low Jump to PlayNote_Low until it has finished.
50832 POP BC Restore the delays from the stack.
50833 PUSH BC Stash the delays on the stack.
50834 LD A,16 Set bit 4.
50836 OUT (254),A Turn the speaker on.
PlayNote_High 50838 DJNZ PlayNote_High Wait for the second half of the cycle.
50840 DEC C Count down the second delay.
50841 JR NZ,PlayNote_High Jump to PlayNote_High until it has finished.
50843 POP BC Restore the delays from the stack.
50844 DEC HL Count down the cycles.
50845 LD A,H Jump to PlayNote_Cycle until all of the cycles have been played.
50846 OR L
50847 JR NZ,PlayNote_Cycle
50849 INC DE Move on to the next note.
50850 RET Return.
A rest is a silent pause of one to four units.
PlayNote_Rest 50851 LD A,(DE) Fetch the length of the rest.
50852 INC DE Move on to the next note.
50853 RLCA Move bits 6 and 7 to bits 0 and 1.
50854 RLCA
50855 AND %00000011 Keep bits 0 and 1.
50857 INC A Add one.
50858 LD L,A Keep it in L.
50859 LD BC,17163 Set BC to 17163.
PlayNote_RestLoop 50862 PUSH BC Stash the pause length on the stack.
PlayNote_RestWait 50863 DEC BC Count down the pause.
50864 LD A,B Jump to PlayNote_RestWait until it has finished.
50865 OR C
50866 JR NZ,PlayNote_RestWait
50868 POP BC Restore the pause length from the stack.
50869 DEC L Count down the units of the rest.
50870 JR NZ,PlayNote_RestLoop Jump to PlayNote_RestLoop until the rest has finished.
50872 RET Return.
Prev: 50774 Up: Map Next: 50873