diff --git a/Core/Console.cpp b/Core/Console.cpp index a2172d5..6a60956 100644 --- a/Core/Console.cpp +++ b/Core/Console.cpp @@ -49,7 +49,6 @@ void Console::Stop() _cpu.reset(); _ppu.reset(); _memoryManager.reset(); - _debugger.reset(); } void Console::LoadRom(VirtualFile romFile, VirtualFile patchFile) @@ -64,7 +63,13 @@ void Console::LoadRom(VirtualFile romFile, VirtualFile patchFile) _memoryManager->Initialize(cart, shared_from_this()); _cpu.reset(new Cpu(_memoryManager)); - _debugger.reset(new Debugger(shared_from_this())); + + if(_debugger) { + //Reset debugger if it was running before + auto lock = _debuggerLock.AcquireSafe(); + _debugger.reset(); + GetDebugger(); + } } } @@ -108,9 +113,19 @@ shared_ptr Console::GetMemoryManager() return _memoryManager; } -shared_ptr Console::GetDebugger(bool allowStart) +shared_ptr Console::GetDebugger(bool autoStart) { - return _debugger; + shared_ptr debugger = _debugger; + if(!debugger && autoStart) { + //Lock to make sure we don't try to start debuggers in 2 separate threads at once + auto lock = _debuggerLock.AcquireSafe(); + debugger = _debugger; + if(!debugger) { + debugger.reset(new Debugger(shared_from_this())); + _debugger = debugger; + } + } + return debugger; } void Console::ProcessCpuRead(uint32_t addr, uint8_t value, MemoryOperationType type) diff --git a/Core/Console.h b/Core/Console.h index 85765e1..a30b764 100644 --- a/Core/Console.h +++ b/Core/Console.h @@ -29,6 +29,7 @@ private: shared_ptr _debugHud; SimpleLock _runLock; + SimpleLock _debuggerLock; atomic _stopFlag; public: @@ -49,7 +50,7 @@ public: shared_ptr GetPpu(); shared_ptr GetCartridge(); shared_ptr GetMemoryManager(); - shared_ptr GetDebugger(bool allowStart = true); + shared_ptr GetDebugger(bool autoStart = true); void ProcessCpuRead(uint32_t addr, uint8_t value, MemoryOperationType type); void ProcessCpuWrite(uint32_t addr, uint8_t value, MemoryOperationType type); diff --git a/InteropDLL/DebugApiWrapper.cpp b/InteropDLL/DebugApiWrapper.cpp index 1246a36..0ce9a03 100644 --- a/InteropDLL/DebugApiWrapper.cpp +++ b/InteropDLL/DebugApiWrapper.cpp @@ -6,15 +6,10 @@ #include "../Core/DebugTypes.h" extern shared_ptr _console; -shared_ptr _debugger; shared_ptr GetDebugger() { - if(!_debugger) { - _debugger = _console->GetDebugger(); - } - - return _debugger; + return _console->GetDebugger(); } extern "C" @@ -27,7 +22,7 @@ extern "C" DllExport void __stdcall ReleaseDebugger() { - _debugger.reset(); + //_debugger.reset(); //_console->StopDebugger(); }