Classic Computer Magazine Archive COMPUTE! ISSUE 146 / NOVEMBER 1992 / PAGE G3

SID simplified. (Sound Interface Device)(includes related article)
by Larry Cotton

THE SOUND INTERFACE DEVICE, AN INTEGRATED CIRCUIT CHIP FONDLY KNOWN AS SID, RESIDES DEEP IN THE ELECTRONIC INNARDS OF THE SID SIMPLIFIED COMMODORE 64 AND 128 COMPUTERS. IT HAS THE ABILITY TO LET YOUR COMPUTER PLAY, SING, MOAN, TALK, RING, THUMP, SCREAM, AND WHISPER. THIS CHIP ALONE HAS BEEN AT BY LARRY COTTON LEAST PARTIALLY RESPONSIBLE FOR THE FACT THAT COMMODORE STILL BUILDS THE 64 ALMOST NINE YEARS AFTER ITS SPLASHY INTRODUCTION--A COMPUTER LONGEVITY RECORD.

With all SID's capabilities, programming it in BASIC 2.0 remains an exercise in tedium, because of the many POKEs required to access the chip. (BASIC's POKE puts a number from 0 to 255 into a specific location in the computer.)

Fortunately for 128 owners, Commodore included with that machine a much-advanced BASIC 7.0, which does support SID and makes programming sounds much easier.

This article will attempt to cut through the confusion of programming SID and show you, step by step, how to access this marvelous chip. I'll confine my remarks to BASIC 2.0's commands, common to both the 64 and 128, and I'll show you how to cut down drastically on the number of POKEs. We'll start with the very simplest exercises and progress to the more advanced. If you'll stay with me from the beginning, you'll be pleased with the results.

If you're confused about programming SID, it will first be necessary to power down your own mind to rid it of all past frustrating programming sessions. Start from scratch. Remember that we're talking about only 29 of the 64's 64,000 or so memory registers. How complicated can they be?

Voices

A human being has only 1 voice: a saxophone has only 1 voice. A six-string guitar has 6; a piano, 88. SID has 3. Think of SID as a three-string guitar. That is, up to three notes can be played simultaneously, each under separate control (except for volume).

We'll limit our initial discussions to voice 1, which occupies SID's first seven memory registers. Remember that number, 7; it'll crop up again.

Order of POKEs

Here's a subject rarely addressed and, I think, fairly critical to the success of SID programming: the order that the memory registers are poked. Here is the normal order for playing a simple sound.

1. Clear the chip.

2. Turn up the volume.

3. Wait.

4. Set a frequency.

5. Set an envelope.

6. Turn on a waveform.

Clear the Chip

SID occupies memory registers 54272 through 54300. All those registers (except the last four, which cannot be poked) should always be cleared of their contents near the beginning of every BASIC program which uses sound. Here's how.

10 S=54272: FORJ=S TO S+24: POKEJ,0: NEXT: REM CLEAR SID

SID's first memory register should be defined as a constant; we'll use S. Then every other register may be defined as an offset of S. A FOR-NEXT loop pokes a 0 into each of the SID memory registers, effectively silencing the chip and preparing it for action.

Turn Up the Volume

SID's last pokable register is the volume control. Its range varies from 0 to 15, with 0 being the quietest setting. Let's turn the volume wide open with the following statement.

20 POKES+24,15: REM FULL VOLUME

Any memory register will accept values from 0 to 255, but 54296 uses only values from 0 (silent) to 15 (loud) to control volume. Normally, S+24 can keep a value of 15 throughout a BASIC program.

Wait

Turning up SID's volume makes a popping noise in the TV or monitor's speaker, and this can interfere with your carefully crafted sound. Always introduce a period of silence after first turning up SID's volume. We'll show a do-nothing time delay, but ordinarily at this point in a program you'd be preparing the screen, reading data, setting variables, and so forth.

30 FORT=1 TO 1000: NEXT

Set a Frequency

SID needs several other values poked to it before it will speak up. For instance, it needs a frequency. A frequency controls a note's pitch.

40 POKES+1,16: REM FREQUENCY

SID's voice 1 memory location 54273 (S+1) can use all values from 0 to 255. A value of, say, 5 produces sounds of low pitch (like a tuba). A value of 200 produces a high-pitched sound (like a piccolo).

Set an Envelope

What's an envelope? Nothing more complicated than how the volume of a single particular note (or sound effect) changes as it plays.

Think about the way a single guitar string sounds as it's being plucked. The guitar makes no sound at first, but its sound level rises from silence to maximum volume immediately after the string is plucked. That's called attack. The sound then gradually fades away to silence. This is called decay.

SID can create sounds quickly, like a guitar, or slowly, more like a bowed violin. It can also do two more things to a sound which a guitar can't. It can prolong a sound's volume at a particular level. This is called sustain. SID can also cause the sound to stop at a controllable rate with a process called release.

So, there you have it. The sound's envelope is made of attack, decay, sustain, and release. Each of these properties is controllable. For now though, the properties we'll use are attack and decay. A value of 12, in fact, poked to the envelope simulates the plucking of a guitar string. Later, we'll see how to determine values to poke. Where do we poke that envelope value? We poked the frequency into S+1, so the envelope must be poked into S+2, right? I'm afraid not; S+2 and S+3 are reserved for fine-tuning the pulse wave. S+4? Nope. That turns on voice 1. S+5 (54277) is voice 1's main envelope-controlling register.

