MemoryCallbackSystem - simplify by having a single Add method with a MemoryCallbackType parameter, and some simplifying of client code as a result

This commit is contained in:
adelikat 2014-12-07 18:53:56 +00:00
parent ba31d7d28b
commit 2d1a43dc65
18 changed files with 91 additions and 88 deletions

View File

@ -206,7 +206,7 @@ namespace BizHawk.Client.Common
var nlf = new NamedLuaFunction(luaf, "OnMemoryExecute", LogOutputCallback, CurrentThread, name);
_luaFunctions.Add(nlf);
Global.Emulator.AsDebuggable().MemoryCallbacks.AddExecute(nlf.Callback, address);
Global.Emulator.AsDebuggable().MemoryCallbacks.Add(MemoryCallbackType.Execute, nlf.Callback, address);
return nlf.Guid.ToString();
}
else
@ -226,7 +226,7 @@ namespace BizHawk.Client.Common
{
var nlf = new NamedLuaFunction(luaf, "OnMemoryRead", LogOutputCallback, CurrentThread, name);
_luaFunctions.Add(nlf);
Global.Emulator.AsDebuggable().MemoryCallbacks.AddRead(nlf.Callback, address);
Global.Emulator.AsDebuggable().MemoryCallbacks.Add(MemoryCallbackType.Read, nlf.Callback, address);
return nlf.Guid.ToString();
}
else
@ -246,7 +246,7 @@ namespace BizHawk.Client.Common
{
var nlf = new NamedLuaFunction(luaf, "OnMemoryWrite", LogOutputCallback, CurrentThread, name);
_luaFunctions.Add(nlf);
Global.Emulator.AsDebuggable().MemoryCallbacks.AddWrite(nlf.Callback, address);
Global.Emulator.AsDebuggable().MemoryCallbacks.Add(MemoryCallbackType.Write, nlf.Callback, address);
return nlf.Guid.ToString();
}
else

View File

@ -7,6 +7,8 @@ using System.Linq;
using System.Text;
using System.Windows.Forms;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk
{
public partial class AddBreakpointDialog : Form
@ -16,26 +18,26 @@ namespace BizHawk.Client.EmuHawk
InitializeComponent();
}
public BreakpointType BreakType
public MemoryCallbackType BreakType
{
get
{
if (ReadRadio.Checked)
{
return BreakpointType.Read;
return MemoryCallbackType.Read;
}
if (WriteRadio.Checked)
{
return BreakpointType.Write;
return MemoryCallbackType.Write;
}
if (ExecuteRadio.Checked)
{
return BreakpointType.Execute;
return MemoryCallbackType.Execute;
}
return BreakpointType.Read;
return MemoryCallbackType.Read;
}
}
@ -54,9 +56,5 @@ namespace BizHawk.Client.EmuHawk
{
}
}
public enum BreakpointType { Read, Write, Execute }
}

View File

