Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 43811ae

Browse files
committed
UI: Added "reload rom" option and changed power cycle to not reload from disk
1 parent 5f7b231 commit 43811ae

24 files changed

+108
-70
lines changed

Core/BaseMapper.cpp

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,6 @@ string BaseMapper::GetBatteryFilename()
433433
{
434434
return FolderUtilities::CombinePath(FolderUtilities::GetSaveFolder(), FolderUtilities::GetFilename(_romInfo.RomName, false) + ".sav");
435435
}
436-
437-
void BaseMapper::RestoreOriginalPrgRam()
438-
{
439-
memcpy(_prgRom, _originalPrgRom.data(), _originalPrgRom.size());
440-
}
441436

442437
void BaseMapper::InitializeChrRam(int32_t chrRamSize)
443438
{
@@ -637,8 +632,6 @@ void BaseMapper::Initialize(RomData &romData)
637632
//Load battery data if present
638633
LoadBattery();
639634

640-
ApplyCheats();
641-
642635
_romInfo.HasChrRam = HasChrRam();
643636
}
644637

@@ -652,24 +645,6 @@ BaseMapper::~BaseMapper()
652645
delete[] _nametableRam;
653646
}
654647

655-
void BaseMapper::ProcessNotification(ConsoleNotificationType type, void* parameter)
656-
{
657-
switch(type) {
658-
case ConsoleNotificationType::CheatAdded:
659-
case ConsoleNotificationType::CheatRemoved:
660-
ApplyCheats();
661-
break;
662-
default:
663-
break;
664-
}
665-
}
666-
667-
void BaseMapper::ApplyCheats()
668-
{
669-
RestoreOriginalPrgRam();
670-
_console->GetCheatManager()->ApplyPrgCodes(_prgRom, _prgSize);
671-
}
672-
673648
void BaseMapper::GetMemoryRanges(MemoryRanges &ranges)
674649
{
675650
if(_romInfo.System == GameSystem::VsSystem) {
@@ -1267,4 +1242,14 @@ bool BaseMapper::HasPrgChrChanges()
12671242
}
12681243
}
12691244
return false;
1245+
}
1246+
1247+
void BaseMapper::CopyPrgChrRom(shared_ptr<BaseMapper> mapper)
1248+
{
1249+
if(_prgSize == mapper->_prgSize && _chrRomSize == mapper->_chrRomSize) {
1250+
memcpy(_prgRom, mapper->_prgRom, _prgSize);
1251+
if(!_onlyChrRam) {
1252+
memcpy(_chrRom, mapper->_chrRom, _chrRomSize);
1253+
}
1254+
}
12701255
}

Core/BaseMapper.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class BaseControlDevice;
1414

15-
class BaseMapper : public IMemoryHandler, public Snapshotable, public INotificationListener, public IBattery
15+
class BaseMapper : public IMemoryHandler, public Snapshotable, public IBattery
1616
{
1717
private:
1818
MirroringType _mirroringType;
@@ -135,7 +135,6 @@ class BaseMapper : public IMemoryHandler, public Snapshotable, public INotificat
135135

136136
void SetupDefaultWorkRam();
137137

138-
void RestoreOriginalPrgRam();
139138
void InitializeChrRam(int32_t chrRamSize = -1);
140139

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

173-
void ApplyCheats();
174-
175171
virtual void SaveBattery() override;
176172

177173
void SetConsole(shared_ptr<Console> console);
@@ -240,4 +236,5 @@ class BaseMapper : public IMemoryHandler, public Snapshotable, public INotificat
240236
void RestorePrgChrBackup(vector<uint8_t>& backupData);
241237
void RevertPrgChrChanges();
242238
bool HasPrgChrChanges();
239+
void CopyPrgChrRom(shared_ptr<BaseMapper> mapper);
243240
};

Core/CheatManager.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "CheatManager.h"
44
#include "Console.h"
5+
#include "BaseMapper.h"
56
#include "MessageManager.h"
67
#include "NotificationManager.h"
78

@@ -98,6 +99,7 @@ void CheatManager::AddCode(CodeInfo &code)
9899
} else {
99100
_absoluteCheatCodes.push_back(code);
100101
}
102+
_hasCode = true;
101103
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::CheatAdded);
102104
}
103105

@@ -137,14 +139,19 @@ void CheatManager::ClearCodes()
137139

138140
cheatRemoved |= _absoluteCheatCodes.size() > 0;
139141
_absoluteCheatCodes.clear();
140-
142+
_hasCode = false;
143+
141144
if(cheatRemoved) {
142145
_console->GetNotificationManager()->SendNotification(ConsoleNotificationType::CheatRemoved);
143146
}
144147
}
145148

