diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ICodeDataLog.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ICodeDataLog.cs index 8625c95d3b..ca0da3ba53 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ICodeDataLog.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ICodeDataLog.cs @@ -1,55 +1,74 @@ using System; using System.IO; -using System.Collections.Generic; using BizHawk.Emulation.Common; namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { - partial class Gameboy + public partial class Gameboy : ICodeDataLogger { - void ICodeDataLogger.SetCDL(ICodeDataLog cdl) + public void SetCDL(ICodeDataLog cdl) { - CDL = cdl; - if(cdl == null) - LibGambatte.gambatte_setcdcallback(GambatteState, null); - else - LibGambatte.gambatte_setcdcallback(GambatteState, CDCallback); + _cdl = cdl; + LibGambatte.gambatte_setcdcallback(GambatteState, cdl == null ? null : _cdCallback); } - void ICodeDataLogger.NewCDL(ICodeDataLog cdl) + public void NewCDL(ICodeDataLog cdl) { cdl["ROM"] = new byte[MemoryDomains["ROM"].Size]; - //cdl["HRAM"] = new byte[_memoryDomains["HRAM"].Size]; //this is probably useless, but it's here if someone needs it + // cdl["HRAM"] = new byte[_memoryDomains["HRAM"].Size]; //this is probably useless, but it's here if someone needs it cdl["WRAM"] = new byte[MemoryDomains["WRAM"].Size]; if (MemoryDomains.Has("CartRAM")) + { cdl["CartRAM"] = new byte[MemoryDomains["CartRAM"].Size]; + } cdl.SubType = "GB"; cdl.SubVer = 0; } - //not supported - void ICodeDataLogger.DisassembleCDL(Stream s, ICodeDataLog cdl) { } - - ICodeDataLog CDL; - LibGambatte.CDCallback CDCallback; - void CDCallbackProc(int addr, LibGambatte.CDLog_AddrType addrtype, LibGambatte.CDLog_Flags flags) + [FeatureNotImplemented] + void ICodeDataLogger.DisassembleCDL(Stream s, ICodeDataLog cdl) { - if (CDL == null) return; - if (!CDL.Active) return; + } + + private ICodeDataLog _cdl; + private readonly LibGambatte.CDCallback _cdCallback; + + private void CDCallbackProc(int addr, LibGambatte.CDLog_AddrType addrtype, LibGambatte.CDLog_Flags flags) + { + if (_cdl == null) + { + return; + } + + if (!_cdl.Active) + { + return; + } + string key; switch (addrtype) { - case LibGambatte.CDLog_AddrType.ROM: key = "ROM"; break; - case LibGambatte.CDLog_AddrType.HRAM: key = "HRAM"; break; - case LibGambatte.CDLog_AddrType.WRAM: key = "WRAM"; break; - case LibGambatte.CDLog_AddrType.CartRAM: key = "CartRAM"; break; - default: throw new InvalidOperationException("Juniper lightbulb proxy"); + case LibGambatte.CDLog_AddrType.ROM: + key = "ROM"; + break; + case LibGambatte.CDLog_AddrType.HRAM: + key = "HRAM"; + break; + case LibGambatte.CDLog_AddrType.WRAM: + key = "WRAM"; + break; + case LibGambatte.CDLog_AddrType.CartRAM: + key = "CartRAM"; + break; + default: + throw new InvalidOperationException("Juniper lightbulb proxy"); } - CDL[key][addr] |= (byte)flags; + + _cdl[key][addr] |= (byte)flags; } } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IDebuggable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IDebuggable.cs index 4f910a677d..91af62e606 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IDebuggable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IDebuggable.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using BizHawk.Emulation.Common; @@ -40,7 +39,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy } [FeatureNotImplemented] - public void Step(StepType type) { throw new NotImplementedException(); } + public void Step(StepType type) + { + throw new NotImplementedException(); + } [FeatureNotImplemented] public int TotalExecutedCycles @@ -48,21 +50,17 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy get { throw new NotImplementedException(); } } - public IMemoryCallbackSystem MemoryCallbacks - { - get { return _memorycallbacks; } - } + public IMemoryCallbackSystem MemoryCallbacks => _memorycallbacks; - private LibGambatte.MemoryCallback readcb; - private LibGambatte.MemoryCallback writecb; - private LibGambatte.MemoryCallback execcb; + private LibGambatte.MemoryCallback _readcb; + private LibGambatte.MemoryCallback _writecb; + private LibGambatte.MemoryCallback _execcb; private MemoryCallbackSystem _memorycallbacks = new MemoryCallbackSystem(); /// /// for use in dual core /// - /// internal void ConnectMemoryCallbackSystem(MemoryCallbackSystem mcs) { _memorycallbacks = mcs; @@ -70,9 +68,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy private void InitMemoryCallbacks() { - readcb = (addr) => MemoryCallbacks.CallReads(addr); - writecb = (addr) => MemoryCallbacks.CallWrites(addr); - execcb = (addr) => MemoryCallbacks.CallExecutes(addr); + _readcb = addr => MemoryCallbacks.CallReads(addr); + _writecb = addr => MemoryCallbacks.CallWrites(addr); + _execcb = addr => MemoryCallbacks.CallExecutes(addr); _memorycallbacks.ActiveChanged += RefreshMemoryCallbacks; } @@ -80,9 +78,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { var mcs = MemoryCallbacks; - LibGambatte.gambatte_setreadcallback(GambatteState, mcs.HasReads ? readcb : null); - LibGambatte.gambatte_setwritecallback(GambatteState, mcs.HasWrites ? writecb : null); - LibGambatte.gambatte_setexeccallback(GambatteState, mcs.HasExecutes ? execcb : null); + LibGambatte.gambatte_setreadcallback(GambatteState, mcs.HasReads ? _readcb : null); + LibGambatte.gambatte_setwritecallback(GambatteState, mcs.HasWrites ? _writecb : null); + LibGambatte.gambatte_setexeccallback(GambatteState, mcs.HasExecutes ? _execcb : null); } } } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IMemoryDomains.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IMemoryDomains.cs index e11a4a3bf2..bd862827a5 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IMemoryDomains.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IMemoryDomains.cs @@ -8,7 +8,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy public partial class Gameboy { private readonly List _memoryDomains = new List(); - internal IMemoryDomains MemoryDomains { get; set; } + internal IMemoryDomains MemoryDomains { get; private set; } private void CreateMemoryDomain(LibGambatte.MemoryAreas which, string name) { diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISaveRam.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISaveRam.cs index 78d1568da0..3776eb00f7 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISaveRam.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISaveRam.cs @@ -10,9 +10,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy get { if (LibGambatte.gambatte_savesavedatalength(GambatteState) == 0) + { return false; - else - return true; // need to wire more stuff into the core to actually know this + } + + return true; // need to wire more stuff into the core to actually know this } } @@ -26,8 +28,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy LibGambatte.gambatte_savesavedata(GambatteState, ret); return ret; } - else - return new byte[0]; + + return new byte[0]; } public void StoreSaveRam(byte[] data) @@ -46,6 +48,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy data = FixRTC(data, 40); break; } + LibGambatte.gambatte_loadsavedata(GambatteState, data); } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISettable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISettable.cs index 8953a4d9b7..f6cf22c4f1 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISettable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISettable.cs @@ -1,7 +1,6 @@ using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Linq; + using Newtonsoft.Json; using BizHawk.Common; @@ -20,9 +19,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { _settings = o; if (IsCGBMode()) + { SetCGBColors(_settings.CGBColors); + } else + { ChangeDMGColors(_settings.GBPalette); + } + return false; } @@ -43,12 +47,12 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy public class GambatteSettings { - private static readonly int[] DefaultPalette = new[] - { - 10798341, 8956165, 1922333, 337157, - 10798341, 8956165, 1922333, 337157, - 10798341, 8956165, 1922333, 337157 - }; + private static readonly int[] DefaultPalette = + { + 10798341, 8956165, 1922333, 337157, + 10798341, 8956165, 1922333, 337157, + 10798341, 8956165, 1922333, 337157 + }; public int[] GBPalette; public GBColors.ColorType CGBColors; @@ -106,20 +110,20 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy } [JsonIgnore] - int _RTCInitialTime; + private int _RTCInitialTime; [DisplayName("Equal Length Frames")] [Description("When false, emulation frames sync to vblank. Only useful for high level TASing.")] [DefaultValue(true)] public bool EqualLengthFrames { - get { return _EqualLengthFrames; } - set { _EqualLengthFrames = value; } + get { return _equalLengthFrames; } + set { _equalLengthFrames = value; } } [JsonIgnore] [DeepEqualsIgnore] - private bool _EqualLengthFrames; + private bool _equalLengthFrames; public GambatteSyncSettings() { diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISoundProvider.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISoundProvider.cs index df1f052613..a4bd1a40c1 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISoundProvider.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ISoundProvider.cs @@ -5,20 +5,17 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { public partial class Gameboy : ISoundProvider { - public bool CanProvideAsync - { - get { return false; } - } + public bool CanProvideAsync => false; public void DiscardSamples() { - soundoutbuffcontains = 0; + _soundoutbuffcontains = 0; } public void GetSamplesSync(out short[] samples, out int nsamp) { - samples = soundoutbuff; - nsamp = soundoutbuffcontains; + samples = _soundoutbuff; + nsamp = _soundoutbuffcontains; } public void SetSyncMode(SyncSoundMode mode) @@ -29,95 +26,90 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy } } - public SyncSoundMode SyncMode - { - get { return SyncSoundMode.Sync; } - } + public SyncSoundMode SyncMode => SyncSoundMode.Sync; public void GetSamplesAsync(short[] samples) { throw new InvalidOperationException("Async mode is not supported."); } - internal bool Muted - { - get { return _settings.Muted; } - } + internal bool Muted => _settings.Muted; // sample pairs before resampling - private short[] soundbuff = new short[(35112 + 2064) * 2]; + private readonly short[] _soundbuff = new short[(35112 + 2064) * 2]; - private int soundoutbuffcontains = 0; + private int _soundoutbuffcontains = 0; - private short[] soundoutbuff = new short[2048]; + private readonly short[] _soundoutbuff = new short[2048]; - private int latchL = 0; - private int latchR = 0; + private int _latchL = 0; + private int _latchR = 0; - private BlipBuffer blipL, blipR; - private uint blipAccumulate; + private BlipBuffer _blipL, _blipR; + private uint _blipAccumulate; private void ProcessSound(int nsamp) { for (uint i = 0; i < nsamp; i++) { - int curr = soundbuff[i * 2]; + int curr = _soundbuff[i * 2]; - if (curr != latchL) + if (curr != _latchL) { - int diff = latchL - curr; - latchL = curr; - blipL.AddDelta(blipAccumulate, diff); + int diff = _latchL - curr; + _latchL = curr; + _blipL.AddDelta(_blipAccumulate, diff); } - curr = soundbuff[i * 2 + 1]; + curr = _soundbuff[(i * 2) + 1]; - if (curr != latchR) + if (curr != _latchR) { - int diff = latchR - curr; - latchR = curr; - blipR.AddDelta(blipAccumulate, diff); + int diff = _latchR - curr; + _latchR = curr; + _blipR.AddDelta(_blipAccumulate, diff); } - blipAccumulate++; + _blipAccumulate++; } } private void ProcessSoundEnd() { - blipL.EndFrame(blipAccumulate); - blipR.EndFrame(blipAccumulate); - blipAccumulate = 0; + _blipL.EndFrame(_blipAccumulate); + _blipR.EndFrame(_blipAccumulate); + _blipAccumulate = 0; - soundoutbuffcontains = blipL.SamplesAvailable(); - if (soundoutbuffcontains != blipR.SamplesAvailable()) + _soundoutbuffcontains = _blipL.SamplesAvailable(); + if (_soundoutbuffcontains != _blipR.SamplesAvailable()) { throw new InvalidOperationException("Audio processing error"); } - blipL.ReadSamplesLeft(soundoutbuff, soundoutbuffcontains); - blipR.ReadSamplesRight(soundoutbuff, soundoutbuffcontains); + _blipL.ReadSamplesLeft(_soundoutbuff, _soundoutbuffcontains); + _blipR.ReadSamplesRight(_soundoutbuff, _soundoutbuffcontains); } private void InitSound() { - blipL = new BlipBuffer(1024); - blipL.SetRates(TICKSPERSECOND, 44100); - blipR = new BlipBuffer(1024); - blipR.SetRates(TICKSPERSECOND, 44100); + _blipL = new BlipBuffer(1024); + _blipL.SetRates(TICKSPERSECOND, 44100); + _blipR = new BlipBuffer(1024); + _blipR.SetRates(TICKSPERSECOND, 44100); } private void DisposeSound() { - if (blipL != null) + if (_blipL != null) { - blipL.Dispose(); - blipL = null; + _blipL.Dispose(); + _blipL = null; } - if (blipR != null) + + if (_blipR != null) { - blipR.Dispose(); - blipR = null; + _blipR.Dispose(); + _blipR = null; } } } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IStatable.cs index 8d3b1d1816..f3d31aee93 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IStatable.cs @@ -1,40 +1,40 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using Newtonsoft.Json; using BizHawk.Emulation.Common; - namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { public partial class Gameboy : IStatable { - public bool BinarySaveStatesPreferred { get { return true; } } + public bool BinarySaveStatesPreferred => true; - public void SaveStateText(System.IO.TextWriter writer) + public void SaveStateText(TextWriter writer) { var s = SaveState(); ser.Serialize(writer, s); + // write extra copy of stuff we don't use writer.WriteLine(); writer.WriteLine("Frame {0}", Frame); } - public void LoadStateText(System.IO.TextReader reader) + public void LoadStateText(TextReader reader) { var s = (TextState)ser.Deserialize(reader, typeof(TextState)); LoadState(s); } - public void SaveStateBinary(System.IO.BinaryWriter writer) + public void SaveStateBinary(BinaryWriter writer) { - if (!LibGambatte.gambatte_newstatesave(GambatteState, savebuff, savebuff.Length)) + if (!LibGambatte.gambatte_newstatesave(GambatteState, _savebuff, _savebuff.Length)) + { throw new Exception("gambatte_newstatesave() returned false"); + } - writer.Write(savebuff.Length); - writer.Write(savebuff); + writer.Write(_savebuff.Length); + writer.Write(_savebuff); // other variables writer.Write(IsLagFrame); @@ -44,16 +44,20 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy writer.Write(_cycleCount); } - public void LoadStateBinary(System.IO.BinaryReader reader) + public void LoadStateBinary(BinaryReader reader) { int length = reader.ReadInt32(); - if (length != savebuff.Length) + if (length != _savebuff.Length) + { throw new InvalidOperationException("Savestate buffer size mismatch!"); + } - reader.Read(savebuff, 0, savebuff.Length); + reader.Read(_savebuff, 0, _savebuff.Length); - if (!LibGambatte.gambatte_newstateload(GambatteState, savebuff, savebuff.Length)) + if (!LibGambatte.gambatte_newstateload(GambatteState, _savebuff, _savebuff.Length)) + { throw new Exception("gambatte_newstateload() returned false"); + } // other variables IsLagFrame = reader.ReadBoolean(); @@ -65,26 +69,29 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy public byte[] SaveStateBinary() { - MemoryStream ms = new MemoryStream(savebuff2); + MemoryStream ms = new MemoryStream(_savebuff2); BinaryWriter bw = new BinaryWriter(ms); SaveStateBinary(bw); bw.Flush(); - if (ms.Position != savebuff2.Length) + if (ms.Position != _savebuff2.Length) + { throw new InvalidOperationException(); + } + ms.Close(); - return savebuff2; + return _savebuff2; } - private byte[] savebuff; - private byte[] savebuff2; + private byte[] _savebuff; + private byte[] _savebuff2; private void NewSaveCoreSetBuff() { - savebuff = new byte[LibGambatte.gambatte_newstatelen(GambatteState)]; - savebuff2 = new byte[savebuff.Length + 4 + 21]; + _savebuff = new byte[LibGambatte.gambatte_newstatelen(GambatteState)]; + _savebuff2 = new byte[_savebuff.Length + 4 + 21]; } - private JsonSerializer ser = new JsonSerializer { Formatting = Formatting.Indented }; + private readonly JsonSerializer ser = new JsonSerializer { Formatting = Formatting.Indented }; // other data in the text state besides core internal class TextStateData diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ITraceable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ITraceable.cs index 150c059a41..f2c92adc34 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ITraceable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.ITraceable.cs @@ -7,8 +7,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { public partial class Gameboy { - private ITraceable Tracer { get; set; } - private LibGambatte.TraceCallback tracecb; + private ITraceable Tracer { get; } + private LibGambatte.TraceCallback _tracecb; private void MakeTrace(IntPtr _s) { @@ -20,7 +20,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { Disassembly = NewDisassembler - .Disassemble((ushort)s[1], (addr) => LibGambatte.gambatte_cpuread(GambatteState, addr), out unused) + .Disassemble((ushort)s[1], addr => LibGambatte.gambatte_cpuread(GambatteState, addr), out unused) .PadRight(36), RegisterInfo = string.Format( @@ -38,8 +38,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy s[10] & 0xff, s[11] != 0 ? "skip" : "", s[12] & 0xff, - s[13] & 0xff - ) + s[13] & 0xff) }); } } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IVideoProvider.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IVideoProvider.cs index 62832b83fb..d8aa4b0391 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IVideoProvider.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.IVideoProvider.cs @@ -1,44 +1,25 @@ -using System; -using BizHawk.Emulation.Common; - -namespace BizHawk.Emulation.Cores.Nintendo.Gameboy +namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { public partial class Gameboy { /// /// stored image of most recent frame /// - private int[] VideoBuffer = new int[160 * 144]; + private readonly int[] VideoBuffer = new int[160 * 144]; public int[] GetVideoBuffer() { return VideoBuffer; } - public int VirtualWidth - { - // only sgb changes this, which we don't emulate here - get { return 160; } - } + public int VirtualWidth => 160; // only sgb changes this, which we don't emulate here - public int VirtualHeight - { - get { return 144; } - } + public int VirtualHeight => 144; - public int BufferWidth - { - get { return 160; } - } + public int BufferWidth => 160; - public int BufferHeight - { - get { return 144; } - } + public int BufferHeight => 144; - public int BackgroundColor - { - get { return 0; } - } + public int BackgroundColor => 0; } } diff --git a/BizHawk.sln.DotSettings b/BizHawk.sln.DotSettings index 32968282e1..162b3f8dd3 100644 --- a/BizHawk.sln.DotSettings +++ b/BizHawk.sln.DotSettings @@ -26,6 +26,7 @@ CDL CGB DB + DMG GB GBA GBC @@ -42,6 +43,7 @@ PCECD PSP PSX + RTC SG SGB SGX