diff --git a/BizHawk.Emulation.Cores/Calculator/TI83.cs b/BizHawk.Emulation.Cores/Calculator/TI83.cs index 9b9686a480..8c1aa705a6 100644 --- a/BizHawk.Emulation.Cores/Calculator/TI83.cs +++ b/BizHawk.Emulation.Cores/Calculator/TI83.cs @@ -657,8 +657,8 @@ namespace BizHawk.Emulation.Cores.Calculators "Main RAM", ram.Length, MemoryDomain.Endian.Little, - addr => ram[addr & RamSizeMask], - (addr, value) => ram[addr & RamSizeMask] = value + addr => ram[addr], + (addr, value) => ram[addr] = value ) }; diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs index 8b8ef69553..587688fa25 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/2600/Atari2600.cs @@ -30,7 +30,7 @@ namespace BizHawk.Emulation.Cores.Atari.Atari2600 var domains = new List(1) { - new MemoryDomain("Main RAM", 128, MemoryDomain.Endian.Little, addr => ram[addr & 127], (addr, value) => ram[addr & 127] = value), + new MemoryDomain("Main RAM", 128, MemoryDomain.Endian.Little, addr => ram[addr], (addr, value) => ram[addr] = value), new MemoryDomain("TIA", 16, MemoryDomain.Endian.Little, addr => tia.ReadMemory((ushort) addr, true), (addr, value) => tia.WriteMemory((ushort) addr, value)), new MemoryDomain("PIA", 1024, MemoryDomain.Endian.Little, addr => m6532.ReadMemory((ushort) addr, true), diff --git a/BizHawk.Emulation.Cores/Consoles/Atari/7800/Atari7800.cs b/BizHawk.Emulation.Cores/Consoles/Atari/7800/Atari7800.cs index 938b918f52..be5d07e8dc 100644 --- a/BizHawk.Emulation.Cores/Consoles/Atari/7800/Atari7800.cs +++ b/BizHawk.Emulation.Cores/Consoles/Atari/7800/Atari7800.cs @@ -257,20 +257,28 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800 "RAM1", 0x800, MemoryDomain.Endian.Unknown, delegate(int addr) { + if (addr < 0 || addr >= 0x800) + throw new ArgumentOutOfRangeException(); return ((Machine7800)theMachine).RAM1[(ushort)addr]; }, delegate(int addr, byte val) { + if (addr < 0 || addr >= 0x800) + throw new ArgumentOutOfRangeException(); ((Machine7800)theMachine).RAM1[(ushort)addr] = val; })); _MemoryDomains.Add(new MemoryDomain( "RAM2", 0x800, MemoryDomain.Endian.Unknown, delegate(int addr) { + if (addr < 0 || addr >= 0x800) + throw new ArgumentOutOfRangeException(); return ((Machine7800)theMachine).RAM2[(ushort)addr]; }, delegate(int addr, byte val) { + if (addr < 0 || addr >= 0x800) + throw new ArgumentOutOfRangeException(); ((Machine7800)theMachine).RAM2[(ushort)addr] = val; })); _MemoryDomains.Add(new MemoryDomain( @@ -308,10 +316,14 @@ namespace BizHawk.Emulation.Cores.Atari.Atari7800 "System Bus", 65536, MemoryDomain.Endian.Unknown, delegate(int addr) { + if (addr < 0 || addr >= 0x10000) + throw new ArgumentOutOfRangeException(); return theMachine.Mem[(ushort)addr]; }, delegate(int addr, byte val) { + if (addr < 0 || addr >= 0x10000) + throw new ArgumentOutOfRangeException(); theMachine.Mem[(ushort)addr] = val; })); } diff --git a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs index 09541a4ef5..5727ed76bd 100644 --- a/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs +++ b/BizHawk.Emulation.Cores/Consoles/Coleco/ColecoVision.cs @@ -57,14 +57,24 @@ namespace BizHawk.Emulation.Cores.ColecoVision { var domains = new List(3); var MainMemoryDomain = new MemoryDomain("Main RAM", Ram.Length, MemoryDomain.Endian.Little, - addr => Ram[addr & RamSizeMask], - (addr, value) => Ram[addr & RamSizeMask] = value); + addr => Ram[addr], + (addr, value) => Ram[addr] = value); var VRamDomain = new MemoryDomain("Video RAM", VDP.VRAM.Length, MemoryDomain.Endian.Little, - addr => VDP.VRAM[addr & 0x3FFF], - (addr, value) => VDP.VRAM[addr & 0x3FFF] = value); + addr => VDP.VRAM[addr], + (addr, value) => VDP.VRAM[addr] = value); var SystemBusDomain = new MemoryDomain("System Bus", 0x10000, MemoryDomain.Endian.Little, - addr => Cpu.ReadMemory((ushort)addr), - (addr, value) => Cpu.WriteMemory((ushort)addr, value)); + (addr) => + { + if (addr < 0 || addr >= 65536) + throw new ArgumentOutOfRangeException(); + return Cpu.ReadMemory((ushort)addr); + }, + (addr, value) => + { + if (addr < 0 || addr >= 65536) + throw new ArgumentOutOfRangeException(); + Cpu.WriteMemory((ushort)addr, value); + }); domains.Add(MainMemoryDomain); domains.Add(VRamDomain); diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/Meteor.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/Meteor.cs index c95656c22c..ecaff46e30 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/Meteor.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/Meteor.cs @@ -268,10 +268,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA MemoryDomain sb = new MemoryDomain("BUS", 1 << 28, MemoryDomain.Endian.Little, delegate(int addr) { + if (addr < 0 || addr >= 0x10000000) + throw new IndexOutOfRangeException(); return LibMeteor.libmeteor_peekbus((uint)addr); }, delegate(int addr, byte val) { + if (addr < 0 || addr >= 0x10000000) + throw new IndexOutOfRangeException(); LibMeteor.libmeteor_writebus((uint)addr, val); }); _MemoryDomains.Add(sb); @@ -283,6 +287,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA MemoryDomain cr = new MemoryDomain("Combined WRAM", (256 + 32) * 1024, MemoryDomain.Endian.Little, delegate(int addr) { + if (addr < 0 || addr >= (256 + 32) * 1024) + throw new IndexOutOfRangeException(); if (addr >= 256 * 1024) return iw.PeekByte(addr & 32767); else @@ -290,6 +296,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA }, delegate(int addr, byte val) { + if (addr < 0 || addr >= (256 + 32) * 1024) + throw new IndexOutOfRangeException(); if (addr >= 256 * 1024) iw.PokeByte(addr & 32767, val); else diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs index c45ff9b36a..3d4bb64c90 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs @@ -604,7 +604,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy System.Runtime.InteropServices.Marshal.Copy(data, CachedMemory, 0, length); readneeded = false; } - return CachedMemory[addr % CachedMemory.Length]; + return CachedMemory[addr]; } public void Poke(int addr, byte val) { @@ -616,7 +616,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy System.Runtime.InteropServices.Marshal.Copy(data, CachedMemory, 0, length); readneeded = false; } - CachedMemory[addr % CachedMemory.Length] = val; + CachedMemory[addr] = val; writeneeded = true; } } @@ -658,10 +658,14 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy _MemoryDomains.Add(new MemoryDomain("System Bus", 65536, MemoryDomain.Endian.Little, delegate(int addr) { + if (addr < 0 || addr >= 65536) + throw new ArgumentOutOfRangeException(); return LibGambatte.gambatte_cpuread(GambatteState, (ushort)addr); }, delegate(int addr, byte val) { + if (addr < 0 || addr >= 65536) + throw new ArgumentOutOfRangeException(); LibGambatte.gambatte_cpuwrite(GambatteState, (ushort)addr, val); })); diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs index e977c2c624..ee24609f9f 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs @@ -401,15 +401,15 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { var domains = new List(); var RAM = new MemoryDomain("RAM", 0x800, MemoryDomain.Endian.Little, - addr => ram[addr & 0x07FF], (addr, value) => ram[addr & 0x07FF] = value); + addr => ram[addr], (addr, value) => ram[addr] = value); var SystemBus = new MemoryDomain("System Bus", 0x10000, MemoryDomain.Endian.Little, addr => ReadMemory((ushort)addr), (addr, value) => ApplySystemBusPoke(addr, value)); var PPUBus = new MemoryDomain("PPU Bus", 0x4000, MemoryDomain.Endian.Little, addr => ppu.ppubus_peek(addr), (addr, value) => ppu.ppubus_write(addr, value)); var CIRAMdomain = new MemoryDomain("CIRAM (nametables)", 0x800, MemoryDomain.Endian.Little, - addr => CIRAM[addr & 0x07FF], (addr, value) => CIRAM[addr & 0x07FF] = value); + addr => CIRAM[addr], (addr, value) => CIRAM[addr] = value); var OAMdoman = new MemoryDomain("OAM", 64 * 4, MemoryDomain.Endian.Unknown, - addr => ppu.OAM[addr & (64 * 4 - 1)], (addr, value) => ppu.OAM[addr & (64 * 4 - 1)] = value); + addr => ppu.OAM[addr], (addr, value) => ppu.OAM[addr] = value); domains.Add(RAM); domains.Add(SystemBus); diff --git a/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs b/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs index d5f6af6b16..d9280d0cf7 100644 --- a/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs +++ b/BizHawk.Emulation.Cores/Consoles/PC Engine/PCEngine.cs @@ -596,13 +596,23 @@ namespace BizHawk.Emulation.Cores.PCEngine var domains = new List(10); int mainmemorymask = Ram.Length - 1; var MainMemoryDomain = new MemoryDomain("Main Memory", Ram.Length, MemoryDomain.Endian.Little, - addr => Ram[addr & mainmemorymask], - (addr, value) => Ram[addr & mainmemorymask] = value); + addr => Ram[addr], + (addr, value) => Ram[addr] = value); domains.Add(MainMemoryDomain); var SystemBusDomain = new MemoryDomain("System Bus", 0x200000, MemoryDomain.Endian.Little, - addr => Cpu.ReadMemory21(addr), - (addr, value) => Cpu.WriteMemory21(addr, value)); + (addr) => + { + if (addr < 0 || addr >= 0x200000) + throw new ArgumentOutOfRangeException(); + return Cpu.ReadMemory21(addr); + }, + (addr, value) => + { + if (addr < 0 || addr >= 0x200000) + throw new ArgumentOutOfRangeException(); + Cpu.WriteMemory21(addr, value); + }); domains.Add(SystemBusDomain); var RomDomain = new MemoryDomain("ROM", RomLength, MemoryDomain.Endian.Little, @@ -614,28 +624,28 @@ namespace BizHawk.Emulation.Cores.PCEngine if (BRAM != null) { var BRAMMemoryDomain = new MemoryDomain("Battery RAM", Ram.Length, MemoryDomain.Endian.Little, - addr => BRAM[addr & 0x7FF], - (addr, value) => BRAM[addr & 0x7FF] = value); + addr => BRAM[addr], + (addr, value) => BRAM[addr] = value); domains.Add(BRAMMemoryDomain); } if (TurboCD) { var CDRamMemoryDomain = new MemoryDomain("TurboCD RAM", CDRam.Length, MemoryDomain.Endian.Little, - addr => CDRam[addr & 0xFFFF], - (addr, value) => CDRam[addr & 0xFFFF] = value); + addr => CDRam[addr], + (addr, value) => CDRam[addr] = value); domains.Add(CDRamMemoryDomain); var AdpcmMemoryDomain = new MemoryDomain("ADPCM RAM", ADPCM.RAM.Length, MemoryDomain.Endian.Little, - addr => ADPCM.RAM[addr & 0xFFFF], - (addr, value) => ADPCM.RAM[addr & 0xFFFF] = value); + addr => ADPCM.RAM[addr], + (addr, value) => ADPCM.RAM[addr] = value); domains.Add(AdpcmMemoryDomain); if (SuperRam != null) { var SuperRamMemoryDomain = new MemoryDomain("Super System Card RAM", SuperRam.Length, MemoryDomain.Endian.Little, - addr => SuperRam[addr & 0x3FFFF], - (addr, value) => SuperRam[addr & 0x3FFFF] = value); + addr => SuperRam[addr], + (addr, value) => SuperRam[addr] = value); domains.Add(SuperRamMemoryDomain); } } @@ -643,16 +653,16 @@ namespace BizHawk.Emulation.Cores.PCEngine if (ArcadeCard) { var ArcadeRamMemoryDomain = new MemoryDomain("Arcade Card RAM", ArcadeRam.Length, MemoryDomain.Endian.Little, - addr => ArcadeRam[addr & 0x1FFFFF], - (addr, value) => ArcadeRam[addr & 0x1FFFFF] = value); + addr => ArcadeRam[addr], + (addr, value) => ArcadeRam[addr] = value); domains.Add(ArcadeRamMemoryDomain); } if (PopulousRAM != null) { var PopulusRAMDomain = new MemoryDomain("Cart Battery RAM", PopulousRAM.Length, MemoryDomain.Endian.Little, - addr => PopulousRAM[addr & 0x7fff], - (addr, value) => PopulousRAM[addr & 0x7fff] = value); + addr => PopulousRAM[addr], + (addr, value) => PopulousRAM[addr] = value); domains.Add(PopulusRAMDomain); } diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.cs b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.cs index aa6f150f00..3f8690561f 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/SMS/SMS.cs @@ -443,17 +443,27 @@ namespace BizHawk.Emulation.Cores.Sega.MasterSystem { var domains = new List(3); var MainMemoryDomain = new MemoryDomain("Main RAM", SystemRam.Length, MemoryDomain.Endian.Little, - addr => SystemRam[addr & RamSizeMask], - (addr, value) => SystemRam[addr & RamSizeMask] = value); + addr => SystemRam[addr], + (addr, value) => SystemRam[addr] = value); var VRamDomain = new MemoryDomain("Video RAM", Vdp.VRAM.Length, MemoryDomain.Endian.Little, - addr => Vdp.VRAM[addr & 0x3FFF], - (addr, value) => Vdp.VRAM[addr & 0x3FFF] = value); + addr => Vdp.VRAM[addr], + (addr, value) => Vdp.VRAM[addr] = value); var SaveRamDomain = new MemoryDomain("Save RAM", SaveRAM.Length, MemoryDomain.Endian.Little, - addr => SaveRAM[addr % SaveRAM.Length], - (addr, value) => { SaveRAM[addr % SaveRAM.Length] = value; SaveRamModified = true; }); + addr => SaveRAM[addr], + (addr, value) => { SaveRAM[addr] = value; SaveRamModified = true; }); var SystemBusDomain = new MemoryDomain("System Bus", 0x10000, MemoryDomain.Endian.Little, - addr => Cpu.ReadMemory((ushort)addr), - (addr, value) => Cpu.WriteMemory((ushort)addr, value)); + (addr) => + { + if (addr < 0 || addr >= 65536) + throw new ArgumentOutOfRangeException(); + return Cpu.ReadMemory((ushort)addr); + }, + (addr, value) => + { + if (addr < 0 || addr >= 65536) + throw new ArgumentOutOfRangeException(); + Cpu.WriteMemory((ushort)addr, value); + }); domains.Add(MainMemoryDomain); domains.Add(VRamDomain); diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.cs b/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.cs index 77b0712e63..e45a570828 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/gpgx/GPGX.cs @@ -554,11 +554,15 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.gpgx mm.Add(new MemoryDomain(name, size, MemoryDomain.Endian.Unknown, delegate(int addr) { - return p[addr & (size - 1)]; + if (addr < 0 || addr >= size) + throw new ArgumentOutOfRangeException(); + return p[addr]; }, delegate(int addr, byte val) { - p[addr & (size - 1)] = val; + if (addr < 0 || addr >= size) + throw new ArgumentOutOfRangeException(); + p[addr] = val; })); }