How to measure how much VBLANK and non-VBLANK time I have left?

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
Soph
Posts: 9
Joined: Mon May 30, 2022 1:40 am

How to measure how much VBLANK and non-VBLANK time I have left?

Post by Soph »

Hello!

I have some long stuff, running in the main cycle, I have some buffer execution running in the VBLANK, but every time I'm adding some stuff or optimising the code I need to know how much time I have left. For example, when I add 8 tiles to the buffer to interpret during the VBLANK everything goes ok. But when I add 16 tiles I'm out of VBLANK time and the only way to discover it - is by visual glitches.

I was thinking about a counter that starts after all my code in the vblank is done and then to look at that counter via the debugger screen in my mednafen but as the screen is being processed 50 to 60 times per second that would require more than twice as much bytes to actually see the values. The perfect visualization would be a live graph, but I'm absolutely sure that no one have ever made this thing. I can patch mednafen to watch a byte in the memory and draw the graph over the screen, but that returns me back to the original question:

How do I measure the time that is left in VBLANK and non-VBLANK after all my own code is done executing?

Thank you in advance. If the question would be successfully answered and that way would work perfectly I will definitely patch mednafen to provide a useful tool for all the future developers. I have already patched it once when we were running a contest for the perfect Bomberman 2 bots in my office.
Снимок экрана_2022-03-13_22-37-55.png
Bomberman II (U) -0001.png
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: How to measure how much VBLANK and non-VBLANK time I have left?

Post by pubby »

The debuggers on most emulators display the current scanline. If you insert a breakpoint to the end of your NMI code and check the scanline, you can see if you're in VBLANK or not.

If you're looking to do it in 6502 code, I think you can poll PPUSTATUS and check bit 7 to see if VBLANK happening, though this has caveats.
Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: How to measure how much VBLANK and non-VBLANK time I have left?

Post by Fiskbit »

For time overall, a common method is to use PPU mask ($2001) settings such as grayscale, color emphasis, or even BG left-column enable when the frame ends to act as a simple CPU meter. The point on the screen at which the setting visibly toggles tells you how much time you have left.

This doesn't work for vblank, though, because vblank isn't visible onscreen. For that, I usually use Mesen in any of a few different ways:
- It has a profiler tool that can tell you how much time is being used by various functions, though it has limitations that prevent it from being universally applicable and I don't have much experience using it.

- You can also set a breakpoint at the start and end of the section you wish to measure, and when you continue from the first breakpoint and break on the second, Mesen will display the cycles elapsed.

- Finally, you can also set a 'mark' breakpoint at the end of the code; this kind of breakpoint doesn't stop execution, but instead places a mark in the event viewer, which is a visual representation of the entire frame including vblank and hblank. Then you can visually see where that mark is relative to the end of vblank. This is probably the easiest way and is similar to the PPU mask solution mentioned above for time overall.
User avatar
TakuikaNinja
Posts: 89
Joined: Mon Jan 09, 2023 6:42 pm
Location: New Zealand
Contact:

Re: How to measure how much VBLANK and non-VBLANK time I have left?

Post by TakuikaNinja »

If I were doing this, I would place a breakpoint after the graphics routines have finished in the NMI handler and see where it lands in Mesen's event viewer. Make sure to enable the blanking periods in the window.
Soph
Posts: 9
Joined: Mon May 30, 2022 1:40 am

Re: How to measure how much VBLANK and non-VBLANK time I have left?

Post by Soph »

If you insert a breakpoint to the end of your NMI code and check the scanline, you can see if you're in VBLANK or not.
I was looking for more exact values than a bool variable for the VBLANK time. The scanline number as the indicator would probably be good for the main code, but in the VBLANK it will only have values somewhere in between ~240 and ~260 and that makes only 20 possible values and... It's not very exact. But would probably work if everything else fails.
Soph
Posts: 9
Joined: Mon May 30, 2022 1:40 am

Re: How to measure how much VBLANK and non-VBLANK time I have left?

Post by Soph »

Unfortunately all Mesen recipes aren't very useful for me as it plays the ROMS and shows the PPU Viewer, but doesn't want to work with the debug tool at my machine.
Снимок экрана_2023-01-27_19-39-12.png
For time overall, a common method is to use PPU mask ($2001) settings such as grayscale, color emphasis, or even BG left-column enable when the frame ends to act as a simple CPU meter. The point on the screen at which the setting visibly toggles tells you how much time you have left.
That sounds as an interesting approach. May I ask for a simpliest example? Or, at least, the algo's overview?
Fiskbit
Posts: 891
Joined: Sat Nov 18, 2017 9:15 pm

Re: How to measure how much VBLANK and non-VBLANK time I have left?

Post by Fiskbit »

Have you tried the new version of Mesen released a couple days ago? You can find it here. It is a significant rewrite and may work on your system.

The method I describe is simple. You simply enable the feature you want to use by writing to $2001 when your frame completes.

Code: Select all

LDA ppu_mask_value
ORA #%11100000  ; R+G+B emphasis
STA $2001
Post Reply