How many clock cycles fit in one vblank?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

djcouchycouch
Posts: 97
Joined: Sat May 28, 2011 10:30 am

How many clock cycles fit in one vblank?

Post by djcouchycouch »

I have no idea how much code I can put into vblank without causing trouble. Are there any rules of thumb or guidelines that would give me an idea?
User avatar
cpow
NESICIDE developer
Posts: 1097
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Re: How many clock cycles fit in one vblank?

Post by cpow »

shawnleblanc wrote:I have no idea how much code I can put into vblank without causing trouble. Are there any rules of thumb or guidelines that would give me an idea?
About 2273 CPU cycles. Depending on your instruction mix that's either a chunk of code (1136 2-cycle opcodes) or a little bit of code (324 7-cycle opcodes). So, I guess that isn't very helpful. You'll be somewhere between 324 and 1136, roughly.
User avatar
Dwedit
Posts: 5086
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit »

262 total scanlines, 240 are visible.
1 scanline is the pre-vblank scanline and you can't use it unless you have IRQs or other timed code to know when that scanline is coming.
1 scanline is the pre-render scanline, you can use it as extra vblank time if you turn off the screen, then do a complete set of scroll writes after turning the screen back on.

If you just use the normal vblank lines, that's 21 scanlines at 341 PPU cycles (113.666 CPU cycles) each, for 2387 total CPU cycles. Of course, there's also the NMI interrupt triggering, and all your other code to get ready to start spitting out graphics, so there's less time than that actually available for your graphics display loop.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
User avatar
qbradq
Posts: 972
Joined: Wed Oct 15, 2008 11:50 am

Post by qbradq »

One thing I do is to set a breakpoint at the end of my VBlank routine in Nintendulator. It has this nice feature in e debugger that shows you what scan line and pixel you are on.
User avatar
tokumaru
Posts: 12536
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

qbradq wrote:One thing I do is to set a breakpoint at the end of my VBlank routine in Nintendulator. It has this nice feature in e debugger that shows you what scan line and pixel you are on.
I'd like to second this suggestion.
User avatar
cpow
NESICIDE developer
Posts: 1097
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Post by cpow »

tokumaru wrote:
qbradq wrote:One thing I do is to set a breakpoint at the end of my VBlank routine in Nintendulator. It has this nice feature in e debugger that shows you what scan line and pixel you are on.
I'd like to second this suggestion.
I'll put an alternative out there:

Image

I set up the red execution marker in the nesyar source over its entire NMI routine, and the green marker over the jsr to the sound routine. The Execution Visualizer shows me where each marked code region is executing in the PPU frame and how many CPU cycles it took last time it ran. It's a bit hard to see in the image but the area to the right of the quotile shield is HBLANK (the red/green stretches into it. The image is 341x262 (or 341x312) pixels big, so you can see it covers the entire PPU clock domain and shows where the CPU is doing whatever you marked to see. I can add eight different marked regions and see where they are in relation to each other or how long they are taking. Plan is to add min/max/avg also to cycle counts. In the image, the top left (0,0) is the screen pixel (0,0), so VBLANK time is at the bottom (where it's all red). Obviously nesyar eats into the screen time a bit in NMI but only to do sound updates.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: the universe
Contact:

Post by thefox »

Yet another option is to use NintendulatorDX with code like this:

Code: Select all

nmi:
  sta $4020 ; Start the timer.

  ; TODO: Do the vblank updates here.

  sta $4030 ; Stop the timer.

  rti
Then you can see the number of cycles taken by the vblank updates in the debugging window. As long as the "max" value is less than ~2270, you're in the good.
tepples
Posts: 22861
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

cpow wrote:Plan is to add min/max/avg also to cycle counts.
With sparklines?
User avatar
qbradq
Posts: 972
Joined: Wed Oct 15, 2008 11:50 am

Post by qbradq »

Dang CPOW! That's awesome! Is that something you wrote? Where can I get it?
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Post by koitsu »

Aww cpow, reminding me of my Apple IIGS days where you'd change the system border colour to something of your choice at the start of your routine, then back to whatever it was previously at the end, effectively using the border as a way to tell how much CPU time something took. :-) So happy.
User avatar
Bregalad
Posts: 8104
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Post by Bregalad »

koitsu wrote:Aww cpow, reminding me of my Apple IIGS days where you'd change the system border colour to something of your choice at the start of your routine, then back to whatever it was previously at the end, effectively using the border as a way to tell how much CPU time something took. :-) So happy.
You can do that on the NES with the $2001 register.
Useless, lumbering half-wits don't scare us.
User avatar
cpow
NESICIDE developer
Posts: 1097
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Post by cpow »

qbradq wrote:Dang CPOW! That's awesome! Is that something you wrote? Where can I get it?
It's part of NESICIDE, which is something I wrote, yes. :D
User avatar
cpow
NESICIDE developer
Posts: 1097
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Post by cpow »

tepples wrote:
cpow wrote:Plan is to add min/max/avg also to cycle counts.
With sparklines?
Funny you should mention that tepples! I attended Edward Tufte's "Information Design" seminar last year. As part of that I got signed copies of all four of his books and a poster. I am a strong believer in techniques for visualization of information. Sparklines are so obviously useful, concise, and unencumbered with need for prior knowledge of the visualization technique.

I could probably whip up a sparkline control in Qt pretty quick...so now that you've planted the idea, expect to see it. :D
User avatar
cpow
NESICIDE developer
Posts: 1097
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Post by cpow »

qbradq wrote:Dang CPOW! That's awesome! Is that something you wrote? Where can I get it?
It just struck me that you might have been referring to the game visual, not the tool. No, I did not write nesyar. That was authored by clueless. Haven't seen him around in a while though.
User avatar
qbradq
Posts: 972
Joined: Wed Oct 15, 2008 11:50 am

Post by qbradq »

I was referring to the visualization. Can I run any ROM with timer writes in that or does it have to be a NESICIDE project?
Post Reply