diff --git a/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs b/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs new file mode 100644 index 0000000000..c74b98dbbb --- /dev/null +++ b/BizHawk.Emulation.Common/Base Implementations/CallbackBasedTraceBuffer.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +using BizHawk.Emulation.Common.IEmulatorExtensions; + +namespace BizHawk.Emulation.Common +{ + public abstract class CallbackBasedTraceBuffer : ITraceable + { + public CallbackBasedTraceBuffer(IDebuggable debuggableCore, IMemoryDomains memoryDomains, IDisassemblable disassembler) + { + if (!debuggableCore.MemoryCallbacksAvailable()) + { + throw new InvalidOperationException("Memory callbacks are required"); + } + + try + { + var dummy = debuggableCore.GetCpuFlagsAndRegisters(); + } + catch (NotImplementedException) + { + throw new InvalidOperationException("GetCpuFlagsAndRegisters is required"); + } + + Header = "Instructions"; + DebuggableCore = debuggableCore; + MemoryDomains = memoryDomains; + Disassembler = disassembler; + } + + protected readonly IMemoryDomains MemoryDomains; + protected readonly IDisassemblable Disassembler; + protected readonly IDebuggable DebuggableCore; + + protected readonly List Buffer = new List(); + + private bool _enabled; + + public abstract void TraceFromCallback(); + + public bool Enabled + { + get + { + return _enabled; + } + + set + { + _enabled = value; + DebuggableCore.MemoryCallbacks.Remove(TraceFromCallback); + + if (_enabled) + { + DebuggableCore.MemoryCallbacks.Add(new TracingMemoryCallback(TraceFromCallback)); + } + } + } + + public string Header { get; set; } + + public IEnumerable Contents + { + get { return Buffer; } + } + + public IEnumerable TakeContents() + { + var contents = Buffer.ToList(); + Buffer.Clear(); + return contents; + } + + public void Put(TraceInfo content) + { + if (Enabled) + { + Buffer.Add(content); + } + } + + public class TracingMemoryCallback : IMemoryCallback + { + public TracingMemoryCallback(Action callback) + { + Callback = callback; + } + + public MemoryCallbackType Type + { + get { return MemoryCallbackType.Execute; } + } + + public string Name + { + get { return "Trace Logging"; } + } + + public Action Callback { get; private set; } + + public uint? Address + { + get { return null; } + } + } + } +} diff --git a/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj b/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj index 4389a2effe..cfb34c0508 100644 --- a/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj +++ b/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj @@ -55,6 +55,7 @@ VersionInfo.cs + diff --git a/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj b/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj index f2fe4a4d32..41154a738b 100644 --- a/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj +++ b/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj @@ -817,7 +817,9 @@ GPGX.cs - + + GPGX.cs + GPGX.cs diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.ITraceable.cs b/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.ITraceable.cs index b01061faab..7b1f2d903b 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.ITraceable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.ITraceable.cs @@ -1,9 +1,8 @@ using BizHawk.Emulation.Common; using System; using System.Collections.Generic; -using System.Linq; using System.Text; -using BizHawk.Emulation.Common.IEmulatorExtensions; + using BizHawk.Common.NumberExtensions; namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx @@ -12,43 +11,15 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx { private readonly ITraceable Tracer; - // TODO: move this to BaseImplementations and make the TraceFromCallback settable by the core - public class CallbackBasedTraceBuffer : ITraceable + public class GPGXTraceBuffer : CallbackBasedTraceBuffer { - public CallbackBasedTraceBuffer(IDebuggable debuggableCore) + public GPGXTraceBuffer(IDebuggable debuggableCore, IMemoryDomains memoryDomains, IDisassemblable disassembler) + : base(debuggableCore, memoryDomains, disassembler) { - if (!debuggableCore.MemoryCallbacksAvailable()) - { - throw new InvalidOperationException("Memory callbacks are required"); - } - - try - { - var dummy = debuggableCore.GetCpuFlagsAndRegisters(); - } - catch(NotImplementedException) - { - throw new InvalidOperationException("GetCpuFlagsAndRegisters is required"); - } - - Header = "Instructions"; - DebuggableCore = debuggableCore; - - // TODO: refactor - MemoryDomains = (debuggableCore as IEmulator).ServiceProvider.GetService(); - Disassembler = (debuggableCore as IEmulator).ServiceProvider.GetService(); + Header = "M68K Instructions"; } - // TODO: think about this - private readonly IMemoryDomains MemoryDomains; - private readonly IDisassemblable Disassembler; - private readonly IDebuggable DebuggableCore; - - private readonly List Buffer = new List(); - - private bool _enabled; - - private void TraceFromCallback() + public override void TraceFromCallback() { var regs = DebuggableCore.GetCpuFlagsAndRegisters(); uint pc = (uint)regs["M68K PC"].Value; @@ -57,7 +28,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx var traceInfo = new TraceInfo { - Disassembly = string.Format("{0:X6}: {1,-32}", pc, disasm) + Disassembly = string.Format("{0:X6}: {1}", pc, disasm) }; var sb = new StringBuilder(); @@ -90,72 +61,6 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx Buffer.Add(traceInfo); } - - public bool Enabled - { - get - { - return _enabled; - } - - set - { - _enabled = value; - DebuggableCore.MemoryCallbacks.Remove(TraceFromCallback); - - if (_enabled) - { - DebuggableCore.MemoryCallbacks.Add(new TracingMemoryCallback(TraceFromCallback)); - } - } - } - - public string Header { get; set; } - - public IEnumerable Contents - { - get { return Buffer; } - } - - public IEnumerable TakeContents() - { - var contents = Buffer.ToList(); - Buffer.Clear(); - return contents; - } - - public void Put(TraceInfo content) - { - if (Enabled) - { - Buffer.Add(content); - } - } - - public class TracingMemoryCallback : IMemoryCallback - { - public TracingMemoryCallback(Action callback) - { - Callback = callback; - } - - public MemoryCallbackType Type - { - get { return MemoryCallbackType.Execute; } - } - - public string Name - { - get { return "Trace Logging"; } - } - - public Action Callback { get; private set; } - - public uint? Address - { - get { return null; } - } - } } } } diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.cs b/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.cs index 7cc3449241..2f0040b36d 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.cs @@ -162,7 +162,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx InitMemCallbacks(); KillMemCallbacks(); - Tracer = new CallbackBasedTraceBuffer(this); + Tracer = new GPGXTraceBuffer(this, MemoryDomains, this); (ServiceProvider as BasicServiceProvider).Register(Tracer); } catch