From 66197f2c0979be6e69a8d347536b229f113d8e81 Mon Sep 17 00:00:00 2001 From: Souryo Date: Sat, 11 Nov 2017 13:24:48 -0500 Subject: [PATCH] Debugger: Improved bound checking for memory read/writes --- Core/BaseMapper.cpp | 39 ++++++++++++++-------- Core/MemoryDumper.cpp | 8 ++--- GUI.NET/Debugger/Controls/ctrlChrViewer.cs | 14 +++++--- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/Core/BaseMapper.cpp b/Core/BaseMapper.cpp index 2209972c..158f2c0b 100644 --- a/Core/BaseMapper.cpp +++ b/Core/BaseMapper.cpp @@ -916,25 +916,38 @@ uint32_t BaseMapper::GetMemorySize(DebugMemoryType type) uint8_t BaseMapper::GetMemoryValue(DebugMemoryType memoryType, uint32_t address) { - switch(memoryType) { - case DebugMemoryType::ChrRom: return _onlyChrRam ? _chrRam[address] : _chrRom[address]; - case DebugMemoryType::ChrRam: return _chrRam[address]; - case DebugMemoryType::SaveRam: return _saveRam[address]; - case DebugMemoryType::PrgRom: return _prgRom[address]; - case DebugMemoryType::WorkRam: return _workRam[address]; - } + uint32_t memorySize = GetMemorySize(memoryType); + if(memorySize > 0) { + if(address > memorySize) { + address %= memorySize; + } + switch(memoryType) { + case DebugMemoryType::ChrRom: return _chrRom[address]; + case DebugMemoryType::ChrRam: return _chrRam[address]; + case DebugMemoryType::SaveRam: return _saveRam[address]; + case DebugMemoryType::PrgRom: return _prgRom[address]; + case DebugMemoryType::WorkRam: return _workRam[address]; + } + } return 0; } void BaseMapper::SetMemoryValue(DebugMemoryType memoryType, uint32_t address, uint8_t value) { - switch(memoryType) { - case DebugMemoryType::ChrRom: _chrRom[address] = value; break; - case DebugMemoryType::ChrRam: _chrRam[address] = value; break; - case DebugMemoryType::SaveRam: _saveRam[address] = value; break; - case DebugMemoryType::PrgRom: _prgRom[address] = value; break; - case DebugMemoryType::WorkRam: _workRam[address] = value; break; + uint32_t memorySize = GetMemorySize(memoryType); + if(memorySize > 0) { + if(address > memorySize) { + address %= memorySize; + } + + switch(memoryType) { + case DebugMemoryType::ChrRom: _chrRom[address] = value; break; + case DebugMemoryType::ChrRam: _chrRam[address] = value; break; + case DebugMemoryType::SaveRam: _saveRam[address] = value; break; + case DebugMemoryType::PrgRom: _prgRom[address] = value; break; + case DebugMemoryType::WorkRam: _workRam[address] = value; break; + } } } diff --git a/Core/MemoryDumper.cpp b/Core/MemoryDumper.cpp index 64aeaf88..fa5e6a14 100644 --- a/Core/MemoryDumper.cpp +++ b/Core/MemoryDumper.cpp @@ -148,8 +148,8 @@ void MemoryDumper::SetMemoryValue(DebugMemoryType memoryType, uint32_t address, case DebugMemoryType::PpuMemory: _mapper->DebugWriteVRAM(address, value, disableSideEffects); break; case DebugMemoryType::PaletteMemory: _ppu->WritePaletteRAM(address, value); break; - case DebugMemoryType::SpriteMemory: _ppu->GetSpriteRam()[address] = value; break; - case DebugMemoryType::SecondarySpriteMemory: _ppu->GetSecondarySpriteRam()[address] = value; break; + case DebugMemoryType::SpriteMemory: _ppu->GetSpriteRam()[address % 0x100] = value; break; + case DebugMemoryType::SecondarySpriteMemory: _ppu->GetSecondarySpriteRam()[address % 0x20] = value; break; case DebugMemoryType::PrgRom: _mapper->SetMemoryValue(memoryType, address, value); @@ -202,8 +202,8 @@ uint8_t MemoryDumper::GetMemoryValue(DebugMemoryType memoryType, uint32_t addres case DebugMemoryType::PpuMemory: return _mapper->DebugReadVRAM(address, disableSideEffects); case DebugMemoryType::PaletteMemory: return _ppu->ReadPaletteRAM(address); - case DebugMemoryType::SpriteMemory: return _ppu->GetSpriteRam()[address]; - case DebugMemoryType::SecondarySpriteMemory: return _ppu->GetSecondarySpriteRam()[address]; + case DebugMemoryType::SpriteMemory: return _ppu->GetSpriteRam()[address % 0x100]; + case DebugMemoryType::SecondarySpriteMemory: return _ppu->GetSecondarySpriteRam()[address % 0x20]; case DebugMemoryType::PrgRom: case DebugMemoryType::ChrRom: diff --git a/GUI.NET/Debugger/Controls/ctrlChrViewer.cs b/GUI.NET/Debugger/Controls/ctrlChrViewer.cs index 5ba30497..af30a051 100644 --- a/GUI.NET/Debugger/Controls/ctrlChrViewer.cs +++ b/GUI.NET/Debugger/Controls/ctrlChrViewer.cs @@ -331,8 +331,11 @@ namespace Mesen.GUI.Debugger.Controls int tileIndex = GetLargeSpriteIndex(_tileIndex); - byte orgByte1 = InteropEmu.DebugGetMemoryValue(ppuMemory ? DebugMemoryType.PpuMemory : DebugMemoryType.ChrRom, (UInt32)(baseAddress + tileIndex * 16 + y)); - byte orgByte2 = InteropEmu.DebugGetMemoryValue(ppuMemory ? DebugMemoryType.PpuMemory : DebugMemoryType.ChrRom, (UInt32)(baseAddress + tileIndex * 16 + y + 8)); + bool isChrRam = InteropEmu.DebugGetMemorySize(DebugMemoryType.ChrRom) == 0; + DebugMemoryType memType = ppuMemory? DebugMemoryType.PpuMemory : (isChrRam ? DebugMemoryType.ChrRam : DebugMemoryType.ChrRom); + + byte orgByte1 = InteropEmu.DebugGetMemoryValue(memType, (UInt32)(baseAddress + tileIndex * 16 + y)); + byte orgByte2 = InteropEmu.DebugGetMemoryValue(memType, (UInt32)(baseAddress + tileIndex * 16 + y + 8)); byte byte1 = (byte)(orgByte1 & ~(0x80 >> x)); byte byte2 = (byte)(orgByte2 & ~(0x80 >> x)); @@ -345,8 +348,8 @@ namespace Mesen.GUI.Debugger.Controls } if(byte1 != orgByte1 || byte2 != orgByte2) { - InteropEmu.DebugSetMemoryValue(ppuMemory ? DebugMemoryType.PpuMemory : DebugMemoryType.ChrRom, (UInt32)(baseAddress + tileIndex * 16 + y), byte1); - InteropEmu.DebugSetMemoryValue(ppuMemory ? DebugMemoryType.PpuMemory : DebugMemoryType.ChrRom, (UInt32)(baseAddress + tileIndex * 16 + y + 8), byte2); + InteropEmu.DebugSetMemoryValue(memType, (UInt32)(baseAddress + tileIndex * 16 + y), byte1); + InteropEmu.DebugSetMemoryValue(memType, (UInt32)(baseAddress + tileIndex * 16 + y + 8), byte2); GetData(); RefreshViewer(); @@ -395,10 +398,11 @@ namespace Mesen.GUI.Debugger.Controls int tileIndex = GetLargeSpriteIndex(_tileIndex); bool isChrRam = InteropEmu.DebugGetMemorySize(DebugMemoryType.ChrRom) == 0; + DebugMemoryType memType = ppuMemory ? DebugMemoryType.PpuMemory : (isChrRam ? DebugMemoryType.ChrRam : DebugMemoryType.ChrRom); StringBuilder sb = new StringBuilder(); if(isChrRam) { for(int i = 0; i < 16; i++) { - sb.Append(InteropEmu.DebugGetMemoryValue(ppuMemory ? DebugMemoryType.PpuMemory : DebugMemoryType.ChrRom, (UInt32)(baseAddress + tileIndex * 16 + i)).ToString("X2")); + sb.Append(InteropEmu.DebugGetMemoryValue(memType, (UInt32)(baseAddress + tileIndex * 16 + i)).ToString("X2")); } } else { int absoluteTileIndex = ppuMemory ? InteropEmu.DebugGetAbsoluteChrAddress((uint)(baseAddress+tileIndex*16))/16 : (baseAddress / 16 + tileIndex);