146-
void CheatManager::ApplyRamCodes(uint16_t addr, uint8_t &value)
149+
void CheatManager::ApplyCodes(uint16_t addr, uint8_t &value)
147150
{
151+
if(!_hasCode) {
152+
return;
153+
}
154+
148155
if(_relativeCheatCodes[addr] != nullptr) {
149156
for(uint32_t i = 0, len = i < _relativeCheatCodes[addr]->size(); i < len; i++) {
150157
CodeInfo code = _relativeCheatCodes[addr]->at(i);
@@ -153,16 +160,14 @@ void CheatManager::ApplyRamCodes(uint16_t addr, uint8_t &value)
153160
return;
154161
}
155162
}
156-
}
157-
}
158-
159-
void CheatManager::ApplyPrgCodes(uint8_t *prgRam, uint32_t prgSize)
160-
{
161-
for(uint32_t i = 0, len = i < _absoluteCheatCodes.size(); i < len; i++) {
162-
CodeInfo code = _absoluteCheatCodes[i];
163-
if(code.Address < prgSize) {
164-
if(code.CompareValue == -1 || code.CompareValue == prgRam[code.Address]) {
165-
prgRam[code.Address] = code.Value;
163+
} else if(!_absoluteCheatCodes.empty()) {
164+
int32_t absAddr = _console->GetMapper()->ToAbsoluteAddress(addr);
165+
if(absAddr >= 0) {
166+
for(CodeInfo &code : _absoluteCheatCodes) {
167+
if(code.Address == (uint32_t)absAddr && (code.CompareValue == -1 || code.CompareValue == value)) {
168+
value = code.Value;
169+
return;
170+
}
166171
}
167172
}
168173
}

Core/CheatManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CheatManager
3636
private:
3737
shared_ptr<Console> _console;
3838

39+
bool _hasCode = false;
3940
vector<unique_ptr<vector<CodeInfo>>> _relativeCheatCodes;
4041
vector<CodeInfo> _absoluteCheatCodes;
4142

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

59-
void ApplyRamCodes(uint16_t addr, uint8_t &value);
60-
void ApplyPrgCodes(uint8_t *prgRam, uint32_t prgSize);
60+
void ApplyCodes(uint16_t addr, uint8_t &value);
6161
};

Core/Console.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ bool Console::Initialize(VirtualFile &romFile)
255255
return Initialize(romFile, patchFile);
256256
}
257257

258-
bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile)
258+
bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile, bool forPowerCycle)
259259
{
260260
if(romFile.IsValid()) {
261261
Pause();
@@ -306,14 +306,17 @@ bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile)
306306
StopRecordingHdPack();
307307
}
308308

309+
shared_ptr<BaseMapper> previousMapper = _mapper;
309310
_mapper = mapper;
310311
_memoryManager.reset(new MemoryManager(shared_from_this()));
311312
_cpu.reset(new CPU(shared_from_this()));
312313
_apu.reset(new APU(shared_from_this()));
313314

314315
_mapper->SetConsole(shared_from_this());
315316
_mapper->Initialize(romData);
316-
GetNotificationManager()->RegisterNotificationListener(_mapper);
317+
if(!isDifferentGame && forPowerCycle) {
318+
_mapper->CopyPrgChrRom(previousMapper);
319+
}
317320

318321
if(_slave) {
319322
_slave->Release(false);
@@ -400,8 +403,10 @@ bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile)
400403
ResetComponents(false);
401404

402405
//Reset components before creating rewindmanager, otherwise the first save state it takes will be invalid
403-
_rewindManager.reset(new RewindManager(shared_from_this()));
404-
_notificationManager->RegisterNotificationListener(_rewindManager);
406+
if(!forPowerCycle) {
407+
_rewindManager.reset(new RewindManager(shared_from_this()));
408+
_notificationManager->RegisterNotificationListener(_rewindManager);
409+
}
405410

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

420425
if(IsMaster()) {
421-
string modelName = _model == NesModel::PAL ? "PAL" : (_model == NesModel::Dendy ? "Dendy" : "NTSC");
422-
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
423-
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(GetRomInfo().RomName, false));
426+
if(!forPowerCycle) {
427+
string modelName = _model == NesModel::PAL ? "PAL" : (_model == NesModel::Dendy ? "Dendy" : "NTSC");
428+
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
429+
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(GetRomInfo().RomName, false));
430+
}
431+
424432
_settings->ClearFlags(EmulationFlags::ForceMaxSpeed);
425433

