using System;
using System.IO;
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Nintendo.NES
{
public sealed partial class NES : ICodeDataLogger
{
public void SetCDL(ICodeDataLog cdl)
{
CDL = cdl;
if (cdl == null)
{
cpu.ReadMemory = ReadMemory;
cpu.WriteMemory = WriteMemory;
cpu.PeekMemory = PeekMemory;
}
else
{
cpu.ReadMemory = ReadMemory_CDL;
cpu.WriteMemory = WriteMemory;
cpu.PeekMemory = FetchMemory_CDL;
}
}
public void NewCDL(ICodeDataLog cdl)
{
cdl["RAM"] = new byte[_memoryDomains["RAM"].Size];
if (_memoryDomains.Has("Save RAM"))
{
cdl["Save RAM"] = new byte[_memoryDomains["Save RAM"].Size];
}
if (_memoryDomains.Has("Battery RAM"))
{
cdl["Battery RAM"] = new byte[_memoryDomains["Battery RAM"].Size];
}
if (_memoryDomains.Has("Battery RAM"))
{
cdl["Battery RAM"] = new byte[_memoryDomains["Battery RAM"].Size];
}
cdl.SubType = "NES";
cdl.SubVer = 0;
}
[FeatureNotImplemented]
public void DisassembleCDL(Stream s, ICodeDataLog cdl)
{
}
private enum CDLog_AddrType
{
None,
ROM,
MainRAM,
SaveRAM,
}
[Flags]
private enum CDLog_Flags
{
ExecFirst = 0x01,
ExecOperand = 0x02,
Data = 0x04
};
private struct CDLog_MapResults
{
public CDLog_AddrType Type;
public int Address;
}
private delegate CDLog_MapResults MapMemoryDelegate(ushort addr, bool write);
private MapMemoryDelegate MapMemory;
private ICodeDataLog CDL;
private void RunCDL(ushort address, CDLog_Flags flags)
{
if (MapMemory != null)
{
CDLog_MapResults results = MapMemory(address, false);
switch (results.Type)
{
case CDLog_AddrType.None: break;
case CDLog_AddrType.MainRAM: CDL["Main RAM"][results.Address] |= (byte)flags; break;
case CDLog_AddrType.SaveRAM: CDL["Save RAM"][results.Address] |= (byte)flags; break;
}
}
}
///
/// A wrapper for FetchMemory which inserts CDL logic
///
private byte FetchMemory_CDL(ushort address)
{
RunCDL(address, CDLog_Flags.ExecFirst);
return PeekMemory(address);
}
///
/// A wrapper for ReadMemory which inserts CDL logic
///
private byte ReadMemory_CDL(ushort address)
{
RunCDL(address, CDLog_Flags.Data);
return ReadMemory(address);
}
}
}