Skip to content

Commit

Permalink
Debugger: NES Header Editor - Fixed behavior/crash when opening heade…
Browse files Browse the repository at this point in the history
…r editor with softpatched roms or compressed files
  • Loading branch information
SourMesen committed Jul 19, 2024
1 parent 9b34e7c commit ff874bc
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Core/Debugger/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ DebuggerType* Debugger::GetDebugger()
return (DebuggerType*)_debuggers[(int)type].Debugger.get();
}

IDebugger* Debugger::GetMainDebugger()
{
return _debuggers[(int)_mainCpuType].Debugger.get();
}

template<CpuType type>
uint64_t Debugger::GetCpuCycleCount()
{
Expand Down
1 change: 1 addition & 0 deletions Core/Debugger/Debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class Debugger
uint32_t GetExecutionTrace(TraceRow output[], uint32_t startOffset, uint32_t maxLineCount);

CpuType GetMainCpuType() { return _mainCpuType; }
IDebugger* GetMainDebugger();

TraceLogFileSaver* GetTraceLogFileSaver() { return _traceLogSaver.get(); }
MemoryDumper* GetMemoryDumper() { return _memoryDumper.get(); }
Expand Down
2 changes: 2 additions & 0 deletions Core/Debugger/IDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class IDebugger
virtual ITraceLogger* GetTraceLogger() = 0;
virtual PpuTools* GetPpuTools() { return nullptr; }

virtual void GetRomHeader(uint8_t* headerData, uint32_t& size) {}

virtual BaseState& GetState() = 0;
virtual void GetPpuState(BaseState& state) {};
virtual void SetPpuState(BaseState& state) {};
Expand Down
1 change: 1 addition & 0 deletions Core/GBA/Cart/GbaRtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ uint64_t GbaRtc::ToDateTime()
uint8_t GbaRtc::GetCommandLength(Command cmd)
{
switch(cmd) {
default:
case Command::Reset: return 0;
case Command::Status: return 8;
case Command::DateTime: return 8 * 7;
Expand Down
11 changes: 11 additions & 0 deletions Core/NES/Debugger/NesDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,17 @@ bool NesDebugger::SaveRomToDisk(string filename, bool saveAsIps, CdlStripOption
return false;
}

void NesDebugger::GetRomHeader(uint8_t* headerData, uint32_t& size)
{
if(size < sizeof(NesHeader) || _console->GetRomFormat() != RomFormat::iNes) {
size = 0;
return;
}

NesHeader header = _mapper->GetRomInfo().Header;
memcpy(headerData, &header, sizeof(NesHeader));
}

void NesDebugger::ProcessInputOverrides(DebugControllerState inputOverrides[8])
{
BaseControlManager* controlManager = _console->GetControlManager();
Expand Down
1 change: 1 addition & 0 deletions Core/NES/Debugger/NesDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class NesDebugger final : public IDebugger
ITraceLogger* GetTraceLogger() override;
PpuTools* GetPpuTools() override;
bool SaveRomToDisk(string filename, bool saveAsIps, CdlStripOption stripOption);
void GetRomHeader(uint8_t* headerData, uint32_t& size) override;
CallstackManager* GetCallstackManager() override;
IAssembler* GetAssembler() override;
BaseEventManager* GetEventManager() override;
Expand Down
2 changes: 2 additions & 0 deletions InteropDLL/DebugApiWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Core/Shared/Emulator.h"
#include "Core/Shared/DebuggerRequest.h"
#include "Core/Debugger/Debugger.h"
#include "Core/Debugger/IDebugger.h"
#include "Core/Debugger/MemoryDumper.h"
#include "Core/Debugger/MemoryAccessCounter.h"
#include "Core/Debugger/CdlManager.h"
Expand Down Expand Up @@ -191,5 +192,6 @@ extern "C"

DllExport uint32_t __stdcall AssembleCode(CpuType cpuType, char* code, uint32_t startAddress, int16_t* assembledOutput) { return WithTool(uint32_t, GetAssembler(cpuType), AssembleCode(code, startAddress, assembledOutput)); }

DllExport void __stdcall GetRomHeader(uint8_t* headerData, uint32_t& size) { WithToolVoid(GetMainDebugger(), GetRomHeader(headerData, size)); }
DllExport bool __stdcall SaveRomToDisk(char* filename, bool saveIpsFile, CdlStripOption cdlStripOption) { return WithDebugger(bool, SaveRomToDisk(filename, saveIpsFile, cdlStripOption)); }
};
16 changes: 5 additions & 11 deletions UI/Debugger/ViewModels/NesHeaderEditViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,12 @@ public class NesHeaderEditViewModel : DisposableViewModel

public NesHeaderEditViewModel()
{
byte[] headerBytes = new byte[16];
_romInfo = EmuApi.GetRomInfo();
string romPath = _romInfo.RomPath;
try {
//TODOv2, get header from core (to support for patches, etc.)
using(FileStream? fileStream = FileHelper.OpenRead(romPath)) {
if(fileStream != null) {
fileStream.Read(headerBytes, 0, 16);
}
}
} catch { }
byte[] headerBytes = DebugApi.GetRomHeader();
if(headerBytes.Length < 16) {
Array.Resize(ref headerBytes, 16);
}

_romInfo = EmuApi.GetRomInfo();
Header = NesHeader.FromBytes(headerBytes);

AddDisposable(this.WhenAnyValue(x => x.Header.SaveRam, x => x.Header.ChrRamBattery).Subscribe(x => {
Expand Down
10 changes: 10 additions & 0 deletions UI/Interop/DebugApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ public static List<int> GetAvailableInputOverrides()
return indexes;
}

[DllImport(DllPath, EntryPoint = "GetRomHeader")] private static extern void GetRomHeaderWrapper([In, Out] byte[] headerData, ref UInt32 size);
public static byte[] GetRomHeader()
{
UInt32 size = 0x1000;
byte[] headerData = new byte[size];
DebugApi.GetRomHeaderWrapper(headerData, ref size);
Array.Resize(ref headerData, (Int32)size);
return headerData;
}

[DllImport(DllPath)][return: MarshalAs(UnmanagedType.I1)] public static extern bool SaveRomToDisk([MarshalAs(UnmanagedType.LPUTF8Str)] string filename, [MarshalAs(UnmanagedType.I1)] bool saveAsIps, CdlStripOption cdlStripOption);

[DllImport(DllPath, EntryPoint = "GetMemoryValues")] private static extern void GetMemoryValuesWrapper(MemoryType type, UInt32 start, UInt32 end, [In, Out] byte[] buffer);
Expand Down

0 comments on commit ff874bc

Please sign in to comment.