Skip to content
This repository has been archived by the owner on Oct 4, 2020. It is now read-only.

Commit

Permalink
UI: Added "reload rom" option and changed power cycle to not reload f…
Browse files Browse the repository at this point in the history
…rom disk
  • Loading branch information
SourMesen committed Nov 20, 2019
1 parent 5f7b231 commit 43811ae
Show file tree
Hide file tree
Showing 24 changed files with 108 additions and 70 deletions.
35 changes: 10 additions & 25 deletions Core/BaseMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,11 +433,6 @@ string BaseMapper::GetBatteryFilename()
{
return FolderUtilities::CombinePath(FolderUtilities::GetSaveFolder(), FolderUtilities::GetFilename(_romInfo.RomName, false) + ".sav");
}

void BaseMapper::RestoreOriginalPrgRam()
{
memcpy(_prgRom, _originalPrgRom.data(), _originalPrgRom.size());
}

void BaseMapper::InitializeChrRam(int32_t chrRamSize)
{
Expand Down Expand Up @@ -637,8 +632,6 @@ void BaseMapper::Initialize(RomData &romData)
//Load battery data if present
LoadBattery();

ApplyCheats();

_romInfo.HasChrRam = HasChrRam();
}

Expand All @@ -652,24 +645,6 @@ BaseMapper::~BaseMapper()
delete[] _nametableRam;
}

void BaseMapper::ProcessNotification(ConsoleNotificationType type, void* parameter)
{
switch(type) {
case ConsoleNotificationType::CheatAdded:
case ConsoleNotificationType::CheatRemoved:
ApplyCheats();
break;
default:
break;
}
}

void BaseMapper::ApplyCheats()
{
RestoreOriginalPrgRam();
_console->GetCheatManager()->ApplyPrgCodes(_prgRom, _prgSize);
}

void BaseMapper::GetMemoryRanges(MemoryRanges &ranges)
{
if(_romInfo.System == GameSystem::VsSystem) {
Expand Down Expand Up @@ -1267,4 +1242,14 @@ bool BaseMapper::HasPrgChrChanges()
}
}
return false;
}

void BaseMapper::CopyPrgChrRom(shared_ptr<BaseMapper> mapper)
{
if(_prgSize == mapper->_prgSize && _chrRomSize == mapper->_chrRomSize) {
memcpy(_prgRom, mapper->_prgRom, _prgSize);
if(!_onlyChrRam) {
memcpy(_chrRom, mapper->_chrRom, _chrRomSize);
}
}
}
7 changes: 2 additions & 5 deletions Core/BaseMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class BaseControlDevice;

class BaseMapper : public IMemoryHandler, public Snapshotable, public INotificationListener, public IBattery
class BaseMapper : public IMemoryHandler, public Snapshotable, public IBattery
{
private:
MirroringType _mirroringType;
Expand Down Expand Up @@ -135,7 +135,6 @@ class BaseMapper : public IMemoryHandler, public Snapshotable, public INotificat

void SetupDefaultWorkRam();

void RestoreOriginalPrgRam();
void InitializeChrRam(int32_t chrRamSize = -1);

void AddRegisterRange(uint16_t startAddr, uint16_t endAddr, MemoryOperation operation = MemoryOperation::Any);
Expand Down Expand Up @@ -167,11 +166,8 @@ class BaseMapper : public IMemoryHandler, public Snapshotable, public INotificat
virtual void SetNesModel(NesModel model) { }
virtual void ProcessCpuClock() { }
virtual void NotifyVRAMAddressChange(uint16_t addr);
void ProcessNotification(ConsoleNotificationType type, void* parameter) override;
virtual void GetMemoryRanges(MemoryRanges &ranges) override;

void ApplyCheats();

virtual void SaveBattery() override;

void SetConsole(shared_ptr<Console> console);
Expand Down Expand Up @@ -240,4 +236,5 @@ class BaseMapper : public IMemoryHandler, public Snapshotable, public INotificat
void RestorePrgChrBackup(vector<uint8_t>& backupData);
void RevertPrgChrChanges();
bool HasPrgChrChanges();
void CopyPrgChrRom(shared_ptr<BaseMapper> mapper);
};
29 changes: 17 additions & 12 deletions Core/CheatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "CheatManager.h"
#include "Console.h"
#include "BaseMapper.h"
#include "MessageManager.h"
#include "NotificationManager.h"

Expand Down Expand Up @@ -98,6 +99,7 @@ void CheatManager::AddCode(CodeInfo &code)
} else {
_absoluteCheatCodes.push_back(code);
}
_hasCode = true;
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::CheatAdded);
}

Expand Down Expand Up @@ -137,14 +139,19 @@ void CheatManager::ClearCodes()

