2019-05-03 14:32:16 -04:00
|
|
|
|
using Mesen.GUI.Debugger.Labels;
|
|
|
|
|
using System;
|
2019-03-01 20:27:49 -05:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Debugger
|
|
|
|
|
{
|
|
|
|
|
public class Breakpoint
|
|
|
|
|
{
|
|
|
|
|
private SnesMemoryType _memoryType = SnesMemoryType.CpuMemory;
|
|
|
|
|
private bool _isCpuBreakpoint = true;
|
|
|
|
|
|
|
|
|
|
public bool BreakOnRead = false;
|
|
|
|
|
public bool BreakOnWrite = false;
|
|
|
|
|
public bool BreakOnExec = true;
|
|
|
|
|
|
|
|
|
|
public bool Enabled = true;
|
|
|
|
|
public bool MarkEvent = false;
|
|
|
|
|
public UInt32 Address = UInt32.MaxValue;
|
|
|
|
|
public UInt32 StartAddress;
|
|
|
|
|
public UInt32 EndAddress;
|
2019-07-25 22:22:09 -04:00
|
|
|
|
public CpuType CpuType;
|
2019-03-01 20:27:49 -05:00
|
|
|
|
public BreakpointAddressType AddressType = BreakpointAddressType.SingleAddress;
|
|
|
|
|
public string Condition = "";
|
|
|
|
|
|
|
|
|
|
public SnesMemoryType MemoryType
|
|
|
|
|
{
|
|
|
|
|
get { return _memoryType; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_memoryType = value;
|
|
|
|
|
_isCpuBreakpoint = IsTypeCpuBreakpoint(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetAddressString(bool showLabel)
|
|
|
|
|
{
|
|
|
|
|
string addr = "";
|
2019-04-07 12:25:14 -04:00
|
|
|
|
string format = _memoryType == SnesMemoryType.SpcMemory ? "X4" : "X6";
|
2019-03-01 20:27:49 -05:00
|
|
|
|
switch(AddressType) {
|
|
|
|
|
case BreakpointAddressType.AnyAddress:
|
|
|
|
|
return "<any>";
|
|
|
|
|
case BreakpointAddressType.SingleAddress:
|
2019-05-03 14:32:16 -04:00
|
|
|
|
addr += $"${Address.ToString(format)}";
|
2019-03-01 20:27:49 -05:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BreakpointAddressType.AddressRange:
|
2019-05-03 14:32:16 -04:00
|
|
|
|
addr = $"${StartAddress.ToString(format)} - ${EndAddress.ToString(format)}";
|
2019-03-01 20:27:49 -05:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-03 14:32:16 -04:00
|
|
|
|
if(showLabel) {
|
|
|
|
|
string label = GetAddressLabel();
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(label)) {
|
|
|
|
|
addr += " [" + label + "]";
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-01 20:27:49 -05:00
|
|
|
|
return addr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsTypeCpuBreakpoint(SnesMemoryType type)
|
|
|
|
|
{
|
|
|
|
|
return (
|
2019-07-25 22:22:09 -04:00
|
|
|
|
type != SnesMemoryType.Register &&
|
|
|
|
|
type != SnesMemoryType.VideoRam &&
|
|
|
|
|
type != SnesMemoryType.CGRam &&
|
|
|
|
|
type != SnesMemoryType.SpriteRam
|
2019-03-01 20:27:49 -05:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetEnabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
Enabled = enabled;
|
|
|
|
|
BreakpointManager.RefreshBreakpoints(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetMarked(bool marked)
|
|
|
|
|
{
|
|
|
|
|
MarkEvent = marked;
|
|
|
|
|
BreakpointManager.RefreshBreakpoints(this);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 21:22:59 -04:00
|
|
|
|
public bool IsAbsoluteAddress { get { return !MemoryType.IsRelativeMemory(); } }
|
2019-03-01 20:27:49 -05:00
|
|
|
|
public bool IsCpuBreakpoint { get { return this._isCpuBreakpoint; } }
|
|
|
|
|
|
|
|
|
|
private BreakpointTypeFlags Type
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
BreakpointTypeFlags type = BreakpointTypeFlags.None;
|
|
|
|
|
if(BreakOnRead) {
|
|
|
|
|
type |= BreakpointTypeFlags.Read;
|
|
|
|
|
}
|
|
|
|
|
if(BreakOnWrite) {
|
|
|
|
|
type |= BreakpointTypeFlags.Write;
|
|
|
|
|
}
|
|
|
|
|
if(BreakOnExec && IsCpuBreakpoint) {
|
|
|
|
|
type |= BreakpointTypeFlags.Execute;
|
|
|
|
|
}
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ToReadableType()
|
|
|
|
|
{
|
|
|
|
|
string type;
|
|
|
|
|
|
|
|
|
|
switch(MemoryType) {
|
|
|
|
|
default: throw new Exception("invalid type");
|
|
|
|
|
case SnesMemoryType.CpuMemory: type = "CPU"; break;
|
2019-07-25 22:22:09 -04:00
|
|
|
|
case SnesMemoryType.SpcMemory: type = "SPC"; break;
|
|
|
|
|
case SnesMemoryType.Sa1Memory: type = "SA1"; break;
|
2019-07-30 22:34:52 -04:00
|
|
|
|
case SnesMemoryType.GsuMemory: type = "GSU"; break;
|
2019-04-27 12:10:31 -04:00
|
|
|
|
|
2019-03-01 20:27:49 -05:00
|
|
|
|
case SnesMemoryType.PrgRom: type = "PRG"; break;
|
|
|
|
|
case SnesMemoryType.WorkRam: type = "WRAM"; break;
|
|
|
|
|
case SnesMemoryType.SaveRam: type = "SRAM"; break;
|
|
|
|
|
case SnesMemoryType.VideoRam: type = "VRAM"; break;
|
|
|
|
|
case SnesMemoryType.SpriteRam: type = "OAM"; break;
|
|
|
|
|
case SnesMemoryType.CGRam: type = "CG"; break;
|
2019-04-27 12:10:31 -04:00
|
|
|
|
|
2019-04-28 22:19:52 -04:00
|
|
|
|
case SnesMemoryType.SpcRam: type = "RAM"; break;
|
|
|
|
|
case SnesMemoryType.SpcRom: type = "ROM"; break;
|
2019-04-27 12:10:31 -04:00
|
|
|
|
|
2019-07-25 22:22:09 -04:00
|
|
|
|
case SnesMemoryType.Sa1InternalRam: type = "IRAM"; break;
|
2019-07-30 22:34:52 -04:00
|
|
|
|
case SnesMemoryType.GsuWorkRam: type = "GWRAM"; break;
|
2019-07-25 22:22:09 -04:00
|
|
|
|
|
2019-04-27 12:10:31 -04:00
|
|
|
|
case SnesMemoryType.Register: type = "REG"; break;
|
2019-03-01 20:27:49 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type += ":";
|
|
|
|
|
type += BreakOnRead ? "R" : "‒";
|
|
|
|
|
type += BreakOnWrite ? "W" : "‒";
|
|
|
|
|
if(IsCpuBreakpoint) {
|
|
|
|
|
type += BreakOnExec ? "X" : "‒";
|
|
|
|
|
}
|
|
|
|
|
return type;
|
|
|
|
|
}
|
2019-05-03 14:32:16 -04:00
|
|
|
|
|
|
|
|
|
public string GetAddressLabel()
|
|
|
|
|
{
|
|
|
|
|
UInt32 address = AddressType == BreakpointAddressType.SingleAddress ? this.Address : this.StartAddress;
|
|
|
|
|
|
|
|
|
|
if(IsCpuBreakpoint) {
|
|
|
|
|
CodeLabel label;
|
|
|
|
|
if(this.IsAbsoluteAddress) {
|
|
|
|
|
label = LabelManager.GetLabel(address, this.MemoryType);
|
|
|
|
|
} else {
|
|
|
|
|
label = LabelManager.GetLabel(new AddressInfo() { Address = (int)address, Type = this.MemoryType });
|
|
|
|
|
}
|
|
|
|
|
if(label != null) {
|
|
|
|
|
return label.Label;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
2019-03-01 20:27:49 -05:00
|
|
|
|
|
|
|
|
|
public int GetRelativeAddress()
|
|
|
|
|
{
|
|
|
|
|
UInt32 address = AddressType == BreakpointAddressType.SingleAddress ? this.Address : this.StartAddress;
|
|
|
|
|
if(IsCpuBreakpoint && this.IsAbsoluteAddress) {
|
2019-04-27 12:10:31 -04:00
|
|
|
|
return DebugApi.GetRelativeAddress(new AddressInfo() { Address = (int)address, Type = this.MemoryType }).Address;
|
2019-03-01 20:27:49 -05:00
|
|
|
|
} else {
|
|
|
|
|
return (int)address;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int GetRelativeAddressEnd()
|
|
|
|
|
{
|
|
|
|
|
if(this.AddressType == BreakpointAddressType.AddressRange){
|
|
|
|
|
if(IsCpuBreakpoint && this.IsAbsoluteAddress) {
|
2019-04-27 12:10:31 -04:00
|
|
|
|
return DebugApi.GetRelativeAddress(new AddressInfo() { Address = (int)this.EndAddress, Type = this.MemoryType }).Address;
|
2019-03-01 20:27:49 -05:00
|
|
|
|
} else {
|
|
|
|
|
return (int)this.EndAddress;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-04-28 22:19:52 -04:00
|
|
|
|
|
2019-07-25 22:22:09 -04:00
|
|
|
|
public bool Matches(UInt32 address, SnesMemoryType type, CpuType? cpuType)
|
2019-03-01 20:27:49 -05:00
|
|
|
|
{
|
2019-07-25 22:22:09 -04:00
|
|
|
|
if((cpuType.HasValue && cpuType.Value != this.CpuType) || IsTypeCpuBreakpoint(type) != this.IsCpuBreakpoint) {
|
2019-03-01 20:27:49 -05:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(this.AddressType == BreakpointAddressType.SingleAddress) {
|
|
|
|
|
return address == this.Address && type == this.MemoryType;
|
|
|
|
|
} else if(this.AddressType == BreakpointAddressType.AddressRange) {
|
|
|
|
|
return address >= this.StartAddress && address <= this.EndAddress && type == this.MemoryType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InteropBreakpoint ToInteropBreakpoint(int breakpointId)
|
|
|
|
|
{
|
|
|
|
|
InteropBreakpoint bp = new InteropBreakpoint() {
|
|
|
|
|
Id = breakpointId,
|
2019-07-25 22:22:09 -04:00
|
|
|
|
CpuType = CpuType,
|
2019-03-01 20:27:49 -05:00
|
|
|
|
MemoryType = MemoryType,
|
|
|
|
|
Type = Type,
|
|
|
|
|
MarkEvent = MarkEvent,
|
|
|
|
|
Enabled = Enabled
|
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bp.Condition = new byte[1000];
|
|
|
|
|
byte[] condition = Encoding.UTF8.GetBytes(Condition.Replace(Environment.NewLine, " "));
|
|
|
|
|
Array.Copy(condition, bp.Condition, condition.Length);
|
|
|
|
|
return bp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum BreakpointAddressType
|
|
|
|
|
{
|
|
|
|
|
AnyAddress,
|
|
|
|
|
SingleAddress,
|
|
|
|
|
AddressRange,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
public enum BreakpointTypeFlags
|
|
|
|
|
{
|
|
|
|
|
None = 0,
|
|
|
|
|
Execute = 1,
|
|
|
|
|
Read = 2,
|
|
|
|
|
Write = 4,
|
|
|
|
|
}
|
|
|
|
|
}
|