Mesen-SX/UI/Interop/DebugApi.cs
2019-03-03 16:34:23 -05:00

190 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mesen.GUI.Config;
using Mesen.GUI.Debugger;
using Mesen.GUI.Forms;
namespace Mesen.GUI
{
public class DebugApi
{
private const string DllPath = "MesenSCore.dll";
[DllImport(DllPath)] public static extern void InitializeDebugger();
[DllImport(DllPath)] public static extern void ReleaseDebugger();
[DllImport(DllPath)] public static extern void ResumeExecution();
[DllImport(DllPath)] public static extern void Step(Int32 instructionCount);
[DllImport(DllPath)] public static extern void StartTraceLogger([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]string filename);
[DllImport(DllPath)] public static extern void StopTraceLogger();
[DllImport(DllPath)] public static extern void SetTraceOptions(InteropTraceLoggerOptions options);
[DllImport(DllPath, EntryPoint = "GetDisassemblyLineData")] private static extern void GetDisassemblyLineDataWrapper(UInt32 lineIndex, ref InteropCodeLineData lineData);
public static CodeLineData GetDisassemblyLineData(UInt32 lineIndex)
{
InteropCodeLineData data = new InteropCodeLineData();
data.Comment = new byte[1000];
data.Text = new byte[1000];
data.ByteCode = new byte[4];
DebugApi.GetDisassemblyLineDataWrapper(lineIndex, ref data);
return new CodeLineData(data);
}
[DllImport(DllPath)] public static extern UInt32 GetDisassemblyLineCount();
[DllImport(DllPath)] public static extern UInt32 GetDisassemblyLineIndex(UInt32 cpuAddress);
[DllImport(DllPath)] public static extern int SearchDisassembly([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]string searchString, int startPosition, int endPosition, [MarshalAs(UnmanagedType.I1)]bool searchBackwards);
[DllImport(DllPath, EntryPoint = "GetExecutionTrace")] private static extern IntPtr GetExecutionTraceWrapper(UInt32 lineCount);
public static string GetExecutionTrace(UInt32 lineCount) { return Utf8Marshaler.PtrToStringUtf8(DebugApi.GetExecutionTraceWrapper(lineCount)); }
[DllImport(DllPath, EntryPoint = "GetState")] private static extern void GetStateWrapper(ref DebugState state);
public static DebugState GetState()
{
DebugState state = new DebugState();
DebugApi.GetStateWrapper(ref state);
return state;
}
[DllImport(DllPath)] public static extern Int32 EvaluateExpression([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]string expression, out EvalResultType resultType, [MarshalAs(UnmanagedType.I1)]bool useCache);
[DllImport(DllPath)] public static extern Int32 GetMemorySize(SnesMemoryType type);
[DllImport(DllPath)] public static extern Byte GetMemoryValue(SnesMemoryType type, UInt32 address);
[DllImport(DllPath)] public static extern void SetMemoryValue(SnesMemoryType type, UInt32 address, byte value);
[DllImport(DllPath)] public static extern void SetMemoryValues(SnesMemoryType type, UInt32 address, [In] byte[] data, Int32 length);
[DllImport(DllPath)] public static extern void SetMemoryState(SnesMemoryType type, [In] byte[] buffer, Int32 length);
[DllImport(DllPath)] public static extern void SetBreakpoints([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]InteropBreakpoint[] breakpoints, UInt32 length);
[DllImport(DllPath, EntryPoint = "GetMemoryState")] private static extern void GetMemoryStateWrapper(SnesMemoryType type, [In, Out] byte[] buffer);
public static byte[] GetMemoryState(SnesMemoryType type)
{
byte[] buffer = new byte[DebugApi.GetMemorySize(type)];
DebugApi.GetMemoryStateWrapper(type, buffer);
return buffer;
}
[DllImport(DllPath, EntryPoint = "GetTilemap")] private static extern void GetTilemapWrapper(GetTilemapOptions options, [In, Out] byte[] buffer);
public static byte[] GetTilemap(GetTilemapOptions options)
{
byte[] buffer = new byte[512*512*4];
DebugApi.GetTilemapWrapper(options, buffer);
return buffer;
}
[DllImport(DllPath)] public static extern void SetViewerUpdateTiming(Int32 viewerId, Int32 scanline, Int32 cycle);
}
public enum SnesMemoryType
{
CpuMemory,
PrgRom,
WorkRam,
SaveRam,
VideoRam,
SpriteRam,
CGRam,
}
public class AddressInfo
{
public Int32 Address;
public SnesMemoryType Type;
}
public struct CpuState
{
public UInt64 CycleCount;
public UInt16 A;
public UInt16 X;
public UInt16 Y;
public UInt16 SP;
public UInt16 D;
public UInt16 PC;
public byte K;
public byte DBR;
public byte PS;
[MarshalAs(UnmanagedType.I1)] public bool EmulationMode;
};
public struct PpuState
{
public UInt16 Cycle;
public UInt16 Scanline;
public UInt32 FrameCount;
[MarshalAs(UnmanagedType.I1)] public bool OverscanMode;
public byte BgMode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public LayerConfig[] Layers;
};
public struct LayerConfig
{
public UInt16 TilemapAddress;
public UInt16 ChrAddress;
public UInt16 HScroll;
public UInt16 VScroll;
[MarshalAs(UnmanagedType.I1)] public bool DoubleWidth;
[MarshalAs(UnmanagedType.I1)] public bool DoubleHeight;
[MarshalAs(UnmanagedType.I1)] public bool LargeTiles;
}
public struct DebugState
{
public CpuState Cpu;
public PpuState Ppu;
}
public struct GetTilemapOptions
{
public byte BgMode;
public byte Layer;
public byte Bpp;
public Int32 TilemapAddr;
public Int32 ChrAddr;
[MarshalAs(UnmanagedType.I1)] public bool ShowTileGrid;
[MarshalAs(UnmanagedType.I1)] public bool ShowScrollOverlay;
}
[Serializable]
public struct InteropTraceLoggerOptions
{
[MarshalAs(UnmanagedType.I1)] public bool ShowExtraInfo;
[MarshalAs(UnmanagedType.I1)] public bool IndentCode;
[MarshalAs(UnmanagedType.I1)] public bool UseLabels;
[MarshalAs(UnmanagedType.I1)] public bool UseWindowsEol;
[MarshalAs(UnmanagedType.I1)] public bool ExtendZeroPage;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1000)]
public byte[] Condition;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1000)]
public byte[] Format;
}
public enum EvalResultType
{
Numeric = 0,
Boolean = 1,
Invalid = 2,
DivideBy0 = 3,
OutOfScope = 4
}
}