2014-06-14 15:27:55 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Console.h"
|
2014-06-24 06:47:32 +00:00
|
|
|
#include "MapperFactory.h"
|
2014-06-23 23:02:09 +00:00
|
|
|
#include "../Utilities/Timer.h"
|
2014-06-14 15:27:55 +00:00
|
|
|
|
2014-06-21 23:03:13 +00:00
|
|
|
uint32_t Console::Flags = 0;
|
2014-06-23 20:38:01 +00:00
|
|
|
uint32_t Console::CurrentFPS = 0;
|
2014-06-21 23:03:13 +00:00
|
|
|
|
2014-06-21 01:48:55 +00:00
|
|
|
Console::Console(wstring filename)
|
2014-06-14 15:27:55 +00:00
|
|
|
{
|
2014-06-22 12:38:42 +00:00
|
|
|
_romFilename = filename;
|
|
|
|
|
2014-06-14 15:27:55 +00:00
|
|
|
_mapper = MapperFactory::InitializeFromFile(filename);
|
2014-06-24 06:47:32 +00:00
|
|
|
_memoryManager.reset(new MemoryManager(_mapper));
|
2014-06-19 23:58:15 +00:00
|
|
|
_cpu.reset(new CPU(_memoryManager.get()));
|
|
|
|
_ppu.reset(new PPU(_memoryManager.get()));
|
2014-06-25 21:30:35 +00:00
|
|
|
_apu.reset(new APU(_memoryManager.get()));
|
2014-06-23 02:15:35 +00:00
|
|
|
|
2014-06-21 19:43:41 +00:00
|
|
|
_controlManager.reset(new ControlManager());
|
|
|
|
|
2014-06-19 23:58:15 +00:00
|
|
|
_memoryManager->RegisterIODevice(_mapper.get());
|
|
|
|
_memoryManager->RegisterIODevice(_ppu.get());
|
2014-06-23 02:15:35 +00:00
|
|
|
_memoryManager->RegisterIODevice(_apu.get());
|
2014-06-21 19:43:41 +00:00
|
|
|
_memoryManager->RegisterIODevice(_controlManager.get());
|
2014-06-19 23:58:15 +00:00
|
|
|
|
2014-06-25 17:30:02 +00:00
|
|
|
ResetComponents(false);
|
2014-06-14 15:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Console::~Console()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::Reset()
|
|
|
|
{
|
2014-06-23 17:52:53 +00:00
|
|
|
_reset = true;
|
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
|
|
|
{
|
2014-06-25 17:30:02 +00:00
|
|
|
_cpu->Reset(softReset);
|
2014-06-24 06:47:32 +00:00
|
|
|
_ppu->Reset();
|
|
|
|
_apu->Reset();
|
|
|
|
}
|
|
|
|
|
2014-06-21 01:48:55 +00:00
|
|
|
void Console::Stop()
|
|
|
|
{
|
|
|
|
_stop = true;
|
2014-06-21 23:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::SetFlags(int flags)
|
|
|
|
{
|
|
|
|
Console::Flags |= flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::ClearFlags(int flags)
|
|
|
|
{
|
2014-06-24 00:00:51 +00:00
|
|
|
Console::Flags &= ~flags;
|
2014-06-21 23:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Console::CheckFlag(int flag)
|
|
|
|
{
|
|
|
|
return (Console::Flags & flag) == flag;
|
2014-06-21 01:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 20:38:01 +00:00
|
|
|
uint32_t Console::GetFPS()
|
|
|
|
{
|
|
|
|
return Console::CurrentFPS;
|
|
|
|
}
|
|
|
|
|
2014-06-14 15:27:55 +00:00
|
|
|
void Console::Run()
|
|
|
|
{
|
2014-06-21 19:43:41 +00:00
|
|
|
Timer clockTimer;
|
|
|
|
Timer fpsTimer;
|
2014-06-21 04:37:44 +00:00
|
|
|
uint32_t lastFrameCount = 0;
|
2014-06-22 14:07:40 +00:00
|
|
|
double elapsedTime = 0;
|
2014-06-23 17:52:53 +00:00
|
|
|
double targetTime = 16.6666666666666666;
|
2014-06-21 19:43:41 +00:00
|
|
|
while(true) {
|
2014-06-23 02:15:35 +00:00
|
|
|
uint32_t executedCycles = _cpu->Exec();
|
2014-06-16 01:45:36 +00:00
|
|
|
_ppu->Exec();
|
2014-06-30 18:44:30 +00:00
|
|
|
|
2014-06-23 17:52:53 +00:00
|
|
|
bool frameDone = _apu->Exec(executedCycles);
|
2014-06-23 02:15:35 +00:00
|
|
|
|
2014-06-30 18:44:30 +00:00
|
|
|
if(frameDone) {
|
|
|
|
_cpu->EndFrame();
|
|
|
|
_ppu->EndFrame();
|
|
|
|
}
|
|
|
|
|
2014-06-23 17:52:53 +00:00
|
|
|
if(CheckFlag(EmulationFlags::LimitFPS) && frameDone) {
|
2014-06-21 19:43:41 +00:00
|
|
|
elapsedTime = clockTimer.GetElapsedMS();
|
|
|
|
while(targetTime > elapsedTime) {
|
|
|
|
if(targetTime - elapsedTime > 2) {
|
2014-06-22 14:07:40 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>((int)(targetTime - elapsedTime - 1)));
|
2014-06-21 19:43:41 +00:00
|
|
|
}
|
|
|
|
elapsedTime = clockTimer.GetElapsedMS();
|
|
|
|
}
|
|
|
|
clockTimer.Reset();
|
2014-06-21 01:48:55 +00:00
|
|
|
}
|
2014-06-21 19:43:41 +00:00
|
|
|
|
2014-06-22 14:07:40 +00:00
|
|
|
if(fpsTimer.GetElapsedMS() > 1000) {
|
2014-06-21 04:37:44 +00:00
|
|
|
uint32_t frameCount = _ppu->GetFrameCount();
|
2014-06-28 18:22:17 +00:00
|
|
|
Console::CurrentFPS = (int)(std::round((double)(frameCount - lastFrameCount) / (fpsTimer.GetElapsedMS() / 1000)));
|
2014-06-21 04:37:44 +00:00
|
|
|
lastFrameCount = frameCount;
|
2014-06-21 19:43:41 +00:00
|
|
|
fpsTimer.Reset();
|
|
|
|
}
|
|
|
|
|
2014-06-26 01:52:37 +00:00
|
|
|
if(!_saveStateFilename.empty()) {
|
|
|
|
SaveState();
|
|
|
|
} else if(!_loadStateFilename.empty()) {
|
|
|
|
LoadState();
|
|
|
|
}
|
|
|
|
|
2014-06-21 19:43:41 +00:00
|
|
|
if(_stop) {
|
2014-06-23 17:52:53 +00:00
|
|
|
_stop = false;
|
2014-06-21 19:43:41 +00:00
|
|
|
break;
|
2014-06-21 04:37:44 +00:00
|
|
|
}
|
2014-06-23 17:52:53 +00:00
|
|
|
|
|
|
|
if(_reset) {
|
|
|
|
clockTimer.Reset();
|
|
|
|
fpsTimer.Reset();
|
|
|
|
lastFrameCount = 0;
|
|
|
|
elapsedTime = 0;
|
2014-06-25 17:30:02 +00:00
|
|
|
ResetComponents(true);
|
2014-06-23 17:52:53 +00:00
|
|
|
_reset = false;
|
|
|
|
}
|
2014-06-14 22:20:56 +00:00
|
|
|
}
|
2014-06-14 15:27:55 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 01:52:37 +00:00
|
|
|
void Console::SaveState(wstring filename)
|
|
|
|
{
|
|
|
|
_saveStateFilename = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Console::SaveState()
|
|
|
|
{
|
|
|
|
ofstream file(_saveStateFilename, ios::out | ios::binary);
|
|
|
|
|
|
|
|
if(file) {
|
|
|
|
_cpu->SaveSnapshot(&file);
|
|
|
|
_ppu->SaveSnapshot(&file);
|
|
|
|
_memoryManager->SaveSnapshot(&file);
|
|
|
|
_mapper->SaveSnapshot(&file);
|
|
|
|
_apu->SaveSnapshot(&file);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
_saveStateFilename.clear();
|
|
|
|
}
|
|
|
|
|
2014-06-29 20:51:58 +00:00
|
|
|
bool Console::LoadState(wstring filename)
|
2014-06-26 01:52:37 +00:00
|
|
|
{
|
2014-06-29 20:51:58 +00:00
|
|
|
ifstream file(filename, ios::out | ios::binary);
|
|
|
|
|
|
|
|
if(file) {
|
|
|
|
file.close();
|
|
|
|
_loadStateFilename = filename;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-06-26 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Console::LoadState()
|
|
|
|
{
|
|
|
|
ifstream file(_loadStateFilename, ios::out | ios::binary);
|
|
|
|
|
|
|
|
if(file) {
|
|
|
|
_cpu->LoadSnapshot(&file);
|
|
|
|
_ppu->LoadSnapshot(&file);
|
|
|
|
_memoryManager->LoadSnapshot(&file);
|
|
|
|
_mapper->LoadSnapshot(&file);
|
|
|
|
_apu->LoadSnapshot(&file);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadStateFilename.clear();
|
|
|
|
}
|
|
|
|
|
2014-06-22 12:38:42 +00:00
|
|
|
bool Console::RunTest(uint8_t *expectedResult)
|
2014-06-14 15:27:55 +00:00
|
|
|
{
|
2014-06-19 02:54:23 +00:00
|
|
|
Timer timer;
|
2014-06-27 00:55:22 +00:00
|
|
|
uint8_t maxWait = 60;
|
|
|
|
uint8_t* lastFrameBuffer = new uint8_t[256 * 240 * 4];
|
2014-06-14 22:20:56 +00:00
|
|
|
while(true) {
|
2014-06-30 18:44:30 +00:00
|
|
|
uint32_t executedCycles = _cpu->Exec();
|
2014-06-16 01:45:36 +00:00
|
|
|
_ppu->Exec();
|
2014-06-19 02:54:23 +00:00
|
|
|
|
2014-06-30 18:44:30 +00:00
|
|
|
if(_apu->Exec(executedCycles)) {
|
|
|
|
_cpu->EndFrame();
|
|
|
|
_ppu->EndFrame();
|
|
|
|
}
|
|
|
|
|
2014-06-22 12:38:42 +00:00
|
|
|
if(timer.GetElapsedMS() > 100) {
|
2014-06-25 16:47:15 +00:00
|
|
|
if(memcmp(_ppu->GetFrameBuffer(), expectedResult, 256 * 240 * 4) == 0) {
|
2014-06-22 12:38:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-27 00:55:22 +00:00
|
|
|
|
2014-06-19 02:54:23 +00:00
|
|
|
timer.Reset();
|
2014-06-22 12:38:42 +00:00
|
|
|
|
2014-06-27 00:55:22 +00:00
|
|
|
if(memcmp(lastFrameBuffer, _ppu->GetFrameBuffer(), 256 * 240 * 4) != 0) {
|
|
|
|
memcpy(lastFrameBuffer, _ppu->GetFrameBuffer(), 256 * 240 * 4);
|
|
|
|
maxWait = 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
maxWait--;
|
2014-06-22 12:38:42 +00:00
|
|
|
if(maxWait == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-19 02:54:23 +00:00
|
|
|
}
|
2014-06-14 22:20:56 +00:00
|
|
|
}
|
2014-06-27 00:55:22 +00:00
|
|
|
|
|
|
|
delete[] lastFrameBuffer;
|
2014-06-14 22:20:56 +00:00
|
|
|
|
2014-06-22 12:38:42 +00:00
|
|
|
return false;
|
2014-06-21 01:48:55 +00:00
|
|
|
}
|
|
|
|
|
2014-06-22 12:38:42 +00:00
|
|
|
void Console::SaveTestResult()
|
2014-06-14 22:20:56 +00:00
|
|
|
{
|
2014-06-22 12:38:42 +00:00
|
|
|
wstring filename = _romFilename + L".trt";
|
|
|
|
|
|
|
|
ofstream testResultFile(filename, ios::out | ios::binary);
|
|
|
|
|
|
|
|
if(!testResultFile) {
|
|
|
|
throw std::exception("File could not be opened");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* buffer = new uint8_t[256 * 240 * 4];
|
|
|
|
memcpy(buffer, _ppu->GetFrameBuffer(), 256 * 240 * 4);
|
|
|
|
|
|
|
|
testResultFile.write((char *)buffer, 256 * 240 * 4);
|
|
|
|
|
|
|
|
testResultFile.close();
|
|
|
|
|
|
|
|
delete[] buffer;
|
2014-06-14 15:27:55 +00:00
|
|
|
}
|