Refactor GPGX trace logger - make a CallbackBasedTraceBuffer abstract class in Base Implementations with an abstract method CreateTrace, and inherit it as a GPGXTraceBuffer.

This commit is contained in:
adelikat 2016-02-23 21:58:40 -05:00
parent 75df712563
commit f9c9994161
5 changed files with 121 additions and 104 deletions

View File

@ -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<TraceInfo> Buffer = new List<TraceInfo>();
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<TraceInfo> Contents
{
get { return Buffer; }
}
public IEnumerable<TraceInfo> 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; }
}
}
}
}

View File

@ -55,6 +55,7 @@
<Link>VersionInfo.cs</Link>
</Compile>
<Compile Include="Base Implementations\BasicServiceProvider.cs" />
<Compile Include="Base Implementations\CallbackBasedTraceBuffer.cs" />
<Compile Include="Base Implementations\ControllerDefinition.cs" />
<Compile Include="Base Implementations\InputCallbackSystem.cs" />
<Compile Include="Base Implementations\MemoryCallbackSystem.cs" />

View File

@ -817,7 +817,9 @@
<Compile Include="Consoles\Sega\gpgx\GPGX.IStatable.cs">
<DependentUpon>GPGX.cs</DependentUpon>
</Compile>
<Compile Include="Consoles\Sega\gpgx\GPGX.ITraceable.cs" />
<Compile Include="Consoles\Sega\gpgx\GPGX.ITraceable.cs">
<DependentUpon>GPGX.cs</DependentUpon>
</Compile>
<Compile Include="Consoles\Sega\gpgx\GPGX.IVideoProvider.cs">
<DependentUpon>GPGX.cs</DependentUpon>
</Compile>

View File

@ -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<IMemoryDomains>();
Disassembler = (debuggableCore as IEmulator).ServiceProvider.GetService<IDisassemblable>();
Header = "M68K Instructions";
}
// TODO: think about this
private readonly IMemoryDomains MemoryDomains;
private readonly IDisassemblable Disassembler;
private readonly IDebuggable DebuggableCore;
private readonly List<TraceInfo> Buffer = new List<TraceInfo>();
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<TraceInfo> Contents
{
get { return Buffer; }
}
public IEnumerable<TraceInfo> 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; }
}
}
}
}
}

View File

@ -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<ITraceable>(Tracer);
}
catch