micronona/Sn76489.cs

198 lines
5.9 KiB
C#
Raw Normal View History

/// <summary>
/// Texas Instruments SN76489 programmable sound generator.
///
/// Three square-wave tone channels plus one noise channel, mono output.
/// Clock: 3.579545 MHz (NTSC). Internal /16 divider yields a ~223.7 kHz
/// counter clock. Tone frequency = ChipClock / (32 * N) for 10-bit reload N.
///
/// Write protocol (single 8-bit port at REG_SOUND):
/// LATCH/DATA byte (MSB=1): 1 R2 R1 R0 D3 D2 D1 D0
/// R2..R0 selects register, D3..D0 is low 4 bits of payload.
/// DATA byte (MSB=0): 0 x D5 D4 D3 D2 D1 D0
/// Upper 6 bits of 10-bit tone reload; appended to the last latched
/// tone register. Ignored for attenuation (kept for completeness).
///
/// Register map (R2 R1 R0):
/// 000 Tone 1 freq 001 Tone 1 attenuation
/// 010 Tone 2 freq 011 Tone 2 attenuation
/// 100 Tone 3 freq 101 Tone 3 attenuation
/// 110 Noise control 111 Noise attenuation
///
/// Attenuation: 0 = loudest, 15 = silent, 2 dB per step.
/// Noise control bits: NF2 = white (1) / periodic (0); NF1 NF0 = shift rate
/// 00 N/512, 01 N/1024, 10 N/2048, 11 = tone 3 rate.
/// </summary>
public class Sn76489
{
public const int ChipClock = 3579545;
public const int ToneDivider = 16;
public const double CounterClock = ChipClock / (double)ToneDivider;
private const int NoiseLfsrInit = 0x4000;
private const int NoiseLfsrMask = 0x7FFF;
private readonly int[] _toneReload = new int[3];
private readonly int[] _toneCounter = new int[3] { 1, 1, 1 };
private readonly int[] _toneOut = new int[3];
private readonly int[] _attenuation = new int[4] { 15, 15, 15, 15 };
private int _noiseCtrl;
private int _noiseCounter = 1;
private int _noiseShift = NoiseLfsrInit;
private int _noiseOut;
private int _latchedReg;
private readonly double _cyclesPerSample;
private double _cyclesAccum;
private static readonly short[] VolumeTable = BuildVolumeTable();
private readonly object _lock = new();
public Sn76489(int sampleRate)
{
_cyclesPerSample = CounterClock / sampleRate;
}
private static short[] BuildVolumeTable()
{
// Per-channel peak of 6000 keeps four channels summed within int16 range.
var t = new short[16];
for (int i = 0; i < 15; i++)
t[i] = (short)(6000.0 * System.Math.Pow(10.0, -i * 2.0 / 20.0));
t[15] = 0;
return t;
}
public void Write(byte value)
{
lock (_lock)
{
if ((value & 0x80) != 0)
{
int reg = (value >> 4) & 0x07;
int data4 = value & 0x0F;
_latchedReg = reg;
ApplyLatch(reg, data4);
}
else
{
int data6 = value & 0x3F;
ApplyContinuation(_latchedReg, data6);
}
}
}
private void ApplyLatch(int reg, int data4)
{
int channel = reg >> 1;
bool isAtt = (reg & 1) != 0;
if (isAtt)
{
_attenuation[channel] = data4;
}
else if (channel < 3)
{
_toneReload[channel] = (_toneReload[channel] & ~0x0F) | data4;
}
else
{
_noiseCtrl = data4 & 0x07;
_noiseShift = NoiseLfsrInit;
}
}
private void ApplyContinuation(int reg, int data6)
{
int channel = reg >> 1;
bool isAtt = (reg & 1) != 0;
if (isAtt)
{
_attenuation[channel] = data6 & 0x0F;
}
else if (channel < 3)
{
_toneReload[channel] = (_toneReload[channel] & 0x0F) | ((data6 & 0x3F) << 4);
}
else
{
_noiseCtrl = data6 & 0x07;
_noiseShift = NoiseLfsrInit;
}
}
/// <summary>Fill a mono 16-bit PCM buffer with freshly generated samples.</summary>
public void GenerateSamples(System.Span<short> output)
{
lock (_lock)
{
for (int s = 0; s < output.Length; s++)
{
_cyclesAccum += _cyclesPerSample;
int cycles = (int)_cyclesAccum;
_cyclesAccum -= cycles;
if (cycles > 0) RunCycles(cycles);
output[s] = MixSample();
}
}
}
private void RunCycles(int cycles)
{
for (int c = 0; c < cycles; c++)
{
for (int ch = 0; ch < 3; ch++)
{
if (--_toneCounter[ch] <= 0)
{
int reload = _toneReload[ch];
// Reload of 0 is treated as 1 — at that rate the channel emits
// DC-ish, which is correct "silent carrier" behaviour on hardware.
_toneCounter[ch] = reload == 0 ? 1 : reload;
_toneOut[ch] ^= 1;
}
}
if (--_noiseCounter <= 0)
{
int rate = _noiseCtrl & 0x03;
int reload = rate switch
{
0 => 32,
1 => 64,
2 => 128,
_ => _toneReload[2] == 0 ? 1 : _toneReload[2],
};
_noiseCounter = reload;
int fb = (_noiseCtrl & 0x04) != 0
? ((_noiseShift & 1) ^ ((_noiseShift >> 3) & 1)) & 1
: _noiseShift & 1;
_noiseShift = ((_noiseShift >> 1) | (fb << 14)) & NoiseLfsrMask;
_noiseOut = _noiseShift & 1;
}
}
}
private short MixSample()
{
int sum = 0;
for (int ch = 0; ch < 3; ch++)
{
int v = VolumeTable[_attenuation[ch]];
sum += _toneOut[ch] != 0 ? v : -v;
}
int nv = VolumeTable[_attenuation[3]];
sum += _noiseOut != 0 ? nv : -nv;
if (sum > short.MaxValue) sum = short.MaxValue;
if (sum < short.MinValue) sum = short.MinValue;
return (short)sum;
}
}