SMS - cleanups

This commit is contained in:
adelikat 2020-05-08 17:31:20 -05:00
parent 099e2f4f97
commit 05cffd887a
5 changed files with 127 additions and 133 deletions

View File

@ -206,36 +206,36 @@ namespace BizHawk.Emulation.Cores.Sega.GGHawkLink
int s_L = L.PSG.current_sample_L;
int s_R = L.PSG.current_sample_R;
if (s_L != L.old_s_L)
if (s_L != L.OldSl)
{
L.blip_L.AddDelta(L.sampleclock, s_L - L.old_s_L);
L.old_s_L = s_L;
L.BlipL.AddDelta(L.SampleClock, s_L - L.OldSl);
L.OldSl = s_L;
}
if (s_R != L.old_s_R)
if (s_R != L.OldSr)
{
L.blip_R.AddDelta(L.sampleclock, s_R - L.old_s_R);
L.old_s_R = s_R;
L.BlipR.AddDelta(L.SampleClock, s_R - L.OldSr);
L.OldSr = s_R;
}
L.sampleclock++;
L.SampleClock++;
s_L = R.PSG.current_sample_L;
s_R = R.PSG.current_sample_R;
if (s_L != R.old_s_L)
if (s_L != R.OldSl)
{
R.blip_L.AddDelta(R.sampleclock, s_L - R.old_s_L);
R.old_s_L = s_L;
R.BlipL.AddDelta(R.SampleClock, s_L - R.OldSl);
R.OldSl = s_L;
}
if (s_R != R.old_s_R)
if (s_R != R.OldSr)
{
R.blip_R.AddDelta(R.sampleclock, s_R - R.old_s_R);
R.old_s_R = s_R;
R.BlipR.AddDelta(R.SampleClock, s_R - R.OldSr);
R.OldSr = s_R;
}
R.sampleclock++;
R.SampleClock++;
}
if (S == scanlinesPerFrame - 1)

View File

@ -1,9 +1,8 @@
using BizHawk.Emulation.Common;
using System;
namespace BizHawk.Emulation.Cores.Sega.MasterSystem
{
public partial class SMS : IEmulator, ISoundProvider
public partial class SMS : IEmulator
{
public IEmulatorServiceProvider ServiceProvider { get; }
@ -41,7 +40,7 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
// not savestated variables
int s_L, s_R;
public bool FrameAdvance(IController controller, bool render, bool rendersound)
public bool FrameAdvance(IController controller, bool render, bool renderSound)
{
_controller = controller;
_lagged = true;
@ -96,19 +95,19 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
s_L = PSG.current_sample_L;
s_R = PSG.current_sample_R;
if (s_L != old_s_L)
if (s_L != OldSl)
{
blip_L.AddDelta(sampleclock, s_L - old_s_L);
old_s_L = s_L;
BlipL.AddDelta(SampleClock, s_L - OldSl);
OldSl = s_L;
}
if (s_R != old_s_R)
if (s_R != OldSr)
{
blip_R.AddDelta(sampleclock, s_R - old_s_R);
old_s_R = s_R;
BlipR.AddDelta(SampleClock, s_R - OldSr);
OldSr = s_R;
}
sampleclock++;
SampleClock++;
}
if (Vdp.ScanLine == scanlinesPerFrame - 1)
@ -172,95 +171,17 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
public void Dispose()
{
if (blip_L != null)
if (BlipL != null)
{
blip_L.Dispose();
blip_L = null;
BlipL.Dispose();
BlipL = null;
}
if (blip_R != null)
if (BlipR != null)
{
blip_R.Dispose();
blip_R = null;
BlipR.Dispose();
BlipR = null;
}
}
#region Audio
public BlipBuffer blip_L = new BlipBuffer(4096);
public BlipBuffer blip_R = new BlipBuffer(4096);
const int blipbuffsize = 4096;
public uint sampleclock;
public int old_s_L = 0;
public int old_s_R = 0;
public bool CanProvideAsync => false;
public void SetSyncMode(SyncSoundMode mode)
{
if (mode != SyncSoundMode.Sync)
{
throw new NotSupportedException("Only sync mode is supported");
}
}
public void GetSamplesAsync(short[] samples)
{
throw new NotSupportedException("Async not supported");
}
public SyncSoundMode SyncMode => SyncSoundMode.Sync;
public void GetSamplesSync(out short[] samples, out int nsamp)
{
if (!disablePSG)
{
blip_L.EndFrame(sampleclock);
blip_R.EndFrame(sampleclock);
nsamp = Math.Max(Math.Max(blip_L.SamplesAvailable(), blip_R.SamplesAvailable()), 1);
samples = new short[nsamp * 2];
blip_L.ReadSamplesLeft(samples, nsamp);
blip_R.ReadSamplesRight(samples, nsamp);
ApplyYMAudio(samples);
}
else
{
nsamp = 735;
samples = new short[nsamp * 2];
ApplyYMAudio(samples);
}
sampleclock = 0;
}
public void DiscardSamples()
{
blip_L.Clear();
blip_R.Clear();
sampleclock = 0;
}
public void ApplyYMAudio(short[] samples)
{
if (HasYM2413)
{
short[] fmsamples = new short[samples.Length];
YM2413.GetSamples(fmsamples);
//naive mixing. need to study more
int len = samples.Length;
for (int i = 0; i < len; i++)
{
short fmsamp = fmsamples[i];
samples[i] = (short)(samples[i] + fmsamp);
}
}
}
#endregion
}
}

