Prev: C656 Up: Map Next: C6B9
C661: Play one note
Used by the routines at PLAY_TUNE_ONCE and PLAY_TUNE.
A note byte's low six bits index NOTES, with 0 a rest; its top two bits are its length less one, one to four units. The note's three bytes are two loop counts that time each half of a wave and how many waves make one unit, and the wave count rises with the pitch, which keeps every unit close to the same length whatever the note. The speaker is driven directly, off for one half-wave and on for the other, with the border black.
The routine and NOTES are byte for byte Knight Lore's (play_note there), Alien 8's and Pentagram's, at other addresses.
Input
A the note byte
DE the address of the note byte; on exit, the next one
PLAY_NOTE C661 AND $3F index 0 is a rest
C663 JR Z,NOTE_REST
C665 LD L,A HL = NOTES + 3 * index
C666 LD H,$00
C668 ADD HL,HL
C669 CALL ADD_HL_A
C66C LD BC,NOTES
C66F ADD HL,BC
C670 LD B,(HL) B and C time the half-wave; HL = the number of waves in one unit
C671 INC HL
C672 LD C,(HL)
C673 INC HL
C674 LD L,(HL)
C675 LD H,$00
C677 LD A,(DE) the top two bits: the length, one to four units
C678 RLCA
C679 RLCA
C67A AND $03
C67C INC A
C67D PUSH DE HL = the waves in one unit times the length
C67E LD E,L
C67F LD D,H
PLAY_NOTE_0 C680 DEC A
C681 JR Z,PLAY_NOTE_1
C683 ADD HL,DE
C684 JR PLAY_NOTE_0
PLAY_NOTE_1 C686 POP DE the tune pointer back in DE
PLAY_NOTE_2 C687 PUSH BC speaker off, border black; wait
C688 XOR A
C689 OUT ($FE),A
PLAY_NOTE_3 C68B DJNZ PLAY_NOTE_3
C68D DEC C
C68E JR NZ,PLAY_NOTE_3
C690 POP BC speaker on; wait
C691 PUSH BC
C692 LD A,$10
C694 OUT ($FE),A
PLAY_NOTE_4 C696 DJNZ PLAY_NOTE_4
C698 DEC C
C699 JR NZ,PLAY_NOTE_4
C69B POP BC until all the waves are done
C69C DEC HL
C69D LD A,H
C69E OR L
C69F JR NZ,PLAY_NOTE_2
C6A1 INC DE on to the next note byte
C6A2 RET
NOTE_REST C6A3 LD A,(DE) NOTE_REST: a rest, one to four units...
C6A4 INC DE
C6A5 RLCA
C6A6 RLCA
C6A7 AND $03
C6A9 INC A
C6AA LD L,A ...each 17163 turns of a 26 T-state loop, about 0.127 seconds (no tune has a rest, and this never ran)
C6AB LD BC,$430B
PLAY_NOTE_5 C6AE PUSH BC
PLAY_NOTE_6 C6AF DEC BC
C6B0 LD A,B
C6B1 OR C
C6B2 JR NZ,PLAY_NOTE_6
C6B4 POP BC
C6B5 DEC L
C6B6 JR NZ,PLAY_NOTE_5
C6B8 RET
Prev: C656 Up: Map Next: C6B9