CPU and PPU clock math

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
RobertLoggia
Posts: 4
Joined: Sun Apr 05, 2015 7:16 am

CPU and PPU clock math

Post by RobertLoggia »

I've read[1] that the NES PPU (NTSC/2C02) is 3 times the speed of the NES CPU.

According to the wiki:

The NES CPU operates at a speed of 21.477272 MHz / 12 = 1789773Hz.
The NES PPU operates at a speed of 21.477272 MHz / 4 = 5369318Hz.

If we take the CPU speed and run the "3 times slower than the PPU" rule:

1789773 * 3 = 5369319hz.

This is 1hz behind the actual PPU speed since 5369319hz != 5369318Hz.

Could this be a big issue if emulator logic is expecting that the PPU clock is 1789773Hz * 3, vs trying to emulate the actual clock rate of 5369319?

[1] http://wiki.nesdev.com/w/index.php/Clock_rate
User avatar
thefox
Posts: 3139
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: CPU and PPU clock math

Post by thefox »

All of those numbers are rounded, so there's no 1 Hz difference in reality. 3 PPU clocks is always equivalent to 1 CPU clock because both clocks are based on the same master clock. Also note that the real clock frequency is never exactly what is advertised, for example on the 21.47 MHz clock you can see a +-40 Hz tolerance in the page you linked.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: CPU and PPU clock math

Post by tepples »

Thefox is right about rounding. If you don't want rounding, you'll need to stay in the rational number domain and multiply everything as fractions.
  • The NTSC audio subcarrier over RF is defined as 9/2 MHz.
  • The NTSC horizontal scan frequency* is defined as 1/286 of the audio subcarrier, or 9/572 MHz.
  • The NTSC color subcarrier is defined as 455/2 times the horizontal scan frequency, or 315/88 MHz. This frequency is often called "color burst" or "3.58" after its rounded value.
  • The exact value for the dot clock rate on NTSC and RGB PPUs is three-halves of color burst, or 945/176 MHz.
  • The exact value for the CPU clock rate is one-third of the PPU dot clock, or one-half of color burst, or 315/176 MHz.

* This is the standard rate for 480i television. Prior to the Dreamcast, 480i was highly uncommon on video game consoles, and their picture generators applied various forms of cheating. Horizontal scan on NES and Super NES is slightly faster than this. Horizontal scan on many other 240p consoles is slightly slower. But it still works because it's within tolerances of actual TV sets.
RobertLoggia
Posts: 4
Joined: Sun Apr 05, 2015 7:16 am

Re: CPU and PPU clock math

Post by RobertLoggia »

thefox wrote:Also note that the real clock frequency is never exactly what is advertised, for example on the 21.47 MHz clock you can see a +-40 Hz tolerance in the page you linked.
If theres a +- 40Hz tolerance, then why is it important to care about being 1 PPU cycle short when its an odd frame. Wouldn't it not matter if the tolerance range is that big?
Joe
Posts: 469
Joined: Mon Apr 01, 2013 11:17 pm

Re: CPU and PPU clock math

Post by Joe »

RobertLoggia wrote:Wouldn't it not matter if the tolerance range is that big?
It would be fine if they used separate clock sources. For example, the SNES CPU and APU use different clock sources, which means the CPU and APU must communicate with each other periodically if they need to remain synchronized.

However, the NES CPU and PPU are both driven by the same crystal resonator. They are always exactly in sync. You could write code for the NES that synchronizes itself against the PPU once, and then runs forever without ever needing to synchronize itself again. I'm not aware of any games that specifically rely on the one-cycle odd/even difference, but there are a few that will break if your timing is too far off.
tepples
Posts: 22345
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: CPU and PPU clock math

Post by tepples »

RobertLoggia wrote:If theres a +- 40Hz tolerance, then why is it important to care about being 1 PPU cycle short when its an odd frame. Wouldn't it not matter if the tolerance range is that big?
To put it another way: There's a 40 Hz tolerance in the absolute rate of a particular clock source. There's no tolerance in the relative rate between two clocks derived from the same source.
User avatar
mikejmoffitt
Posts: 1352
Joined: Sun May 27, 2012 8:43 pm

Re: CPU and PPU clock math

Post by mikejmoffitt »

tepples wrote:
RobertLoggia wrote:If theres a +- 40Hz tolerance, then why is it important to care about being 1 PPU cycle short when its an odd frame. Wouldn't it not matter if the tolerance range is that big?
To put it another way: There's a 40 Hz tolerance in the absolute rate of a particular clock source. There's no tolerance in the relative rate between two clocks derived from the same source.
To put it another another way: The minute difference in frequency will be proportionately reflected in all further divisions of the frequency. The PPU and CPU clock will maintain their clock ratio.
Post Reply