2014-06-14 15:27:55 +00:00
|
|
|
#include "stdafx.h"
|
2015-07-02 03:17:14 +00:00
|
|
|
#include <thread>
|
2014-06-14 15:27:55 +00:00
|
|
|
#include "Console.h"
|
2015-07-02 03:17:14 +00:00
|
|
|
#include "BaseMapper.h"
|
2014-06-24 06:47:32 +00:00
|
|
|
#include "MapperFactory.h"
|
2015-07-02 03:17:14 +00:00
|
|
|
#include "Debugger.h"
|
2015-07-18 00:58:57 +00:00
|
|
|
#include "MessageManager.h"
|
|
|
|
#include "EmulationSettings.h"
|
2014-06-23 23:02:09 +00:00
|
|
|
#include "../Utilities/Timer.h"
|
2014-07-09 22:29:07 +00:00
|
|
|
#include "../Utilities/FolderUtilities.h"
|
2014-06-14 15:27:55 +00:00
|
|
|
|
2015-07-06 02:23:44 +00:00
|
|
|
shared_ptr<Console> Console::Instance(new Console());
|
2014-06-21 23:03:13 +00:00
|
|
|
|
2015-07-06 02:23:44 +00:00
|
|
|
Console::Console()
|
2014-06-14 15:27:55 +00:00
|
|
|
{
|
2014-07-09 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Console::~Console()
|
|
|
|
{
|
|
|
|
Movie::Stop();
|
|
|
|
}
|
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
shared_ptr<Console> Console::GetInstance()
|
2014-07-09 22:29:07 +00:00
|
|
|
{
|
|
|
|
return Console::Instance;
|
|
|
|
}
|
|
|
|
|
2015-07-21 03:20:41 +00:00
|
|
|
void Console::Release()
|
|
|
|
{
|
|
|
|
Console::Instance.reset(new Console());
|
|
|
|
}
|
|
|
|
|
2015-07-11 12:27:22 +00:00
|
|
|
void Console::Initialize(string filename)
|
2014-07-09 22:29:07 +00:00
|
|
|
{
|
2015-07-05 23:05:33 +00:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GameStopped);
|
2014-07-10 23:25:35 +00:00
|
|
|
shared_ptr<BaseMapper> mapper = MapperFactory::InitializeFromFile(filename);
|
|
|
|
if(mapper) {
|
|
|
|
_romFilepath = filename;
|
2014-06-22 12:38:42 +00:00
|
|
|
|
2014-07-10 23:25:35 +00:00
|
|
|
_mapper = mapper;
|
|
|
|
_memoryManager.reset(new MemoryManager(_mapper));
|
|
|
|
_cpu.reset(new CPU(_memoryManager.get()));
|
|
|
|
_ppu.reset(new PPU(_memoryManager.get()));
|
|
|
|
_apu.reset(new APU(_memoryManager.get()));
|
2014-06-23 02:15:35 +00:00
|
|
|
|
2014-07-10 23:25:35 +00:00
|
|
|
_controlManager.reset(new ControlManager());
|
2014-06-21 19:43:41 +00:00
|
|
|
|
2014-07-10 23:25:35 +00:00
|
|
|
_memoryManager->RegisterIODevice(_mapper.get());
|
|
|
|
_memoryManager->RegisterIODevice(_ppu.get());
|
|
|
|
_memoryManager->RegisterIODevice(_apu.get());
|
|
|
|
_memoryManager->RegisterIODevice(_controlManager.get());
|
2014-06-19 23:58:15 +00:00
|
|
|
|
2014-07-10 23:25:35 +00:00
|
|
|
ResetComponents(false);
|
|
|
|
|
2015-07-06 02:23:44 +00:00
|
|
|
_initialized = true;
|
|
|
|
|
|
|
|
FolderUtilities::AddKnowGameFolder(FolderUtilities::GetFolderName(filename));
|
2015-07-11 12:27:22 +00:00
|
|
|
MessageManager::DisplayMessage("Game loaded", FolderUtilities::GetFilename(filename, false));
|
2014-07-10 23:25:35 +00:00
|
|
|
} else {
|
2015-07-11 12:27:22 +00:00
|
|
|
MessageManager::DisplayMessage("Error", string("Could not load file: ") + FolderUtilities::GetFilename(filename, true));
|
2014-07-10 23:25:35 +00:00
|
|
|
}
|
2015-07-06 02:23:44 +00:00
|
|
|
}
|
2014-07-01 16:44:01 +00:00
|
|
|
|
2015-07-11 12:27:22 +00:00
|
|
|
void Console::LoadROM(string filepath)
|
2015-07-06 02:23:44 +00:00
|
|
|
{
|
|
|
|
Console::Pause();
|
|
|
|
Instance->Initialize(filepath);
|
|
|
|
Console::Resume();
|
2014-06-14 15:27:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 12:27:22 +00:00
|
|
|
bool Console::LoadROM(string filename, uint32_t crc32Hash)
|
2014-06-14 15:27:55 +00:00
|
|
|
{
|
2015-07-11 12:27:22 +00:00
|
|
|
string currentRomFilepath = Console::GetROMPath();
|
|
|
|
string currentFolder = FolderUtilities::GetFolderName(currentRomFilepath);
|
2015-07-06 02:23:44 +00:00
|
|
|
if(!currentRomFilepath.empty()) {
|
|
|
|
if(ROMLoader::GetCRC32(Console::GetROMPath()) == crc32Hash) {
|
|
|
|
//Current game matches, no need to do anything
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Try to find the game in the same folder as the current game's folder
|
2015-07-11 12:27:22 +00:00
|
|
|
string match = ROMLoader::FindMatchingRomInFolder(currentFolder, filename, crc32Hash);
|
2015-07-06 02:23:44 +00:00
|
|
|
if(!match.empty()) {
|
|
|
|
Console::LoadROM(match);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-11 12:27:22 +00:00
|
|
|
for(string folder : FolderUtilities::GetKnowGameFolders()) {
|
2015-07-06 02:23:44 +00:00
|
|
|
if(folder != currentFolder) {
|
2015-07-11 12:27:22 +00:00
|
|
|
string match = ROMLoader::FindMatchingRomInFolder(folder, filename, crc32Hash);
|
2015-07-06 02:23:44 +00:00
|
|
|
if(!match.empty()) {
|
|
|
|
Console::LoadROM(match);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 22:29:07 +00:00
|
|
|
}
|
2015-07-06 02:23:44 +00:00
|
|
|
return false;
|
2014-07-09 22:29:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 12:27:22 +00:00
|
|
|
string Console::GetROMPath()
|
2014-07-09 22:29:07 +00:00
|
|
|
{
|
2015-07-11 12:27:22 +00:00
|
|
|
return Instance->_romFilepath;
|
2014-06-14 15:27:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 23:35:38 +00:00
|
|
|
void Console::Reset(bool softReset)
|
2014-06-14 15:27:55 +00:00
|
|
|
{
|
2014-07-01 22:05:54 +00:00
|
|
|
Movie::Stop();
|
2015-07-06 02:23:44 +00:00
|
|
|
if(Instance->_initialized) {
|
2015-07-02 03:17:14 +00:00
|
|
|
Console::Pause();
|
2015-07-05 23:35:38 +00:00
|
|
|
Instance->ResetComponents(softReset);
|
2015-07-02 03:17:14 +00:00
|
|
|
Console::Resume();
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|
2014-06-14 15:27:55 +00:00
|
|
|
}
|
|
|
|
|
2014-06-25 17:30:02 +00:00
|
|
|
void Console::ResetComponents(bool softReset)
|
2014-06-24 06:47:32 +00:00
|
|
|
{
|
2015-07-06 02:23:44 +00:00
|
|
|
Movie::Stop();
|
|
|
|
|
2014-06-24 06:47:32 +00:00
|
|
|
_ppu->Reset();
|
2015-07-19 05:30:13 +00:00
|
|
|
_apu->Reset(softReset);
|
2015-07-02 03:17:14 +00:00
|
|
|
_cpu->Reset(softReset);
|
2015-07-06 00:30:39 +00:00
|
|
|
_memoryManager->Reset(softReset);
|
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
if(softReset) {
|
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GameReset);
|
|
|
|
} else {
|
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GameLoaded);
|
|
|
|
}
|
2014-06-24 06:47:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-21 01:48:55 +00:00
|
|
|
void Console::Stop()
|
|
|
|
{
|
|
|
|
_stop = true;
|
2015-07-18 00:58:57 +00:00
|
|
|
EmulationSettings::ClearFlags(EmulationFlags::Paused);
|
2015-07-05 23:12:41 +00:00
|
|
|
_stopLock.Acquire();
|
|
|
|
_stopLock.Release();
|
2014-06-21 23:03:13 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 16:44:01 +00:00
|
|
|
void Console::Pause()
|
|
|
|
{
|
2015-07-06 02:23:44 +00:00
|
|
|
Console::Instance->_pauseLock.Acquire();
|
|
|
|
//Spin wait until emu pauses
|
|
|
|
Console::Instance->_runLock.Acquire();
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::Resume()
|
|
|
|
{
|
2015-07-06 02:23:44 +00:00
|
|
|
Console::Instance->_runLock.Release();
|
|
|
|
Console::Instance->_pauseLock.Release();
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 15:27:55 +00:00
|
|
|
void Console::Run()
|
|
|
|
{
|
2014-06-21 19:43:41 +00:00
|
|
|
Timer clockTimer;
|
2014-06-22 14:07:40 +00:00
|
|
|
double elapsedTime = 0;
|
2015-07-18 00:58:57 +00:00
|
|
|
double targetTime = 16.63926405550947; //~60.0988fps
|
2015-07-15 01:51:39 +00:00
|
|
|
|
2015-07-05 23:05:33 +00:00
|
|
|
_runLock.Acquire();
|
|
|
|
_stopLock.Acquire();
|
2014-07-06 23:54:47 +00:00
|
|
|
|
2015-07-16 20:55:16 +00:00
|
|
|
uint32_t lastFrameNumber = -1;
|
2014-06-21 19:43:41 +00:00
|
|
|
while(true) {
|
2015-07-16 20:55:16 +00:00
|
|
|
_cpu->Exec();
|
|
|
|
uint32_t currentFrameNumber = PPU::GetFrameCount();
|
|
|
|
if(currentFrameNumber != lastFrameNumber) {
|
|
|
|
lastFrameNumber = currentFrameNumber;
|
2014-06-30 18:44:30 +00:00
|
|
|
|
2015-07-18 00:58:57 +00:00
|
|
|
if(EmulationSettings::CheckFlag(EmulationFlags::LimitFPS)) {
|
2014-07-01 16:44:01 +00:00
|
|
|
elapsedTime = clockTimer.GetElapsedMS();
|
|
|
|
while(targetTime > elapsedTime) {
|
|
|
|
if(targetTime - elapsedTime > 2) {
|
|
|
|
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>((int)(targetTime - elapsedTime - 1)));
|
|
|
|
}
|
|
|
|
elapsedTime = clockTimer.GetElapsedMS();
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 23:54:47 +00:00
|
|
|
|
2015-07-05 23:05:33 +00:00
|
|
|
if(!_pauseLock.IsFree()) {
|
2014-07-06 23:54:47 +00:00
|
|
|
//Need to temporarely pause the emu (to save/load a state, etc.)
|
2015-07-05 23:05:33 +00:00
|
|
|
_runLock.Release();
|
2014-07-06 23:54:47 +00:00
|
|
|
|
|
|
|
//Spin wait until we are allowed to start again
|
2015-07-05 23:05:33 +00:00
|
|
|
_pauseLock.WaitForRelease();
|
2014-07-01 16:44:01 +00:00
|
|
|
|
2015-07-05 23:05:33 +00:00
|
|
|
_runLock.Acquire();
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|
2014-07-06 23:54:47 +00:00
|
|
|
|
2015-07-18 00:58:57 +00:00
|
|
|
if(EmulationSettings::CheckFlag(EmulationFlags::Paused) && !_stop) {
|
2015-07-02 03:17:14 +00:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GamePaused);
|
2015-07-05 23:05:33 +00:00
|
|
|
_runLock.Release();
|
2015-07-02 03:17:14 +00:00
|
|
|
|
|
|
|
//Prevent audio from looping endlessly while game is paused
|
|
|
|
_apu->StopAudio();
|
|
|
|
|
2015-07-18 00:58:57 +00:00
|
|
|
while(EmulationSettings::CheckFlag(EmulationFlags::Paused)) {
|
2014-07-10 01:48:54 +00:00
|
|
|
//Sleep until emulation is resumed
|
|
|
|
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(100));
|
|
|
|
}
|
2015-07-05 23:05:33 +00:00
|
|
|
_runLock.Acquire();
|
2015-07-02 03:17:14 +00:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::GameResumed);
|
2014-07-10 01:48:54 +00:00
|
|
|
}
|
2014-07-06 23:54:47 +00:00
|
|
|
clockTimer.Reset();
|
|
|
|
|
|
|
|
if(_stop) {
|
|
|
|
_stop = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-06-14 22:20:56 +00:00
|
|
|
}
|
2015-07-02 03:17:14 +00:00
|
|
|
_apu->StopAudio();
|
2015-07-05 23:05:33 +00:00
|
|
|
_stopLock.Release();
|
|
|
|
_runLock.Release();
|
2014-06-26 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 16:44:01 +00:00
|
|
|
void Console::SaveState(ostream &saveStream)
|
2014-06-26 01:52:37 +00:00
|
|
|
{
|
2015-07-06 02:23:44 +00:00
|
|
|
if(Instance->_initialized) {
|
2014-07-06 23:54:47 +00:00
|
|
|
Instance->_cpu->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_ppu->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_memoryManager->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_mapper->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_apu->SaveSnapshot(&saveStream);
|
|
|
|
Instance->_controlManager->SaveSnapshot(&saveStream);
|
|
|
|
}
|
2014-07-01 16:44:01 +00:00
|
|
|
}
|
2014-06-26 01:52:37 +00:00
|
|
|
|
2014-07-01 16:44:01 +00:00
|
|
|
void Console::LoadState(istream &loadStream)
|
|
|
|
{
|
2015-07-06 02:23:44 +00:00
|
|
|
if(Instance->_initialized) {
|
2014-07-06 23:54:47 +00:00
|
|
|
Instance->_cpu->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_ppu->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_memoryManager->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_mapper->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_apu->LoadSnapshot(&loadStream);
|
|
|
|
Instance->_controlManager->LoadSnapshot(&loadStream);
|
2014-07-10 01:11:02 +00:00
|
|
|
|
2015-07-02 03:17:14 +00:00
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::StateLoaded);
|
2014-07-06 23:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::LoadState(uint8_t *buffer, uint32_t bufferSize)
|
|
|
|
{
|
|
|
|
stringstream stream;
|
|
|
|
stream.write((char*)buffer, bufferSize);
|
|
|
|
stream.seekg(0, ios::beg);
|
|
|
|
LoadState(stream);
|
2014-06-26 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 23:26:19 +00:00
|
|
|
shared_ptr<Debugger> Console::GetDebugger()
|
|
|
|
{
|
2015-07-02 03:17:14 +00:00
|
|
|
return shared_ptr<Debugger>(new Debugger(Console::Instance, _cpu, _ppu, _memoryManager, _mapper));
|
2015-06-24 23:26:19 +00:00
|
|
|
}
|