@ -446,7 +446,7 @@ namespace BizHawk.Client.EmuHawk
{
public Action Callback { get; set; }
public void Add(Atari2600 core, uint address, BreakpointType type)
public void Add(Atari2600 core, uint address, MemoryCallbackType type)
{
Add(new AtariBreakpoint(core, Callback, address, type));
}
@ -457,7 +457,7 @@ namespace BizHawk.Client.EmuHawk
private bool _active;
private readonly Atari2600 _core;
public AtariBreakpoint(Atari2600 core, Action callBack, uint address, BreakpointType type, bool enabled = true)
public AtariBreakpoint(Atari2600 core, Action callBack, uint address, MemoryCallbackType type, bool enabled = true)
{
_core = core;
@ -473,7 +473,7 @@ namespace BizHawk.Client.EmuHawk
public Action Callback { get; set; }
public uint Address { get; set; }
public BreakpointType Type { get; set; }
public MemoryCallbackType Type { get; set; }
public bool Active
{
@ -500,18 +500,7 @@ namespace BizHawk.Client.EmuHawk
private void AddCallback()
{
switch (Type)
{
case BreakpointType.Read:
_core.MemoryCallbacks.AddRead(Callback, Address);
break;
case BreakpointType.Write:
_core.MemoryCallbacks.AddWrite(Callback, Address);
break;
case BreakpointType.Execute:
_core.MemoryCallbacks.AddExecute(Callback, Address);
break;
}
_core.MemoryCallbacks.Add(Type, Callback, Address);
}
private void RemoveCallback()

View File

@ -10,7 +10,7 @@ namespace BizHawk.Client.EmuHawk
{
public Action Callback { get; set; }
public void Add(IDebuggable core, uint address, BreakpointType type)
public void Add(IDebuggable core, uint address, MemoryCallbackType type)
{
Add(new Breakpoint(core, Callback, address, type));
}
@ -33,7 +33,7 @@ namespace BizHawk.Client.EmuHawk
private bool _active;
private readonly IDebuggable _core;
public Breakpoint(IDebuggable core, Action callBack, uint address, BreakpointType type, bool enabled = true)
public Breakpoint(IDebuggable core, Action callBack, uint address, MemoryCallbackType type, bool enabled = true)
{
_core = core;
@ -49,7 +49,7 @@ namespace BizHawk.Client.EmuHawk
public Action Callback { get; set; }
public uint Address { get; set; }
public BreakpointType Type { get; set; }
public MemoryCallbackType Type { get; set; }
public bool Active
{
@ -78,14 +78,14 @@ namespace BizHawk.Client.EmuHawk
{
switch (Type)
{
case BreakpointType.Read:
_core.MemoryCallbacks.AddRead(Callback, Address);
case MemoryCallbackType.Read:
_core.MemoryCallbacks.Add(MemoryCallbackType.Read, Callback, Address);
break;
case BreakpointType.Write:
_core.MemoryCallbacks.AddWrite(Callback, Address);
case MemoryCallbackType.Write:
_core.MemoryCallbacks.Add(MemoryCallbackType.Write, Callback, Address);
break;
case BreakpointType.Execute:
_core.MemoryCallbacks.AddExecute(Callback, Address);
case MemoryCallbackType.Execute:
_core.MemoryCallbacks.Add(MemoryCallbackType.Execute, Callback, Address);
break;
}
}

View File

@ -16,7 +16,31 @@ namespace BizHawk.Emulation.Common
private readonly List<Action> _executes = new List<Action>();
private readonly List<uint> _execAddrs = new List<uint>();
public void AddRead(Action function, uint? addr)
public void Add(MemoryCallbackType type, Action function, uint? addr)
{
switch (type)
{
case MemoryCallbackType.Read:
AddRead(function, addr);
break;
case MemoryCallbackType.Write:
AddWrite(function, addr);
break;
case MemoryCallbackType.Execute:
if (!addr.HasValue)
{
throw new InvalidOperationException("When assigning an execute callback, an address must be specified");
}
else
{
AddExecute(function, addr.Value);
}
break;
}
}
private void AddRead(Action function, uint? addr)
{
var hadAny = _reads.Any() || _writes.Any() || _executes.Any();
@ -27,7 +51,7 @@ namespace BizHawk.Emulation.Common
Changes(hadAny, hasAny);
}
public void AddWrite(Action function, uint? addr)
private void AddWrite(Action function, uint? addr)
{
var hadAny = _reads.Any() || _writes.Any() || _executes.Any();
@ -38,7 +62,7 @@ namespace BizHawk.Emulation.Common
Changes(hadAny, hasAny);
}
public void AddExecute(Action function, uint addr)
private void AddExecute(Action function, uint addr)
{
var hadAny = _reads.Any() || _writes.Any() || _executes.Any();
@ -49,7 +73,7 @@ namespace BizHawk.Emulation.Common
Changes(hadAny, hasAny);
}
public void CallRead(uint addr)
public void CallReads(uint addr)
{
for (int i = 0; i < _reads.Count; i++)
{
@ -60,7 +84,7 @@ namespace BizHawk.Emulation.Common
}
}
public void CallWrite(uint addr)
public void CallWrites(uint addr)
{
for (int i = 0; i < _writes.Count; i++)
{
@ -71,7 +95,7 @@ namespace BizHawk.Emulation.Common
}
}
public void CallExecute(uint addr)
public void CallExecutes(uint addr)
{
for (int i = 0; i < _executes.Count; i++)
{

View File

@ -21,36 +21,26 @@ namespace BizHawk.Emulation.Common
bool HasExecutes { get; }
/// <summary>
/// Adds a Read callback for the given address
/// Adds a callback for the given type to the given address
/// If no address is specified the callback will be hooked to all addresses
/// Note: an execute callback can not be added without an address, else an InvalidOperationException will occur
/// </summary>
void AddRead(Action function, uint? addr);
/// <summary>
/// Adds a Write callback for the given address
/// If no address is specified the callback will be hooked to all addresses
/// </summary>
void AddWrite(Action function, uint? addr);
/// <summary>
/// Adds an Execute callback for the given address
/// </summary>
void AddExecute(Action function, uint addr);
void Add(MemoryCallbackType type, Action function, uint? addr);
/// <summary>
/// Executes all Read callbacks for the given addr
/// </summary>
void CallRead(uint addr);
void CallReads(uint addr);
/// <summary>
/// Executes all Write callbacks for the given addr
/// </summary>
void CallWrite(uint addr);
void CallWrites(uint addr);
/// <summary>
/// Executes all Execute callbacks for the given addr
/// </summary>
void CallExecute(uint addr);
void CallExecutes(uint addr);
/// <summary>
/// Removes the given callback from the list
@ -67,4 +57,6 @@ namespace BizHawk.Emulation.Common
/// </summary>
void Clear();
}
public enum MemoryCallbackType { Read, Write, Execute }
}

View File

@ -59,7 +59,7 @@ namespace BizHawk.Emulation.Cores.Components.H6280
LagIFlag = FlagI;
if (Debug) Logger(State());
Core.MemoryCallbacks.CallExecute(PC);
Core.MemoryCallbacks.CallExecutes(PC);
if (CDLLoggingActive) CDLOpcode();
byte opcode = ReadMemory(PC++);

View File

@ -94,7 +94,7 @@ namespace BizHawk.Emulation.Cores.Calculators
ret = rom[romPage * 0x4000 + addr - 0x4000]; //other rom page
else ret = ram[addr - 0x8000];
MemoryCallbacks.CallRead(addr);
MemoryCallbacks.CallReads(addr);
return ret;
}
@ -107,7 +107,7 @@ namespace BizHawk.Emulation.Cores.Calculators
return; //other rom page
else ram[addr - 0x8000] = value;
MemoryCallbacks.CallWrite(addr);
MemoryCallbacks.CallWrites(addr);
}
public void WriteHardware(ushort addr, byte value)

View File

@ -108,7 +108,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
_mapper.Bit13 = addr.Bit(13);
var temp = _mapper.ReadMemory((ushort)(addr & 0x1FFF));
MemoryCallbacks.CallRead(addr);
MemoryCallbacks.CallReads(addr);
return temp;
}
@ -130,7 +130,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
_mapper.WriteMemory((ushort)(addr & 0x1FFF), value);
MemoryCallbacks.CallWrite(addr);
MemoryCallbacks.CallWrites(addr);
}
public void PokeMemory(ushort addr, byte value)
@ -140,7 +140,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600
public void ExecFetch(ushort addr)
{
MemoryCallbacks.CallExecute(addr);
MemoryCallbacks.CallExecutes(addr);
}
private static MapperBase SetMultiCartMapper(int romLength, int gameTotal)

View File

@ -285,9 +285,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA
void InitCallbacks()
{
padcb = new LibVBANext.StandardCallback(() => InputCallbacks.Call());
fetchcb = new LibVBANext.AddressCallback((addr) => MemoryCallbacks.CallExecute(addr));
readcb = new LibVBANext.AddressCallback((addr) => MemoryCallbacks.CallRead(addr));
writecb = new LibVBANext.AddressCallback((addr) => MemoryCallbacks.CallWrite(addr));
fetchcb = new LibVBANext.AddressCallback((addr) => MemoryCallbacks.CallExecutes(addr));
readcb = new LibVBANext.AddressCallback((addr) => MemoryCallbacks.CallReads(addr));
writecb = new LibVBANext.AddressCallback((addr) => MemoryCallbacks.CallWrites(addr));
tracecb = new LibVBANext.TraceCallback((addr, opcode) => Tracer.Put(Trace(addr, opcode)));
_inputCallbacks.ActiveChanged += SyncPadCallback;
_memorycallbacks.ActiveChanged += SyncMemoryCallbacks;

View File

@ -655,9 +655,9 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
void InitMemoryCallbacks()
{
readcb = (addr) => MemoryCallbacks.CallRead(addr);
writecb = (addr) => MemoryCallbacks.CallWrite(addr);
execcb = (addr) => MemoryCallbacks.CallExecute(addr);
readcb = (addr) => MemoryCallbacks.CallReads(addr);
writecb = (addr) => MemoryCallbacks.CallWrites(addr);
execcb = (addr) => MemoryCallbacks.CallExecutes(addr);
_memorycallbacks.ActiveChanged += RefreshMemoryCallbacks;
}

View File

@ -81,7 +81,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64
// we RefreshMemoryCallbacks() after the triggers in case the trigger turns itself off at that point
if (mcs.HasReads)
{
_readcb = delegate(uint addr) { mcs.CallRead(addr); };
_readcb = delegate(uint addr) { mcs.CallReads(addr); };
}
else
{
@ -90,7 +90,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.N64
if (mcs.HasWrites)
{
_writecb = delegate(uint addr) { mcs.CallWrite(addr); };
_writecb = delegate(uint addr) { mcs.CallWrites(addr); };
}
else
{

View File

@ -534,7 +534,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
public void ExecFetch(ushort addr)
{
MemoryCallbacks.CallExecute(addr);
MemoryCallbacks.CallExecutes(addr);
}
public byte ReadMemory(ushort addr)
@ -579,7 +579,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
ret = sysbus_watch[addr].ApplyGameGenie(ret);
}
MemoryCallbacks.CallRead(addr);
MemoryCallbacks.CallReads(addr);
DB = ret;
@ -633,7 +633,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
board.WritePRG(addr - 0x8000, value);
}
MemoryCallbacks.CallWrite(addr);
MemoryCallbacks.CallWrites(addr);
}
}

View File

@ -387,7 +387,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES
void ReadHook(uint addr)
{
MemoryCallbacks.CallRead(addr);
MemoryCallbacks.CallReads(addr);
//we RefreshMemoryCallbacks() after the trigger in case the trigger turns itself off at that point
//EDIT: for now, theres some IPC re-entrancy problem
//RefreshMemoryCallbacks();
@ -395,7 +395,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES
}
void ExecHook(uint addr)
{
MemoryCallbacks.CallExecute(addr);
MemoryCallbacks.CallExecutes(addr);
//we RefreshMemoryCallbacks() after the trigger in case the trigger turns itself off at that point
//EDIT: for now, theres some IPC re-entrancy problem
//RefreshMemoryCallbacks();
@ -403,7 +403,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES
}
void WriteHook(uint addr, byte val)
{
MemoryCallbacks.CallWrite(addr);
MemoryCallbacks.CallWrites(addr);
//we RefreshMemoryCallbacks() after the trigger in case the trigger turns itself off at that point
//EDIT: for now, theres some IPC re-entrancy problem
//RefreshMemoryCallbacks();

View File

@ -16,7 +16,7 @@
else
ret = SystemRam[address & RamSizeMask];
MemoryCallbacks.CallRead(address);
MemoryCallbacks.CallReads(address);
return ret;
}
@ -27,7 +27,7 @@
else if (address >= 0xC000)
SystemRam[address & RamSizeMask] = value;
MemoryCallbacks.CallWrite((uint)address);
MemoryCallbacks.CallWrites((uint)address);
}
void InitExt2kMapper(int size)

