ehguacho wrote:
i'd already taked a look to the Open Source emulators, but since the source code it's not commented it's hard to understand.
i'm just asking for a few advices to write my own PPU emulator.
Unfortunately, there's really no advice that can be given, beyond simply stating the obvious: read other people's code, even if it's not commented. Just take your time with it, and try to understand it as best you can.
That being said, you can at least get a first-order approximation of how the PPU works by breaking your emulator's time into different chunks. For example, on NTSC displays, each horizontal scanline takes 63 microseconds or so to complete. So,
1) you can write your emulator to emulate 63 microseconds worth of instructions (IIRC, the NES runs its 6502 at 1.79MHz -- same as Atari 800 I think), so that is something close to 96 CPU clock cycles worth of emulation. Then,
2) you render a complete scanline into your emulator's frame buffer. Then,
3) Wait for a some amount of time to ensure that 63 microseconds actually passes in the real-world. Since modern computers can compute a scanline and emulate 63us of 6502 instructions much faster than the original systems could, you need this to make sure game play is manageable.
4) Goto step 1.
NOTE: this kind of emulation loop will NOT handle cycle-perfect emulation. But, it is a good first-step, and as long as you're games don't do timing tricks shorter than 63.5us long, it should work out OK. I think.
The experience from writing this will also give you the knowledge and background needed to decipher what the open-source emulators are doing too, so when you need cycle-accurate emulation, re-using ideas becomes much easier.