VIC Sound Generator
Robert Lee, Vancouver, B.C.
Adding sounds to VIC can significantly slow down a BASIC program. The action stops and waits for the sound to finish. This could be especially annoying when you want a game to run as fast as possible. With this sound generator, you can add sounds in BASIC easily and without a speed penalty.
Among the novel features of the VIC-20 are its sound capabilities. These give it an advantage over the PET, bringing a new dimension to game programs. However, one of the problems I and undoubtedly other VIC owners have encountered is that, while manipulating the sound generators in a BASIC program, it is not possible to do anything else.
This is especially a problem in game programs written in BASIC and using extensive graphics. Either you have to write such programs without complex sound effects, or you have to settle for slow motion.
Faster Sound
Faced with this problem, I decided to write a machine language (ML) program for the VIC which adds speed to its sound generation capabilities. Most of the sound effects we use in game programs are sounds with increasing or decreasing tones. For example, a simple way to simulate the sound of a laser with the VIC is:
FOR K = 250 TO 240 STEP -1 : POKE36876, K : NEXT
The ML program works along these lines, except that it is necessary to use only one POKE command. It generates sounds with increasing or decreasing frequency to make almost any kind of sound effect possible.
The program "VIC Sound" places a machine language program in the cassette buffer of the VIC. This means, of course, that you cannot transfer data using the cassette player while you are running the program. By changing the contents of memory locations 788-789 (decimal), the interrupt system of the computer is used to run the ML program.
As you know, the VIC has four "speakers" to make music and noise. The first and second speakers, activated by POKEing memory locations 36874 and 36875, are used for sounds with increasing tones. The third speaker (36876) is used for sounds with decreasing tones. The fourth speaker, activated by memory location 36877, is used mainly for explosions.
The ML program stores a starting number into the appropriate location and increases or decreases it for the period specified by the user. The interrupt of the computer will run through the program 60 times a second, which means that the starting number or tone will increase or decrease 60 times in one second.
Sound Duration
To make this a little clearer, let me explain that four memory locations have been assigned in the ML program to activate the four speakers, and four others to control the duration of the sounds.
Speaker | To activate | Duration |
1st | 846 | 858 |
2nd | 847 | 888 |
3rd | 848 | 918 |
4th | 849 | 948 |
The number POKEd into locations 846-849 is the starting number which is stored in location 853 (dec); the initial value is 222, but this may be changed for the kind of sound you require. Locations 858, 888, 918, and 948 control the duration of the sounds. The program will generate the sounds for the number of jiffies (the l/60th of a second interval used to measure time in Commodore machines) specified in these locations.
For a demonstration, RUN the program and then type SYS828; this will trap the interrupt. It will also set the volume control (location 36878) to maximum. Now POKE 846,222.
Location 858 contains 10 (dec), so the sound you heard was for ten jiffies. What the program has done is store 222 in location 36874 (first speaker), incremented it by one every 60th of a second until ten jiffies elapsed, then stored 0 into the memory location to switch off the speaker. To change the duration of the sound to, say, 20 jiffies, POKE 858,20. Now POKE 846,222.
The same method can be used for the other speakers. POKE 858,10. To change the starting number (i.e., to get a tone which starts higher or lower), simply POKE into memory location 853. For example, POKE 853,240. Now POKE 846,240.
Explosion Simulation
It is necessary to POKE the starting number into locations 846-849; any other number will give only silence. Try POKE 847, 240 (second speaker); it gives a sound of increasing frequency like the first. Now POKE 853, 222 : POKE 848, 222. You notice this gives a sound that decreases in frequency. POKE 849, 222 will simulate an explosion. By manipulating the durations and starting number, you can get almost any kind of sound from the first three speakers and explosions from the fourth. However, when you are changing the duration of the sounds, make sure it is not too long; e.g., if you POKE 853, 50 : POKE 846, 222 the program will store 222 in location 36874 and increment by one every jiffy for 50 jiffies. But in this case the contents of 36874 would increase to 255 and then cycle back to zero. You would hear a note for only 33 jiffies, since a number less than 128 in the sound generators of the VIC produces silence.
When using this program, you cannot generate sounds the normal way. To do so, you must first reset the interrupt vector by SYS996. This will also set the volume control to zero. To use the ML program, add the subroutine starting at line 8900 to your own BASIC program; and you can create sound effects using just one POKE, which would otherwise require a series of POKEs.
In a BASIC program with lines 8900-9240 added, you would first have a line like this in the main program to enter the ML into memory:
10 GOSUB 8900 : REM SOUND GENERATOR
10 PRINT "{CLEAR}" 20 PRINT "{03 DOWN} {08 RIGHT} {REV} VIC20 {OFF}" 30 PRINT "{02 DOWN} {06 RIGHT} VIC SOUND" 800 GOSUB8900 900 END 8900 FOR J = 828TO1019 : READF : POKEJ, F : NEXT 9000 DATA 169, 15, 141, 14, 144, 120, 169, 82 9010 DATA 141, 20, 3, 169, 3, 141, 21, 3 9020 DATA 88, 96, 10, 15, 16, 64, 160, 0 9030 DATA 162, 222, 173, 78, 3, 201, 10, 176 9040 DATA 9, 238, 78, 3, 238, 10, 144, 76 9050 DATA 116, 3, 140, 10, 144, 236, 78, 3 9060 DATA 208, 6, 140, 78, 3, 142, 10, 144 9070 DATA 173, 79, 3, 201, 25, 176, 9, 238 9080 DATA 79, 3, 238, 11, 144, 76, 146, 3 9090 DATA 140, 11, 144, 236, 79, 3, 208, 6 9100 DATA 140, 79, 3, 142, 11, 144, 173, 80 9110 DATA 3, 201, 16, 176, 9, 238, 80, 3 9120 DATA 206, 12, 144, 76, 176, 3, 140, 12 9130 DATA 144, 236, 80, 3, 208, 6, 140, 80 9140 DATA 3, 142, 12, 144, 173, 81, 3, 201 9150 DATA 64, 176, 28, 238, 81, 3, 173, 81 9160 DATA 3, 201, 22, 208, 7, 169, 176, 141 9170 DATA 13, 144, 240, 25, 201, 43, 208, 21 9180 DATA 169, 160, 141, 13, 144, 240, 14, 140 9190 DATA 13, 144, 236, 81, 3, 208, 6, 140 9200 DATA 81, 3, 142, 13, 144, 76, 191, 234 9210 DATA 169, 0, 141, 14, 144, 120, 169, 191 9220 DATA 141, 20, 3, 169, 234, 141, 21, 3 9230 DATA 88, 96, 0, 0, 0, 0, 0, 0 9240 RETURN