50 POKES+5, 12: REM ATTACK/DECAY

If you want to experiment with sustain and release, add this line.

52 POKES+6,4: REM SUSTAIN/RELEASE

Turn On a Waveform

Last, but certainly not least, the sound needs a waveform. The 64 and 128 both feature four waveforms, each with a characteristic timbre. The triangle's sound is soft and mellow, the sawtooth mimics a saxophone, the pulse is hollow, and the noise is, well, noisy.

To actually begin the sound, we use voice 1's control register, S+4. We usually poke one of four particular values to produce the desired waveform. Triangle 17 Sawtooth 33 Pulse 65 Noise 129

Here's the way we'll select a waveform in our program. For this example, let's select a triangle waveform and poke its value into S+4.

60 POKES+4,33: REM TURN ON SAWTOOTH WAVEFORM

I like waveform 33, the triangle; it has a nice bite to it. If you've been entering the lines as presented, you can now run the program. You should be rewarded with a nice strong note that begins suddenly and gradually dies out. (Be sure to turn up the volume on your TV or monitor. The 15 that we poked to 54296 ensures that a good strong signal leaves the computer, but it won't be heard if your monitor volume is too low.)

Six lines to create a sound; that's not too bad, is it? Just remember the order.

1. Clear the chip (S through S+24).

2. Turn up the volume (S+24).

3. Wait.

4. Set voice 1's frequency (S+1).

5. Set voice 1's envelope (S+5).

6. Turn on voice 1's waveform (S+4).

Other Registers

We produced sound with only three of voice 1's memory registers; we didn't use registers S, S+2, S+3, and S+6. Let's look at them now.

S is the register that fine-tunes voice 1's frequency, which was coarsely set with S+1. If you wanted just a noise or a beep of no particular frequency, S+1 would be enough frequency control. To accurately produce musical notes, however, we must also poke a value to S.

What value? For frequencies of musical notes, the values are listed in your User's Guide in a table appropriately called Music Note Values. For nonmusical sounds, such as drums, it's mostly a matter of trial and error. Let's fine-tune the frequency we poked into S+1 in line 40. Add this line to the program to give us an exact pitch of middle C on the piano.

45 POKES, 195: REM FINE-TUNE FREQUENCY

Shaping the Pulse

While S+2 and S+3 control the shape of voice 1's pulse waveform, S+2 is rarely used. Poking a value of 8 to S+3 will give the pulse waveform a nice, even shape. It's not necessary, however, to shape a pulse waveform unless you plan to use it. To hear what the pulse sounds like, add line 55 and change line 60 as follows.

55 POKES+3,8: REM SHAPE OF PULSE 60 POKES+4,65: REM TURN ON PULSE WAVE FORM

Run the program again, and listen to the difference in the sound. Now experiment. Try waveforms 17 (triangle) and 129 (noise). Try various frequencies and envelopes. A reminder: Don't confuse voices with waveforms. SID has three voices (remember our three-string guitar?) and four waveforms (triangle, sawtooth, pulse, and noise).

Voices 2 and 3

So much for voice 1. If you want to play more than one voice at a time, each must be set up independently. For instance, let's add another note to harmonize with the last one. Modify lines 40-60.

40 POKES+1,16: POKES+8,21 45 POKES, 195: POKES+7,31 50 POKES+5,12: POKES+12,12 55 POKES+3,8: POKES+10,8 60 POKES+4,65: POKES+11,65

Voice 2's values follow the colon in each line. To program voice 2, just add 7 to voice 1's memory registers. In line 40, S+1 for voice 1 becomes S+8 for voice 2; in line 45, voice 1's S becomes voice 2's S+7; and so on.

Notice that in this example I've poked all voice 2 registers with the same values--except frequency in lines 40 and 45. Frequency values 21 and 31 (from the Music Note Values table) are needed to produce E above middle C on the piano. You may, if you like, set different envelopes for each voice (line 50) or different waveforms (line 60). If you run the program now, you'll hear a two-note chord in perfect harmony.

As you've probably noticed by now, SID's three voices are arranged within the chip in groups of seven registers each. Thus the control registers for voices 1, 2, and 3 are 54276, 54283, and 54290, respectively. The attack/decay portion of the three envelopes is set in registers 54277, 54284, and 54291, respectively. Therefore, to program voice 3, just offset the memory registers by 7 again.

As promised, here's how to reduce the proliferation of POKEs for this particular program. This technique won't always be applicable, but it may give you some ideas. Begin by copying lines 10 and 30 from the above program. Then delete the remaining lines. Now add these lines.

40 FORG=1 TO 10: READL,D:POKES+L,D 50 NEXT:END 100 DATA 1,16,8,21,0,195,7,31, 5,12,12,12,3,8,10,8,4,65,11,65

That's it! All SID's offsets from S (54272) and the pokable values have been compressed into one data line. One FOR-NEXT loop does the rest of the work.

While this simple program touches on only a few of the SID chip's wonderful possibilities, you can have fun experimenting with changing waveforms, frequency values, and voices. I hope programmers will be encouraged to further explore the sound capabilities of their 64s and 128s.