APU Frame Counter: Difference between revisions

From NESdev Wiki
Jump to navigationJump to search
(Adding some timing explanations, hopefully I've interpreted these correctly)
m (I guess it isn't quite the "next" APU cycle)
Line 17: Line 17:
|colspan=2| Side effects || After 2 or 3 clock cycles*, the timer is reset.<br>If the mode flag is set, then both "quarter frame" and "half frame" signals are also generated.
|colspan=2| Side effects || After 2 or 3 clock cycles*, the timer is reset.<br>If the mode flag is set, then both "quarter frame" and "half frame" signals are also generated.
|}
|}
<nowiki>*</nowiki> The side effects take effect on the next APU cycle. If the write occurs ''during'' an APU cycle, the effects occur 2 CPU cycles later, and if the write occurs ''between'' APU cycles, the effects occur 3 CPU cycles later.
<nowiki>*</nowiki> If the write occurs ''during'' an APU cycle, the effects occur 2 CPU cycles later, and if the write occurs ''between'' APU cycles, the effects occur 3 CPU cycles later.


''TODO: describe PAL''
''TODO: describe PAL''

Revision as of 01:18, 13 July 2011

The NES APU frame counter generates low-frequency clocks for the channels and an optional 60 Hz interrupt. The name "frame counter" might be slightly misleading because the clocks have nothing to do with the video signal.

The frame counter contains the following: divider, looping clock sequencer, frame interrupt flag.

The sequencer is clocked on every other CPU cycle, so 2 CPU cycles = 1 APU cycle. The sequencer keeps track of how many APU cycles have elapsed in total, and each step of the sequence will occur once that total has reached the indicated amount. Once the last step has executed, two extra APU cycles elapse before the count resets to 0.

Address Bitfield Description
$4017 MI--.---- Set mode and interrupt (write)
Bit 7 M--- ---- Sequencer mode: 0 selects 4-step sequence, 1 selects 5-step sequence
Bit 6 -I-- ---- Interrupt inhibit flag. If set, the frame interrupt flag is cleared, otherwise it is unaffected.
Side effects After 2 or 3 clock cycles*, the timer is reset.
If the mode flag is set, then both "quarter frame" and "half frame" signals are also generated.

* If the write occurs during an APU cycle, the effects occur 2 CPU cycles later, and if the write occurs between APU cycles, the effects occur 3 CPU cycles later.

TODO: describe PAL

The frame interrupt flag is connected to the CPU's IRQ line.

Mode 0: 4-Step Sequence (bit 7 of $4017 clear)

Step APU cycles Envelopes & triangle's linear counter (Quarter frame) Length counters & sweep units (Half frame) Frame interrupt flag
1 7456 Clock    
2 14912 Clock Clock  
3 22370 Clock    
4 29828 Clock Clock Set if interrupt inhibit is clear
29829     Set if interrupt inhibit is clear
29830 -> 0     Set if interrupt inhibit is clear
  240 Hz (approx.) 120 Hz (approx.) 60 Hz (approx.)

In this mode, the interrupt flag is set every 29830 CPU cycles, which is slightly slower than the 29780.5 CPU cycles per NTSC PPU frame.

Some Nintendo arcade boards, even those not directly based on the NES, use the 2A03 CPU as a sound processor. One of these is Punch-Out!!. This IRQ allows the CPU to keep time even if no PPU is connected to the bus.

Mode 1: 5-Step Sequence (bit 7 of $4017 set)

Step APU cycles Envelopes & triangle's linear counter (Quarter frame) Length counters & sweep units (Half frame)
1 7456 Clock  
2 14912 Clock Clock
3 22370 Clock  
4 29828    
5 37280 Clock Clock
37282 -> 0    
  192 Hz (approx.), uneven timing 96 Hz (approx.), uneven timing

In this mode, the frame interrupt flag is never set.