Hello everyone,
I've started writing my first emulator a few months ago (18 months actually according to hg log, but stopped 7 months ago). I want to continue making this thing for educational purposes. As for now it's working quite ok - 6502 emulation is almost 100% complete (per-cycle emulation), few simple non-MMC mappers are implemented, simple APU with pulse1 and pulse2 is added and PPU is good enough for games like Contra.
As for now main loop of emulator looks like this (simplified, not raw copy paste):
In CPU->Step one cycle is emulated, same in ppu.Step. Frame is rendered on last scanline if I remember correctly. Everything works ok unless game changes PPU values like ScrollX or ScrollY during visible scanline - my emulator cares only about values that in registers during frame rendering (so registers values on last scanline are only used). For example game like Micro Machines looks terribly wrong in menu and NEStress menu is jumping around.
My question is: how to implement changing values of PPU registers during visible scanlines correctly? One of my ideas is just to plot pixel during every PPU cycle, but this is extremely inefficient. Second thought is to render screen scanline by scanline, not whole frame at once. My last concept is to store register values when CPU is writing to them in some kind of vector in form of [scanline, pixel, value] per register. Then use those values during frame rendering.
Which approach is best one? Is there better way to implement this?
I've started writing my first emulator a few months ago (18 months actually according to hg log, but stopped 7 months ago). I want to continue making this thing for educational purposes. As for now it's working quite ok - 6502 emulation is almost 100% complete (per-cycle emulation), few simple non-MMC mappers are implemented, simple APU with pulse1 and pulse2 is added and PPU is good enough for games like Contra.
As for now main loop of emulator looks like this (simplified, not raw copy paste):
Code:
while(1)
{
//HandleEvents()
//Input()
while (!cpu->ppu.framerendered)
{
for(int i=0; i<3; i++) cpu->ppu.Step();
cpu->Step();
}
DrawFrame();
// Sleep long enough to keep 60fps
}
{
//HandleEvents()
//Input()
while (!cpu->ppu.framerendered)
{
for(int i=0; i<3; i++) cpu->ppu.Step();
cpu->Step();
}
DrawFrame();
// Sleep long enough to keep 60fps
}
In CPU->Step one cycle is emulated, same in ppu.Step. Frame is rendered on last scanline if I remember correctly. Everything works ok unless game changes PPU values like ScrollX or ScrollY during visible scanline - my emulator cares only about values that in registers during frame rendering (so registers values on last scanline are only used). For example game like Micro Machines looks terribly wrong in menu and NEStress menu is jumping around.
My question is: how to implement changing values of PPU registers during visible scanlines correctly? One of my ideas is just to plot pixel during every PPU cycle, but this is extremely inefficient. Second thought is to render screen scanline by scanline, not whole frame at once. My last concept is to store register values when CPU is writing to them in some kind of vector in form of [scanline, pixel, value] per register. Then use those values during frame rendering.
Which approach is best one? Is there better way to implement this?