Fergulator
Fergulator copied to clipboard
RAM (0x00 - 0x7FF) is not mirrored (through 0x800 - 0x1FFF)
It doesn't look like RAM access is mirrored per this diagram: http://en.wikibooks.org/wiki/NES_Programming/Memory_Map
I believe a simple fix would be to bitwise & the address with 0x7FFF
which should be equivalent to modulus 0x800.
func (m Memory) Read(a uint16) (Word, error) {
switch {
case a >= 0x2008 && a < 0x4000:
offset := a % 0x8
return ppu.RegRead(int(0x2000 + offset))
case a <= 0x2007 && a >= 0x2000:
return ppu.RegRead(int(a))
case a == 0x4016:
return Pads[0].Read(), nil
case a == 0x4017:
return Pads[1].Read(), nil
case a&0xF000 == 0x4000:
return apu.RegRead(int(a))
case a >= 0x8000 && a <= 0xFFFF:
return rom.Read(int(a)), nil
case a >= 0x5100 && a <= 0x6000:
if _, ok := rom.(*Mmc5); ok {
// MMC5 register handling
return rom.Read(int(a)), nil
}
}
return m[a & 0x7FFF], nil // <--- Mirroring handled here
}