Mesen/Core/Console.cpp

135 lines
2.6 KiB
C++
Raw Normal View History

2014-06-14 15:27:55 +00:00
#include "stdafx.h"
#include "Console.h"
2014-06-14 22:20:56 +00:00
#include "Timer.h"
2014-06-14 15:27:55 +00:00
2014-06-21 23:03:13 +00:00
uint32_t Console::Flags = 0;
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);
_memoryManager.reset(new MemoryManager(_mapper->GetHeader()));
_cpu.reset(new CPU(_memoryManager.get()));
_ppu.reset(new PPU(_memoryManager.get()));
2014-06-21 19:43:41 +00:00
_controlManager.reset(new ControlManager());
_memoryManager->RegisterIODevice(_mapper.get());
_memoryManager->RegisterIODevice(_ppu.get());
2014-06-21 19:43:41 +00:00
_memoryManager->RegisterIODevice(_controlManager.get());
Reset();
2014-06-14 15:27:55 +00:00
}
Console::~Console()
{
}
void Console::Reset()
{
_cpu->Reset();
}
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)
{
Console::Flags ^= flags;
}
bool Console::CheckFlag(int flag)
{
return (Console::Flags & flag) == flag;
}
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-21 19:43:41 +00:00
double elapsedTime = 0.0;
uint32_t executedCycles = 0;
bool limitFrameRate = true;
while(true) {
executedCycles += _cpu->Exec();
2014-06-16 01:45:36 +00:00
_ppu->Exec();
2014-06-21 23:03:13 +00:00
if(CheckFlag(EmulationFlags::LimitFPS) && executedCycles > 30000) {
2014-06-21 19:43:41 +00:00
double targetTime = 16.77;
elapsedTime = clockTimer.GetElapsedMS();
while(targetTime > elapsedTime) {
if(targetTime - elapsedTime > 2) {
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(1));
}
elapsedTime = clockTimer.GetElapsedMS();
}
executedCycles = 0;
clockTimer.Reset();
}
2014-06-21 19:43:41 +00:00
if(fpsTimer.GetElapsedMS() > 2000) {
2014-06-21 04:37:44 +00:00
uint32_t frameCount = _ppu->GetFrameCount();
2014-06-21 19:43:41 +00:00
std::cout << ((frameCount - lastFrameCount) / (fpsTimer.GetElapsedMS() / 1000)) << std::endl;
2014-06-21 04:37:44 +00:00
lastFrameCount = frameCount;
2014-06-21 19:43:41 +00:00
fpsTimer.Reset();
}
if(_stop) {
break;
2014-06-21 04:37:44 +00:00
}
2014-06-14 22:20:56 +00:00
}
2014-06-14 15:27:55 +00:00
}
2014-06-22 12:38:42 +00:00
bool Console::RunTest(uint8_t *expectedResult)
2014-06-14 15:27:55 +00:00
{
Timer timer;
2014-06-22 12:38:42 +00:00
uint8_t maxWait = 30;
2014-06-14 22:20:56 +00:00
while(true) {
2014-06-16 01:45:36 +00:00
_cpu->Exec();
_ppu->Exec();
2014-06-22 12:38:42 +00:00
if(timer.GetElapsedMS() > 100) {
if(memcmp(_ppu->GetFrameBuffer(), expectedResult, 256 * 240 * 4)) {
return true;
}
timer.Reset();
2014-06-22 12:38:42 +00:00
maxWait--;
if(maxWait == 0) {
return false;
}
}
2014-06-14 22:20:56 +00:00
}
2014-06-22 12:38:42 +00:00
return false;
}
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
}