2017-08-14 23:44:01 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Debugger
|
|
|
|
|
{
|
|
|
|
|
public class DebugWindowManager
|
|
|
|
|
{
|
2017-12-26 15:59:58 -05:00
|
|
|
|
private static HashSet<Form> _openedWindows = new HashSet<Form>();
|
2017-08-14 23:44:01 -04:00
|
|
|
|
|
2018-04-15 10:52:38 -04:00
|
|
|
|
public static bool ScriptWindowOpened { get { return _openedWindows.Any(wnd => wnd is frmScript); } }
|
|
|
|
|
|
2017-08-14 23:44:01 -04:00
|
|
|
|
public static void OpenDebugWindow(DebugWindow window)
|
|
|
|
|
{
|
|
|
|
|
Form existingWindow = GetExistingSingleInstanceWindow(window);
|
|
|
|
|
if(existingWindow != null) {
|
|
|
|
|
existingWindow.BringToFront();
|
|
|
|
|
existingWindow.Focus();
|
|
|
|
|
} else {
|
|
|
|
|
Form frm = null;
|
|
|
|
|
switch(window) {
|
|
|
|
|
case DebugWindow.PpuViewer: frm = new frmPpuViewer(); break;
|
|
|
|
|
case DebugWindow.TraceLogger: frm = new frmTraceLogger(); break;
|
|
|
|
|
case DebugWindow.MemoryViewer: frm = new frmMemoryViewer(); break;
|
|
|
|
|
case DebugWindow.Assembler: frm = new frmAssembler(); break;
|
|
|
|
|
case DebugWindow.Debugger: frm = new frmDebugger(); break;
|
2017-08-30 18:31:27 -04:00
|
|
|
|
case DebugWindow.ScriptWindow: frm = new frmScript(); break;
|
2018-01-01 23:23:18 -05:00
|
|
|
|
case DebugWindow.ApuViewer: frm = new frmApuViewer(); break;
|
2018-02-18 22:41:50 -05:00
|
|
|
|
case DebugWindow.EventViewer: frm = new frmEventViewer(); break;
|
2017-08-14 23:44:01 -04:00
|
|
|
|
}
|
|
|
|
|
_openedWindows.Add(frm);
|
|
|
|
|
frm.FormClosed += Debugger_FormClosed;
|
|
|
|
|
frm.Show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-19 14:36:00 -04:00
|
|
|
|
public static void OpenAssembler(string code = "", UInt16 startAddress = 0, UInt16 blockLength = 0)
|
|
|
|
|
{
|
|
|
|
|
frmAssembler frm = new frmAssembler(code, startAddress, blockLength);
|
|
|
|
|
_openedWindows.Add(frm);
|
|
|
|
|
frm.FormClosed += Debugger_FormClosed;
|
|
|
|
|
frm.Show();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-18 19:57:56 -04:00
|
|
|
|
public static void OpenMemoryViewer(int address, bool usePrgRom)
|
2017-12-26 15:59:58 -05:00
|
|
|
|
{
|
|
|
|
|
frmMemoryViewer frm = GetMemoryViewer();
|
|
|
|
|
if(frm == null) {
|
|
|
|
|
frm = new frmMemoryViewer();
|
|
|
|
|
frm.FormClosed += Debugger_FormClosed;
|
|
|
|
|
_openedWindows.Add(frm);
|
|
|
|
|
}
|
|
|
|
|
frm.Show();
|
2018-03-18 19:57:56 -04:00
|
|
|
|
frm.ShowAddress(address, usePrgRom);
|
2017-12-26 15:59:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-25 13:34:51 -04:00
|
|
|
|
public static frmScript OpenScriptWindow(bool forceBlank)
|
2017-09-01 14:57:02 -04:00
|
|
|
|
{
|
|
|
|
|
frmScript frm = new frmScript(forceBlank);
|
|
|
|
|
_openedWindows.Add(frm);
|
|
|
|
|
frm.FormClosed += Debugger_FormClosed;
|
|
|
|
|
frm.Show();
|
2018-03-25 13:34:51 -04:00
|
|
|
|
return frm;
|
2017-09-01 14:57:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-14 23:44:01 -04:00
|
|
|
|
public static frmDebugger GetDebugger()
|
|
|
|
|
{
|
2017-12-26 15:59:58 -05:00
|
|
|
|
return _openedWindows.ToList().Find((form) => form.GetType() == typeof(frmDebugger)) as frmDebugger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static frmMemoryViewer GetMemoryViewer()
|
|
|
|
|
{
|
|
|
|
|
return _openedWindows.ToList().Find((form) => form.GetType() == typeof(frmMemoryViewer)) as frmMemoryViewer;
|
2017-08-14 23:44:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void CloseAll()
|
|
|
|
|
{
|
|
|
|
|
List<Form> openedWindows = new List<Form>(_openedWindows);
|
|
|
|
|
foreach(Form frm in openedWindows) {
|
|
|
|
|
frm.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Form GetExistingSingleInstanceWindow(DebugWindow window)
|
|
|
|
|
{
|
|
|
|
|
//Only one of each of these windows can be opened at once, check if one is already opened
|
|
|
|
|
switch(window) {
|
2017-12-26 15:59:58 -05:00
|
|
|
|
case DebugWindow.TraceLogger: return _openedWindows.ToList().Find((form) => form.GetType() == typeof(frmTraceLogger));
|
|
|
|
|
case DebugWindow.Assembler: return _openedWindows.ToList().Find((form) => form.GetType() == typeof(frmAssembler));
|
|
|
|
|
case DebugWindow.Debugger: return _openedWindows.ToList().Find((form) => form.GetType() == typeof(frmDebugger));
|
2018-01-01 23:23:18 -05:00
|
|
|
|
case DebugWindow.ApuViewer: return _openedWindows.ToList().Find((form) => form.GetType() == typeof(frmApuViewer));
|
2017-08-14 23:44:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 15:59:58 -05:00
|
|
|
|
return null;
|
2017-08-14 23:44:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 21:24:14 -05:00
|
|
|
|
public static void CleanupDebugger()
|
2017-08-14 23:44:01 -04:00
|
|
|
|
{
|
|
|
|
|
if(_openedWindows.Count == 0) {
|
|
|
|
|
//All windows have been closed, disable debugger
|
|
|
|
|
DebugWorkspaceManager.Clear();
|
2018-04-14 21:53:52 -04:00
|
|
|
|
|
|
|
|
|
Task.Run(() => {
|
|
|
|
|
//Run this in another thread to avoid deadlocks when this is called within a notification handler
|
|
|
|
|
InteropEmu.DebugRelease();
|
|
|
|
|
});
|
2017-08-14 23:44:01 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-20 21:24:14 -05:00
|
|
|
|
|
|
|
|
|
private static void Debugger_FormClosed(object sender, FormClosedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_openedWindows.Remove((Form)sender);
|
|
|
|
|
CleanupDebugger();
|
|
|
|
|
}
|
2017-08-14 23:44:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum DebugWindow
|
|
|
|
|
{
|
|
|
|
|
PpuViewer,
|
|
|
|
|
MemoryViewer,
|
|
|
|
|
TraceLogger,
|
|
|
|
|
Assembler,
|
2017-08-30 18:31:27 -04:00
|
|
|
|
Debugger,
|
|
|
|
|
ScriptWindow,
|
2018-02-18 22:41:50 -05:00
|
|
|
|
ApuViewer,
|
|
|
|
|
EventViewer
|
2017-08-14 23:44:01 -04:00
|
|
|
|
}
|
|
|
|
|
}
|