Sound
The console has two sound chips. The YM2612 makes six channels of sound from sine waves that change
each other (FM), and can play samples too. The PSG has three square waves and noise. The
32X adds PWM sound, and the Mega CD adds a chip for PCM and music from the
disc. This page has the first two chips, and how all five are put together.
The chips are in crates/mdemu-core/src/ym2612.rs, ym2612/ and psg.rs, ported from jgenesis
("the base"). The mixer, sound.rs, is mdemu's own, with the base's volumes
and filter frequencies. Performance says how samples are made many at once.
Who can write, and when it counts
| YM2612 | PSG |
| Z80 | $4000-$5FFF, on its own bus | odd addresses from $7F11 to $7F17, which costs time |
| 68000 | from $A04000 on, only while it has the Z80's bus | odd addresses from $C00011 to $C00017, or words |
| Can be read | the status, at each address | no |
Both processors write to the same chips, and nothing keeps one's writes from mixing with the
other's. Each chip follows one processor's time:
- The YM2612 follows the Z80's time. Before a read or write by either processor, it is brought
up to where the Z80 is. Then a write counts. Samples that were due before it are made as if it had
not come, and later ones have it. If one processor selects a register, the other's next data goes
there.
- The PSG follows the 68000's time, and is also brought up to it before each write. When the Z80
writes, the 68000's instruction that runs beside it does not count yet. So such a write counts
from the start of that instruction, and one by the 68000 from the end of its own.
A Z80 instruction makes all of its accesses at its start, so a write counts from there. A write by
the 68000 arrives once its instruction's time has passed and the Z80 has run for as long
(the console). At the end of the frame, both chips are brought up to then, and all
sound is mixed. It makes no difference when samples are worked out. A test compares mixing after
each step with mixing once a frame.
The YM2612
A write to the first of four addresses selects a register for the whole chip or channels 1 to 3. One
to the third selects one for channels 4 to 6. The second and fourth both take the data, so only the
last selection says which half it is for.
The chip makes a sample every 1,008 cycles of the master clock. That is 24 cycles of its own,
and about 53,267 samples a second on NTSC or 52,781 on PAL. The LFO and the timers move on with each
sample, and the envelopes with each third one.
Operators and channels
A channel has four operators. Each one has a counter of 20 bits that goes round, and its upper 10
bits are its place in a sine wave. What it adds comes from the note, a small detune and a multiplier
from a half to 15. The chip works with logarithms: a table has the sine as an attenuation, the
envelope is added to it, and a second table gives the output of 14 bits. The tables are made when
they are first needed, and a test checks that they always have the same values.
An operator's output can move another one's place in its wave. The channel's algorithm says
which does. Those at the end of each chain are heard, and their outputs are added:
| Algorithm | Who moves whom |
| 0 | 1 → 2 → 3 → 4 |
| 1 | (1 + 2) → 3 → 4 |
| 2 | (1 + (2 → 3)) → 4 |
| 3 | ((1 → 2) + 3) → 4 |
| 4 | 1 → 2, and 3 → 4 |
| 5 | 1 → 2, 1 → 3, and 1 → 4 |
| 6 | 1 → 2, 3, and 4 |
| 7 | 1, 2, 3, and 4 |
Operator 1 can also move itself, by its last two outputs. The chip works in the order 1, 3, 2, 4,
and an output is not ready for the operator right after. So from 1 to 3, from 2 to 3 and from 2 to
4, the output of the sample before is used.
A note has a frequency of 11 bits and an octave. Its upper part waits until the lower part is
written, so that both count together. On channel 3, each operator can have a note of its own
(register $27).
Envelopes
An envelope is an attenuation of 10 bits, from 0 to 96 dB. When a key goes down, its wave starts at
0, and its attack begins. A key that is down already changes nothing. In the attack, a quiet sound
gets louder in larger steps than a loud one. Then come decay down to a level, sustain, and release
when the key goes up. A fixed attenuation (total level) is added to all of it. Higher notes go
faster, as far as the key scale says. At the two highest rates, there is no attack: the sound is at
its loudest at once. How often an envelope changes comes from a counter of 12 bits, which leaves out
0, and a table of steps.
SSG-EG lets an envelope repeat. It fades four times as fast, and a pass ends at half of the
range ($200). Then it can start again, stay where it is, and turn its output upside down, always
or every second time. The chip looks for the end of a pass at each sample and not at each third one.
If it only repeats, the wave stays at 0 until the attenuation is below $200 again.
LFO, DAC and timers
The LFO has 128 steps and eight speeds from 3.85 to 83.23 Hz. It moves the pitch of a channel,
and makes the operators that have this turned on quieter by up to 11.8 dB. Each channel says how
much. While the LFO is off, it stays at step 0. That is where these operators are the quietest, if
their channel uses it.
The DAC plays samples. While register $2B has it on, the 8 bits in register $2A are played
instead of channel 6. That channel's sides still count, and its own sound goes on unheard. The core
takes the value when each of the chip's samples is due. Of two writes between these, only the second
is heard. Nothing in the chip sends the samples at the right time. The processor that writes them
has to do that.
Timer A has 10 bits and counts each sample, and timer B has 8 bits and counts every 16th.
When one overflows, it can set its bit in the status, and starts again at the value that was set.
There are no interrupts from them, so a program has to look. A change that turns a timer on or off
counts from the next sample. With one setting (CSM), each overflow of timer A presses and lets go
all keys of channel 3 that are not down.
The busy flag is bit 7 of the status. Data sets it for 32 of the chip's cycles, which are 1,344
of the master clock. Selecting a register does not. The flag only says so: a write while it is set
counts as any other. All four addresses give the status, as on a YM3438. Consoles had one chip
or the other. On a YM2612, three of the addresses give an old status, which fades.
What comes out
The chip's converter has 9 bits. So the lowest 5 bits of each operator that is heard are left out
before they are added, and the sum stays within 14 bits. Each channel can be on at the left, the
right or both. The six are added.
A channel that is turned off on a side is not silent there. It still puts out 4 of the
converter's steps up or down, by the sign of its sample. On a side that is on, 4 steps are added to
0 and above, and 3 are taken from numbers below. This is called the ladder effect. Because of it, a
quiet sound jumps over 0.
The Z80's reset line resets this chip too, and so does the reset button. All of its registers have
their first values again then.
The PSG
It makes a sample every 240 cycles of the master clock, which are about 223,722 a second on NTSC
(psg.rs). Each of three square waves turns over after as many samples as its 10 bits say, where 0
is the same as 1. A wave is 0 or 1 and not -1 or 1, and the four channels are added. Volumes go down
by 2 dB at each of 14 steps, and the last of the 16 values is silence.
Noise comes from a shift register of 16 bits. It moves on at one of three speeds or at that of the
third square wave. Its new bit is its last bit (periodic), or that bit XOR bit 3 (white noise).
Each write to the noise register puts the same first value into the shift register.
A byte with its highest bit set selects a register and changes its low bits. One without it changes
the upper six bits of a tone that is selected. For noise and volume, it has the same effect as the
first kind. The PSG has one output, which goes to both sides. The reset button does not change the
chip.
The mixer
The mixer runs at the YM2612's rate. One of its samples is one of that chip's, and lasts 1,008
cycles of the master clock. Each other source gives its sound for the same time. All rates come from
whole numbers of cycles, so nothing drifts. The base resamples the sound to the computer's rate. In
mdemu, a front end does that, after it asks for the exact rate.
YM2612 ------------------------------------+
PSG ------- mean of 4.2 samples -- -7 dB --+-- high pass, -- low pass, --+-- 16 bits,
32X PWM --- mean over a sample --- -2 dB --+ 5 Hz 3,390 Hz | left and right
|
disc music, 44,100 Hz -------------- curve -- -7 dB ---------------------+
Mega CD PCM, 32,552 Hz -- low pass -- curve -- -6 dB --------------------+
7,973 Hz
- The YM2612 is divided by 49,152, which is six channels at their loudest, without the ladder
effect.
- A sample of it lasts as long as 4.2 of the PSG's. A PSG sample that goes on into the next one
counts in both, for as long as it lasts in each. So there are 21 samples of the PSG for each 5 of
the mixer, and a tone that is too high for the mixer is only smoothed by that mean.
- The 32X's PWM is the mean of its level over each sample, also by the exact time.
- The Mega CD's two sounds are on a curve through four samples (
mega_cd/sound.rs). Its PCM
chip has a filter of the second order before that. They are added after the console's filters.
- The two console filters are of the first order, and are those of a first model. They are the
same for all sources that go through them. The high pass takes out a level that stays, such as the
middle of the PSG's waves. Their numbers are written in the code for both regions, and are not
worked out when it runs, as
tan can differ between machines.
Each sample needs all sources that the console has. The mixer makes as many as all of them are far
enough for, and what is left waits for the next frame. Too loud sounds are cut off at 16 bits.
What is not known, or differs
- Not compared with the base sample by sample, as it resamples in another way. Five Mega Drive
games are as loud, and their parts mostly match closely. What makes the others different was not
looked into. Status has the numbers.
- The mix of the 32X and the Mega CD was not compared. Their volumes are the base's numbers.
- A YM2612's fading status is not emulated, and neither is any other console model's filter.
- When the LFO's speed is changed, what it should do is open (
ym2612/lfo.rs).
- The exact time of a write within an instruction is not known to the core.
The whole list is in Status.