Reset / power cycle

This commit is contained in:
Sour 2021-04-15 21:32:04 -04:00
parent 2029191669
commit 7293249cdd
20 changed files with 133 additions and 129 deletions

View file

@ -55,6 +55,7 @@
<ClInclude Include="MemoryOperationType.h" />
<ClInclude Include="Shared\Interfaces\stdafx.h" />
<ClInclude Include="Shared\Movies\stdafx.h" />
<ClInclude Include="Shared\RomInfo.h" />
<ClInclude Include="Shared\stdafx.h" />
<ClInclude Include="Shared\Video\NesDefaultVideoFilter.h" />
<ClInclude Include="NES\APU\ApuEnvelope.h" />

View file

@ -733,6 +733,7 @@
<ClInclude Include="SNES\stdafx.h" />
<ClInclude Include="Netplay\stdafx.h" />
<ClInclude Include="Shared\BaseControlManager.h" />
<ClInclude Include="Shared\RomInfo.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="SNES\Cpu.cpp">

View file

@ -3,6 +3,7 @@
#include "Shared/Emulator.h"
#include "Shared/NotificationManager.h"
#include "Utilities/FolderUtilities.h"
#include "SNES/CartTypes.h"
struct MissingFirmwareMessage
{

View file

@ -348,13 +348,15 @@ void Gameboy::Stop()
void Gameboy::Reset()
{
//The GB has no reset button, behave like power cycle
_emu->ReloadRom(true);
}
void Gameboy::OnBeforeRun()
{
}
bool Gameboy::LoadRom(VirtualFile& romFile, VirtualFile& patchFile)
bool Gameboy::LoadRom(VirtualFile& romFile)
{
vector<uint8_t> romData;
romFile.ReadFile(romData);
@ -427,12 +429,6 @@ double Gameboy::GetFps()
return 59.72750056960583;
}
RomInfo Gameboy::GetRomInfo()
{
//TODO
return RomInfo();
}
void Gameboy::RunSingleFrame()
{
//TODO

View file

@ -101,14 +101,13 @@ public:
virtual void Stop() override;
virtual void Reset() override;
virtual void OnBeforeRun() override;
virtual bool LoadRom(VirtualFile& romFile, VirtualFile& patchFile) override;
virtual bool LoadRom(VirtualFile& romFile) override;
virtual void Init() override;
virtual void RunFrame() override;
virtual shared_ptr<IControlManager> GetControlManager() override;
virtual ConsoleType GetConsoleType() override;
virtual double GetFrameDelay() override;
virtual double GetFps() override;
virtual RomInfo GetRomInfo() override;
virtual void RunSingleFrame() override;
virtual PpuFrameInfo GetPpuFrame() override;
virtual vector<CpuType> GetCpuTypes() override;

View file

@ -11,6 +11,7 @@
#include "Shared/CheatManager.h"
#include "Shared/BatteryManager.h"
#include "Shared/EmuSettings.h"
#include "Shared/RomInfo.h"
#include "Utilities/FolderUtilities.h"
#include "Utilities/Patches/IpsPatcher.h"
#include "Utilities/Serializer.h"
@ -1158,7 +1159,7 @@ void BaseMapper::GetRomFileData(vector<uint8_t> &out, bool asIpsFile, uint8_t* h
//Get edited rom
if(asIpsFile) {
vector<uint8_t> originalFile;
_console->GetRomInfo().RomFile.ReadFile(originalFile);
_emu->GetRomInfo().RomFile.ReadFile(originalFile);
vector<uint8_t> patchData = IpsPatcher::CreatePatch(originalFile, newFile);
out.insert(out.end(), patchData.begin(), patchData.end());

View file

@ -78,9 +78,10 @@ void NesConsole::OnBeforeRun()
//TODO
}
bool NesConsole::LoadRom(VirtualFile& romFile, VirtualFile& patchFile)
bool NesConsole::LoadRom(VirtualFile& romFile)
{
RomData romData;
shared_ptr<BaseMapper> mapper = MapperFactory::InitializeFromFile(shared_from_this(), romFile, romData);
if(mapper) {
shared_ptr<BaseMapper> previousMapper = _mapper;
@ -206,12 +207,6 @@ double NesConsole::GetFps()
return 60;
}
RomInfo NesConsole::GetRomInfo()
{
//TODO
return RomInfo();
}
void NesConsole::RunSingleFrame()
{
//TODO

View file

@ -63,13 +63,12 @@ public:
virtual void Stop() override;
virtual void Reset() override;
virtual void OnBeforeRun() override;
virtual bool LoadRom(VirtualFile& romFile, VirtualFile& patchFile) override;
virtual bool LoadRom(VirtualFile& romFile) override;
virtual void Init() override;
virtual void RunFrame() override;
virtual shared_ptr<IControlManager> GetControlManager() override;
virtual double GetFrameDelay() override;
virtual double GetFps() override;
virtual RomInfo GetRomInfo() override;
virtual void RunSingleFrame() override;
virtual PpuFrameInfo GetPpuFrame() override;
virtual ConsoleType GetConsoleType() override;

View file

@ -23,6 +23,7 @@
#include "Shared/BatteryManager.h"
#include "Shared/MessageManager.h"
#include "Shared/Emulator.h"
#include "Shared/RomInfo.h"
#include "Utilities/HexUtilities.h"
#include "Utilities/VirtualFile.h"
#include "Utilities/FolderUtilities.h"
@ -39,16 +40,10 @@ BaseCartridge::~BaseCartridge()
delete[] _saveRam;
}
shared_ptr<BaseCartridge> BaseCartridge::CreateCartridge(Console* console, VirtualFile &romFile, VirtualFile &patchFile)
shared_ptr<BaseCartridge> BaseCartridge::CreateCartridge(Console* console, VirtualFile &romFile)
{
if(romFile.IsValid()) {
shared_ptr<BaseCartridge> cart(new BaseCartridge());
if(patchFile.IsValid()) {
cart->_patchPath = patchFile;
if(romFile.ApplyPatch(patchFile)) {
MessageManager::DisplayMessage("Patch", "ApplyingPatch", patchFile.GetFileName());
}
}
vector<uint8_t> romData;
romFile.ReadFile(romData);
@ -68,7 +63,7 @@ shared_ptr<BaseCartridge> BaseCartridge::CreateCartridge(Console* console, Virtu
return nullptr;
}
} else if(fileExt == ".gb" || fileExt == ".gbc") {
if(cart->LoadGameboy(romFile, patchFile)) {
if(cart->LoadGameboy(romFile)) {
return cart;
} else {
return nullptr;
@ -324,18 +319,19 @@ void BaseCartridge::Reset()
RomInfo BaseCartridge::GetRomInfo()
{
RomInfo info;
info.Header = _cartInfo;
info.HeaderOffset = _headerOffset;
//TODO
//info.Header = _cartInfo;
//info.HeaderOffset = _headerOffset;
//info.Coprocessor = _coprocessorType;
info.RomFile = static_cast<VirtualFile>(_romPath);
info.PatchFile = static_cast<VirtualFile>(_patchPath);
info.Coprocessor = _coprocessorType;
return info;
}
vector<uint8_t> BaseCartridge::GetOriginalPrgRom()
{
RomInfo romInfo = GetRomInfo();
shared_ptr<BaseCartridge> originalCart = BaseCartridge::CreateCartridge(_console, romInfo.RomFile, romInfo.PatchFile);
//TODO
shared_ptr<BaseCartridge> originalCart = BaseCartridge::CreateCartridge(_console, romInfo.RomFile);
if(originalCart->_gameboy) {
uint8_t* orgPrgRom = originalCart->_gameboy->DebugGetMemory(SnesMemoryType::GbPrgRom);
uint32_t orgRomSize = originalCart->_gameboy->DebugGetMemorySize(SnesMemoryType::GbPrgRom);
@ -622,7 +618,7 @@ void BaseCartridge::LoadSpc()
SetupCpuHalt();
}
bool BaseCartridge::LoadGameboy(VirtualFile& romFile, VirtualFile& patchFile)
bool BaseCartridge::LoadGameboy(VirtualFile& romFile)
{
_cartInfo = { };
_headerOffset = Gameboy::HeaderOffset;
@ -633,7 +629,7 @@ bool BaseCartridge::LoadGameboy(VirtualFile& romFile, VirtualFile& patchFile)
LoadRom();
if(_coprocessorType == CoprocessorType::SGB) {
_gameboy.reset(new Gameboy(_emu, true));
if(_gameboy->LoadRom(romFile, patchFile)) {
if(_gameboy->LoadRom(romFile)) {
return _gameboy->IsSgb();
}
}

View file

@ -4,6 +4,7 @@
#include "CartTypes.h"
#include "Coprocessors/BaseCoprocessor.h"
#include "Utilities/ISerializable.h"
#include "Shared/RomInfo.h"
class MemoryMappings;
class VirtualFile;
@ -49,7 +50,6 @@ private:
bool _hasBattery = false;
bool _hasRtc = false;
string _romPath;
string _patchPath;
uint8_t* _prgRom = nullptr;
uint8_t* _saveRam = nullptr;
@ -76,7 +76,7 @@ private:
void LoadRom();
void LoadSpc();
bool LoadGameboy(VirtualFile& romFile, VirtualFile& patchFile);
bool LoadGameboy(VirtualFile& romFile);
void SetupCpuHalt();
void InitCoprocessor();
void LoadEmbeddedFirmware();
@ -87,7 +87,7 @@ private:
public:
virtual ~BaseCartridge();
static shared_ptr<BaseCartridge> CreateCartridge(Console* console, VirtualFile &romFile, VirtualFile &patchFile);
static shared_ptr<BaseCartridge> CreateCartridge(Console* console, VirtualFile &romFile);
void Reset();

View file

@ -68,15 +68,6 @@ enum class FirmwareType
SGB2
};
struct RomInfo
{
SnesCartInformation Header;
uint32_t HeaderOffset;
VirtualFile RomFile;
VirtualFile PatchFile;
CoprocessorType Coprocessor;
};
namespace CartFlags
{
enum CartFlags

View file

@ -59,7 +59,9 @@ void Console::RunFrame()
void Console::OnBeforeRun()
{
_memoryManager->IncMasterClockStartup();
_controlManager->UpdateInputState();
//TODO?
//_controlManager->UpdateInputState();
}
void Console::ProcessEndOfFrame()
@ -72,8 +74,8 @@ void Console::ProcessEndOfFrame()
_emu->ProcessEndOfFrame();
_controlManager->UpdateInputState();
_controlManager->UpdateControlDevices();
_controlManager->UpdateInputState();
_internalRegisters->ProcessAutoJoypadRead();
#endif
_frameRunning = false;
@ -132,11 +134,11 @@ void Console::Reset()
}
}
bool Console::LoadRom(VirtualFile& romFile, VirtualFile& patchFile)
bool Console::LoadRom(VirtualFile& romFile)
{
bool result = false;
EmulationConfig orgConfig = _settings->GetEmulationConfig(); //backup emulation config (can be temporarily overriden to control the power on RAM state)
shared_ptr<BaseCartridge> cart = BaseCartridge::CreateCartridge(this, romFile, patchFile);
shared_ptr<BaseCartridge> cart = BaseCartridge::CreateCartridge(this, romFile);
if(cart) {
_cart = cart;
@ -180,16 +182,6 @@ void Console::Init()
{
}
RomInfo Console::GetRomInfo()
{
shared_ptr<BaseCartridge> cart = _cart;
if(cart) {
return cart->GetRomInfo();
} else {
return {};
}
}
uint64_t Console::GetMasterClock()
{
return _memoryManager->GetMasterClock();

View file

@ -7,6 +7,7 @@
#include "Utilities/Timer.h"
#include "Utilities/VirtualFile.h"
#include "Utilities/SimpleLock.h"
#include "Shared/RomInfo.h"
class Cpu;
class Ppu;
@ -81,10 +82,9 @@ public:
void ProcessEndOfFrame();
bool LoadRom(VirtualFile& romFile, VirtualFile& patchFile) override;
bool LoadRom(VirtualFile& romFile) override;
void Init() override;
RomInfo GetRomInfo() override;
uint64_t GetMasterClock() override;
uint32_t GetMasterClockRate();
ConsoleRegion GetRegion();

View file

@ -29,8 +29,8 @@ public:
BaseControlManager(Emulator* emu);
virtual ~BaseControlManager();
virtual void UpdateControlDevices() {}
virtual void UpdateInputState();
void UpdateControlDevices() override {}
void UpdateInputState() override;
uint32_t GetPollCounter() override;
void SetPollCounter(uint32_t value) override;

View file

@ -292,6 +292,7 @@ void Emulator::Reset()
_runLock.Acquire();
_console->Reset();
GetControlManager()->UpdateInputState();
_notificationManager->SendNotification(ConsoleNotificationType::GameReset);
ProcessEvent(EventType::Reset);
@ -325,6 +326,18 @@ void Emulator::PowerCycle()
bool Emulator::LoadRom(VirtualFile romFile, VirtualFile patchFile, bool stopRom, bool forPowerCycle)
{
if(!romFile.IsValid()) {
return false;
}
if(patchFile.IsValid()) {
//TODO
//_patchPath = patchFile;
if(romFile.ApplyPatch(patchFile)) {
MessageManager::DisplayMessage("Patch", "ApplyingPatch", patchFile.GetFileName());
}
}
if(_console) {
//Make sure the battery is saved to disk before we load another game (or reload the same game)
//TODO
@ -336,81 +349,87 @@ bool Emulator::LoadRom(VirtualFile romFile, VirtualFile patchFile, bool stopRom,
memset(_consoleMemory, 0, sizeof(_consoleMemory));
shared_ptr<IConsole> console = shared_ptr<IConsole>(new NesConsole(this));
if(!console->LoadRom(romFile, patchFile)) {
if(!console->LoadRom(romFile)) {
memset(_consoleMemory, 0, sizeof(_consoleMemory));
console.reset(new Console(this));
if(!console->LoadRom(romFile, patchFile)) {
if(!console->LoadRom(romFile)) {
memset(_consoleMemory, 0, sizeof(_consoleMemory));
console.reset(new Gameboy(this, false));
if(!console->LoadRom(romFile, patchFile)) {
if(!console->LoadRom(romFile)) {
memset(_consoleMemory, 0, sizeof(_consoleMemory));
MessageManager::DisplayMessage("Error", "CouldNotLoadFile", romFile.GetFileName());
//TODO
_settings->SetEmulationConfig(orgConfig);
return false;
}
}
}
if(console) {
bool debuggerActive = _debugger != nullptr;
if(stopRom) {
KeyManager::UpdateDevices();
Stop(false);
}
_romFile = romFile;
_patchFile = patchFile;
_cheatManager->ClearCheats(false);
auto lock = _debuggerLock.AcquireSafe();
if(_debugger) {
//Reset debugger if it was running before
_debugger->Release();
_debugger.reset();
}
_batteryManager->Initialize(FolderUtilities::GetFilename(romFile.GetFileName(), false));
//TODO
//UpdateRegion();
_console = console;
console->Init();
if(debuggerActive) {
GetDebugger();
}
_rewindManager.reset(new RewindManager(shared_from_this()));
_notificationManager->RegisterNotificationListener(_rewindManager);
//TODO
GetControlManager()->UpdateControlDevices();
//UpdateRegion();
_notificationManager->SendNotification(ConsoleNotificationType::GameLoaded, (void*)forPowerCycle);
_paused = false;
if(!forPowerCycle) {
string modelName = _region == ConsoleRegion::Pal ? "PAL" : "NTSC";
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(GetRomInfo().RomFile.GetFileName(), false));
}
if(stopRom) {
#ifndef LIBRETRO
_emuThread.reset(new thread(&Emulator::Run, this));
#endif
}
result = true;
} else {
MessageManager::DisplayMessage("Error", "CouldNotLoadFile", romFile.GetFileName());
bool debuggerActive = _debugger != nullptr;
if(stopRom) {
KeyManager::UpdateDevices();
Stop(false);
}
_settings->SetEmulationConfig(orgConfig);
_cheatManager->ClearCheats(false);
auto lock = _debuggerLock.AcquireSafe();
if(_debugger) {
//Reset debugger if it was running before
_debugger->Release();
_debugger.reset();
}
_batteryManager->Initialize(FolderUtilities::GetFilename(romFile.GetFileName(), false));
//TODO
//UpdateRegion();
_console = console;
console->Init();
if(debuggerActive) {
GetDebugger();
}
_rewindManager.reset(new RewindManager(shared_from_this()));
_notificationManager->RegisterNotificationListener(_rewindManager);
//TODO
GetControlManager()->UpdateControlDevices();
GetControlManager()->UpdateInputState();
//UpdateRegion();
_notificationManager->SendNotification(ConsoleNotificationType::GameLoaded, (void*)forPowerCycle);
_paused = false;
if(!forPowerCycle) {
string modelName = _region == ConsoleRegion::Pal ? "PAL" : "NTSC";
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(GetRomInfo().RomFile.GetFileName(), false));
}
if(stopRom) {
#ifndef LIBRETRO
_emuThread.reset(new thread(&Emulator::Run, this));
#endif
}
result = true;
return result;
}
RomInfo Emulator::GetRomInfo()
{
return _console->GetRomInfo();
RomInfo romInfo = {};
romInfo.RomFile = _romFile;
romInfo.PatchFile = _patchFile;
return romInfo;
}
string Emulator::GetHash(HashType type)

View file

@ -81,6 +81,9 @@ private:
atomic<bool> _isRunAheadFrame;
bool _frameRunning = false;
string _romFile;
string _patchFile;
ConsoleMemoryInfo _consoleMemory[(int)SnesMemoryType::Register] = {};
unique_ptr<DebugStats> _stats;

View file

@ -2,7 +2,7 @@
#include "stdafx.h"
#include "Utilities/ISerializable.h"
#include "Core/Debugger/DebugTypes.h"
#include "Core/SNES/CartTypes.h"
#include "Shared/RomInfo.h"
class IControlManager;
class VirtualFile;
@ -25,7 +25,7 @@ public:
virtual void OnBeforeRun() = 0;
virtual bool LoadRom(VirtualFile& romFile, VirtualFile& patchFile) = 0;
virtual bool LoadRom(VirtualFile& romFile) = 0;
virtual void Init() = 0;
//virtual void RunFrameWithRunAhead() = 0;
@ -41,8 +41,6 @@ public:
virtual double GetFrameDelay() = 0;
virtual double GetFps() = 0;
virtual RomInfo GetRomInfo() = 0;
virtual void RunSingleFrame() = 0;
virtual PpuFrameInfo GetPpuFrame() = 0;

View file

@ -17,4 +17,5 @@ public:
virtual void SetPollCounter(uint32_t pollCounter) = 0;
virtual uint32_t GetPollCounter() = 0;
virtual void UpdateControlDevices() = 0;
virtual void UpdateInputState() = 0;
};

9
Core/Shared/RomInfo.h Normal file
View file

@ -0,0 +1,9 @@
#pragma once
#include "stdafx.h"
#include "Utilities/VirtualFile.h"
struct RomInfo
{
VirtualFile RomFile;
VirtualFile PatchFile;
};

View file

@ -42,8 +42,9 @@ struct InteropRomInfo
{
const char* RomPath;
const char* PatchPath;
CoprocessorType Coprocessor;
SnesCartInformation Header;
//TODO
//CoprocessorType Coprocessor;
//SnesCartInformation Header;
char Sha1[40];
};
@ -126,10 +127,11 @@ extern "C" {
_romPath = romInfo.RomFile;
_patchPath = romInfo.PatchFile;
info.Header = romInfo.Header;
info.RomPath = _romPath.c_str();
info.PatchPath = _patchPath.c_str();
info.Coprocessor = romInfo.Coprocessor;
//TODO
//info.Header = romInfo.Header;
//info.Coprocessor = romInfo.Coprocessor;
memcpy(info.Sha1, sha1.c_str(), sha1.size());
}