So now I have finally introduced a catchup method for my emulator. Instead of just catching the PPU and the APU up straight away, they are only allowed to be caught up when there is a write to a register. However, most games have the same problem, namely the screen will shudder all over the place and I think that I have tracked it down to the code switching the Background and Sprites on and off constantly. This of course makes the VRAM pointer go crazy, causing the screen to shake and all kinds of problems. I have observed this behaviour mainly in Spy Vs Spy, which constantly switches the Background and Sprites on and off all the time. But it seems to me that the game itself does this, not some emulator bug...
I know that the timing of the code is absolutely fine, so that can't be it. So I'd like to clarify a few things first.
i. There are (NTSC) 89342 PPU cycles per even frame. Therefore there should be 29780.666666666666666666666666667 CPU cycles per frame. (I am currently using 29781 per frame, and this makes the emulator 1 PPU cycle out, but I know that isn't the cause of my problems.)
ii. If either the Background or the Sprites are on, then render the PPU as normal, but if both are off, then there are no PPU memory fetches and the VRAM pointer is left alone. All that happens is the colour of Background's Palette entry #0 is rendered, unless the VRAM pointer is in the 3F00-1F region, then that colour is rendered instead. Also the VBlank flag is set/cleared too.
iii. Whenever a PPU register is written to, the emulator catches up first, and then allows the modifications. I have made the catchup afterwards, to the same effect.
I know that the timing of the code is absolutely fine, so that can't be it. So I'd like to clarify a few things first.
i. There are (NTSC) 89342 PPU cycles per even frame. Therefore there should be 29780.666666666666666666666666667 CPU cycles per frame. (I am currently using 29781 per frame, and this makes the emulator 1 PPU cycle out, but I know that isn't the cause of my problems.)
ii. If either the Background or the Sprites are on, then render the PPU as normal, but if both are off, then there are no PPU memory fetches and the VRAM pointer is left alone. All that happens is the colour of Background's Palette entry #0 is rendered, unless the VRAM pointer is in the 3F00-1F region, then that colour is rendered instead. Also the VBlank flag is set/cleared too.
iii. Whenever a PPU register is written to, the emulator catches up first, and then allows the modifications. I have made the catchup afterwards, to the same effect.