cheatRemoved |= _absoluteCheatCodes.size() > 0;
_absoluteCheatCodes.clear();

_hasCode = false;

if(cheatRemoved) {
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::CheatRemoved);
}
}

void CheatManager::ApplyRamCodes(uint16_t addr, uint8_t &value)
void CheatManager::ApplyCodes(uint16_t addr, uint8_t &value)
{
if(!_hasCode) {
return;
}

if(_relativeCheatCodes[addr] != nullptr) {
for(uint32_t i = 0, len = i < _relativeCheatCodes[addr]->size(); i < len; i++) {
CodeInfo code = _relativeCheatCodes[addr]->at(i);
Expand All @@ -153,16 +160,14 @@ void CheatManager::ApplyRamCodes(uint16_t addr, uint8_t &value)
return;
}
}
}
}

void CheatManager::ApplyPrgCodes(uint8_t *prgRam, uint32_t prgSize)
{
for(uint32_t i = 0, len = i < _absoluteCheatCodes.size(); i < len; i++) {
CodeInfo code = _absoluteCheatCodes[i];
if(code.Address < prgSize) {
if(code.CompareValue == -1 || code.CompareValue == prgRam[code.Address]) {
prgRam[code.Address] = code.Value;
} else if(!_absoluteCheatCodes.empty()) {
int32_t absAddr = _console->GetMapper()->ToAbsoluteAddress(addr);
if(absAddr >= 0) {
for(CodeInfo &code : _absoluteCheatCodes) {
if(code.Address == (uint32_t)absAddr && (code.CompareValue == -1 || code.CompareValue == value)) {
value = code.Value;
return;
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Core/CheatManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CheatManager
private:
shared_ptr<Console> _console;

bool _hasCode = false;
vector<unique_ptr<vector<CodeInfo>>> _relativeCheatCodes;
vector<CodeInfo> _absoluteCheatCodes;

Expand All @@ -56,6 +57,5 @@ class CheatManager
void SetCheats(vector<CodeInfo> &cheats);
void SetCheats(CheatInfo cheats[], uint32_t length);

void ApplyRamCodes(uint16_t addr, uint8_t &value);
void ApplyPrgCodes(uint8_t *prgRam, uint32_t prgSize);
void ApplyCodes(uint16_t addr, uint8_t &value);
};
29 changes: 21 additions & 8 deletions Core/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ bool Console::Initialize(VirtualFile &romFile)
return Initialize(romFile, patchFile);
}

bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile)
bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile, bool forPowerCycle)
{
if(romFile.IsValid()) {
Pause();
Expand Down Expand Up @@ -306,14 +306,17 @@ bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile)
StopRecordingHdPack();
}

shared_ptr<BaseMapper> previousMapper = _mapper;
_mapper = mapper;
_memoryManager.reset(new MemoryManager(shared_from_this()));
_cpu.reset(new CPU(shared_from_this()));
_apu.reset(new APU(shared_from_this()));

_mapper->SetConsole(shared_from_this());
_mapper->Initialize(romData);
GetNotificationManager()->RegisterNotificationListener(_mapper);
if(!isDifferentGame && forPowerCycle) {
_mapper->CopyPrgChrRom(previousMapper);
}

if(_slave) {
_slave->Release(false);
Expand Down Expand Up @@ -400,8 +403,10 @@ bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile)
ResetComponents(false);

//Reset components before creating rewindmanager, otherwise the first save state it takes will be invalid
_rewindManager.reset(new RewindManager(shared_from_this()));
_notificationManager->RegisterNotificationListener(_rewindManager);
if(!forPowerCycle) {
_rewindManager.reset(new RewindManager(shared_from_this()));
_notificationManager->RegisterNotificationListener(_rewindManager);
}

//Poll controller input after creating rewind manager, to make sure it catches the first frame's input
_controlManager->UpdateInputState();
Expand All @@ -418,9 +423,12 @@ bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile)
FolderUtilities::AddKnownGameFolder(romFile.GetFolderPath());

if(IsMaster()) {
string modelName = _model == NesModel::PAL ? "PAL" : (_model == NesModel::Dendy ? "Dendy" : "NTSC");
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(GetRomInfo().RomName, false));
if(!forPowerCycle) {
string modelName = _model == NesModel::PAL ? "PAL" : (_model == NesModel::Dendy ? "Dendy" : "NTSC");
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(GetRomInfo().RomName, false));
}

_settings->ClearFlags(EmulationFlags::ForceMaxSpeed);

if(_slave) {
Expand Down Expand Up @@ -565,11 +573,16 @@ shared_ptr<SystemActionManager> Console::GetSystemActionManager()
}

void Console::PowerCycle()
{
ReloadRom(true);
}

void Console::ReloadRom(bool forPowerCycle)
{
if(_initialized && !_romFilepath.empty()) {
VirtualFile romFile = _romFilepath;
VirtualFile patchFile = _patchFilename;
Initialize(romFile, patchFile);
Initialize(romFile, patchFile, forPowerCycle);
}
}

Expand Down
3 changes: 2 additions & 1 deletion Core/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Console : public std::enable_shared_from_this<Console>

bool Initialize(string romFile, string patchFile = "");
bool Initialize(VirtualFile &romFile);
bool Initialize(VirtualFile &romFile, VirtualFile &patchFile);
bool Initialize(VirtualFile &romFile, VirtualFile &patchFile, bool forPowerCycle = false);

void SaveBatteries();

Expand Down Expand Up @@ -179,6 +179,7 @@ class Console : public std::enable_shared_from_this<Console>

void Reset(bool softReset = true);
void PowerCycle();
void ReloadRom(bool forPowerCycle = false);
void ResetComponents(bool softReset);

//Used to pause the emu loop to perform thread-safe operations
Expand Down
1 change: 1 addition & 0 deletions Core/EmulationSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ enum class EmulatorShortcut
Pause,
Reset,
PowerCycle,
ReloadRom,
PowerOff,
Exit,

Expand Down
4 changes: 2 additions & 2 deletions Core/MemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ uint8_t MemoryManager::DebugRead(uint16_t addr, bool disableSideEffects)
}
}

