![]() |
Routines |
| Prev: 45775 | Up: Map | Next: 45874 |
|
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 MusicNotes 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.
|
||||||||||||||
|
Look up the delays and the number of cycles.
|
||||||||||||||
| PlayNote | 45786 | AND %00111111 | Isolate the number of the note. | |||||||||||
| 45788 | JR Z,PlayNote_Rest | Jump to PlayNote_Rest if the note is a rest. | ||||||||||||
| 45790 | LD L,A | Set HL to the note number. | ||||||||||||
| 45791 | LD H,0 | |||||||||||||
| 45793 | ADD HL,HL | Double the number. | ||||||||||||
| 45794 | CALL AddAToHL | Call AddAToHL. | ||||||||||||
|
Point HL at the entry of the note in the table, MusicNotes.
|
||||||||||||||
| 45797 | LD BC,45874 | Point BC at MusicNotes. | ||||||||||||
| 45800 | ADD HL,BC | Find the entry for the note. | ||||||||||||
|
Fetch the first byte of the delay between the clicks of the speaker.
|
||||||||||||||
| 45801 | LD B,(HL) | Fetch the first delay. | ||||||||||||
| 45802 | INC HL | Move on. | ||||||||||||
|
Fetch the second byte of the delay.
|
||||||||||||||
| 45803 | LD C,(HL) | Fetch the second delay. | ||||||||||||
| 45804 | INC HL | Move on. | ||||||||||||
|
Fetch the basic length of the note.
|
||||||||||||||
| 45805 | LD L,(HL) | Fetch the number of cycles. | ||||||||||||
| 45806 | LD H,0 | Set H to 0. | ||||||||||||
|
Bits 6 and 7 are the length, less one.
|
||||||||||||||
| 45808 | LD A,(DE) | Fetch the note again. | ||||||||||||
| 45809 | RLCA | Move bits 6 and 7 to bits 0 and 1. | ||||||||||||
| 45810 | RLCA | |||||||||||||
|
Isolate the length of the note.
|
||||||||||||||
| 45811 | AND %00000011 | Isolate the length of the note. | ||||||||||||
|
Make it 1 to 4.
|
||||||||||||||
| 45813 | INC A | Add one. | ||||||||||||
| 45814 | PUSH DE | Stash the note address on the stack. | ||||||||||||
| 45815 | LD E,L | Copy the number of cycles to DE. | ||||||||||||
| 45816 | LD D,H | |||||||||||||
|
Multiply the number of cycles by the length.
|
||||||||||||||
| PlayNote_Multiply | 45817 | DEC A | Count down the length. | |||||||||||
| 45818 | JR Z,PlayNote_MultiplyDone | Jump to PlayNote_MultiplyDone if the length has been used up. | ||||||||||||
| 45820 | ADD HL,DE | Add the number of cycles again. | ||||||||||||
| 45821 | JR PlayNote_Multiply | Jump to PlayNote_Multiply. | ||||||||||||
| PlayNote_MultiplyDone | 45823 | POP DE | Restore the note address from the stack. | |||||||||||
|
Each pass makes one cycle of the note.
|
||||||||||||||
| PlayNote_Cycle | 45824 | PUSH BC | Stash the delays on the stack. | |||||||||||
| 45825 | XOR A | Set A to 0. | ||||||||||||
| 45826 | OUT (254),A | Turn the speaker off. | ||||||||||||
| PlayNote_OffDelay | 45828 | DJNZ PlayNote_OffDelay | Wait with the speaker off, using the first byte of the delay for the first pass and 256 for each pass after it. | |||||||||||
| 45830 | DEC C | Count down the second delay. | ||||||||||||
| 45831 | JR NZ,PlayNote_OffDelay | Jump to PlayNote_OffDelay until it has finished. | ||||||||||||
| 45833 | POP BC | Restore the delays from the stack. | ||||||||||||
| 45834 | PUSH BC | Stash the delays on the stack. | ||||||||||||
| 45835 | LD A,16 | Set bit 4. | ||||||||||||
| 45837 | OUT (254),A | Turn the speaker on. | ||||||||||||
| PlayNote_OnDelay | 45839 | DJNZ PlayNote_OnDelay | Wait with the speaker on for the same length of time. | |||||||||||
| 45841 | DEC C | Count down the second delay. | ||||||||||||
| 45842 | JR NZ,PlayNote_OnDelay | Jump to PlayNote_OnDelay until it has finished. | ||||||||||||
| 45844 | POP BC | Restore the delays from the stack. | ||||||||||||
| 45845 | DEC HL | Decrease the number of cycles left to play. | ||||||||||||
| 45846 | LD A,H | Jump to PlayNote_Cycle until there are none left. | ||||||||||||
| 45847 | OR L | |||||||||||||
| 45848 | JR NZ,PlayNote_Cycle | |||||||||||||
| 45850 | INC DE | Point at the next note. | ||||||||||||
| 45851 | RET | Return. | ||||||||||||
|
A rest is a silent pause of one to four units.
|
||||||||||||||
| PlayNote_Rest | 45852 | LD A,(DE) | Fetch the length of the rest. | |||||||||||
| 45853 | INC DE | Point at the next note. | ||||||||||||
| 45854 | RLCA | Rotate bits 6-7 of the note into the low bits. | ||||||||||||
| 45855 | RLCA | |||||||||||||
| 45856 | AND %00000011 | Keep bits 0 and 1. | ||||||||||||
| 45858 | INC A | Add one. | ||||||||||||
| 45859 | LD L,A | Keep it in L. | ||||||||||||
| 45860 | LD BC,17163 | Set BC to 17163. | ||||||||||||
| PlayNote_RestUnit | 45863 | PUSH BC | Stash the pause length on the stack. | |||||||||||
| PlayNote_RestDelay | 45864 | DEC BC | Count down the pause. | |||||||||||
| 45865 | LD A,B | Jump to PlayNote_RestDelay until the length of the wait is zero. | ||||||||||||
| 45866 | OR C | |||||||||||||
| 45867 | JR NZ,PlayNote_RestDelay | |||||||||||||
| 45869 | POP BC | Restore the pause length from the stack. | ||||||||||||
| 45870 | DEC L | Count down the units of the rest. | ||||||||||||
| 45871 | JR NZ,PlayNote_RestUnit | Jump to PlayNote_RestUnit until it is zero. | ||||||||||||
| 45873 | RET | Return. | ||||||||||||
|
View the equivalent code in;
|
||||||||||||||
| Prev: 45775 | Up: Map | Next: 45874 |