2011-08-29 22:49:24 +00:00
|
|
|
|
using System;
|
2011-09-03 20:32:18 +00:00
|
|
|
|
using BizHawk.Emulation.Sound;
|
2011-08-29 22:49:24 +00:00
|
|
|
|
|
|
|
|
|
namespace BizHawk.Emulation.Consoles.TurboGrafx
|
|
|
|
|
{
|
2011-09-04 04:38:11 +00:00
|
|
|
|
public sealed class ADPCM
|
2011-08-29 22:49:24 +00:00
|
|
|
|
{
|
|
|
|
|
public ushort adpcm_io_address;
|
|
|
|
|
public ushort adpcm_read_address;
|
|
|
|
|
public ushort adpcm_write_address;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
public ushort adpcm_length;
|
|
|
|
|
|
2011-09-04 04:38:11 +00:00
|
|
|
|
public int adpcm_read_timer, adpcm_write_timer;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
public byte adpcm_read_buffer, adpcm_write_buffer;
|
|
|
|
|
public bool adpcm_read_pending, adpcm_write_pending;
|
2011-08-29 22:49:24 +00:00
|
|
|
|
|
2011-09-04 04:38:11 +00:00
|
|
|
|
public byte[] RAM = new byte[0x10000];
|
|
|
|
|
public MetaspuSoundProvider SoundProvider = new MetaspuSoundProvider(ESynchMethod.ESynchMethod_V);
|
|
|
|
|
|
2011-09-03 20:32:18 +00:00
|
|
|
|
long LastThink;
|
|
|
|
|
float adpcm_playback_timer;
|
2011-09-04 04:38:11 +00:00
|
|
|
|
|
|
|
|
|
ScsiCDBus SCSI;
|
|
|
|
|
PCEngine pce;
|
2011-09-03 20:32:18 +00:00
|
|
|
|
|
2011-09-04 04:38:11 +00:00
|
|
|
|
public ADPCM(PCEngine pcEngine, ScsiCDBus scsi)
|
|
|
|
|
{
|
|
|
|
|
pce = pcEngine;
|
|
|
|
|
SCSI = scsi;
|
|
|
|
|
}
|
2011-09-03 20:32:18 +00:00
|
|
|
|
|
|
|
|
|
static readonly int[] StepSize =
|
|
|
|
|
{
|
|
|
|
|
16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
|
|
|
|
|
50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 140, 143,
|
|
|
|
|
157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449,
|
|
|
|
|
494, 544, 598, 658, 724, 796, 876, 963,1060,1166,1282,1411,
|
|
|
|
|
1552
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static readonly int[] StepFactor = { -1, -1, -1, -1, 2, 4, 6, 8 };
|
|
|
|
|
|
|
|
|
|
int AddClamped(int num1, int num2, int min, int max)
|
|
|
|
|
{
|
|
|
|
|
int result = num1 + num2;
|
|
|
|
|
if (result < min) return min;
|
|
|
|
|
if (result > max) return max;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2011-08-29 22:49:24 +00:00
|
|
|
|
|
|
|
|
|
public void AdpcmControlWrite(byte value)
|
|
|
|
|
{
|
|
|
|
|
Log.Error("CD","ADPCM CONTROL WRITE {0:X2}",value);
|
2011-09-04 04:38:11 +00:00
|
|
|
|
if ((Port180D & 0x80) != 0 && (value & 0x80) == 0)
|
2011-08-29 22:49:24 +00:00
|
|
|
|
{
|
|
|
|
|
Log.Note("CD", "Reset ADPCM!");
|
|
|
|
|
adpcm_read_address = 0;
|
|
|
|
|
adpcm_write_address = 0;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
adpcm_io_address = 0;
|
2011-09-03 20:32:18 +00:00
|
|
|
|
nibble = false;
|
|
|
|
|
playingSample = 0;
|
|
|
|
|
adpcm_playback_timer = 0;
|
|
|
|
|
magnitude = 0;
|
|
|
|
|
AdpcmIsPlaying = false;
|
2011-08-29 22:49:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-03 20:32:18 +00:00
|
|
|
|
if ((value & 8) != 0)
|
2011-08-29 22:49:24 +00:00
|
|
|
|
{
|
|
|
|
|
adpcm_read_address = adpcm_io_address;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
if ((value & 4) == 0)
|
|
|
|
|
adpcm_read_address--;
|
2011-08-29 22:49:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-04 04:38:11 +00:00
|
|
|
|
if ((Port180D & 2) == 0 && (value & 2) != 0)
|
2011-08-29 22:49:24 +00:00
|
|
|
|
{
|
|
|
|
|
adpcm_write_address = adpcm_io_address;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
if ((value & 1) == 0)
|
|
|
|
|
adpcm_write_address--;
|
2011-08-29 22:49:24 +00:00
|
|
|
|
}
|
2011-09-03 20:32:18 +00:00
|
|
|
|
|
|
|
|
|
if ((value & 0x10) != 0)
|
|
|
|
|
{
|
|
|
|
|
adpcm_length = adpcm_io_address;
|
|
|
|
|
Console.WriteLine("SET LENGTH={0:X4}", adpcm_length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (AdpcmIsPlaying && (value & 0x20) == 0)
|
|
|
|
|
AdpcmIsPlaying = false; // only plays as long as this bit is set
|
|
|
|
|
|
|
|
|
|
if (AdpcmIsPlaying == false && (value & 0x20) != 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Start playing!");
|
|
|
|
|
AdpcmIsPlaying = true;
|
|
|
|
|
nibble = false;
|
|
|
|
|
playingSample = 0;
|
|
|
|
|
adpcm_playback_timer = 0;
|
|
|
|
|
magnitude = 0;
|
|
|
|
|
}
|
2011-08-29 22:49:24 +00:00
|
|
|
|
|
2011-09-04 04:38:11 +00:00
|
|
|
|
Port180D = value;
|
2011-08-29 22:49:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-03 20:32:18 +00:00
|
|
|
|
public bool AdpcmIsPlaying { get; private set; }
|
2011-08-29 22:49:24 +00:00
|
|
|
|
public bool AdpcmBusyWriting { get { return AdpcmCdDmaRequested; } }
|
2011-08-31 04:06:54 +00:00
|
|
|
|
public bool AdpcmBusyReading { get { return adpcm_read_pending; } }
|
2011-09-03 20:32:18 +00:00
|
|
|
|
|
|
|
|
|
Random rnd = new Random();
|
|
|
|
|
|
|
|
|
|
int playingSample;
|
|
|
|
|
int nextSampleTimer = 0;
|
|
|
|
|
bool nibble;
|
|
|
|
|
|
|
|
|
|
int magnitude;
|
|
|
|
|
|
|
|
|
|
void DecodeAdpcmSample()
|
|
|
|
|
{
|
|
|
|
|
// get sample. it's one nibble.
|
|
|
|
|
byte sample;
|
|
|
|
|
if (nibble == false)
|
|
|
|
|
{
|
2011-09-04 04:38:11 +00:00
|
|
|
|
sample = (byte) (RAM[adpcm_read_address] >> 4);
|
2011-09-03 20:32:18 +00:00
|
|
|
|
nibble = true;
|
|
|
|
|
} else {
|
2011-09-04 04:38:11 +00:00
|
|
|
|
sample = (byte)(RAM[adpcm_read_address] & 0xF);
|
2011-09-03 20:32:18 +00:00
|
|
|
|
nibble = false;
|
|
|
|
|
adpcm_length--;
|
|
|
|
|
adpcm_read_address++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool positive = (sample & 8) == 0;
|
|
|
|
|
int mag = sample & 7;
|
|
|
|
|
int m = StepFactor[mag];
|
|
|
|
|
|
|
|
|
|
magnitude = AddClamped(magnitude, m, 0, 48);
|
|
|
|
|
int adjustment = StepSize[magnitude];
|
|
|
|
|
if (positive == false) adjustment *= -1;
|
|
|
|
|
playingSample = AddClamped(playingSample, adjustment, 0, 4095);
|
|
|
|
|
|
|
|
|
|
adpcm_length--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AdpcmEmitSample()
|
|
|
|
|
{
|
|
|
|
|
if (AdpcmIsPlaying == false)
|
2011-09-04 04:38:11 +00:00
|
|
|
|
SoundProvider.buffer.enqueue_sample(0, 0);
|
2011-09-03 20:32:18 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2011-09-04 04:38:11 +00:00
|
|
|
|
int rate = 16 - (Port180E & 0x0F);
|
2011-09-03 20:32:18 +00:00
|
|
|
|
float khz = 32 / rate;
|
|
|
|
|
|
|
|
|
|
if (nextSampleTimer == 0)
|
|
|
|
|
{
|
|
|
|
|
DecodeAdpcmSample();
|
|
|
|
|
nextSampleTimer = 4;
|
|
|
|
|
}
|
|
|
|
|
nextSampleTimer--;
|
|
|
|
|
|
|
|
|
|
if (adpcm_length == 0)
|
|
|
|
|
{
|
|
|
|
|
AdpcmIsPlaying = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
short adjustedSample = (short)((playingSample - 2048) << 3);
|
2011-09-04 04:38:11 +00:00
|
|
|
|
SoundProvider.buffer.enqueue_sample(adjustedSample, adjustedSample);
|
2011-09-03 20:32:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-29 22:49:24 +00:00
|
|
|
|
|
2011-09-04 04:38:11 +00:00
|
|
|
|
public void Think()
|
2011-08-29 22:49:24 +00:00
|
|
|
|
{
|
2011-09-04 04:38:11 +00:00
|
|
|
|
int cycles = (int) (pce.Cpu.TotalExecutedCycles - LastThink);
|
|
|
|
|
LastThink = pce.Cpu.TotalExecutedCycles;
|
2011-09-03 20:32:18 +00:00
|
|
|
|
|
|
|
|
|
adpcm_playback_timer -= cycles;
|
|
|
|
|
if (adpcm_playback_timer < 0)
|
|
|
|
|
{
|
|
|
|
|
adpcm_playback_timer += 162.81f; // # of CPU cycles that translate to one 44100hz sample.
|
|
|
|
|
AdpcmEmitSample();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (adpcm_read_timer > 0) adpcm_read_timer -= cycles;
|
|
|
|
|
if (adpcm_write_timer > 0) adpcm_write_timer -= cycles;
|
|
|
|
|
|
|
|
|
|
if (adpcm_read_pending && adpcm_read_timer <= 0)
|
2011-08-31 04:06:54 +00:00
|
|
|
|
{
|
2011-09-04 04:38:11 +00:00
|
|
|
|
adpcm_read_buffer = RAM[adpcm_read_address++];
|
2011-08-31 04:06:54 +00:00
|
|
|
|
adpcm_read_pending = false;
|
2011-09-03 20:32:18 +00:00
|
|
|
|
if (adpcm_length > ushort.MinValue)
|
|
|
|
|
adpcm_length--;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-03 20:32:18 +00:00
|
|
|
|
if (adpcm_write_pending && adpcm_write_timer <= 0)
|
2011-08-31 04:06:54 +00:00
|
|
|
|
{
|
2011-09-04 04:38:11 +00:00
|
|
|
|
RAM[adpcm_write_address++] = adpcm_write_buffer;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
adpcm_write_pending = false;
|
2011-09-03 20:32:18 +00:00
|
|
|
|
if (adpcm_length < ushort.MaxValue)
|
|
|
|
|
adpcm_length++;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-29 22:49:24 +00:00
|
|
|
|
if (AdpcmCdDmaRequested)
|
|
|
|
|
{
|
|
|
|
|
if (SCSI.REQ && SCSI.IO && !SCSI.CD && !SCSI.ACK)
|
|
|
|
|
{
|
|
|
|
|
byte dmaByte = SCSI.DataBits;
|
2011-09-04 04:38:11 +00:00
|
|
|
|
RAM[adpcm_write_address++] = dmaByte;
|
2011-08-29 22:49:24 +00:00
|
|
|
|
|
|
|
|
|
SCSI.ACK = false;
|
|
|
|
|
SCSI.REQ = false;
|
|
|
|
|
SCSI.Think();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SCSI.DataTransferInProgress == false)
|
|
|
|
|
{
|
2011-09-04 04:38:11 +00:00
|
|
|
|
Port180B = 0;
|
2011-08-31 04:06:54 +00:00
|
|
|
|
Console.WriteLine(" ADPCM DMA COMPLETED");
|
2011-08-29 22:49:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-09-03 20:32:18 +00:00
|
|
|
|
|
2011-09-04 04:38:11 +00:00
|
|
|
|
pce.IRQ2Monitor &= 0xF3;
|
|
|
|
|
if (AdpcmIsPlaying == false) pce.IRQ2Monitor |= 0x08;
|
|
|
|
|
pce.RefreshIRQ2();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool AdpcmCdDmaRequested { get { return (Port180B & 3) != 0; } }
|
|
|
|
|
|
|
|
|
|
public byte Port180A
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
adpcm_write_buffer = value;
|
|
|
|
|
adpcm_write_timer = 24;
|
|
|
|
|
adpcm_write_pending = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
adpcm_read_pending = true;
|
|
|
|
|
adpcm_read_timer = 24;
|
|
|
|
|
return adpcm_read_buffer;
|
|
|
|
|
}
|
2011-08-29 22:49:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-04 04:38:11 +00:00
|
|
|
|
public byte Port180B;
|
|
|
|
|
public byte Port180D;
|
|
|
|
|
public byte Port180E;
|
2011-08-29 22:49:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|