Sound Out Your Spectrum If snowdrops and Spectrums are among your favourite things, these valuable machine-code routines by Jeremy Hall will help improve your micro's sound-producing ability. Impressive noises can be produced to rival the machine-code routines used by software houses. With a little knowledge of machine code and of how the Spectrum produces sound, some quite impressive noises can be produced, despite its limited sound facility. The Spectrum produces sound by sending a series of clicks to its internal loudspeaker. The time interval between each clock, and hence the pitch of the note, is dependent on the value held in the HL register pair of the Z80. The higher the value stored in HL, the londer the interval between clicks, hence the lower the pitch of the note produced, that is, the number of clicks, is controlled by the value stored in the DE register pair; the larger the number, the longer the note. Having set these registers to the required values, it is then simply a matter of calling the sound-producing routine in the Basic ROM. This starts at address 03 B5 hex, 949 decimal. Program 1 demonstrates this idea very simply, and figure 1 shows the machine-code mnemonics of this program. Try changing some of the values of HL and DE in this pro- gram by altering the DATA statements, but before you do this, save the program on cassette in case you crash the system. It probably will not take you very long to tire of program 1 and you will want to move on to some more inter- esting sounds. This is where program 2 comes into it; figure 2 shows the machine-code mnemonics for this program. Register B is loaded with the number of times that the whole sound is to be repeated. Try loading it with 1, that is change the second number in the data statement from 10 to 1. HL and DE are set to the required value and the sound routine called. On returning from the routine, DE is loaded with 16, which is then added to HL to increase its value, and lower the pitch of the next note. The sound routine is then called again, and this process repeated 255 times. Register B is then decremented and if it is zero the pro- gram will end and return to Basic, otherwise the whole process will be repeated. Note that registers HL and BC must be saved by it. In the final program, program 3, the machine code held in each data statement is based on the previous program, but with different values of HL and DE in each case. Enter the program exactly as shown, with the correct number of zeros after each DATA statement. These zeros are used as padding to make each routine 30 bytes long and this make each USR address easier to remember - 32400 to 32430 and so on. Try experimenting with the values of HL and DE again; you might be surprised at the results. ___________________________________________________________ Mnemonic Hex Decimal Comment LD DE,128 11 7F 00 17 128 0 Note length LD HL,768 21 00 03 33 0 3 Pitch CALL 949 CD B5 03 205 181 3 Call sound RET C9 201 Return to Basic Figure 1. ___________________________________________________________ Mnemonic Hex Decimal Comment LD B,10 06 0A 6 10 Repeat sound 10 times PUSH BC C5 197 LD HL,15 21 0F 00 33 15 0 Initial pitch LD DE,20 11 14 00 17 20 0 Note duration PUSH HL E5 229 CALL 949 CD B5 03 205 181 3 Sound routine POP HL E1 225 LD DE,16 11 10 00 17 16 0 Decrease AND A A7 167 the ADC HL,DE ED 5A 237 90 pitch LD A,L 7D 125 Repeat CP 255 FE FF 254 255 256 times JRNZ -18 20 ED 32 237 POP BC C1 193 DJNZ -25 10 E6 16 230 Dec B, repeat if not zero RET C9 201 Figure 2. ___________________________________________________________