Jet Set Willy: Routine at 38562

| Routines

38562: Play the theme tune (Moonlight Sonata)

Used by the routine at 34762. For each of the 99 bytes in the tune data table at 34299, this routine produces two notes, each lasting approximately 0.15s; the second note is played at half the frequency of the first. Returns with the zero flag reset if ENTER, 0 or the fire button is pressed while the tune is being played.
Input
HL | 34299
38562 | LD A,(HL) | Pick up the next byte of tune data from the table at 34299
38563 | CP 255 | Has the tune finished?
38565 | RET Z | Return (with the zero flag set) if so
38566 | LD BC,100 | B=0 (short note duration counter), C=100 (short note counter)
38569 | XOR A | A=0 (border colour and speaker state)
38570 | LD E,(HL) | Save the byte of tune data in E for retrieval during the short note loop
38571 | LD D,E | Initialise D (pitch delay counter)
38572 | OUT (254),A | Produce a short note (approximately 0.003s) whose pitch is determined by the value in E
38574 | DEC D
38575 | JR NZ,38580
38577 | LD D,E
38578 | XOR 24
38580 | DJNZ 38572
38582 | EX AF,AF' | Save A briefly
38583 | LD A,C | Is the short note counter in C (which starts off at 100) down to 50 yet?
38584 | CP 50
38586 | JR NZ,38590 | Jump if not
38588 | RL E | Otherwise double the value in E (which halves the note frequency)
38590 | EX AF,AF' | Restore the value of A
38591 | DEC C | Decrement the short note counter in C
38592 | JR NZ,38572 | Jump back unless we've finished playing 50 short notes at the lower frequency
38594 | CALL 38601 | Check whether ENTER, 0 or the fire button is being pressed
38597 | RET NZ | Return (with the zero flag reset) if it is
38598 | INC HL | Move HL along to the next byte of tune data
38599 | JR 38562 | Jump back to play the next batch of 100 short notes