![]() |
Routines |
| Prev: 44943 | Up: Map | Next: 45020 |
|
Used by the routines at PlayMusicOnce and PlayMusic.
Plays a note of a tune, or a rest. Bits 0 to 5 of the byte are the number of the note (0 is a rest), and bits 6 and 7 are one less than the length of the note in beats. The pitch of each note comes from the table at NoteTable, in which a note has three bytes: the low and high bytes of the length of each half of a pulse, and the number of pulses in a beat.
|
||||||||||||
| PlayNote | 44954 | AND %00111111 | Keep the number of the note. | |||||||||
| 44956 | JR Z,PlayRest | Play a rest if there is no note. | ||||||||||
|
Look up the delays and the number of cycles in the table at NoteTable.
|
||||||||||||
| 44958 | LD L,A | Set HL to the note number. | ||||||||||
| 44959 | LD H,0 | |||||||||||
| 44961 | ADD HL,HL | Double the number. | ||||||||||
| 44962 | CALL AddAToHL | Add the number again to treble it. | ||||||||||
| 44965 | LD BC,45042 | Point HL at the entry for the note. | ||||||||||
| 44968 | ADD HL,BC | |||||||||||
| 44969 | LD B,(HL) | Fetch the low byte of the half-period. | ||||||||||
| 44970 | INC HL | Move on to the high byte. | ||||||||||
| 44971 | LD C,(HL) | Fetch the high byte of the half-period. | ||||||||||
| 44972 | INC HL | Move on to the number of pulses in a beat. | ||||||||||
| 44973 | LD L,(HL) | Fetch the number of pulses in a beat. | ||||||||||
| 44974 | LD H,0 | Clear H. | ||||||||||
|
Bits 6 and 7 of the note are the length, less one.
|
||||||||||||
| 44976 | LD A,(DE) | Fetch the note again. | ||||||||||
| 44977 | RLCA | Rotate the length bits into the bottom of the byte. | ||||||||||
| 44978 | RLCA | |||||||||||
| 44979 | AND %00000011 | Keep them. | ||||||||||
| 44981 | INC A | Add one to get the length in beats. | ||||||||||
| 44982 | PUSH DE | Stash the address of the note on the stack. | ||||||||||
| 44983 | LD E,L | Copy the number of pulses into DE. | ||||||||||
| 44984 | LD D,H | |||||||||||
|
Multiply the number of cycles by the length of the note.
|
||||||||||||
| PlayNote_Multiply | 44985 | DEC A | Count down the beats. | |||||||||
| 44986 | JR Z,PlayNote_MultiplyDone | Finish multiplying when the beats are used up. | ||||||||||
| 44988 | ADD HL,DE | Add the number of pulses for another beat. | ||||||||||
| 44989 | JR PlayNote_Multiply | Go back to count the next beat. | ||||||||||
|
The number of cycles now matches the length of the note.
|
||||||||||||
| PlayNote_MultiplyDone | 44991 | POP DE | Retrieve the address of the note. | |||||||||
|
Each pass of this loop makes one cycle of the note, so the length of the note is the number of cycles.
|
||||||||||||
| PlayNote_Cycle | 44992 | PUSH BC | Stash the half-period on the stack. | |||||||||
| 44993 | XOR A | Clear the speaker bit. | ||||||||||
| 44994 | OUT (254),A | Turn the speaker off. | ||||||||||
| PlayNote_OffDelay | 44996 | DJNZ PlayNote_OffDelay | Wait for the low half of the pulse. | |||||||||
| 44998 | DEC C | Count down the high byte of the half-period. | ||||||||||
| 44999 | JR NZ,PlayNote_OffDelay | Loop back until the low half is over. | ||||||||||
| 45001 | POP BC | Retrieve the half-period. | ||||||||||
| 45002 | PUSH BC | Stash the half-period on the stack again. | ||||||||||
| 45003 | LD A,16 | Set the speaker bit. | ||||||||||
| 45005 | OUT (254),A | Turn the speaker on. | ||||||||||
| PlayNote_OnDelay | 45007 | DJNZ PlayNote_OnDelay | Wait for the high half of the pulse. | |||||||||
| 45009 | DEC C | Count down the high byte of the half-period. | ||||||||||
| 45010 | JR NZ,PlayNote_OnDelay | Loop back until the high half is over. | ||||||||||
| 45012 | POP BC | Retrieve the half-period. | ||||||||||
| 45013 | DEC HL | Count down the pulses. | ||||||||||
| 45014 | LD A,H | Jump to PlayNote_Cycle if any pulses are left. | ||||||||||
| 45015 | OR L | |||||||||||
| 45016 | JR NZ,PlayNote_Cycle | |||||||||||
| 45018 | INC DE | Move on to the next note. | ||||||||||
| 45019 | RET | Return. | ||||||||||
|
View the equivalent code in;
|
||||||||||||
| Prev: 44943 | Up: Map | Next: 45020 |