View File

@ -53,7 +53,7 @@
ret = SystemRam[address & RamSizeMask];
}
MemoryCallbacks.CallRead(address);
MemoryCallbacks.CallReads(address);
return ret;
}
@ -91,7 +91,7 @@
else if (address == 0xFFFF) RomBank2 = (byte)(value % RomBanks);
return;
}
MemoryCallbacks.CallWrite((uint)address);
MemoryCallbacks.CallWrites((uint)address);
}
void InitSegaMapper()

View File

@ -661,9 +661,9 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx
void InitMemCallbacks()
{
ExecCallback = new LibGPGX.mem_cb(a => MemoryCallbacks.CallExecute(a));
ReadCallback = new LibGPGX.mem_cb(a => MemoryCallbacks.CallRead(a));
WriteCallback = new LibGPGX.mem_cb(a => MemoryCallbacks.CallWrite(a));
ExecCallback = new LibGPGX.mem_cb(a => MemoryCallbacks.CallExecutes(a));
ReadCallback = new LibGPGX.mem_cb(a => MemoryCallbacks.CallReads(a));
WriteCallback = new LibGPGX.mem_cb(a => MemoryCallbacks.CallWrites(a));
_memoryCallbacks.ActiveChanged += RefreshMemCallbacks;
}

View File

@ -354,15 +354,15 @@ namespace BizHawk.Emulation.Cores.WonderSwan
void ReadCallback(uint addr)
{
MemoryCallbacks.CallRead(addr);
MemoryCallbacks.CallReads(addr);
}
void WriteCallback(uint addr)
{
MemoryCallbacks.CallWrite(addr);
MemoryCallbacks.CallWrites(addr);
}
void ExecCallback(uint addr)
{
MemoryCallbacks.CallExecute(addr);
MemoryCallbacks.CallExecutes(addr);
}
void ButtonCallback()
{