View File

@ -0,0 +1,82 @@
using System;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Components;
namespace BizHawk.Emulation.Cores.Sega.MasterSystem
{
public partial class SMS : ISoundProvider
{
private readonly YM2413 YM2413;
internal BlipBuffer BlipL { get; set; } = new BlipBuffer(4096);
internal BlipBuffer BlipR { get; set; } = new BlipBuffer(4096);
internal uint SampleClock;
internal int OldSl;
internal int OldSr;
public bool CanProvideAsync => false;
public SyncSoundMode SyncMode => SyncSoundMode.Sync;
public void SetSyncMode(SyncSoundMode mode)
{
if (mode != SyncSoundMode.Sync)
{
throw new NotSupportedException("Only sync mode is supported");
}
}
public void GetSamplesSync(out short[] samples, out int nsamp)
{
if (!disablePSG)
{
BlipL.EndFrame(SampleClock);
BlipR.EndFrame(SampleClock);
nsamp = Math.Max(Math.Max(BlipL.SamplesAvailable(), BlipR.SamplesAvailable()), 1);
samples = new short[nsamp * 2];
BlipL.ReadSamplesLeft(samples, nsamp);
BlipR.ReadSamplesRight(samples, nsamp);
ApplyYmAudio(samples);
}
else
{
nsamp = 735;
samples = new short[nsamp * 2];
ApplyYmAudio(samples);
}
SampleClock = 0;
}
public void GetSamplesAsync(short[] samples)
{
throw new NotSupportedException("Async not supported");
}
public void DiscardSamples()
{
BlipL.Clear();
BlipR.Clear();
SampleClock = 0;
}
private void ApplyYmAudio(short[] samples)
{
if (HasYM2413)
{
short[] fmSamples = new short[samples.Length];
YM2413.GetSamples(fmSamples);
// naive mixing. need to study more
int len = samples.Length;
for (int i = 0; i < len; i++)
{
short fmSample = fmSamples[i];
samples[i] = (short)(samples[i] + fmSample);
}
}
}
}
}

View File