426434
if(_slave) {
@@ -565,11 +573,16 @@ shared_ptr<SystemActionManager> Console::GetSystemActionManager()
565573
}
566574

567575
void Console::PowerCycle()
576+
{
577+
ReloadRom(true);
578+
}
579+
580+
void Console::ReloadRom(bool forPowerCycle)
568581
{
569582
if(_initialized && !_romFilepath.empty()) {
570583
VirtualFile romFile = _romFilepath;
571584
VirtualFile patchFile = _patchFilename;
572-
Initialize(romFile, patchFile);
585+
Initialize(romFile, patchFile, forPowerCycle);
573586
}
574587
}
575588

Core/Console.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class Console : public std::enable_shared_from_this<Console>
143143

144144
bool Initialize(string romFile, string patchFile = "");
145145
bool Initialize(VirtualFile &romFile);
146-
bool Initialize(VirtualFile &romFile, VirtualFile &patchFile);
146+
bool Initialize(VirtualFile &romFile, VirtualFile &patchFile, bool forPowerCycle = false);
147147

148148
void SaveBatteries();
149149

@@ -179,6 +179,7 @@ class Console : public std::enable_shared_from_this<Console>
179179

180180
void Reset(bool softReset = true);
181181
void PowerCycle();
182+
void ReloadRom(bool forPowerCycle = false);
182183
void ResetComponents(bool softReset);
183184

184185
//Used to pause the emu loop to perform thread-safe operations

Core/EmulationSettings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ enum class EmulatorShortcut
431431
Pause,
432432
Reset,
433433
PowerCycle,
434+
ReloadRom,
434435
PowerOff,
435436
Exit,
436437

Core/MemoryManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ uint8_t MemoryManager::DebugRead(uint16_t addr, bool disableSideEffects)
101101
}
102102
}
103103

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

106106
return value;
107107
}
@@ -114,7 +114,7 @@ uint16_t MemoryManager::DebugReadWord(uint16_t addr)
114114
uint8_t MemoryManager::Read(uint16_t addr, MemoryOperationType operationType)
115115
{
116116
uint8_t value = _ramReadHandlers[addr]->ReadRAM(addr);
117-
_console->GetCheatManager()->ApplyRamCodes(addr, value);
117+
_console->GetCheatManager()->ApplyCodes(addr, value);
118118
_console->DebugProcessRamOperation(operationType, addr, value);
119119

120120
_openBusHandler.SetOpenBus(value);

Docs/content/configuration/Preferences.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ Available shortcuts:
6666
* **Rewind 10 seconds**: Instantly rewinds 10 seconds of gameplay.
6767
* **Rewind 1 minute**: Instantly rewinds 1 minute of gameplay.
6868
* **Pause**: Pauses or unpauses the game.
69-
* **Reset**: Resets the game.
70-
* **Power Cycle**: Power cycles the game and reloads the file from disk.
69+
* **Reset**: Resets the game (equivalent to pressing the reset button on the NES.)
70+
* **Power Cycle**: Power cycles the game (equivalent to turning the power off and then back on.)
71+
* **Reload ROM**: Reloads the ROM from the disk and power cycles the console.
7172
* **Power Off**: Powers off the game, returning to the game selection screen.
7273
* **Exit**: Exits the emulator.
7374
* **FDS - Insert Next Disk**: Inserts face A of the next disk.

GUI.NET/Dependencies/resources.ca.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Control ID="mnuPause">Pausa</Control>
1414
<Control ID="mnuReset">Reinicia el joc</Control>
1515
<Control ID="mnuPowerCycle">Atura i reinicia el joc</Control>
16+
<Control ID="mnuReloadRom">Reload ROM</Control>
1617
<Control ID="mnuPowerOff">Atura el joc</Control>
1718
<Control ID="mnuSwitchDiskSide">Canvia la cara del disc</Control>
1819
<Control ID="mnuSelectDisk">Escull el disc</Control>
@@ -856,6 +857,7 @@
856857
<Message ID="EmulatorShortcutMappings_Pause">Pausa</Message>
857858
<Message ID="EmulatorShortcutMappings_Reset">Reinicia el joc</Message>
858859
<Message ID="EmulatorShortcutMappings_PowerCycle">Atura i reinicia el joc</Message>
860+
<Message ID="EmulatorShortcutMappings_ReloadRom">Reload ROM</Message>
859861
<Message ID="EmulatorShortcutMappings_PowerOff">Atura el joc</Message>
860862
<Message ID="EmulatorShortcutMappings_Exit">Surt</Message>
861863
<Message ID="EmulatorShortcutMappings_TakeScreenshot">Captura de pantalla</Message>

0 commit comments

Comments
 (0)