Ppu and Cpu multithreding sync

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
User avatar
Anes
Posts: 702
Joined: Tue Dec 21, 2004 8:35 pm
Location: Mendoza, Argentina

Ppu and Cpu multithreding sync

Post by Anes »

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?
ANes
calima
Posts: 1745
Joined: Tue Oct 06, 2015 10:16 am

Re: Ppu and Cpu multithreding sync

Post by calima »

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?
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Ppu and Cpu multithreding sync

Post by lidnariq »

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.
User avatar
Dwedit
Posts: 4924
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Ppu and Cpu multithreding sync

Post by Dwedit »

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.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Ppu and Cpu multithreding sync

Post by tepples »

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.
strat
Posts: 409
Joined: Mon Apr 07, 2008 6:08 pm
Location: Missouri

Re: Ppu and Cpu multithreding sync

Post by strat »

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.
Post Reply