2014-06-14 11:27:55 -04:00
|
|
|
#include "stdafx.h"
|
2015-07-01 23:17:14 -04:00
|
|
|
#include <thread>
|
2014-06-14 11:27:55 -04:00
|
|
|
#include "Console.h"
|
2015-07-01 23:17:14 -04:00
|
|
|
#include "BaseMapper.h"
|
2016-04-30 20:08:53 -04:00
|
|
|
#include "ControlManager.h"
|
|
|
|
#include "VsControlManager.h"
|
2014-06-24 02:47:32 -04:00
|
|
|
#include "MapperFactory.h"
|
2015-07-01 23:17:14 -04:00
|
|
|
#include "Debugger.h"
|
2015-07-17 20:58:57 -04:00
|
|
|
#include "MessageManager.h"
|
2016-01-28 20:47:16 -05:00
|
|
|
#include "RomLoader.h"
|
2015-07-17 20:58:57 -04:00
|
|
|
#include "EmulationSettings.h"
|
2014-06-23 19:02:09 -04:00
|
|
|
#include "../Utilities/Timer.h"
|
2014-07-09 18:29:07 -04:00
|
|
|
#include "../Utilities/FolderUtilities.h"
|
2016-08-29 21:07:52 -04:00
|
|
|
#include "../Utilities/PlatformUtilities.h"
|
2015-08-14 21:50:14 -04:00
|
|
|
#include "HdPpu.h"
|
2016-06-25 20:46:54 -04:00
|
|
|
#include "NsfPpu.h"
|
2016-01-14 19:33:16 -05:00
|
|
|
#include "SoundMixer.h"
|
2016-06-25 20:46:54 -04:00
|
|
|
#include "NsfMapper.h"
|
2016-09-02 19:36:37 -04:00
|
|
|
#include "ShortcutKeyHandler.h"
|
2017-04-18 22:39:45 -04:00
|
|
|
#include "MovieManager.h"
|
2017-04-28 19:54:58 -04:00
|
|
|
#include "RewindManager.h"
|
2014-06-14 11:27:55 -04:00
|
|
|
|
2015-07-05 22:23:44 -04:00
|
|
|
shared_ptr<Console> Console::Instance(new Console());
|
2014-06-21 19:03:13 -04:00
|
|
|
|
2015-07-05 22:23:44 -04:00
|
|
|
Console::Console()
|
2014-06-14 11:27:55 -04:00
|
|
|
{
|
2016-12-11 10:56:23 -05:00
|
|
|
_resetRequested = false;
|
|
|
|
_lagCounter = 0;
|
2014-07-09 18:29:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Console::~Console()
|
|
|
|
{
|
2017-04-18 22:39:45 -04:00
|
|
|
MovieManager::Stop();
|
2016-06-05 14:36:20 -04:00
|
|
|
SoundMixer::StopRecording();
|
2014-07-09 18:29:07 -04:00
|
|
|
}
|
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
shared_ptr<Console> Console::GetInstance()
|
2014-07-09 18:29:07 -04:00
|
|
|
{
|
|
|
|
return Console::Instance;
|
|
|
|
}
|
|
|
|
|
2015-07-20 23:20:41 -04:00
|
|
|
void Console::Release()
|
|
|
|
{
|
|
|
|
Console::Instance.reset(new Console());
|
|
|
|
}
|
|
|
|
|
2017-04-23 18:47:28 -04:00
|
|
|
bool Console::Initialize(string romFilename, stringstream *filestream, string patchFilename, int32_t archiveFileIndex)
|
2014-07-09 18:29:07 -04:00
|
|
|
{
|
2016-06-19 16:55:26 -04:00
|
|
|
SoundMixer::StopAudio();
|
|
|
|
|
2016-07-26 19:19:28 -04:00
|
|
|
if(_mapper) {
|
|
|
|
//Ensure we save any battery file before loading a new game
|
|
|
|
_mapper->SaveBattery();
|
|
|
|
}
|
|
|
|
|
2015-07-05 19:05:33 -04:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GameStopped);
|
2017-04-09 22:44:13 -04:00
|
|
|
shared_ptr<BaseMapper> mapper = MapperFactory::InitializeFromFile(romFilename, filestream, patchFilename, archiveFileIndex);
|
2015-12-26 17:11:00 -05:00
|
|
|
|
2014-07-10 19:25:35 -04:00
|
|
|
if(mapper) {
|
2015-12-27 18:41:38 -05:00
|
|
|
_romFilepath = romFilename;
|
2015-08-30 21:04:21 -04:00
|
|
|
|
2016-08-31 20:54:38 -04:00
|
|
|
_autoSaveManager.reset(new AutoSaveManager());
|
2015-08-30 21:04:21 -04:00
|
|
|
VideoDecoder::GetInstance()->StopThread();
|
2014-06-22 08:38:42 -04:00
|
|
|
|
2014-07-10 19:25:35 -04:00
|
|
|
_mapper = mapper;
|
|
|
|
_memoryManager.reset(new MemoryManager(_mapper));
|
|
|
|
_cpu.reset(new CPU(_memoryManager.get()));
|
2015-08-14 21:50:14 -04:00
|
|
|
if(HdNesPack::HasHdPack(_romFilepath)) {
|
2017-03-31 22:14:16 -04:00
|
|
|
_ppu.reset(new HdPpu(_mapper.get()));
|
2016-06-25 20:46:54 -04:00
|
|
|
} else if(NsfMapper::GetInstance()) {
|
|
|
|
//Disable most of the PPU for NSFs
|
2017-03-31 22:14:16 -04:00
|
|
|
_ppu.reset(new NsfPpu(_mapper.get()));
|
2015-08-14 21:50:14 -04:00
|
|
|
} else {
|
2017-03-31 22:14:16 -04:00
|
|
|
_ppu.reset(new PPU(_mapper.get()));
|
2015-08-14 21:50:14 -04:00
|
|
|
}
|
2014-07-10 19:25:35 -04:00
|
|
|
_apu.reset(new APU(_memoryManager.get()));
|
2014-06-22 22:15:35 -04:00
|
|
|
|
2016-06-15 21:59:34 -04:00
|
|
|
_controlManager.reset(_mapper->GetGameSystem() == GameSystem::VsUniSystem ? new VsControlManager() : new ControlManager());
|
2016-06-22 19:23:08 -04:00
|
|
|
_controlManager->UpdateControlDevices();
|
2014-06-21 15:43:41 -04:00
|
|
|
|
2014-07-10 19:25:35 -04:00
|
|
|
_memoryManager->RegisterIODevice(_ppu.get());
|
|
|
|
_memoryManager->RegisterIODevice(_apu.get());
|
|
|
|
_memoryManager->RegisterIODevice(_controlManager.get());
|
2016-06-25 20:46:54 -04:00
|
|
|
_memoryManager->RegisterIODevice(_mapper.get());
|
2014-06-19 19:58:15 -04:00
|
|
|
|
2016-07-10 19:15:00 -04:00
|
|
|
_model = NesModel::Auto;
|
2016-02-10 21:41:51 -05:00
|
|
|
UpdateNesModel(false);
|
2016-01-23 00:52:06 -05:00
|
|
|
|
2015-07-05 22:23:44 -04:00
|
|
|
_initialized = true;
|
2016-01-28 22:34:23 -05:00
|
|
|
|
2016-06-04 08:55:52 -04:00
|
|
|
if(_debugger) {
|
2016-07-31 14:31:44 -04:00
|
|
|
auto lock = _debuggerLock.AcquireSafe();
|
2016-06-04 08:55:52 -04:00
|
|
|
StopDebugger();
|
|
|
|
GetDebugger();
|
|
|
|
}
|
|
|
|
|
2016-01-28 22:34:23 -05:00
|
|
|
ResetComponents(false);
|
2016-01-23 00:52:06 -05:00
|
|
|
|
2017-04-28 19:54:58 -04:00
|
|
|
_rewindManager.reset(new RewindManager());
|
|
|
|
|
2015-08-30 21:04:21 -04:00
|
|
|
VideoDecoder::GetInstance()->StartThread();
|
2015-07-05 22:23:44 -04:00
|
|
|
|
2016-12-09 12:49:17 -05:00
|
|
|
FolderUtilities::AddKnownGameFolder(FolderUtilities::GetFolderName(romFilename));
|
2016-07-10 19:15:00 -04:00
|
|
|
|
|
|
|
string modelName = _model == NesModel::PAL ? "PAL" : (_model == NesModel::Dendy ? "Dendy" : "NTSC");
|
|
|
|
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
|
|
|
|
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(_mapper->GetRomName(), false));
|
2016-06-12 18:11:31 -04:00
|
|
|
if(EmulationSettings::GetOverclockRate() != 100) {
|
|
|
|
MessageManager::DisplayMessage("ClockRate", std::to_string(EmulationSettings::GetOverclockRate()) + "%");
|
|
|
|
}
|
2017-04-23 18:47:28 -04:00
|
|
|
return true;
|
2014-07-10 19:25:35 -04:00
|
|
|
} else {
|
2016-02-19 13:05:04 -05:00
|
|
|
MessageManager::DisplayMessage("Error", "CouldNotLoadFile", FolderUtilities::GetFilename(romFilename, true));
|
2017-04-23 18:47:28 -04:00
|
|
|
return false;
|
2014-07-10 19:25:35 -04:00
|
|
|
}
|
2015-07-05 22:23:44 -04:00
|
|
|
}
|
2014-07-01 12:44:01 -04:00
|
|
|
|
2017-04-23 18:47:28 -04:00
|
|
|
bool Console::LoadROM(string filepath, stringstream *filestream, int32_t archiveFileIndex, string patchFilepath)
|
2015-12-27 18:41:38 -05:00
|
|
|
{
|
|
|
|
Console::Pause();
|
2017-04-23 18:47:28 -04:00
|
|
|
bool result = Instance->Initialize(filepath, filestream, patchFilepath, archiveFileIndex);
|
2015-07-05 22:23:44 -04:00
|
|
|
Console::Resume();
|
2017-04-23 18:47:28 -04:00
|
|
|
return result;
|
2014-06-14 11:27:55 -04:00
|
|
|
}
|
|
|
|
|
2017-04-22 13:19:21 -04:00
|
|
|
bool Console::LoadROM(string romName, uint32_t crc32Hash)
|
|
|
|
{
|
2017-04-24 18:28:50 -04:00
|
|
|
HashInfo hashInfo;
|
|
|
|
hashInfo.Crc32Hash = crc32Hash;
|
2017-04-22 13:19:21 -04:00
|
|
|
return Console::LoadROM(romName, hashInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Console::LoadROM(string romName, string sha1Hash)
|
|
|
|
{
|
2017-04-24 18:28:50 -04:00
|
|
|
HashInfo hashInfo;
|
|
|
|
hashInfo.Sha1Hash = sha1Hash;
|
2017-04-22 13:19:21 -04:00
|
|
|
return Console::LoadROM(romName, hashInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Console::LoadROM(string romName, HashInfo hashInfo)
|
2014-06-14 11:27:55 -04:00
|
|
|
{
|
2015-07-11 08:27:22 -04:00
|
|
|
string currentRomFilepath = Console::GetROMPath();
|
|
|
|
string currentFolder = FolderUtilities::GetFolderName(currentRomFilepath);
|
2015-07-05 22:23:44 -04:00
|
|
|
if(!currentRomFilepath.empty()) {
|
2017-04-22 13:19:21 -04:00
|
|
|
HashInfo gameHashInfo = Instance->_mapper->GetHashInfo();
|
2017-04-24 18:28:50 -04:00
|
|
|
if(gameHashInfo.Crc32Hash == hashInfo.Crc32Hash || gameHashInfo.Sha1Hash.compare(hashInfo.Sha1Hash) == 0 || gameHashInfo.PrgChrMd5Hash.compare(hashInfo.PrgChrMd5Hash) == 0) {
|
2015-07-05 22:23:44 -04:00
|
|
|
//Current game matches, no need to do anything
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-08 23:34:48 -05:00
|
|
|
}
|
2015-07-05 22:23:44 -04:00
|
|
|
|
2016-06-17 20:53:05 -04:00
|
|
|
int32_t archiveFileIndex = -1;
|
2016-12-09 12:49:17 -05:00
|
|
|
for(string folder : FolderUtilities::GetKnownGameFolders()) {
|
2017-04-22 13:19:21 -04:00
|
|
|
string match = RomLoader::FindMatchingRomInFolder(folder, romName, hashInfo, true, archiveFileIndex);
|
2015-07-05 22:23:44 -04:00
|
|
|
if(!match.empty()) {
|
2017-04-23 18:47:28 -04:00
|
|
|
return Console::LoadROM(match, nullptr, archiveFileIndex);
|
2015-07-05 22:23:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 23:34:48 -05:00
|
|
|
//Perform slow CRC32 search for ROM
|
2016-12-09 12:49:17 -05:00
|
|
|
for(string folder : FolderUtilities::GetKnownGameFolders()) {
|
2017-04-22 13:19:21 -04:00
|
|
|
string match = RomLoader::FindMatchingRomInFolder(folder, romName, hashInfo, false, archiveFileIndex);
|
2016-02-08 23:34:48 -05:00
|
|
|
if(!match.empty()) {
|
2017-04-23 18:47:28 -04:00
|
|
|
return Console::LoadROM(match, nullptr, archiveFileIndex);
|
2015-07-05 22:23:44 -04:00
|
|
|
}
|
2014-07-09 18:29:07 -04:00
|
|
|
}
|
2016-02-08 23:34:48 -05:00
|
|
|
|
2015-07-05 22:23:44 -04:00
|
|
|
return false;
|
2014-07-09 18:29:07 -04:00
|
|
|
}
|
|
|
|
|
2015-07-11 08:27:22 -04:00
|
|
|
string Console::GetROMPath()
|
2014-07-09 18:29:07 -04:00
|
|
|
{
|
2015-07-11 08:27:22 -04:00
|
|
|
return Instance->_romFilepath;
|
2014-06-14 11:27:55 -04:00
|
|
|
}
|
|
|
|
|
2016-06-17 20:53:05 -04:00
|
|
|
string Console::GetRomName()
|
|
|
|
{
|
|
|
|
if(Instance->_mapper) {
|
|
|
|
return Instance->_mapper->GetRomName();
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 22:24:41 -05:00
|
|
|
RomFormat Console::GetRomFormat()
|
|
|
|
{
|
|
|
|
if(Instance->_mapper) {
|
|
|
|
return Instance->_mapper->GetRomFormat();
|
|
|
|
} else {
|
|
|
|
return RomFormat::Unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-28 20:47:16 -05:00
|
|
|
uint32_t Console::GetCrc32()
|
|
|
|
{
|
2016-06-17 20:53:05 -04:00
|
|
|
if(Instance->_mapper) {
|
2017-04-22 13:19:21 -04:00
|
|
|
return Instance->_mapper->GetHashInfo().Crc32Hash;
|
2016-06-17 20:53:05 -04:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-28 20:47:16 -05:00
|
|
|
}
|
|
|
|
|
2016-07-10 09:05:41 -04:00
|
|
|
uint32_t Console::GetPrgCrc32()
|
|
|
|
{
|
|
|
|
if(Instance->_mapper) {
|
|
|
|
return Instance->_mapper->GetPrgCrc32();
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 23:54:31 -04:00
|
|
|
NesModel Console::GetModel()
|
|
|
|
{
|
|
|
|
return Instance->_model;
|
|
|
|
}
|
|
|
|
|
2015-07-05 19:35:38 -04:00
|
|
|
void Console::Reset(bool softReset)
|
2014-06-14 11:27:55 -04:00
|
|
|
{
|
2015-07-05 22:23:44 -04:00
|
|
|
if(Instance->_initialized) {
|
2017-02-24 23:06:13 -05:00
|
|
|
if(softReset && EmulationSettings::CheckFlag(EmulationFlags::DisablePpuReset)) {
|
|
|
|
//Allow mid-frame resets to allow the PPU to get out-of-sync
|
|
|
|
RequestReset();
|
2015-12-26 17:11:00 -05:00
|
|
|
} else {
|
2017-04-18 22:39:45 -04:00
|
|
|
MovieManager::Stop();
|
2017-02-24 23:06:13 -05:00
|
|
|
SoundMixer::StopRecording();
|
|
|
|
|
|
|
|
Console::Pause();
|
|
|
|
if(softReset) {
|
|
|
|
Instance->ResetComponents(softReset);
|
|
|
|
} else {
|
|
|
|
//Full reset of all objects to ensure the emulator always starts in the exact same state
|
|
|
|
Instance->Initialize(Instance->_romFilepath);
|
|
|
|
}
|
|
|
|
Console::Resume();
|
2015-12-26 17:11:00 -05:00
|
|
|
}
|
2014-07-01 12:44:01 -04:00
|
|
|
}
|
2014-06-14 11:27:55 -04:00
|
|
|
}
|
|
|
|
|
2014-06-25 13:30:02 -04:00
|
|
|
void Console::ResetComponents(bool softReset)
|
2014-06-24 02:47:32 -04:00
|
|
|
{
|
2017-04-18 22:39:45 -04:00
|
|
|
MovieManager::Stop();
|
2017-04-14 19:42:31 -04:00
|
|
|
if(!softReset) {
|
|
|
|
SoundMixer::StopRecording();
|
|
|
|
}
|
2015-07-05 22:23:44 -04:00
|
|
|
|
2016-07-17 14:07:22 -04:00
|
|
|
_memoryManager->Reset(softReset);
|
2017-02-24 23:06:13 -05:00
|
|
|
if(!EmulationSettings::CheckFlag(EmulationFlags::DisablePpuReset) || !softReset) {
|
|
|
|
_ppu->Reset();
|
|
|
|
}
|
2015-07-19 01:30:13 -04:00
|
|
|
_apu->Reset(softReset);
|
2017-04-20 21:58:35 -04:00
|
|
|
_cpu->Reset(softReset, _model);
|
2016-06-21 22:13:26 -04:00
|
|
|
_controlManager->Reset(softReset);
|
2016-07-10 18:22:37 -04:00
|
|
|
|
|
|
|
_lagCounter = 0;
|
2016-08-31 20:54:38 -04:00
|
|
|
|
2016-01-14 19:33:16 -05:00
|
|
|
SoundMixer::StopAudio(true);
|
2015-08-24 20:27:07 -04:00
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
if(softReset) {
|
2016-11-22 00:14:49 -05:00
|
|
|
if(_debugger) {
|
|
|
|
auto lock = _debuggerLock.AcquireSafe();
|
|
|
|
StopDebugger();
|
|
|
|
GetDebugger();
|
|
|
|
}
|
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GameReset);
|
|
|
|
} else {
|
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GameLoaded);
|
|
|
|
}
|
2014-06-24 02:47:32 -04:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:48:55 -04:00
|
|
|
void Console::Stop()
|
|
|
|
{
|
|
|
|
_stop = true;
|
2016-07-31 14:31:44 -04:00
|
|
|
|
|
|
|
shared_ptr<Debugger> debugger = _debugger;
|
|
|
|
if(debugger) {
|
|
|
|
debugger->Run();
|
2015-08-21 22:42:44 -04:00
|
|
|
}
|
2015-07-05 19:12:41 -04:00
|
|
|
_stopLock.Acquire();
|
|
|
|
_stopLock.Release();
|
2014-06-21 19:03:13 -04:00
|
|
|
}
|
|
|
|
|
2014-07-01 12:44:01 -04:00
|
|
|
void Console::Pause()
|
|
|
|
{
|
2016-07-31 14:31:44 -04:00
|
|
|
shared_ptr<Debugger> debugger = Console::Instance->_debugger;
|
|
|
|
if(debugger) {
|
2015-08-21 22:42:44 -04:00
|
|
|
//Make sure debugger resumes if we try to pause the emu, otherwise we will get deadlocked.
|
2016-07-31 14:31:44 -04:00
|
|
|
debugger->Suspend();
|
2015-08-21 22:42:44 -04:00
|
|
|
}
|
2015-07-05 22:23:44 -04:00
|
|
|
Console::Instance->_pauseLock.Acquire();
|
|
|
|
//Spin wait until emu pauses
|
|
|
|
Console::Instance->_runLock.Acquire();
|
2014-07-01 12:44:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::Resume()
|
|
|
|
{
|
2015-07-05 22:23:44 -04:00
|
|
|
Console::Instance->_runLock.Release();
|
|
|
|
Console::Instance->_pauseLock.Release();
|
2016-06-04 08:55:52 -04:00
|
|
|
|
2016-07-31 14:31:44 -04:00
|
|
|
shared_ptr<Debugger> debugger = Console::Instance->_debugger;
|
|
|
|
if(debugger) {
|
2016-06-04 08:55:52 -04:00
|
|
|
//Make sure debugger resumes if we try to pause the emu, otherwise we will get deadlocked.
|
2016-07-31 14:31:44 -04:00
|
|
|
debugger->Resume();
|
2016-06-04 08:55:52 -04:00
|
|
|
}
|
2014-07-01 12:44:01 -04:00
|
|
|
}
|
|
|
|
|
2014-06-14 11:27:55 -04:00
|
|
|
void Console::Run()
|
|
|
|
{
|
2014-06-21 15:43:41 -04:00
|
|
|
Timer clockTimer;
|
2015-07-21 23:05:27 -04:00
|
|
|
double targetTime;
|
|
|
|
uint32_t lastFrameNumber = -1;
|
2016-08-31 20:54:38 -04:00
|
|
|
|
2016-09-02 19:36:37 -04:00
|
|
|
ShortcutKeyHandler shortcutKeyHandler;
|
2016-08-31 20:54:38 -04:00
|
|
|
_autoSaveManager.reset(new AutoSaveManager());
|
2015-07-14 21:51:39 -04:00
|
|
|
|
2015-07-05 19:05:33 -04:00
|
|
|
_runLock.Acquire();
|
|
|
|
_stopLock.Acquire();
|
2014-07-06 19:54:47 -04:00
|
|
|
|
2016-07-10 19:15:00 -04:00
|
|
|
targetTime = GetFrameDelay();
|
2015-07-21 23:05:27 -04:00
|
|
|
|
2015-08-30 21:04:21 -04:00
|
|
|
VideoDecoder::GetInstance()->StartThread();
|
2016-08-29 21:07:52 -04:00
|
|
|
|
|
|
|
PlatformUtilities::DisableScreensaver();
|
2015-08-30 21:04:21 -04:00
|
|
|
|
2014-06-21 15:43:41 -04:00
|
|
|
while(true) {
|
2016-02-11 22:59:31 -05:00
|
|
|
try {
|
|
|
|
_cpu->Exec();
|
|
|
|
} catch(const std::runtime_error &ex) {
|
2016-02-19 13:05:04 -05:00
|
|
|
MessageManager::DisplayMessage("Error", "GameCrash", ex.what());
|
2016-02-11 22:59:31 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-24 23:06:13 -05:00
|
|
|
if(_resetRequested) {
|
|
|
|
//Used by NSF player to reset console after changing track
|
|
|
|
//Also used with DisablePpuReset option to reset mid-frame
|
2017-04-18 22:39:45 -04:00
|
|
|
MovieManager::Stop();
|
2017-02-24 23:06:13 -05:00
|
|
|
ResetComponents(true);
|
|
|
|
_resetRequested = false;
|
|
|
|
}
|
|
|
|
|
2015-07-16 16:55:16 -04:00
|
|
|
uint32_t currentFrameNumber = PPU::GetFrameCount();
|
|
|
|
if(currentFrameNumber != lastFrameNumber) {
|
2017-04-28 19:54:58 -04:00
|
|
|
_rewindManager->ProcessEndOfFrame();
|
2016-12-23 13:56:45 -05:00
|
|
|
EmulationSettings::DisableOverclocking(_disableOcNextFrame);
|
|
|
|
_disableOcNextFrame = false;
|
|
|
|
|
2017-04-28 19:54:58 -04:00
|
|
|
lastFrameNumber = PPU::GetFrameCount();
|
2014-06-30 14:44:30 -04:00
|
|
|
|
2015-08-30 21:04:21 -04:00
|
|
|
//Sleep until we're ready to start the next frame
|
|
|
|
clockTimer.WaitUntil(targetTime);
|
2016-06-25 20:46:54 -04:00
|
|
|
|
2015-07-05 19:05:33 -04:00
|
|
|
if(!_pauseLock.IsFree()) {
|
2014-07-06 19:54:47 -04:00
|
|
|
//Need to temporarely pause the emu (to save/load a state, etc.)
|
2015-07-05 19:05:33 -04:00
|
|
|
_runLock.Release();
|
2014-07-06 19:54:47 -04:00
|
|
|
|
|
|
|
//Spin wait until we are allowed to start again
|
2015-07-05 19:05:33 -04:00
|
|
|
_pauseLock.WaitForRelease();
|
2014-07-01 12:44:01 -04:00
|
|
|
|
2015-07-05 19:05:33 -04:00
|
|
|
_runLock.Acquire();
|
2014-07-01 12:44:01 -04:00
|
|
|
}
|
2014-07-06 19:54:47 -04:00
|
|
|
|
2016-02-05 23:14:27 -05:00
|
|
|
bool paused = EmulationSettings::IsPaused();
|
2016-01-31 13:53:17 -05:00
|
|
|
if(paused && !_stop) {
|
2015-07-01 23:17:14 -04:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GamePaused);
|
|
|
|
|
|
|
|
//Prevent audio from looping endlessly while game is paused
|
2016-01-14 19:33:16 -05:00
|
|
|
SoundMixer::StopAudio();
|
2016-09-04 19:04:52 -04:00
|
|
|
|
|
|
|
_runLock.Release();
|
2016-08-29 21:07:52 -04:00
|
|
|
|
|
|
|
PlatformUtilities::EnableScreensaver();
|
2016-12-14 20:48:47 -05:00
|
|
|
while(paused && !_stop && _debugger == nullptr) {
|
2014-07-09 21:48:54 -04:00
|
|
|
//Sleep until emulation is resumed
|
2017-04-22 18:08:38 -04:00
|
|
|
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(30));
|
2016-02-05 23:14:27 -05:00
|
|
|
paused = EmulationSettings::IsPaused();
|
2014-07-09 21:48:54 -04:00
|
|
|
}
|
2016-12-14 20:48:47 -05:00
|
|
|
|
|
|
|
if(_debugger != nullptr) {
|
|
|
|
//Prevent pausing when debugger is active
|
|
|
|
EmulationSettings::ClearFlags(EmulationFlags::Paused);
|
|
|
|
}
|
|
|
|
|
2016-08-29 21:07:52 -04:00
|
|
|
PlatformUtilities::DisableScreensaver();
|
|
|
|
_runLock.Acquire();
|
2015-07-01 23:17:14 -04:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GameResumed);
|
2014-07-09 21:48:54 -04:00
|
|
|
}
|
2015-07-21 23:05:27 -04:00
|
|
|
|
2015-08-30 21:04:21 -04:00
|
|
|
//Get next target time, and adjust based on whether we are ahead or behind
|
|
|
|
double timeLag = EmulationSettings::GetEmulationSpeed() == 0 ? 0 : clockTimer.GetElapsedMS() - targetTime;
|
2016-07-10 19:15:00 -04:00
|
|
|
UpdateNesModel(true);
|
|
|
|
targetTime = GetFrameDelay();
|
|
|
|
|
2014-07-06 19:54:47 -04:00
|
|
|
clockTimer.Reset();
|
2015-08-30 21:04:21 -04:00
|
|
|
targetTime -= timeLag;
|
|
|
|
if(targetTime < 0) {
|
|
|
|
targetTime = 0;
|
|
|
|
}
|
2016-07-10 18:22:37 -04:00
|
|
|
|
|
|
|
if(_controlManager->GetLagFlag()) {
|
|
|
|
_lagCounter++;
|
|
|
|
}
|
2014-07-06 19:54:47 -04:00
|
|
|
|
|
|
|
if(_stop) {
|
|
|
|
_stop = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-14 18:20:56 -04:00
|
|
|
}
|
2017-04-28 19:54:58 -04:00
|
|
|
_rewindManager.reset();
|
2016-01-14 19:33:16 -05:00
|
|
|
SoundMixer::StopAudio();
|
2017-04-18 22:39:45 -04:00
|
|
|
MovieManager::Stop();
|
2016-06-05 14:36:20 -04:00
|
|
|
SoundMixer::StopRecording();
|
2016-08-29 21:07:52 -04:00
|
|
|
PlatformUtilities::EnableScreensaver();
|
2015-08-30 21:04:21 -04:00
|
|
|
|
2016-08-31 20:54:38 -04:00
|
|
|
_autoSaveManager.reset();
|
|
|
|
|
2015-08-30 21:04:21 -04:00
|
|
|
VideoDecoder::GetInstance()->StopThread();
|
|
|
|
|
2016-01-31 00:41:33 -05:00
|
|
|
_initialized = false;
|
2016-01-28 22:34:23 -05:00
|
|
|
_romFilepath = "";
|
|
|
|
|
2015-07-05 19:05:33 -04:00
|
|
|
_stopLock.Release();
|
|
|
|
_runLock.Release();
|
2014-06-25 21:52:37 -04:00
|
|
|
}
|
|
|
|
|
2016-02-14 12:58:35 -05:00
|
|
|
bool Console::IsRunning()
|
|
|
|
{
|
|
|
|
return !Instance->_stopLock.IsFree();
|
|
|
|
}
|
|
|
|
|
2016-07-10 19:15:00 -04:00
|
|
|
void Console::UpdateNesModel(bool sendNotification)
|
2015-07-21 23:05:27 -04:00
|
|
|
{
|
2016-02-05 23:14:27 -05:00
|
|
|
bool configChanged = false;
|
|
|
|
if(EmulationSettings::NeedControllerUpdate()) {
|
|
|
|
_controlManager->UpdateControlDevices();
|
|
|
|
configChanged = true;
|
|
|
|
}
|
|
|
|
|
2015-07-21 23:05:27 -04:00
|
|
|
NesModel model = EmulationSettings::GetNesModel();
|
|
|
|
if(model == NesModel::Auto) {
|
2016-06-15 21:59:34 -04:00
|
|
|
switch(_mapper->GetGameSystem()) {
|
|
|
|
case GameSystem::NesPal: model = NesModel::PAL; break;
|
|
|
|
case GameSystem::Dendy: model = NesModel::Dendy; break;
|
|
|
|
default: model = NesModel::NTSC; break;
|
|
|
|
}
|
2015-07-21 23:05:27 -04:00
|
|
|
}
|
2016-02-05 23:14:27 -05:00
|
|
|
if(_model != model) {
|
|
|
|
_model = model;
|
|
|
|
configChanged = true;
|
2016-07-10 19:15:00 -04:00
|
|
|
|
|
|
|
if(sendNotification) {
|
|
|
|
MessageManager::DisplayMessage("Region", model == NesModel::PAL ? "PAL" : (model == NesModel::Dendy ? "Dendy" : "NTSC"));
|
|
|
|
}
|
2016-02-05 23:14:27 -05:00
|
|
|
}
|
2016-07-10 19:15:00 -04:00
|
|
|
|
|
|
|
_mapper->SetNesModel(model);
|
|
|
|
_ppu->SetNesModel(model);
|
|
|
|
_apu->SetNesModel(model);
|
|
|
|
|
|
|
|
if(configChanged && sendNotification) {
|
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::ConfigChanged);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double Console::GetFrameDelay()
|
|
|
|
{
|
|
|
|
uint32_t emulationSpeed = EmulationSettings::GetEmulationSpeed();
|
2016-01-23 00:52:06 -05:00
|
|
|
double frameDelay;
|
2015-08-24 20:27:07 -04:00
|
|
|
if(emulationSpeed == 0) {
|
2015-08-23 20:24:24 -04:00
|
|
|
frameDelay = 0;
|
|
|
|
} else {
|
2016-01-30 19:33:32 -05:00
|
|
|
//60.1fps (NTSC), 50.01fps (PAL/Dendy)
|
2016-07-10 19:15:00 -04:00
|
|
|
switch(_model) {
|
2016-06-02 23:56:11 -04:00
|
|
|
default:
|
2016-01-30 19:33:32 -05:00
|
|
|
case NesModel::NTSC: frameDelay = 16.63926405550947; break;
|
|
|
|
case NesModel::PAL:
|
|
|
|
case NesModel::Dendy: frameDelay = 19.99720920217466; break;
|
|
|
|
}
|
2015-08-24 20:27:07 -04:00
|
|
|
frameDelay /= (double)emulationSpeed / 100.0;
|
2015-08-23 20:24:24 -04:00
|
|
|
}
|
2015-08-24 20:27:07 -04:00
|
|
|
|
2016-01-23 00:52:06 -05:00
|
|
|
return frameDelay;
|
2015-07-21 23:05:27 -04:00
|
|
|
}
|
|
|
|
|
2014-07-01 12:44:01 -04:00
|
|
|
void Console::SaveState(ostream &saveStream)
|
2014-06-25 21:52:37 -04:00
|
|
|
{
|
2015-07-05 22:23:44 -04:00
|
|
|
if(Instance->_initialized) {
|
2014-07-06 19:54:47 -04:00
|
|
|
Instance->_cpu->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_ppu->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_memoryManager->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_apu->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_controlManager->SaveSnapshot(&saveStream);
|
2016-06-11 16:08:16 -04:00
|
|
|
Instance->_mapper->SaveSnapshot(&saveStream);
|
2014-07-06 19:54:47 -04:00
|
|
|
}
|
2014-07-01 12:44:01 -04:00
|
|
|
}
|
2014-06-25 21:52:37 -04:00
|
|
|
|
2014-07-01 12:44:01 -04:00
|
|
|
void Console::LoadState(istream &loadStream)
|
|
|
|
{
|
2015-07-05 22:23:44 -04:00
|
|
|
if(Instance->_initialized) {
|
2016-07-19 16:36:07 -04:00
|
|
|
//Stop any movie that might have been playing/recording if a state is loaded
|
|
|
|
//(Note: Loading a state is disabled in the UI while a movie is playing/recording)
|
2017-04-18 22:39:45 -04:00
|
|
|
MovieManager::Stop();
|
2016-07-19 16:36:07 -04:00
|
|
|
|
2014-07-06 19:54:47 -04:00
|
|
|
Instance->_cpu->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_ppu->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_memoryManager->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_apu->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_controlManager->LoadSnapshot(&loadStream);
|
2016-06-11 16:08:16 -04:00
|
|
|
Instance->_mapper->LoadSnapshot(&loadStream);
|
2014-07-09 21:11:02 -04:00
|
|
|
|
2015-07-01 23:17:14 -04:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::StateLoaded);
|
2014-07-06 19:54:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::LoadState(uint8_t *buffer, uint32_t bufferSize)
|
|
|
|
{
|
2017-04-28 19:54:58 -04:00
|
|
|
//Send any unprocessed sound to the SoundMixer - needed for rewind
|
|
|
|
Instance->_apu->EndFrame();
|
|
|
|
|
2014-07-06 19:54:47 -04:00
|
|
|
stringstream stream;
|
|
|
|
stream.write((char*)buffer, bufferSize);
|
|
|
|
stream.seekg(0, ios::beg);
|
|
|
|
LoadState(stream);
|
2014-06-25 21:52:37 -04:00
|
|
|
}
|
|
|
|
|
2016-08-25 19:02:33 -04:00
|
|
|
std::shared_ptr<Debugger> Console::GetDebugger(bool autoStart)
|
2015-06-24 19:26:19 -04:00
|
|
|
{
|
2016-07-31 14:31:44 -04:00
|
|
|
auto lock = _debuggerLock.AcquireSafe();
|
2016-08-25 19:02:33 -04:00
|
|
|
if(!_debugger && autoStart) {
|
2015-08-21 22:42:44 -04:00
|
|
|
_debugger.reset(new Debugger(Console::Instance, _cpu, _ppu, _memoryManager, _mapper));
|
|
|
|
}
|
|
|
|
return _debugger;
|
2015-06-24 19:26:19 -04:00
|
|
|
}
|
2015-08-21 22:42:44 -04:00
|
|
|
|
|
|
|
void Console::StopDebugger()
|
|
|
|
{
|
2016-10-26 22:09:15 -04:00
|
|
|
auto lock = _debuggerLock.AcquireSafe();
|
2015-08-21 22:42:44 -04:00
|
|
|
_debugger.reset();
|
2016-02-05 23:14:27 -05:00
|
|
|
}
|
|
|
|
|
2016-06-25 20:46:54 -04:00
|
|
|
void Console::RequestReset()
|
|
|
|
{
|
|
|
|
Instance->_resetRequested = true;
|
|
|
|
}
|
|
|
|
|
2016-02-05 23:14:27 -05:00
|
|
|
NesModel Console::GetNesModel()
|
|
|
|
{
|
|
|
|
return Instance->_model;
|
2016-07-10 18:22:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Console::GetLagCounter()
|
|
|
|
{
|
|
|
|
return Instance->_lagCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::ResetLagCounter()
|
|
|
|
{
|
|
|
|
Instance->_lagCounter = 0;
|
2016-11-26 18:04:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Console::IsDebuggerAttached()
|
|
|
|
{
|
|
|
|
return (bool)Instance->_debugger;
|
2016-12-23 13:56:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::DisableOcNextFrame()
|
|
|
|
{
|
|
|
|
Instance->_disableOcNextFrame = true;
|
2015-08-21 22:42:44 -04:00
|
|
|
}
|