@ -43,9 +43,9 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
ser.Sync(nameof(p4_read), ref p4_read);
ser.Sync(nameof(stand_alone), ref stand_alone);
ser.Sync(nameof(disablePSG), ref disablePSG);
ser.Sync(nameof(sampleclock), ref sampleclock);
ser.Sync(nameof(old_s_L), ref old_s_L);
ser.Sync(nameof(old_s_R), ref old_s_R);
ser.Sync("sampleclock", ref SampleClock);
ser.Sync("old_s_L", ref OldSl);
ser.Sync("old_s_R", ref OldSr);
if (SaveRAM != null)
{

View File

@ -20,13 +20,14 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
isPorted: false,
isReleased: true)]
[ServiceNotApplicable(new[] { typeof(IDriveLight) })]
public partial class SMS : IEmulator, ISaveRam, IInputPollable, IRegionable,
public partial class SMS : IEmulator, ISoundProvider, ISaveRam, IInputPollable, IRegionable,
IDebuggable, ISettable<SMS.SmsSettings, SMS.SmsSyncSettings>, ICodeDataLogger
{
[CoreConstructor(new[] { "SMS", "SG", "GG" })]
public SMS(CoreComm comm, GameInfo game, byte[] rom, object settings, object syncSettings)
{
ServiceProvider = new BasicServiceProvider(this);
var ser = new BasicServiceProvider(this);
ServiceProvider = ser;
Settings = (SmsSettings)settings ?? new SmsSettings();
SyncSettings = (SmsSyncSettings)syncSettings ?? new SmsSyncSettings();
@ -77,7 +78,7 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
HasYM2413 = true;
}
Cpu = new Z80A()
Cpu = new Z80A
{
ReadHardware = ReadPort,
WriteHardware = WritePort,
@ -99,7 +100,7 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
}
Vdp = new VDP(this, Cpu, IsGameGear ? VdpMode.GameGear : VdpMode.SMS, Region);
(ServiceProvider as BasicServiceProvider).Register<IVideoProvider>(Vdp);
ser.Register<IVideoProvider>(Vdp);
PSG = new SN76489sms();
YM2413 = new YM2413();
//SoundMixer = new SoundMixer(YM2413, PSG);
@ -108,10 +109,10 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
disablePSG = true;
}
blip_L.SetRates(3579545, 44100);
blip_R.SetRates(3579545, 44100);
BlipL.SetRates(3579545, 44100);
BlipR.SetRates(3579545, 44100);
(ServiceProvider as BasicServiceProvider).Register<ISoundProvider>(this);
ser.Register<ISoundProvider>(this);
SystemRam = new byte[0x2000];
@ -195,10 +196,9 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
Tracer = new TraceBuffer { Header = Cpu.TraceHeader };
var serviceProvider = ServiceProvider as BasicServiceProvider;
serviceProvider.Register<ITraceable>(Tracer);
serviceProvider.Register<IDisassemblable>(Cpu);
serviceProvider.Register<IStatable>(new StateSerializer(SyncState));
ser.Register(Tracer);
ser.Register<IDisassemblable>(Cpu);
ser.Register<IStatable>(new StateSerializer(SyncState));
Vdp.ProcessOverscan();
Cpu.ReadMemory = ReadMemory;
@ -212,7 +212,6 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
public void HardReset()
{
}
// Constants
@ -230,7 +229,7 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
public byte[] SystemRam;
public VDP Vdp;
public SN76489sms PSG;
private YM2413 YM2413;
public bool IsGameGear { get; set; }
public bool IsGameGear_C { get; set; }
public bool IsSG1000 { get; set; }
@ -340,14 +339,6 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
/// </summary>
private Action<ushort, byte> WriteMemoryMapper;
/// <summary>
/// A dummy FetchMemory that simply reads the memory
/// </summary>
private byte FetchMemory_StubThunk(ushort address)
{
return ReadMemory(address);
}
private byte ReadPort(ushort port)
{
port &= 0xFF;
@ -425,6 +416,6 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem
else if (port == 0xF2 && HasYM2413) YM2413.DetectionValue = value;
}
private SmsSyncSettings.Regions _region;
private readonly SmsSyncSettings.Regions _region;
}
}