Prev: B4BA Up: Map Next: B51D
B4C5: 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. A 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 0.155 seconds 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 and its table, and Pentagram's PLAY_NOTE.
Input
A the note byte
DE the address of the note byte; on exit, the next one
PLAY_NOTE B4C5 AND $3F index 0 is a rest
B4C7 JR Z,PLAY_NOTE_5
B4C9 LD L,A HL = NOTES + 3 * index
B4CA LD H,$00
B4CC ADD HL,HL
B4CD CALL ADD_HL_A
B4D0 LD BC,NOTES
B4D3 ADD HL,BC
B4D4 LD B,(HL) B and C time the half-wave; HL = the number of waves in one unit
B4D5 INC HL
B4D6 LD C,(HL)
B4D7 INC HL
B4D8 LD L,(HL)
B4D9 LD H,$00
B4DB LD A,(DE) the top two bits: the length, one to four units
B4DC RLCA
B4DD RLCA
B4DE AND $03
B4E0 INC A
B4E1 PUSH DE HL = the waves in one unit times the length
B4E2 LD E,L
B4E3 LD D,H
PLAY_NOTE_0 B4E4 DEC A
B4E5 JR Z,PLAY_NOTE_1
B4E7 ADD HL,DE
B4E8 JR PLAY_NOTE_0
PLAY_NOTE_1 B4EA POP DE
PLAY_NOTE_2 B4EB PUSH BC speaker off, border black; wait
B4EC XOR A
B4ED OUT ($FE),A
PLAY_NOTE_3 B4EF DJNZ PLAY_NOTE_3
B4F1 DEC C
B4F2 JR NZ,PLAY_NOTE_3
B4F4 POP BC
B4F5 PUSH BC speaker on; wait
B4F6 LD A,$10
B4F8 OUT ($FE),A
PLAY_NOTE_4 B4FA DJNZ PLAY_NOTE_4
B4FC DEC C
B4FD JR NZ,PLAY_NOTE_4
B4FF POP BC
B500 DEC HL until all the waves are done
B501 LD A,H
B502 OR L
B503 JR NZ,PLAY_NOTE_2
B505 INC DE on to the next note byte
B506 RET
PLAY_NOTE_5 B507 LD A,(DE) a rest: one to four units...
B508 INC DE
B509 RLCA
B50A RLCA
B50B AND $03
B50D INC A
B50E LD L,A ...each 17163 turns of a 26 T-state loop, about 0.127 seconds (no tune has a rest, and the sessions never played one)
B50F LD BC,$430B
PLAY_NOTE_6 B512 PUSH BC
PLAY_NOTE_7 B513 DEC BC
B514 LD A,B
B515 OR C
B516 JR NZ,PLAY_NOTE_7
B518 POP BC
B519 DEC L
B51A JR NZ,PLAY_NOTE_6
B51C RET
Prev: B4BA Up: Map Next: B51D