_console->GetCheatManager()->ApplyRamCodes(addr, value);
_console->GetCheatManager()->ApplyCodes(addr, value);

return value;
}
Expand All @@ -114,7 +114,7 @@ uint16_t MemoryManager::DebugReadWord(uint16_t addr)
uint8_t MemoryManager::Read(uint16_t addr, MemoryOperationType operationType)
{
uint8_t value = _ramReadHandlers[addr]->ReadRAM(addr);
_console->GetCheatManager()->ApplyRamCodes(addr, value);
_console->GetCheatManager()->ApplyCodes(addr, value);
_console->DebugProcessRamOperation(operationType, addr, value);

_openBusHandler.SetOpenBus(value);
Expand Down
5 changes: 3 additions & 2 deletions Docs/content/configuration/Preferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ Available shortcuts:
* **Rewind 10 seconds**: Instantly rewinds 10 seconds of gameplay.
* **Rewind 1 minute**: Instantly rewinds 1 minute of gameplay.
* **Pause**: Pauses or unpauses the game.
* **Reset**: Resets the game.
* **Power Cycle**: Power cycles the game and reloads the file from disk.
* **Reset**: Resets the game (equivalent to pressing the reset button on the NES.)
* **Power Cycle**: Power cycles the game (equivalent to turning the power off and then back on.)
* **Reload ROM**: Reloads the ROM from the disk and power cycles the console.
* **Power Off**: Powers off the game, returning to the game selection screen.
* **Exit**: Exits the emulator.
* **FDS - Insert Next Disk**: Inserts face A of the next disk.
Expand Down
2 changes: 2 additions & 0 deletions GUI.NET/Dependencies/resources.ca.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Control ID="mnuPause">Pausa</Control>
<Control ID="mnuReset">Reinicia el joc</Control>
<Control ID="mnuPowerCycle">Atura i reinicia el joc</Control>
<Control ID="mnuReloadRom">Reload ROM</Control>
<Control ID="mnuPowerOff">Atura el joc</Control>
<Control ID="mnuSwitchDiskSide">Canvia la cara del disc</Control>
<Control ID="mnuSelectDisk">Escull el disc</Control>
Expand Down Expand Up @@ -856,6 +857,7 @@
<Message ID="EmulatorShortcutMappings_Pause">Pausa</Message>
<Message ID="EmulatorShortcutMappings_Reset">Reinicia el joc</Message>
<Message ID="EmulatorShortcutMappings_PowerCycle">Atura i reinicia el joc</Message>
<Message ID="EmulatorShortcutMappings_ReloadRom">Reload ROM</Message>
<Message ID="EmulatorShortcutMappings_PowerOff">Atura el joc</Message>
<Message ID="EmulatorShortcutMappings_Exit">Surt</Message>
<Message ID="EmulatorShortcutMappings_TakeScreenshot">Captura de pantalla</Message>
Expand Down
2 changes: 2 additions & 0 deletions GUI.NET/Dependencies/resources.en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Control ID="mnuPause">Pause</Control>
<Control ID="mnuReset">Reset</Control>
<Control ID="mnuPowerCycle">Power Cycle</Control>
<Control ID="mnuReloadRom">Reload ROM</Control>
<Control ID="mnuPowerOff">Power Off</Control>
<Control ID="mnuSwitchDiskSide">Switch Disk Side</Control>
<Control ID="mnuSelectDisk">Select Disk</Control>
Expand Down Expand Up @@ -888,6 +889,7 @@
<Message ID="EmulatorShortcutMappings_Pause">Pause</Message>
<Message ID="EmulatorShortcutMappings_Reset">Reset</Message>
<Message ID="EmulatorShortcutMappings_PowerCycle">Power Cycle</Message>
<Message ID="EmulatorShortcutMappings_ReloadRom">Reload ROM</Message>
<Message ID="EmulatorShortcutMappings_PowerOff">Power Off</Message>
<Message ID="EmulatorShortcutMappings_Exit">Exit</Message>
<Message ID="EmulatorShortcutMappings_TakeScreenshot">Take Screenshot</Message>
Expand Down
2 changes: 2 additions & 0 deletions GUI.NET/Dependencies/resources.es.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Control ID="mnuPause">Pausa</Control>
<Control ID="mnuReset">Reiniciar</Control>
<Control ID="mnuPowerCycle">Detener y reiniciar</Control>
<Control ID="mnuReloadRom">Reload ROM</Control>
<Control ID="mnuPowerOff">Detener el juego</Control>
<Control ID="mnuSwitchDiskSide">Cambiar la cara del disco</Control>
<Control ID="mnuSelectDisk">Elegir el disco</Control>
Expand Down Expand Up @@ -873,6 +874,7 @@
<Message ID="EmulatorShortcutMappings_Pause">Pausa</Message>
<Message ID="EmulatorShortcutMappings_Reset">Reiniciar</Message>
<Message ID="EmulatorShortcutMappings_PowerCycle">Detener y reiniciar</Message>
<Message ID="EmulatorShortcutMappings_ReloadRom">Reload ROM</Message>
<Message ID="EmulatorShortcutMappings_PowerOff">Detener el juego</Message>
<Message ID="EmulatorShortcutMappings_Exit">Salir</Message>
<Message ID="EmulatorShortcutMappings_TakeScreenshot">Captura de pantalla</Message>
Expand Down
2 changes: 2 additions & 0 deletions GUI.NET/Dependencies/resources.fr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Control ID="mnuPause">Pause</Control>
<Control ID="mnuReset">Reset</Control>
<Control ID="mnuPowerCycle">Arrêt &amp;&amp; redémarrage</Control>
<Control ID="mnuReloadRom">Recharger le ROM à partir du disque</Control>
<Control ID="mnuPowerOff">Arrêter le jeu</Control>
<Control ID="mnuSwitchDiskSide">Changer le disque de côté</Control>
<Control ID="mnuSelectDisk">Choisir le disque</Control>
Expand Down Expand Up @@ -887,6 +888,7 @@
<Message ID="EmulatorShortcutMappings_Pause">Pause</Message>
<Message ID="EmulatorShortcutMappings_Reset">Reset</Message>
<Message ID="EmulatorShortcutMappings_PowerCycle">Arrêt &amp; redémarrage</Message>
<Message ID="EmulatorShortcutMappings_ReloadRom">Recharger le ROM à partir du disque</Message>
<Message ID="EmulatorShortcutMappings_PowerOff">Arrêter le jeu</Message>
<Message ID="EmulatorShortcutMappings_Exit">Quitter</Message>
<Message ID="EmulatorShortcutMappings_TakeScreenshot">Capture d'écran</Message>
Expand Down
2 changes: 2 additions & 0 deletions GUI.NET/Dependencies/resources.it.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Control ID="mnuPause">Pausa</Control>
<Control ID="mnuReset">Reset</Control>
<Control ID="mnuPowerCycle">Spegni/Riaccendi</Control>
<Control ID="mnuReloadRom">Reload ROM</Control>
<Control ID="mnuPowerOff">Spegni</Control>
<Control ID="mnuSwitchDiskSide">Cambia Lato Disco</Control>
<Control ID="mnuSelectDisk">Seleziona Disco</Control>
Expand Down Expand Up @@ -888,6 +889,7 @@
<Message ID="EmulatorShortcutMappings_Pause">Pausa</Message>
<Message ID="EmulatorShortcutMappings_Reset">Reset</Message>
<Message ID="EmulatorShortcutMappings_PowerCycle">Spegni e Riaccendi</Message>
<Message ID="EmulatorShortcutMappings_ReloadRom">Reload ROM</Message>
<Message ID="EmulatorShortcutMappings_PowerOff">Spegni</Message>
<Message ID="EmulatorShortcutMappings_Exit">Esci</Message>
<Message ID="EmulatorShortcutMappings_TakeScreenshot">Fai uno Screenshot</Message>
Expand Down
Loading

0 comments on commit 43811ae

Please sign in to comment.