This article is intended to give a basic overview of using the Yamaha YMF-262 (OPL3) chip on the Color Computer, to help aid in the understanding of its use on the MEGA mini MPI and the upcoming CoCo SDC sound extender.
The YMF-262 (OPL3) is an FM synthesis sound chip that offers a huge amount of flexibility in generating musical notes and sound effects. It’s backwards compatible with OPL2 sound chips, and starts up in this mode by default.
Programming the chip can be a little intimidating at first as there are a lot of options to work with. The OPL3 is extremely flexible in how it generates sound, which leads to a lot of complexity (potentially), but it can be operated in a fairly simple manner as well. How it’s used is up to the programmer and how much CPU time he wants to devote to manipulating chip settings / channels to get the desired effects.
For the purposes of simplifying things here as we begin, our examples will stick to two-operator melodic tones.
Eighteen operator pairs are available on the OPL3. The simplest way to think about an operator pair is probably as an instrument. So essentially there are 18 ‘instruments’ that you can define.
For each instrument, there are two operators; a carrier wave, and a modulator. The carrier is the basic sound wave for the instrument, which is then modulated in either AM or FM mode to produce a more complex sound.
All the settings for an instrument pair need to be written to specific registers in the chip. Let’s take a look at registers for the first operator pair (or channel).
These registers define the carrier and modulator for the sound.
MODULATOR CARRIER PURPOSE
$20 $23 VIBRATO,SUSTAIN,SCALING,MULTI
$40 $43 KEY SCALE LEVEL, OUTPUT LEVEL
$60 $63 ATTACK / DECAY
$80 $83 SUSTAIN LEVEL / RELEASE RATE
$E0 $E3 WAVEFORM
In addition to the operators, there are three registers that control the overall channel (instrument). Again for the first channel.
$A0 – F-NUM, OR FREQUENCY (LOWER 8 BITS)
$B0 – KEY ON/OFF, BLOCK NUMBER (OCTAVE), UPPER TWO BITS OF F-NUM
$C0 – OUTPUT CHANNEL, FEEDBACK, SYNTHESIS TYPE
These 13 registers are all that’s needed to define a sound and play it. In its simplest usage, the 10 registers for the operator pair would be set up, and then afterward the sound controlled using only two registers ($A0 and $B0) to change note frequency and key-on / key-off.
Some assembly code
Let’s go through some 6809 assembly code that sets up an instrument and plays through the natural notes in an octave as an example. Leaving out the equates, here is the code that sets up the MEGA mini YMF-262 and plays the scale.
This portion does a little basic setup for the CoCo system and switches the sound source to the cartridge port sound-in line.
Now we set up the MEGA mini’s analog switch to use the YMF-262 for sound, and select it for /SCS routing so that we can program the chip.
Here, we set up the instrument by writing the registers for the operator pair. You can see the register numbers and their data below the routine for writing them. You’ll notice the NOP instruction between selecting the register to be written and sending the data. This is because the YMF-262 requires 32 of its clock cycles to process the register selection (at 14.318MHz). This works out to a 2,233.6ns, which is just slightly less than 2 cycles for the 6809 at .89MHz (2,235ns). You’d want a 4 cycle delay at 1.79MHz.
Now our instrument is set up, except for the frequency data (F-NUM and octave).
Let’s play the octave. At the bottom of the following code, you can see the frequency data for each note based on the A440 pitch standard. This is a 10 digit binary number (0-1023) and the base frequency for each note. This, along with the octave data (block number) in register $B0, determines the frequency of the note.
Three register writes are required per note if the frequency changes. Frequency data, key-on, and key off. A note could be played with only two without changing the lower byte of frequency data.
Notes here are played in succession through the octave and then repeated endlessly.
And that’s really all there is to it for regular melodic notes. Your program of course has to supply the timing for key-on/key-off events, and handle frequency changes, but the OPL can do the rest for simple music. You can also go nuts with the CPU and manipulate the various registers during play for different effects, but you don’t have to.
It’s a very flexible system.
I hope that gives enough insight to get some of us started on writing for the OPL3 on the CoCo. As you can (hopefully) see, it’s really not as difficult to program as it might seem at first. The OPL is *very* geared towards synthesizer type music, and it really shows in its design and operation.
Coming up with instrument definitions
Many instruments and sound effects have been designed for the OPL3, from general midi to all sorts of sounds used in games from back in the 90’s. You can use instrument data from these sources as well as use bank editors such as this OPL3 Bank Editor to experiment and come up with your own instruments.