2019-02-12 22:13:09 -05:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "Cpu.h"
|
2019-02-16 11:23:01 -05:00
|
|
|
#include "Ppu.h"
|
|
|
|
#include "Spc.h"
|
2019-07-14 21:45:12 -04:00
|
|
|
#include "NecDsp.h"
|
2019-02-17 15:37:31 -05:00
|
|
|
#include "InternalRegisters.h"
|
2019-02-17 19:54:29 -05:00
|
|
|
#include "ControlManager.h"
|
2019-02-12 22:13:09 -05:00
|
|
|
#include "MemoryManager.h"
|
2019-02-19 21:09:12 -05:00
|
|
|
#include "DmaController.h"
|
2019-02-26 22:27:09 -05:00
|
|
|
#include "BaseCartridge.h"
|
|
|
|
#include "RamHandler.h"
|
2019-02-12 22:13:09 -05:00
|
|
|
#include "Debugger.h"
|
2019-03-01 20:27:49 -05:00
|
|
|
#include "DebugTypes.h"
|
2019-02-15 21:33:13 -05:00
|
|
|
#include "NotificationManager.h"
|
2019-02-16 11:23:01 -05:00
|
|
|
#include "SoundMixer.h"
|
2019-02-13 23:02:43 -05:00
|
|
|
#include "VideoDecoder.h"
|
|
|
|
#include "VideoRenderer.h"
|
|
|
|
#include "DebugHud.h"
|
2019-02-21 17:18:56 -05:00
|
|
|
#include "FrameLimiter.h"
|
2019-02-17 14:42:35 -05:00
|
|
|
#include "MessageManager.h"
|
2019-02-24 12:54:14 -05:00
|
|
|
#include "KeyManager.h"
|
2019-03-07 20:12:32 -05:00
|
|
|
#include "EventType.h"
|
2019-03-10 11:12:50 -04:00
|
|
|
#include "EmuSettings.h"
|
2019-03-12 09:15:57 -04:00
|
|
|
#include "SaveStateManager.h"
|
2019-03-10 17:39:14 -04:00
|
|
|
#include "DebugStats.h"
|
2019-03-12 09:15:57 -04:00
|
|
|
#include "CartTypes.h"
|
2019-03-12 12:06:42 -04:00
|
|
|
#include "RewindManager.h"
|
2019-03-12 09:15:57 -04:00
|
|
|
#include "ConsoleLock.h"
|
|
|
|
#include "../Utilities/Serializer.h"
|
2019-02-12 22:13:09 -05:00
|
|
|
#include "../Utilities/Timer.h"
|
|
|
|
#include "../Utilities/VirtualFile.h"
|
2019-02-21 17:18:56 -05:00
|
|
|
#include "../Utilities/PlatformUtilities.h"
|
2019-02-24 23:53:14 -05:00
|
|
|
#include "../Utilities/FolderUtilities.h"
|
2019-02-12 22:13:09 -05:00
|
|
|
|
2019-07-16 19:08:16 -04:00
|
|
|
Console::Console()
|
|
|
|
{
|
|
|
|
_settings.reset(new EmuSettings());
|
2019-07-17 20:31:29 -04:00
|
|
|
KeyManager::SetSettings(_settings.get());
|
|
|
|
|
2019-07-16 21:02:56 -04:00
|
|
|
_paused = false;
|
|
|
|
_pauseOnNextFrame = false;
|
|
|
|
_stopFlag = false;
|
|
|
|
_lockCounter = 0;
|
2019-07-16 19:08:16 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 22:27:09 -05:00
|
|
|
Console::~Console()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:02:43 -05:00
|
|
|
void Console::Initialize()
|
|
|
|
{
|
2019-03-12 09:15:57 -04:00
|
|
|
_lockCounter = 0;
|
|
|
|
|
2019-02-15 21:33:13 -05:00
|
|
|
_notificationManager.reset(new NotificationManager());
|
2019-02-13 23:02:43 -05:00
|
|
|
_videoDecoder.reset(new VideoDecoder(shared_from_this()));
|
|
|
|
_videoRenderer.reset(new VideoRenderer(shared_from_this()));
|
2019-03-12 09:15:57 -04:00
|
|
|
_saveStateManager.reset(new SaveStateManager(shared_from_this()));
|
2019-03-10 17:39:14 -04:00
|
|
|
_soundMixer.reset(new SoundMixer(this));
|
2019-02-13 23:02:43 -05:00
|
|
|
_debugHud.reset(new DebugHud());
|
|
|
|
|
|
|
|
_videoDecoder->StartThread();
|
|
|
|
_videoRenderer->StartThread();
|
|
|
|
}
|
|
|
|
|
2019-02-16 01:22:31 -05:00
|
|
|
void Console::Release()
|
|
|
|
{
|
2019-03-14 18:07:25 -04:00
|
|
|
Stop(true);
|
2019-02-16 01:22:31 -05:00
|
|
|
|
|
|
|
_videoDecoder->StopThread();
|
|
|
|
_videoRenderer->StopThread();
|
|
|
|
|
|
|
|
_videoDecoder.reset();
|
|
|
|
_videoRenderer.reset();
|
|
|
|
_debugHud.reset();
|
|
|
|
_notificationManager.reset();
|
2019-03-12 09:15:57 -04:00
|
|
|
_saveStateManager.reset();
|
|
|
|
_soundMixer.reset();
|
|
|
|
_settings.reset();
|
2019-02-16 01:22:31 -05:00
|
|
|
}
|
|
|
|
|
2019-02-12 22:13:09 -05:00
|
|
|
void Console::Run()
|
|
|
|
{
|
|
|
|
if(!_cpu) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-13 13:43:56 -04:00
|
|
|
|
|
|
|
auto emulationLock = _emulationLock.AcquireSafe();
|
2019-02-12 22:13:09 -05:00
|
|
|
|
2019-03-10 17:39:14 -04:00
|
|
|
DebugStats stats(this);
|
|
|
|
Timer lastFrameTimer;
|
2019-02-12 22:13:09 -05:00
|
|
|
_stopFlag = false;
|
2019-02-21 17:18:56 -05:00
|
|
|
uint32_t previousFrameCount = 0;
|
|
|
|
|
2019-03-11 17:56:54 -04:00
|
|
|
double frameDelay = GetFrameDelay();
|
|
|
|
FrameLimiter frameLimiter(frameDelay);
|
2019-02-21 17:18:56 -05:00
|
|
|
|
|
|
|
PlatformUtilities::EnableHighResolutionTimer();
|
2019-02-12 22:13:09 -05:00
|
|
|
|
2019-03-08 17:08:28 -05:00
|
|
|
_videoDecoder->StartThread();
|
2019-03-07 20:12:32 -05:00
|
|
|
_emulationThreadId = std::this_thread::get_id();
|
|
|
|
|
2019-07-12 23:55:18 -04:00
|
|
|
_memoryManager->IncMasterClockStartup();
|
2019-05-04 09:25:10 -04:00
|
|
|
|
2019-02-12 22:13:09 -05:00
|
|
|
auto lock = _runLock.AcquireSafe();
|
|
|
|
while(!_stopFlag) {
|
|
|
|
_cpu->Exec();
|
2019-02-21 17:18:56 -05:00
|
|
|
|
|
|
|
if(previousFrameCount != _ppu->GetFrameCount()) {
|
2019-07-14 21:45:12 -04:00
|
|
|
if(_cart->GetCoprocessor()) {
|
|
|
|
_cart->GetCoprocessor()->Run();
|
|
|
|
}
|
2019-03-12 12:06:42 -04:00
|
|
|
_rewindManager->ProcessEndOfFrame();
|
|
|
|
|
2019-03-12 09:15:57 -04:00
|
|
|
WaitForLock();
|
|
|
|
|
2019-07-16 21:02:56 -04:00
|
|
|
if(_pauseOnNextFrame) {
|
|
|
|
_pauseOnNextFrame = false;
|
|
|
|
_paused = true;
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:13:32 -04:00
|
|
|
if(_paused && !_stopFlag && !_debugger) {
|
|
|
|
WaitForPauseEnd();
|
|
|
|
if(_stopFlag) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:56:54 -04:00
|
|
|
frameLimiter.ProcessFrame();
|
|
|
|
frameLimiter.WaitForNextFrame();
|
2019-03-13 22:56:33 -04:00
|
|
|
|
|
|
|
_controlManager->UpdateControlDevices();
|
|
|
|
|
2019-03-11 17:56:54 -04:00
|
|
|
double newFrameDelay = GetFrameDelay();
|
|
|
|
if(newFrameDelay != frameDelay) {
|
|
|
|
frameDelay = newFrameDelay;
|
|
|
|
frameLimiter.SetDelay(frameDelay);
|
2019-02-24 12:54:14 -05:00
|
|
|
}
|
2019-03-10 17:39:14 -04:00
|
|
|
|
2019-03-11 17:56:54 -04:00
|
|
|
PreferencesConfig cfg = _settings->GetPreferences();
|
|
|
|
if(cfg.ShowDebugInfo) {
|
2019-03-10 17:39:14 -04:00
|
|
|
double lastFrameTime = lastFrameTimer.GetElapsedMS();
|
|
|
|
lastFrameTimer.Reset();
|
|
|
|
stats.DisplayStats(lastFrameTime);
|
|
|
|
}
|
|
|
|
|
2019-07-07 19:55:08 -04:00
|
|
|
_controlManager->UpdateInputState();
|
|
|
|
_internalRegisters->ProcessAutoJoypadRead();
|
|
|
|
|
2019-02-21 17:18:56 -05:00
|
|
|
previousFrameCount = _ppu->GetFrameCount();
|
|
|
|
}
|
2019-02-12 22:13:09 -05:00
|
|
|
}
|
2019-02-21 17:18:56 -05:00
|
|
|
|
2019-03-07 20:12:32 -05:00
|
|
|
_emulationThreadId = thread::id();
|
|
|
|
|
2019-02-21 17:18:56 -05:00
|
|
|
PlatformUtilities::RestoreTimerResolution();
|
2019-02-12 22:13:09 -05:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:56:00 -04:00
|
|
|
void Console::RunSingleFrame()
|
|
|
|
{
|
|
|
|
//Used by Libretro
|
|
|
|
uint32_t lastFrameNumber = _ppu->GetFrameCount();
|
|
|
|
_emulationThreadId = std::this_thread::get_id();
|
|
|
|
|
2019-07-07 19:55:08 -04:00
|
|
|
_controlManager->UpdateInputState();
|
|
|
|
_internalRegisters->ProcessAutoJoypadRead();
|
|
|
|
|
2019-07-02 19:56:00 -04:00
|
|
|
while(_ppu->GetFrameCount() == lastFrameNumber) {
|
|
|
|
_cpu->Exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
_controlManager->UpdateControlDevices();
|
|
|
|
}
|
|
|
|
|
2019-03-14 18:07:25 -04:00
|
|
|
void Console::Stop(bool sendNotification)
|
2019-02-12 22:13:09 -05:00
|
|
|
{
|
|
|
|
_stopFlag = true;
|
2019-02-16 08:10:08 -05:00
|
|
|
|
|
|
|
shared_ptr<Debugger> debugger = _debugger;
|
|
|
|
if(debugger) {
|
|
|
|
debugger->Run();
|
|
|
|
}
|
|
|
|
|
2019-07-13 13:43:56 -04:00
|
|
|
_emulationLock.WaitForRelease();
|
2019-02-12 22:13:09 -05:00
|
|
|
|
2019-03-14 18:07:25 -04:00
|
|
|
if(_cart) {
|
2019-05-04 16:54:53 -04:00
|
|
|
RomInfo romInfo = _cart->GetRomInfo();
|
|
|
|
_saveStateManager->SaveRecentGame(romInfo.RomFile.GetFileName(), romInfo.RomFile, romInfo.PatchFile);
|
2019-03-14 18:07:25 -04:00
|
|
|
}
|
|
|
|
|
2019-03-30 22:41:37 -04:00
|
|
|
if(sendNotification) {
|
|
|
|
_notificationManager->SendNotification(ConsoleNotificationType::BeforeEmulationStop);
|
|
|
|
}
|
|
|
|
|
2019-02-26 22:27:09 -05:00
|
|
|
//Make sure we release both pointers to destroy the debugger before everything else
|
|
|
|
_debugger.reset();
|
|
|
|
debugger.reset();
|
|
|
|
|
2019-03-08 17:08:28 -05:00
|
|
|
_videoDecoder->StopThread();
|
2019-03-12 12:06:42 -04:00
|
|
|
_rewindManager.reset();
|
2019-03-08 17:08:28 -05:00
|
|
|
|
2019-02-12 22:13:09 -05:00
|
|
|
_cpu.reset();
|
2019-02-13 13:32:21 -05:00
|
|
|
_ppu.reset();
|
2019-02-16 11:23:01 -05:00
|
|
|
_spc.reset();
|
2019-02-16 01:16:57 -05:00
|
|
|
_cart.reset();
|
2019-02-17 15:37:31 -05:00
|
|
|
_internalRegisters.reset();
|
2019-02-17 19:54:29 -05:00
|
|
|
_controlManager.reset();
|
2019-02-12 22:13:09 -05:00
|
|
|
_memoryManager.reset();
|
2019-02-19 21:09:12 -05:00
|
|
|
_dmaController.reset();
|
2019-02-24 12:54:14 -05:00
|
|
|
|
|
|
|
_soundMixer->StopAudio();
|
2019-03-14 18:07:25 -04:00
|
|
|
|
|
|
|
if(sendNotification) {
|
|
|
|
_notificationManager->SendNotification(ConsoleNotificationType::EmulationStopped);
|
|
|
|
}
|
2019-02-12 22:13:09 -05:00
|
|
|
}
|
|
|
|
|
2019-03-16 12:20:18 -04:00
|
|
|
void Console::Reset()
|
2019-02-12 22:13:09 -05:00
|
|
|
{
|
2019-03-16 12:20:18 -04:00
|
|
|
shared_ptr<Debugger> debugger = _debugger;
|
|
|
|
if(debugger) {
|
|
|
|
debugger->Run();
|
|
|
|
}
|
|
|
|
|
2019-03-30 21:47:30 -04:00
|
|
|
Lock();
|
|
|
|
|
2019-03-16 12:20:18 -04:00
|
|
|
_dmaController->Reset();
|
|
|
|
_internalRegisters->Reset();
|
|
|
|
_memoryManager->Reset();
|
|
|
|
_spc->Reset();
|
|
|
|
_ppu->Reset();
|
2019-04-20 09:30:51 -04:00
|
|
|
_cpu->Reset();
|
2019-03-16 12:20:18 -04:00
|
|
|
//_cart->Reset();
|
|
|
|
//_controlManager->Reset();
|
|
|
|
|
2019-04-30 21:05:53 -04:00
|
|
|
_notificationManager->SendNotification(ConsoleNotificationType::GameReset);
|
2019-05-04 09:25:10 -04:00
|
|
|
ProcessEvent(EventType::Reset);
|
2019-04-30 21:05:53 -04:00
|
|
|
|
2019-07-12 23:55:18 -04:00
|
|
|
_memoryManager->IncMasterClockStartup();
|
2019-04-20 09:30:51 -04:00
|
|
|
|
2019-03-16 12:20:18 -04:00
|
|
|
Unlock();
|
2019-07-16 22:38:39 -04:00
|
|
|
|
|
|
|
if(debugger) {
|
|
|
|
debugger->SuspendDebugger(true);
|
|
|
|
}
|
2019-03-16 12:20:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::PowerCycle()
|
|
|
|
{
|
|
|
|
shared_ptr<BaseCartridge> cart = _cart;
|
|
|
|
if(cart) {
|
2019-03-30 21:47:30 -04:00
|
|
|
shared_ptr<Debugger> debugger = _debugger;
|
|
|
|
if(debugger) {
|
|
|
|
debugger->Run();
|
|
|
|
}
|
|
|
|
|
2019-03-16 12:20:18 -04:00
|
|
|
RomInfo info = cart->GetRomInfo();
|
|
|
|
Lock();
|
|
|
|
LoadRom(info.RomFile, info.PatchFile, false);
|
2019-07-12 23:55:18 -04:00
|
|
|
|
|
|
|
_memoryManager->IncMasterClockStartup();
|
2019-03-16 12:20:18 -04:00
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Console::LoadRom(VirtualFile romFile, VirtualFile patchFile, bool stopRom)
|
|
|
|
{
|
|
|
|
if(_cart) {
|
|
|
|
//Make sure the battery is saved to disk before we load another game (or reload the same game)
|
|
|
|
_cart->SaveBattery();
|
|
|
|
}
|
|
|
|
|
2019-07-14 21:45:12 -04:00
|
|
|
shared_ptr<BaseCartridge> cart = BaseCartridge::CreateCartridge(this, romFile, patchFile);
|
2019-02-12 22:13:09 -05:00
|
|
|
if(cart) {
|
2019-03-16 12:20:18 -04:00
|
|
|
if(stopRom) {
|
|
|
|
Stop(false);
|
|
|
|
}
|
2019-03-22 21:12:58 -04:00
|
|
|
|
|
|
|
_cart = cart;
|
|
|
|
UpdateRegion();
|
2019-02-24 12:54:14 -05:00
|
|
|
|
2019-04-10 20:45:59 -04:00
|
|
|
_memoryManager.reset(new MemoryManager());
|
2019-07-12 23:34:19 -04:00
|
|
|
_ppu.reset(new Ppu(this));
|
|
|
|
_controlManager.reset(new ControlManager(this));
|
2019-02-24 19:57:34 -05:00
|
|
|
_dmaController.reset(new DmaController(_memoryManager.get()));
|
2019-07-12 23:34:19 -04:00
|
|
|
_spc.reset(new Spc(this));
|
2019-02-19 21:09:12 -05:00
|
|
|
|
2019-07-06 09:29:35 -04:00
|
|
|
_cpu.reset(new Cpu(this));
|
2019-07-12 23:34:19 -04:00
|
|
|
_internalRegisters.reset(new InternalRegisters(this));
|
|
|
|
_memoryManager->Initialize(this);
|
2019-02-15 00:08:50 -05:00
|
|
|
|
2019-03-16 12:20:18 -04:00
|
|
|
if(_debugger) {
|
2019-02-16 00:47:53 -05:00
|
|
|
//Reset debugger if it was running before
|
2019-03-16 12:20:18 -04:00
|
|
|
auto lock = _debuggerLock.AcquireSafe();
|
2019-04-29 20:40:52 -04:00
|
|
|
_debugger->Release();
|
2019-03-16 12:20:18 -04:00
|
|
|
_debugger.reset();
|
|
|
|
GetDebugger();
|
|
|
|
}
|
2019-03-22 21:12:58 -04:00
|
|
|
|
2019-04-10 16:04:55 -04:00
|
|
|
_ppu->PowerOn();
|
2019-03-22 21:12:58 -04:00
|
|
|
_cpu->PowerOn();
|
2019-04-30 21:05:53 -04:00
|
|
|
|
|
|
|
_notificationManager->SendNotification(ConsoleNotificationType::GameLoaded);
|
|
|
|
|
2019-03-22 21:12:58 -04:00
|
|
|
_rewindManager.reset(new RewindManager(shared_from_this()));
|
|
|
|
_notificationManager->RegisterNotificationListener(_rewindManager);
|
|
|
|
|
|
|
|
_controlManager->UpdateControlDevices();
|
|
|
|
|
|
|
|
UpdateRegion();
|
|
|
|
|
2019-03-12 13:13:32 -04:00
|
|
|
_paused = false;
|
2019-03-14 18:07:25 -04:00
|
|
|
|
2019-03-31 15:59:13 -04:00
|
|
|
string modelName = _region == ConsoleRegion::Pal ? "PAL" : "NTSC";
|
|
|
|
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
|
|
|
|
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(GetRomInfo().RomFile.GetFileName(), false));
|
|
|
|
|
2019-03-14 18:07:25 -04:00
|
|
|
return true;
|
2019-03-12 09:15:57 -04:00
|
|
|
}
|
2019-03-14 18:07:25 -04:00
|
|
|
|
|
|
|
return false;
|
2019-03-12 09:15:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
RomInfo Console::GetRomInfo()
|
|
|
|
{
|
|
|
|
shared_ptr<BaseCartridge> cart = _cart;
|
|
|
|
if(cart) {
|
|
|
|
return cart->GetRomInfo();
|
|
|
|
} else {
|
|
|
|
return {};
|
2019-02-12 22:13:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:25:35 -04:00
|
|
|
uint32_t Console::GetMasterClockRate()
|
|
|
|
{
|
|
|
|
return _masterClockRate;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleRegion Console::GetRegion()
|
|
|
|
{
|
|
|
|
return _region;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::UpdateRegion()
|
|
|
|
{
|
|
|
|
switch(_settings->GetEmulationConfig().Region) {
|
|
|
|
case ConsoleRegion::Auto:
|
|
|
|
{
|
|
|
|
uint8_t destCode = _cart->GetRomInfo().Header.DestinationCode;
|
|
|
|
if((destCode >= 0x02 && destCode <= 0x0C) || destCode == 0x11) {
|
|
|
|
_region = ConsoleRegion::Pal;
|
|
|
|
} else {
|
|
|
|
_region = ConsoleRegion::Ntsc;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
case ConsoleRegion::Ntsc: _region = ConsoleRegion::Ntsc; break;
|
|
|
|
case ConsoleRegion::Pal: _region = ConsoleRegion::Pal; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_masterClockRate = _region == ConsoleRegion::Pal ? 21281370 : 21477270;
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:56:54 -04:00
|
|
|
double Console::GetFrameDelay()
|
|
|
|
{
|
|
|
|
uint32_t emulationSpeed = _settings->GetEmulationSpeed();
|
|
|
|
double frameDelay;
|
|
|
|
if(emulationSpeed == 0) {
|
|
|
|
frameDelay = 0;
|
|
|
|
} else {
|
2019-03-14 15:25:35 -04:00
|
|
|
UpdateRegion();
|
|
|
|
switch(_region) {
|
2019-03-31 14:50:12 -04:00
|
|
|
default:
|
2019-03-14 15:29:13 -04:00
|
|
|
case ConsoleRegion::Ntsc: frameDelay = _settings->GetVideoConfig().IntegerFpsMode ? 16.6666666666666666667 : 16.63926405550947; break;
|
|
|
|
case ConsoleRegion::Pal: frameDelay = _settings->GetVideoConfig().IntegerFpsMode ? 20 : 19.99720882631146; break;
|
2019-03-14 15:25:35 -04:00
|
|
|
}
|
|
|
|
frameDelay /= (emulationSpeed / 100.0);
|
2019-03-11 17:56:54 -04:00
|
|
|
}
|
|
|
|
return frameDelay;
|
|
|
|
}
|
|
|
|
|
2019-07-16 21:02:56 -04:00
|
|
|
void Console::PauseOnNextFrame()
|
|
|
|
{
|
|
|
|
shared_ptr<Debugger> debugger = _debugger;
|
|
|
|
if(debugger) {
|
|
|
|
debugger->Step(240, StepType::SpecificScanline);
|
|
|
|
} else {
|
|
|
|
_pauseOnNextFrame = true;
|
|
|
|
_paused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:13:32 -04:00
|
|
|
void Console::Pause()
|
|
|
|
{
|
|
|
|
shared_ptr<Debugger> debugger = _debugger;
|
|
|
|
if(debugger) {
|
|
|
|
debugger->Step(1);
|
|
|
|
} else {
|
|
|
|
_paused = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::Resume()
|
|
|
|
{
|
|
|
|
shared_ptr<Debugger> debugger = _debugger;
|
|
|
|
if(debugger) {
|
|
|
|
debugger->Run();
|
|
|
|
} else {
|
|
|
|
_paused = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Console::IsPaused()
|
|
|
|
{
|
|
|
|
shared_ptr<Debugger> debugger = _debugger;
|
|
|
|
if(debugger) {
|
|
|
|
return debugger->IsExecutionStopped();
|
|
|
|
} else {
|
|
|
|
return _paused;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::WaitForPauseEnd()
|
|
|
|
{
|
|
|
|
_notificationManager->SendNotification(ConsoleNotificationType::GamePaused);
|
|
|
|
|
|
|
|
//Prevent audio from looping endlessly while game is paused
|
|
|
|
_soundMixer->StopAudio();
|
|
|
|
_runLock.Release();
|
|
|
|
|
|
|
|
PlatformUtilities::EnableScreensaver();
|
|
|
|
PlatformUtilities::RestoreTimerResolution();
|
|
|
|
while(_paused && !_stopFlag && !_debugger) {
|
|
|
|
//Sleep until emulation is resumed
|
|
|
|
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(30));
|
|
|
|
}
|
|
|
|
|
|
|
|
PlatformUtilities::DisableScreensaver();
|
|
|
|
_runLock.Acquire();
|
|
|
|
_notificationManager->SendNotification(ConsoleNotificationType::GameResumed);
|
|
|
|
}
|
|
|
|
|
2019-03-12 09:15:57 -04:00
|
|
|
ConsoleLock Console::AcquireLock()
|
|
|
|
{
|
|
|
|
return ConsoleLock(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::Lock()
|
|
|
|
{
|
|
|
|
_lockCounter++;
|
|
|
|
_runLock.Acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::Unlock()
|
|
|
|
{
|
|
|
|
_runLock.Release();
|
|
|
|
_lockCounter--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::WaitForLock()
|
|
|
|
{
|
|
|
|
if(_lockCounter > 0) {
|
|
|
|
//Need to temporarely pause the emu (to save/load a state, etc.)
|
|
|
|
_runLock.Release();
|
|
|
|
|
|
|
|
//Spin wait until we are allowed to start again
|
|
|
|
while(_lockCounter > 0) {}
|
|
|
|
|
|
|
|
_runLock.Acquire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::Serialize(ostream &out)
|
|
|
|
{
|
|
|
|
Serializer serializer(SaveStateManager::FileFormatVersion);
|
|
|
|
serializer.Stream(_cpu.get());
|
|
|
|
serializer.Stream(_memoryManager.get());
|
|
|
|
serializer.Stream(_ppu.get());
|
|
|
|
serializer.Stream(_dmaController.get());
|
|
|
|
serializer.Stream(_internalRegisters.get());
|
|
|
|
serializer.Stream(_cart.get());
|
|
|
|
serializer.Stream(_controlManager.get());
|
|
|
|
serializer.Stream(_spc.get());
|
|
|
|
serializer.Save(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::Deserialize(istream &in, uint32_t fileFormatVersion)
|
|
|
|
{
|
|
|
|
Serializer serializer(in, fileFormatVersion);
|
|
|
|
serializer.Stream(_cpu.get());
|
|
|
|
serializer.Stream(_memoryManager.get());
|
|
|
|
serializer.Stream(_ppu.get());
|
|
|
|
serializer.Stream(_dmaController.get());
|
|
|
|
serializer.Stream(_internalRegisters.get());
|
|
|
|
serializer.Stream(_cart.get());
|
|
|
|
serializer.Stream(_controlManager.get());
|
|
|
|
serializer.Stream(_spc.get());
|
2019-03-12 12:06:42 -04:00
|
|
|
|
|
|
|
_notificationManager->SendNotification(ConsoleNotificationType::StateLoaded);
|
2019-03-12 09:15:57 -04:00
|
|
|
}
|
|
|
|
|
2019-02-16 11:23:01 -05:00
|
|
|
shared_ptr<SoundMixer> Console::GetSoundMixer()
|
|
|
|
{
|
|
|
|
return _soundMixer;
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:02:43 -05:00
|
|
|
shared_ptr<VideoRenderer> Console::GetVideoRenderer()
|
|
|
|
{
|
|
|
|
return _videoRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<VideoDecoder> Console::GetVideoDecoder()
|
|
|
|
{
|
|
|
|
return _videoDecoder;
|
|
|
|
}
|
|
|
|
|
2019-02-15 21:33:13 -05:00
|
|
|
shared_ptr<NotificationManager> Console::GetNotificationManager()
|
|
|
|
{
|
|
|
|
return _notificationManager;
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:12:50 -04:00
|
|
|
shared_ptr<EmuSettings> Console::GetSettings()
|
|
|
|
{
|
|
|
|
return _settings;
|
|
|
|
}
|
|
|
|
|
2019-03-12 09:15:57 -04:00
|
|
|
shared_ptr<SaveStateManager> Console::GetSaveStateManager()
|
|
|
|
{
|
|
|
|
return _saveStateManager;
|
|
|
|
}
|
|
|
|
|
2019-03-12 12:06:42 -04:00
|
|
|
shared_ptr<RewindManager> Console::GetRewindManager()
|
|
|
|
{
|
|
|
|
return _rewindManager;
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:02:43 -05:00
|
|
|
shared_ptr<DebugHud> Console::GetDebugHud()
|
|
|
|
{
|
|
|
|
return _debugHud;
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:44:39 -05:00
|
|
|
shared_ptr<Cpu> Console::GetCpu()
|
|
|
|
{
|
|
|
|
return _cpu;
|
|
|
|
}
|
|
|
|
|
2019-02-13 13:32:21 -05:00
|
|
|
shared_ptr<Ppu> Console::GetPpu()
|
|
|
|
{
|
|
|
|
return _ppu;
|
|
|
|
}
|
|
|
|
|
2019-02-16 11:23:01 -05:00
|
|
|
shared_ptr<Spc> Console::GetSpc()
|
|
|
|
{
|
|
|
|
return _spc;
|
|
|
|
}
|
|
|
|
|
2019-02-15 21:33:13 -05:00
|
|
|
shared_ptr<BaseCartridge> Console::GetCartridge()
|
|
|
|
{
|
|
|
|
return _cart;
|
|
|
|
}
|
|
|
|
|
2019-02-13 23:02:43 -05:00
|
|
|
shared_ptr<MemoryManager> Console::GetMemoryManager()
|
|
|
|
{
|
|
|
|
return _memoryManager;
|
|
|
|
}
|
|
|
|
|
2019-02-17 15:37:31 -05:00
|
|
|
shared_ptr<InternalRegisters> Console::GetInternalRegisters()
|
|
|
|
{
|
|
|
|
return _internalRegisters;
|
|
|
|
}
|
|
|
|
|
2019-02-17 19:54:29 -05:00
|
|
|
shared_ptr<ControlManager> Console::GetControlManager()
|
|
|
|
{
|
|
|
|
return _controlManager;
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:09:12 -05:00
|
|
|
shared_ptr<DmaController> Console::GetDmaController()
|
|
|
|
{
|
|
|
|
return _dmaController;
|
|
|
|
}
|
|
|
|
|
2019-02-16 00:47:53 -05:00
|
|
|
shared_ptr<Debugger> Console::GetDebugger(bool autoStart)
|
2019-02-12 22:13:09 -05:00
|
|
|
{
|
2019-02-16 00:47:53 -05:00
|
|
|
shared_ptr<Debugger> 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;
|
2019-02-12 22:13:09 -05:00
|
|
|
}
|
|
|
|
|
2019-03-16 12:20:18 -04:00
|
|
|
void Console::StopDebugger()
|
|
|
|
{
|
|
|
|
_debugger.reset();
|
|
|
|
}
|
|
|
|
|
2019-04-06 17:38:14 -04:00
|
|
|
bool Console::IsDebugging()
|
|
|
|
{
|
|
|
|
return _debugger != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-07 20:12:32 -05:00
|
|
|
thread::id Console::GetEmulationThreadId()
|
|
|
|
{
|
|
|
|
return _emulationThreadId;
|
|
|
|
}
|
|
|
|
|
2019-02-17 15:02:33 -05:00
|
|
|
bool Console::IsRunning()
|
|
|
|
{
|
|
|
|
return _cpu != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-12 22:13:09 -05:00
|
|
|
void Console::ProcessCpuRead(uint32_t addr, uint8_t value, MemoryOperationType type)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessCpuRead(addr, value, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::ProcessCpuWrite(uint32_t addr, uint8_t value, MemoryOperationType type)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessCpuWrite(addr, value, type);
|
|
|
|
}
|
|
|
|
}
|
2019-03-01 20:27:49 -05:00
|
|
|
|
|
|
|
void Console::ProcessPpuRead(uint32_t addr, uint8_t value, SnesMemoryType memoryType)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessPpuRead(addr, value, memoryType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::ProcessPpuWrite(uint32_t addr, uint8_t value, SnesMemoryType memoryType)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessPpuWrite(addr, value, memoryType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:47:43 -04:00
|
|
|
void Console::ProcessSpcRead(uint32_t addr, uint8_t value, MemoryOperationType type)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessSpcRead(addr, value, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::ProcessSpcWrite(uint32_t addr, uint8_t value, MemoryOperationType type)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessSpcWrite(addr, value, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 20:27:49 -05:00
|
|
|
void Console::ProcessWorkRamRead(uint32_t addr, uint8_t value)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessWorkRamRead(addr, value);
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 16:34:23 -05:00
|
|
|
|
2019-03-01 20:27:49 -05:00
|
|
|
void Console::ProcessWorkRamWrite(uint32_t addr, uint8_t value)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessWorkRamWrite(addr, value);
|
|
|
|
}
|
2019-03-03 16:34:23 -05:00
|
|
|
}
|
|
|
|
|
2019-07-14 21:45:12 -04:00
|
|
|
void Console::ProcessNecDspExec(uint32_t addr, uint32_t value)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessNecDspExec(addr, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 16:34:23 -05:00
|
|
|
void Console::ProcessPpuCycle()
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessPpuCycle();
|
2019-04-20 18:52:07 -04:00
|
|
|
_spc->Run();
|
2019-07-14 21:45:12 -04:00
|
|
|
if(_cart->GetCoprocessor()) {
|
|
|
|
_cart->GetCoprocessor()->Run();
|
|
|
|
}
|
2019-03-03 16:34:23 -05:00
|
|
|
}
|
2019-03-07 20:12:32 -05:00
|
|
|
}
|
|
|
|
|
2019-03-24 16:42:52 -04:00
|
|
|
void Console::ProcessInterrupt(uint32_t originalPc, uint32_t currentPc, bool forNmi)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessInterrupt(originalPc, currentPc, forNmi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 20:12:32 -05:00
|
|
|
void Console::ProcessEvent(EventType type)
|
|
|
|
{
|
|
|
|
if(_debugger) {
|
|
|
|
_debugger->ProcessEvent(type);
|
|
|
|
}
|
2019-03-01 20:27:49 -05:00
|
|
|
}
|