![]() |
Routines |
| Prev: 41409 | Up: Map | Next: 41487 |
|
Used by the routines at PlayMusic_WaitKey and PlayMusic.
Plays a note of a piece of music, using the table at NoteTable. Each entry in the table has three bytes: the lengths of the inner and outer delay loops, which set the pitch, and the number of cycles.
|
||||||||||||||
| PlayNote | 41420 | AND %00111111 | Use bits 0-5, the number of the note. | |||||||||||
| 41422 | JR Z,PlayRest | Jump to PlayRest if it is a rest. | ||||||||||||
|
Look up the delays and the number of cycles in the table at NoteTable.
|
||||||||||||||
| 41424 | LD L,A | Set HL to the note number. | ||||||||||||
| 41425 | LD H,0 | |||||||||||||
| 41427 | ADD HL,HL | Double it. | ||||||||||||
| 41428 | CALL AddToAddress | Add the note number to make three times the number. | ||||||||||||
| 41431 | LD BC,41510 | Point BC at NoteTable. | ||||||||||||
| 41434 | ADD HL,BC | Find the entry of the note. | ||||||||||||
| 41435 | LD B,(HL) | Fetch the length of the inner delay loop. | ||||||||||||
| 41436 | INC HL | Move onto the next byte. | ||||||||||||
| 41437 | LD C,(HL) | Fetch the length of the outer delay loop. | ||||||||||||
| 41438 | INC HL | Move onto the next byte. | ||||||||||||
| 41439 | LD L,(HL) | Fetch the number of cycles. | ||||||||||||
| 41440 | LD H,0 | Clear H. | ||||||||||||
|
Bits 6 and 7 of the note are the length, less one.
|
||||||||||||||
| 41442 | LD A,(DE) | Fetch the byte of the music again. | ||||||||||||
| 41443 | RLCA | Rotate bits 6 and 7 of the note down... | ||||||||||||
| 41444 | RLCA | ...to bits 0 and 1. | ||||||||||||
| 41445 | AND %00000011 | Keep them, but the result is not used. | ||||||||||||
| 41447 | XOR A | Set the multiplier to zero... | ||||||||||||
| 41448 | INC A | ...and then to one. | ||||||||||||
|
Multiply the number of cycles by the length of the note.
|
||||||||||||||
| PlayNote_Multiply | 41449 | PUSH DE | Save the address of the music. | |||||||||||
| 41450 | LD E,L | Copy the low byte of the number of cycles into E. | ||||||||||||
| 41451 | LD D,H | Copy the high byte into D. | ||||||||||||
| PlayNote_MultiplyLoop | 41452 | DEC A | Decrease the multiplier by one. | |||||||||||
| 41453 | JR Z,PlayNote_Multiplied | Jump to PlayNote_Multiplied if it is zero. | ||||||||||||
| 41455 | ADD HL,DE | Add the number of cycles again. | ||||||||||||
| 41456 | JR PlayNote_MultiplyLoop | Jump back to count the multiplier down. | ||||||||||||
| PlayNote_Multiplied | 41458 | POP DE | Restore the address of the music. | |||||||||||
|
Each pass of this loop makes one cycle of the note, so the length of the note is the number of cycles.
|
||||||||||||||
| PlayNote_Cycle | 41459 | PUSH BC | Save the lengths of the delay loops. | |||||||||||
| 41460 | XOR A | Clear the speaker bit. | ||||||||||||
| 41461 | OUT (254),A | Write to the port. | ||||||||||||
| PlayNote_LowDelay | 41463 | DJNZ PlayNote_LowDelay | Wait for the inner delay loop. | |||||||||||
| 41465 | DEC C | Decrease the outer loop count by one. | ||||||||||||
| 41466 | JR NZ,PlayNote_LowDelay | Jump back until the outer delay loop has finished. | ||||||||||||
| 41468 | POP BC | Restore the lengths of the delay loops. | ||||||||||||
| 41469 | PUSH BC | Save them again. | ||||||||||||
| 41470 | LD A,16 | Set the speaker bit. | ||||||||||||
| 41472 | OUT (254),A | Write to the port. | ||||||||||||
| PlayNote_HighDelay | 41474 | DJNZ PlayNote_HighDelay | Wait for the inner delay loop. | |||||||||||
| 41476 | DEC C | Decrease the outer loop count by one. | ||||||||||||
| 41477 | JR NZ,PlayNote_HighDelay | Jump back until the outer delay loop has finished. | ||||||||||||
| 41479 | POP BC | Restore the lengths of the delay loops. | ||||||||||||
| 41480 | DEC HL | Decrease the number of cycles by one. | ||||||||||||
| 41481 | LD A,H | Fetch the high byte of the number of cycles left. | ||||||||||||
| 41482 | OR L | Combine it with the low byte, to test whether it is zero. | ||||||||||||
| 41483 | JR NZ,PlayNote_Cycle | Jump back for the next cycle until the note has finished. | ||||||||||||
| 41485 | INC DE | Move onto the next byte of the music. | ||||||||||||
| 41486 | RET | Return. | ||||||||||||
|
View the equivalent code in;
|
||||||||||||||
| Prev: 41409 | Up: Map | Next: 41487 |