diff --git a/UI/Debugger/Controls/ctrlWatch.Designer.cs b/UI/Debugger/Controls/ctrlWatch.Designer.cs index 14d5f51..8ad6724 100644 --- a/UI/Debugger/Controls/ctrlWatch.Designer.cs +++ b/UI/Debugger/Controls/ctrlWatch.Designer.cs @@ -16,7 +16,9 @@ if(disposing && (components != null)) { components.Dispose(); } - WatchManager.WatchChanged -= WatchManager_WatchChanged; + if(_watchManager != null) { + _watchManager.WatchChanged -= WatchManager_WatchChanged; + } base.Dispose(disposing); } diff --git a/UI/Debugger/Controls/ctrlWatch.cs b/UI/Debugger/Controls/ctrlWatch.cs index 353f12a..f56e18d 100644 --- a/UI/Debugger/Controls/ctrlWatch.cs +++ b/UI/Debugger/Controls/ctrlWatch.cs @@ -29,6 +29,8 @@ namespace Mesen.GUI.Debugger private bool _isEditing = false; ListViewItem _keyDownItem = null; + private CpuType _cpuType; + private WatchManager _watchManager; public ctrlWatch() { @@ -37,13 +39,24 @@ namespace Mesen.GUI.Debugger this.DoubleBuffered = true; } - public CpuType CpuType { get; set; } + public CpuType CpuType + { + get { return _cpuType; } + set + { + _cpuType = value; + if(_watchManager != null) { + _watchManager.WatchChanged -= WatchManager_WatchChanged; + } + _watchManager = WatchManager.GetWatchManager(value); + _watchManager.WatchChanged += WatchManager_WatchChanged; + } + } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if(!IsDesignMode) { - WatchManager.WatchChanged += WatchManager_WatchChanged; mnuRemoveWatch.InitShortcut(this, nameof(DebuggerShortcutsConfig.WatchList_Delete)); mnuEditInMemoryViewer.InitShortcut(this, nameof(DebuggerShortcutsConfig.CodeWindow_EditInMemoryViewer)); mnuViewInDisassembly.InitShortcut(this, nameof(DebuggerShortcutsConfig.MemoryViewer_ViewInDisassembly)); @@ -104,7 +117,7 @@ namespace Mesen.GUI.Debugger public void UpdateWatch(bool autoResizeColumns = true) { - List watchContent = WatchManager.GetWatchContent(this.CpuType, _previousValues); + List watchContent = _watchManager.GetWatchContent(this.CpuType, _previousValues); _previousValues = watchContent; bool updating = false; @@ -161,7 +174,7 @@ namespace Mesen.GUI.Debugger lstWatch.EndUpdate(); } } - + private void lstWatch_SelectedIndexChanged(object sender, EventArgs e) { mnuRemoveWatch.Enabled = lstWatch.SelectedItems.Count >= 1; @@ -216,7 +229,7 @@ namespace Mesen.GUI.Debugger foreach(ListViewItem item in lstWatch.SelectedItems) { itemsToRemove.Add(item.Index); } - WatchManager.RemoveWatch(itemsToRemove.ToArray()); + _watchManager.RemoveWatch(itemsToRemove.ToArray()); } } @@ -287,7 +300,7 @@ namespace Mesen.GUI.Debugger { if(lstWatch.SelectedItems.Count > 0) { lstWatch.SelectedItems[0].Text = txtEdit.Text; - WatchManager.UpdateWatch(lstWatch.SelectedIndices[0], txtEdit.Text); + _watchManager.UpdateWatch(lstWatch.SelectedIndices[0], txtEdit.Text); } lstWatch.Focus(); } @@ -355,8 +368,8 @@ namespace Mesen.GUI.Debugger string currentEntry = lstWatch.Items[index].SubItems[0].Text; string entryAbove = lstWatch.Items[index - 1].SubItems[0].Text; SetSelectedItem(index - 1); - WatchManager.UpdateWatch(index - 1, currentEntry); - WatchManager.UpdateWatch(index, entryAbove); + _watchManager.UpdateWatch(index - 1, currentEntry); + _watchManager.UpdateWatch(index, entryAbove); } else { SetSelectedItem(index); } @@ -373,8 +386,8 @@ namespace Mesen.GUI.Debugger string currentEntry = lstWatch.Items[index].SubItems[0].Text; string entryBelow = lstWatch.Items[index + 1].SubItems[0].Text; SetSelectedItem(index + 1); - WatchManager.UpdateWatch(index + 1, currentEntry); - WatchManager.UpdateWatch(index, entryBelow); + _watchManager.UpdateWatch(index + 1, currentEntry); + _watchManager.UpdateWatch(index, entryBelow); } else { SetSelectedItem(index); } @@ -396,7 +409,7 @@ namespace Mesen.GUI.Debugger using(OpenFileDialog ofd = new OpenFileDialog()) { ofd.SetFilter("Watch files (*.mwf)|*.mwf"); if(ofd.ShowDialog() == DialogResult.OK) { - WatchManager.Import(ofd.FileName); + _watchManager.Import(ofd.FileName); } } } @@ -406,7 +419,7 @@ namespace Mesen.GUI.Debugger using(SaveFileDialog sfd = new SaveFileDialog()) { sfd.SetFilter("Watch files (*.mwf)|*.mwf"); if(sfd.ShowDialog() == DialogResult.OK) { - WatchManager.Export(sfd.FileName); + _watchManager.Export(sfd.FileName); } } } @@ -455,15 +468,15 @@ namespace Mesen.GUI.Debugger private void SetSelectionFormat(string formatString) { - List entries = WatchManager.WatchEntries; + List entries = _watchManager.WatchEntries; foreach(int i in lstWatch.SelectedIndices) { if(i < entries.Count) { Match match = WatchManager.FormatSuffixRegex.Match(entries[i]); if(match.Success) { - WatchManager.UpdateWatch(i, match.Groups[1].Value + formatString); + _watchManager.UpdateWatch(i, match.Groups[1].Value + formatString); } else { - WatchManager.UpdateWatch(i, entries[i] + formatString); - } + _watchManager.UpdateWatch(i, entries[i] + formatString); + } } } } diff --git a/UI/Debugger/WatchManager.cs b/UI/Debugger/WatchManager.cs index 864b3d4..3b3d236 100644 --- a/UI/Debugger/WatchManager.cs +++ b/UI/Debugger/WatchManager.cs @@ -11,12 +11,25 @@ namespace Mesen.GUI.Debugger { class WatchManager { - public static event EventHandler WatchChanged; - private static List _watchEntries = new List(); - private static Regex _arrayWatchRegex = new Regex(@"\[((\$[0-9A-Fa-f]+)|(\d+)|([@_a-zA-Z0-9]+))\s*,\s*(\d+)\]", RegexOptions.Compiled); public static Regex FormatSuffixRegex = new Regex(@"^(.*),\s*([B|H|S|U])([\d]){0,1}$", RegexOptions.Compiled); + private static Regex _arrayWatchRegex = new Regex(@"\[((\$[0-9A-Fa-f]+)|(\d+)|([@_a-zA-Z0-9]+))\s*,\s*(\d+)\]", RegexOptions.Compiled); - public static List WatchEntries + public event EventHandler WatchChanged; + private List _watchEntries = new List(); + + private static Dictionary _watchManagers = new Dictionary(); + + public static WatchManager GetWatchManager(CpuType cpuType) + { + WatchManager manager; + if(!_watchManagers.TryGetValue(cpuType, out manager)) { + manager = new WatchManager(); + _watchManagers[cpuType] = manager; + } + return manager; + } + + public List WatchEntries { get { return _watchEntries; } set @@ -26,7 +39,7 @@ namespace Mesen.GUI.Debugger } } - public static List GetWatchContent(CpuType cpuType, List previousValues) + public List GetWatchContent(CpuType cpuType, List previousValues) { WatchFormatStyle defaultStyle = ConfigManager.Config.Debug.Debugger.WatchFormat; int defaultByteLength = 1; @@ -72,7 +85,7 @@ namespace Mesen.GUI.Debugger return list; } - private static string FormatValue(int value, WatchFormatStyle style, int byteLength) + private string FormatValue(int value, WatchFormatStyle style, int byteLength) { switch(style) { case WatchFormatStyle.Unsigned: return ((UInt32)value).ToString(); @@ -101,12 +114,12 @@ namespace Mesen.GUI.Debugger } } - public static bool IsArraySyntax(string expression) + public bool IsArraySyntax(string expression) { return _arrayWatchRegex.IsMatch(expression); } - private static bool ProcessFormatSpecifier(ref string expression, ref WatchFormatStyle style, ref int byteLength) + private bool ProcessFormatSpecifier(ref string expression, ref WatchFormatStyle style, ref int byteLength) { Match match = WatchManager.FormatSuffixRegex.Match(expression); if(!match.Success) { @@ -132,7 +145,7 @@ namespace Mesen.GUI.Debugger return true; } - private static string ProcessArrayDisplaySyntax(WatchFormatStyle style, ref bool forceHasChanged, Match match) + private string ProcessArrayDisplaySyntax(WatchFormatStyle style, ref bool forceHasChanged, Match match) { string newValue; int address; @@ -166,7 +179,7 @@ namespace Mesen.GUI.Debugger return newValue; } - public static void AddWatch(params string[] expressions) + public void AddWatch(params string[] expressions) { foreach(string expression in expressions) { _watchEntries.Add(expression); @@ -174,7 +187,7 @@ namespace Mesen.GUI.Debugger WatchChanged?.Invoke(null, EventArgs.Empty); } - public static void UpdateWatch(int index, string expression) + public void UpdateWatch(int index, string expression) { if(string.IsNullOrWhiteSpace(expression)) { RemoveWatch(index); @@ -188,7 +201,7 @@ namespace Mesen.GUI.Debugger } } - public static void RemoveWatch(params int[] indexes) + public void RemoveWatch(params int[] indexes) { HashSet set = new HashSet(indexes); _watchEntries = _watchEntries.Where((el, index) => !set.Contains(index)).ToList(); @@ -196,16 +209,16 @@ namespace Mesen.GUI.Debugger WatchChanged?.Invoke(null, EventArgs.Empty); } - public static void Import(string filename) + public void Import(string filename) { if(File.Exists(filename)) { - WatchManager.WatchEntries = new List(File.ReadAllLines(filename)); + WatchEntries = new List(File.ReadAllLines(filename)); } } - public static void Export(string filename) + public void Export(string filename) { - File.WriteAllLines(filename, WatchManager.WatchEntries); + File.WriteAllLines(filename, WatchEntries); } } diff --git a/UI/Debugger/Workspace/DebugWorkspace.cs b/UI/Debugger/Workspace/DebugWorkspace.cs index c8ef7ee..f5ade38 100644 --- a/UI/Debugger/Workspace/DebugWorkspace.cs +++ b/UI/Debugger/Workspace/DebugWorkspace.cs @@ -15,6 +15,7 @@ namespace Mesen.GUI.Debugger.Workspace { public List Breakpoints = new List(); public List WatchValues = new List(); + public List SpcWatchValues = new List(); //public List Labels = new List(); public List TblMappings = null; private string _filePath; diff --git a/UI/Debugger/Workspace/DebugWorkspaceManager.cs b/UI/Debugger/Workspace/DebugWorkspaceManager.cs index b54e4ee..ae6cada 100644 --- a/UI/Debugger/Workspace/DebugWorkspaceManager.cs +++ b/UI/Debugger/Workspace/DebugWorkspaceManager.cs @@ -15,7 +15,8 @@ namespace Mesen.GUI.Debugger.Workspace public static void SaveWorkspace() { if(_workspace != null) { - _workspace.WatchValues = new List(WatchManager.WatchEntries); + _workspace.WatchValues = new List(WatchManager.GetWatchManager(CpuType.Cpu).WatchEntries); + _workspace.SpcWatchValues = new List(WatchManager.GetWatchManager(CpuType.Spc).WatchEntries); _workspace.Breakpoints = new List(BreakpointManager.Breakpoints); _workspace.Save(); } @@ -32,7 +33,9 @@ namespace Mesen.GUI.Debugger.Workspace if(_workspace != null) { _workspace.Breakpoints = new List(); _workspace.WatchValues = new List(); - WatchManager.WatchEntries = _workspace.WatchValues; + _workspace.SpcWatchValues = new List(); + WatchManager.GetWatchManager(CpuType.Cpu).WatchEntries = _workspace.WatchValues; + WatchManager.GetWatchManager(CpuType.Spc).WatchEntries = _workspace.SpcWatchValues; BreakpointManager.SetBreakpoints(_workspace.Breakpoints); _workspace.Save(); Clear(); @@ -50,7 +53,8 @@ namespace Mesen.GUI.Debugger.Workspace _workspace = DebugWorkspace.GetWorkspace(); //Load watch entries - WatchManager.WatchEntries = _workspace.WatchValues; + WatchManager.GetWatchManager(CpuType.Cpu).WatchEntries = _workspace.WatchValues; + WatchManager.GetWatchManager(CpuType.Spc).WatchEntries = _workspace.SpcWatchValues; //Load breakpoints BreakpointManager.SetBreakpoints(_workspace.Breakpoints);