2018-02-10 21:23:22 -05:00
|
|
|
|
using Mesen.GUI.Forms;
|
|
|
|
|
using System;
|
2015-08-02 19:27:02 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-01-09 13:15:43 -05:00
|
|
|
|
using System.Runtime.InteropServices;
|
2015-08-02 19:27:02 -04:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Debugger
|
|
|
|
|
{
|
|
|
|
|
public class Breakpoint
|
|
|
|
|
{
|
2018-02-11 15:58:23 -05:00
|
|
|
|
private DebugMemoryType _memoryType = DebugMemoryType.CpuMemory;
|
|
|
|
|
private bool _isCpuBreakpoint = true;
|
|
|
|
|
private AddressType _equivalentAddressType = GUI.AddressType.InternalRam;
|
|
|
|
|
|
|
|
|
|
public DebugMemoryType MemoryType
|
|
|
|
|
{
|
|
|
|
|
get { return _memoryType; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_memoryType = value;
|
2018-02-12 18:49:35 -05:00
|
|
|
|
_isCpuBreakpoint = IsTypeCpuBreakpoint(value);
|
2018-02-11 15:58:23 -05:00
|
|
|
|
if(_isCpuBreakpoint) {
|
|
|
|
|
_equivalentAddressType = value.ToAddressType();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-09 13:15:43 -05:00
|
|
|
|
public bool BreakOnRead = false;
|
|
|
|
|
public bool BreakOnWrite = false;
|
|
|
|
|
public bool BreakOnExec = true;
|
2015-08-02 19:27:02 -04:00
|
|
|
|
|
|
|
|
|
public bool Enabled = true;
|
2018-02-18 22:41:50 -05:00
|
|
|
|
public bool MarkEvent = false;
|
2018-12-23 13:12:46 -05:00
|
|
|
|
public bool ProcessDummyReadWrites = false;
|
2019-02-18 21:01:12 -05:00
|
|
|
|
public UInt32 Address = UInt32.MaxValue;
|
2016-12-04 12:21:20 -05:00
|
|
|
|
public UInt32 StartAddress;
|
|
|
|
|
public UInt32 EndAddress;
|
|
|
|
|
public BreakpointAddressType AddressType = BreakpointAddressType.SingleAddress;
|
2016-01-09 13:15:43 -05:00
|
|
|
|
public string Condition = "";
|
2015-08-02 19:27:02 -04:00
|
|
|
|
|
2017-12-31 17:22:54 -05:00
|
|
|
|
public string GetAddressString(bool showLabel)
|
2016-12-04 12:21:20 -05:00
|
|
|
|
{
|
2017-12-31 17:22:54 -05:00
|
|
|
|
string addr = "";
|
2016-12-04 12:21:20 -05:00
|
|
|
|
switch(AddressType) {
|
|
|
|
|
case BreakpointAddressType.AnyAddress: return "<any>";
|
|
|
|
|
case BreakpointAddressType.SingleAddress:
|
|
|
|
|
if(IsAbsoluteAddress) {
|
2017-08-03 21:30:44 -04:00
|
|
|
|
int relativeAddress = this.GetRelativeAddress();
|
|
|
|
|
if(relativeAddress >= 0) {
|
2018-02-10 21:23:22 -05:00
|
|
|
|
addr += $"${relativeAddress.ToString("X4")} ";
|
|
|
|
|
} else {
|
|
|
|
|
addr += "N/A ";
|
2017-08-03 21:30:44 -04:00
|
|
|
|
}
|
2018-02-10 21:23:22 -05:00
|
|
|
|
addr += $"[${Address.ToString("X4")}]";
|
2016-12-04 12:21:20 -05:00
|
|
|
|
} else {
|
2018-02-10 21:23:22 -05:00
|
|
|
|
addr = $"${Address.ToString("X4")}";
|
2016-12-04 12:21:20 -05:00
|
|
|
|
}
|
2017-12-31 17:22:54 -05:00
|
|
|
|
break;
|
2016-12-04 12:21:20 -05:00
|
|
|
|
|
|
|
|
|
case BreakpointAddressType.AddressRange:
|
|
|
|
|
if(IsAbsoluteAddress) {
|
2017-12-31 17:22:54 -05:00
|
|
|
|
addr = $"[${StartAddress.ToString("X4")}] - [${EndAddress.ToString("X4")}]";
|
2016-12-04 12:21:20 -05:00
|
|
|
|
} else {
|
2017-12-31 17:22:54 -05:00
|
|
|
|
addr = $"${StartAddress.ToString("X4")} - ${EndAddress.ToString("X4")}";
|
2016-12-04 12:21:20 -05:00
|
|
|
|
}
|
2017-12-31 17:22:54 -05:00
|
|
|
|
break;
|
2016-12-04 12:21:20 -05:00
|
|
|
|
}
|
2017-12-31 17:22:54 -05:00
|
|
|
|
|
|
|
|
|
string label = GetAddressLabel();
|
|
|
|
|
if(showLabel && !string.IsNullOrWhiteSpace(label)) {
|
|
|
|
|
addr = label + $", {addr}";
|
|
|
|
|
}
|
|
|
|
|
return addr;
|
2016-12-04 12:21:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 18:49:35 -05:00
|
|
|
|
public static bool IsTypeCpuBreakpoint(DebugMemoryType type)
|
|
|
|
|
{
|
|
|
|
|
return (
|
|
|
|
|
type == DebugMemoryType.CpuMemory ||
|
|
|
|
|
type == DebugMemoryType.WorkRam ||
|
|
|
|
|
type == DebugMemoryType.SaveRam ||
|
|
|
|
|
type == DebugMemoryType.PrgRom
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-09 13:15:43 -05:00
|
|
|
|
public void SetEnabled(bool enabled)
|
2015-08-02 19:27:02 -04:00
|
|
|
|
{
|
2016-01-09 13:15:43 -05:00
|
|
|
|
Enabled = enabled;
|
2016-01-10 00:33:33 -05:00
|
|
|
|
BreakpointManager.RefreshBreakpoints(this);
|
2015-08-02 19:27:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-19 23:23:26 -05:00
|
|
|
|
public void SetMarked(bool marked)
|
|
|
|
|
{
|
|
|
|
|
MarkEvent = marked;
|
|
|
|
|
BreakpointManager.RefreshBreakpoints(this);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 21:23:22 -05:00
|
|
|
|
public bool IsAbsoluteAddress { get { return MemoryType != DebugMemoryType.CpuMemory && MemoryType != DebugMemoryType.PpuMemory; } }
|
|
|
|
|
|
2018-02-11 15:58:23 -05:00
|
|
|
|
public bool IsCpuBreakpoint { get { return this._isCpuBreakpoint; } }
|
2017-08-03 21:30:44 -04:00
|
|
|
|
|
2018-12-23 00:10:00 -05:00
|
|
|
|
private BreakpointTypeFlags Type
|
2015-08-02 19:27:02 -04:00
|
|
|
|
{
|
2016-01-09 13:15:43 -05:00
|
|
|
|
get
|
|
|
|
|
{
|
2018-12-23 00:10:00 -05:00
|
|
|
|
BreakpointTypeFlags type = BreakpointTypeFlags.Global;
|
2016-01-09 13:15:43 -05:00
|
|
|
|
if(BreakOnRead) {
|
2018-12-23 00:10:00 -05:00
|
|
|
|
type |= IsCpuBreakpoint ? BreakpointTypeFlags.Read : BreakpointTypeFlags.ReadVram;
|
2016-01-09 13:15:43 -05:00
|
|
|
|
}
|
|
|
|
|
if(BreakOnWrite) {
|
2018-12-23 00:10:00 -05:00
|
|
|
|
type |= IsCpuBreakpoint ? BreakpointTypeFlags.Write : BreakpointTypeFlags.WriteVram;
|
2016-01-09 13:15:43 -05:00
|
|
|
|
}
|
2018-02-10 21:23:22 -05:00
|
|
|
|
if(BreakOnExec && IsCpuBreakpoint) {
|
2018-12-23 00:10:00 -05:00
|
|
|
|
type |= BreakpointTypeFlags.Execute;
|
2016-01-09 13:15:43 -05:00
|
|
|
|
}
|
|
|
|
|
return type;
|
|
|
|
|
}
|
2015-08-02 19:27:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 21:23:22 -05:00
|
|
|
|
public string ToReadableType()
|
|
|
|
|
{
|
|
|
|
|
string type;
|
|
|
|
|
|
|
|
|
|
switch(MemoryType) {
|
2019-01-14 19:01:13 -05:00
|
|
|
|
default: throw new Exception("invalid type");
|
2018-02-10 21:23:22 -05:00
|
|
|
|
case DebugMemoryType.CpuMemory: type = "CPU"; break;
|
|
|
|
|
case DebugMemoryType.PpuMemory: type = "PPU"; break;
|
|
|
|
|
case DebugMemoryType.PrgRom: type = "PRG"; break;
|
|
|
|
|
case DebugMemoryType.WorkRam: type = "WRAM"; break;
|
|
|
|
|
case DebugMemoryType.SaveRam: type = "SRAM"; break;
|
|
|
|
|
case DebugMemoryType.ChrRam: type = "CHR"; break;
|
|
|
|
|
case DebugMemoryType.ChrRom: type = "CHR"; break;
|
2019-01-14 19:01:13 -05:00
|
|
|
|
case DebugMemoryType.NametableRam: type = "NT"; break;
|
2018-02-10 21:23:22 -05:00
|
|
|
|
case DebugMemoryType.PaletteMemory: type = "PAL"; break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type += ":";
|
|
|
|
|
type += BreakOnRead ? "R" : "‒";
|
|
|
|
|
type += BreakOnWrite ? "W" : "‒";
|
|
|
|
|
if(IsCpuBreakpoint) {
|
|
|
|
|
type += BreakOnExec ? "X" : "‒";
|
|
|
|
|
}
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 17:22:54 -05:00
|
|
|
|
public string GetAddressLabel()
|
2017-08-03 21:30:44 -04:00
|
|
|
|
{
|
2017-12-31 17:22:54 -05:00
|
|
|
|
UInt32 address = AddressType == BreakpointAddressType.SingleAddress ? this.Address : this.StartAddress;
|
|
|
|
|
|
|
|
|
|
if(IsCpuBreakpoint) {
|
|
|
|
|
CodeLabel label;
|
2017-08-03 21:30:44 -04:00
|
|
|
|
if(this.IsAbsoluteAddress) {
|
2018-02-10 21:23:22 -05:00
|
|
|
|
label = LabelManager.GetLabel(address, this.MemoryType.ToAddressType());
|
2017-12-31 17:22:54 -05:00
|
|
|
|
} else {
|
|
|
|
|
label = LabelManager.GetLabel((UInt16)address);
|
|
|
|
|
}
|
|
|
|
|
if(label != null) {
|
|
|
|
|
return label.Label;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int GetRelativeAddress()
|
|
|
|
|
{
|
|
|
|
|
UInt32 address = AddressType == BreakpointAddressType.SingleAddress ? this.Address : this.StartAddress;
|
|
|
|
|
if(this.IsAbsoluteAddress) {
|
|
|
|
|
if(IsCpuBreakpoint) {
|
2018-02-10 21:23:22 -05:00
|
|
|
|
return InteropEmu.DebugGetRelativeAddress(address, this.MemoryType.ToAddressType());
|
2019-01-14 19:01:13 -05:00
|
|
|
|
} else {
|
|
|
|
|
return InteropEmu.DebugGetRelativePpuAddress(address, this.MemoryType.ToPpuAddressType());
|
2017-08-03 21:30:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-03 14:27:10 -05:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetRelativeAddressEnd()
|
|
|
|
|
{
|
|
|
|
|
if(this.AddressType == BreakpointAddressType.AddressRange && this.IsAbsoluteAddress) {
|
|
|
|
|
if(IsCpuBreakpoint) {
|
|
|
|
|
return InteropEmu.DebugGetRelativeAddress(this.EndAddress, this.MemoryType.ToAddressType());
|
2019-01-14 19:01:13 -05:00
|
|
|
|
} else {
|
|
|
|
|
return InteropEmu.DebugGetRelativePpuAddress(this.EndAddress, this.MemoryType.ToPpuAddressType());
|
2019-01-03 14:27:10 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Matches(int address, DebugMemoryType type)
|
|
|
|
|
{
|
|
|
|
|
if(IsTypeCpuBreakpoint(type) != this.IsCpuBreakpoint) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isRelativeMemory = type == DebugMemoryType.CpuMemory || type == DebugMemoryType.PpuMemory;
|
|
|
|
|
|
|
|
|
|
if(this.AddressType == BreakpointAddressType.SingleAddress) {
|
|
|
|
|
if(isRelativeMemory && this.IsAbsoluteAddress) {
|
|
|
|
|
return address == this.GetRelativeAddress();
|
|
|
|
|
}
|
|
|
|
|
return address == this.Address && type == this.MemoryType;
|
|
|
|
|
} else if(this.AddressType == BreakpointAddressType.AddressRange) {
|
|
|
|
|
if(isRelativeMemory && this.IsAbsoluteAddress) {
|
|
|
|
|
return address >= GetRelativeAddress() && address <= this.GetRelativeAddressEnd();
|
|
|
|
|
}
|
|
|
|
|
return address >= this.StartAddress && address <= this.EndAddress && type == this.MemoryType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2017-08-03 21:30:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 00:41:52 -04:00
|
|
|
|
public bool Matches(int relativeAddress, AddressTypeInfo info)
|
2017-08-03 21:30:44 -04:00
|
|
|
|
{
|
2018-02-20 21:20:45 -05:00
|
|
|
|
if(this.IsCpuBreakpoint && this.AddressType == BreakpointAddressType.SingleAddress) {
|
2018-02-10 21:23:22 -05:00
|
|
|
|
if(this.MemoryType == DebugMemoryType.CpuMemory) {
|
2017-08-03 21:30:44 -04:00
|
|
|
|
return relativeAddress == this.Address;
|
2018-02-10 21:23:22 -05:00
|
|
|
|
} else {
|
2018-02-11 15:58:23 -05:00
|
|
|
|
return _equivalentAddressType == info.Type && info.Address == this.Address;
|
2017-08-03 21:30:44 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-19 23:23:26 -05:00
|
|
|
|
public InteropBreakpoint ToInteropBreakpoint(int breakpointId)
|
2015-08-02 19:27:02 -04:00
|
|
|
|
{
|
2016-12-04 12:21:20 -05:00
|
|
|
|
InteropBreakpoint bp = new InteropBreakpoint() {
|
2018-02-19 23:23:26 -05:00
|
|
|
|
Id = breakpointId,
|
2018-02-10 21:23:22 -05:00
|
|
|
|
MemoryType = MemoryType,
|
2016-12-04 12:21:20 -05:00
|
|
|
|
Type = Type,
|
2018-02-18 22:41:50 -05:00
|
|
|
|
MarkEvent = MarkEvent,
|
2018-12-23 13:12:46 -05:00
|
|
|
|
ProcessDummyReadWrites = ProcessDummyReadWrites,
|
2018-02-18 22:41:50 -05:00
|
|
|
|
Enabled = Enabled
|
2016-12-04 12:21:20 -05:00
|
|
|
|
};
|
|
|
|
|
switch(AddressType) {
|
|
|
|
|
case BreakpointAddressType.AnyAddress:
|
|
|
|
|
bp.StartAddress = -1;
|
|
|
|
|
bp.EndAddress = -1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BreakpointAddressType.SingleAddress:
|
|
|
|
|
bp.StartAddress = (Int32)Address;
|
|
|
|
|
bp.EndAddress = -1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BreakpointAddressType.AddressRange:
|
|
|
|
|
bp.StartAddress = (Int32)StartAddress;
|
|
|
|
|
bp.EndAddress = (Int32)EndAddress;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-09 13:15:43 -05:00
|
|
|
|
bp.Condition = new byte[1000];
|
2018-02-10 21:23:22 -05:00
|
|
|
|
byte[] condition = Encoding.UTF8.GetBytes(Condition.Replace(Environment.NewLine, " "));
|
2016-01-09 13:15:43 -05:00
|
|
|
|
Array.Copy(condition, bp.Condition, condition.Length);
|
|
|
|
|
return bp;
|
2015-08-02 19:27:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-10 21:23:22 -05:00
|
|
|
|
|
|
|
|
|
public enum BreakpointAddressType
|
|
|
|
|
{
|
|
|
|
|
AnyAddress,
|
|
|
|
|
SingleAddress,
|
|
|
|
|
AddressRange,
|
|
|
|
|
}
|
2016-01-09 13:15:43 -05:00
|
|
|
|
}
|