Hi, nice to see new faces here.
I haven't been posting for a lot.
I know there should be threads like this, but i was thinking in making emulator multithreading.
So we have the cpu and ppu always running. If (in an ntsc nes) have 1:3 ratio, so the ppu runs 3 times speed the cpu.
I can't figure out how to make it multithreding. We know that NES get things synced with vblank period.
Any suggestion how to have a RunCpu() and a RunPpu() running at the same time?
Ppu and Cpu multithreding sync
Moderator: Moderators
Re: Ppu and Cpu multithreding sync
The only way to get decent performance would be running one line at a time, not one tick. Find all possible sync points, such as the zero sprite hit, and if that happened, re-run that cpu line with the info on where it happened. Such an emu would be threaded and faster than a single-threaded one, but I have to ask, why bother when a single-threaded emu can run on a 200 MHz cpu?
Re: Ppu and Cpu multithreding sync
Actual use of multiple threads performs worse the more often the unrelated threads need to synchronize state. For something where you're hoping to synchronize state every CPU cycle, it's going to be unbearable.
Re: Ppu and Cpu multithreding sync
It takes about 10000 clock cycles to context switch, including waking up a thread that's sleeping. So only make threads go to sleep when you really mean it. There are also spin waits (full cpu usage), or also "pause" waits where the processor waits for 200 cycles to save power.
So you could theoretically use a two thread system and be able to run faster than one thread, but it would be hard to pull off. Also a NES emulator generally doesn't need performance help. Unless you're using some kind of slow Thumb-2 dual core processor, you won't get a benefit from multiple threads.
So you could theoretically use a two thread system and be able to run faster than one thread, but it would be hard to pull off. Also a NES emulator generally doesn't need performance help. Unless you're using some kind of slow Thumb-2 dual core processor, you won't get a benefit from multiple threads.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
Re: Ppu and Cpu multithreding sync
You might get a noticeable benefit from doing APU downsampling or PPU NTSC demodulation in another thread, especially if you're targeting mobile, but that's it.
Re: Ppu and Cpu multithreding sync
I tried making a multithreaded emulator as an exercise (and eventually adapted it to just being a single thread). It used semaphones on any event where the CPU and PPU relied on each other, such as writing to vram or reading PPUSTAT. There was a third thread to synchronize both per scanline. It's not advisable, like others said, you're just adding more unwieldy code to make things like sprite zero hit workable. But it's not a bad way to learn the basics of multithreading.