I'm curious about the behavior of MMC1, and I was looking at the Nintendulator and Nestopia MMC1 source. Oddly, I think they differ - which was quite a surprise. Or maybe I'm not understanding it correctly. Below is the code from Nintendulator.
I'm curious about the LastReg variable. Does this look right? It seems that it will reset the latch if you write to a different bank from the previous write. I've never seen any documentation claiming this is the way it works. In fact our wiki says that the following code is valid for loading the PRG bank reg, but I think it will not work properly on Nintendulator because of this LastReg feature.
Any insight into this? Am I not understanding the Nintendulator code, or is Nestopia and the wiki incorrect?
Code:
void MAPINT MMC1_Write (int Bank, int Addr, int Val)
{
u8 Reg = (Bank >> 1) & 3;
if (Val & 0x80)
{
MMC1.Latch = MMC1.LatchPos = 0;
MMC1.Regs[0] |= 0x0C;
return;
}
if (Reg != LastReg)
MMC1.Latch = MMC1.LatchPos = 0;
LastReg = Reg;
MMC1.Latch |= (Val & 1) << MMC1.LatchPos++;
if (MMC1.LatchPos == 5)
{
MMC1.Regs[Reg] = MMC1.Latch & 0x1F;
MMC1.Latch = MMC1.LatchPos = 0;
MMC1.Sync();
}
}
{
u8 Reg = (Bank >> 1) & 3;
if (Val & 0x80)
{
MMC1.Latch = MMC1.LatchPos = 0;
MMC1.Regs[0] |= 0x0C;
return;
}
if (Reg != LastReg)
MMC1.Latch = MMC1.LatchPos = 0;
LastReg = Reg;
MMC1.Latch |= (Val & 1) << MMC1.LatchPos++;
if (MMC1.LatchPos == 5)
{
MMC1.Regs[Reg] = MMC1.Latch & 0x1F;
MMC1.Latch = MMC1.LatchPos = 0;
MMC1.Sync();
}
}
I'm curious about the LastReg variable. Does this look right? It seems that it will reset the latch if you write to a different bank from the previous write. I've never seen any documentation claiming this is the way it works. In fact our wiki says that the following code is valid for loading the PRG bank reg, but I think it will not work properly on Nintendulator because of this LastReg feature.
Code:
But because only the fifth write sets the destination register, the following equivalent (if obfuscated) subroutine changes the PRG ROM bank in the same manner:
setPRGBank:
sta $8765
lsr a
sta $FACE
lsr a
sta $BA11
lsr a
sta $AD2E
lsr a
sta $EAD5
rts
setPRGBank:
sta $8765
lsr a
sta $FACE
lsr a
sta $BA11
lsr a
sta $AD2E
lsr a
sta $EAD5
rts
Any insight into this? Am I not understanding the Nintendulator code, or is Nestopia and the wiki incorrect?