Scanlines to CPU cycles

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

Post Reply
NewRisingSun
Posts: 1510
Joined: Thu May 19, 2005 11:30 am

Scanlines to CPU cycles

Post by NewRisingSun »

I want to hack an MMC3 game to use a mapper that only has an M2-based IRQ counter. My previous method has been to make a 240-word table containing the results of the formula

Code: Select all

cycles = (scanline +20) *341 /3 +176
for 240 values of $C000. The "+20" scanline and "+176" cycle offsets account for the duration of vertical blanking when the counter is started at the beginning of the NMI routine.

The game I am modifying right now (Kirby's Adventure) has no unused space anywhere to store such a 240 word table. Since parts of the game require a variable scanline at which to generate the IRQ, I cannot use a hard-coded count like one would use for a simple status bar. So I wonder whether it is possible to make the above computation entirely without a table, space-saving and reasonably fast. I have started reading up on the subject [1][2], but am at my limits when it comes to combining both the multiplication and the division into one short routine.
User avatar
Dwedit
Posts: 4921
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Scanlines to CPU cycles

Post by Dwedit »

341 PPU dots per scanline, or about 113 CPU cycles.
Is the approximation (128 * X) - (16 * X) for 114 CPU cycles per scanline fast and simple enough?
Or (128 * X) - (16 * X) - X for 113 cycles per scanline.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: Scanlines to CPU cycles

Post by lidnariq »

Just throwing out some ideas:
1 - There are 341 M2 cycles in 3 scanlines. There are 1023 M2 cycles in 9 scanlines. Multiply by (1024-1), then divide by 9

2 - 341/3 = 114 - 1/3, so expand the math as scanline*114 - scanline/3 + 2449 + 1/3
User avatar
krzysiobal
Posts: 1036
Joined: Sun Jun 12, 2011 12:06 pm
Location: Poland
Contact:

Re: Scanlines to CPU cycles

Post by krzysiobal »

What about rom offset $6fe86 - I see around 368 zeros here, maybe you can use it and make some kind of compression of your table (calculate only value for even scanlines, and the one for odd will be average of two neighbours?

If not PRG ROM, then maybe CHR? I see there are a lot of identical tile-boxes in CHR-ROM at $b6810-$b69f0 (exactly 460 bytes)
User avatar
aquasnake
Posts: 515
Joined: Fri Sep 13, 2019 11:22 pm

Re: Scanlines to CPU cycles

Post by aquasnake »

341/3 = 113+2/3

You need a prescaler, set the counting cycle to 113, and then count down. When the current counter value is 0, 113 will be reloaded automatically. By this way, a pseudo scanline delay is approximately simulated.

For every 3 pseudo scanlines (3 subtraction cycles of 113), reload 115 in prescaler next to count leap
Post Reply