simple way to see visualize code timing?

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

Post Reply
ninja_kun
Posts: 13
Joined: Fri Jun 17, 2022 5:02 am

simple way to see visualize code timing?

Post by ninja_kun »

On the C64, a very easy way to "profile" code is to change the border color at the start and end of different sections of code. That way, you can see how many scanlines your code is taking in real time.

Is there a similar trick available on the NES? Near as I can see, there are no registers for setting "border" color on the PPU?

Or (maybe better?) are there any tools which can statically analyze a subroutine to count worst-case cycles?

Thanks in advance..
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: simple way to see visualize code timing?

Post by Quietust »

ninja_kun wrote: Sat Jun 18, 2022 6:40 am On the C64, a very easy way to "profile" code is to change the border color at the start and end of different sections of code. That way, you can see how many scanlines your code is taking in real time.

Is there a similar trick available on the NES? Near as I can see, there are no registers for setting "border" color on the PPU?

Or (maybe better?) are there any tools which can statically analyze a subroutine to count worst-case cycles?

Thanks in advance..
The simplest equivalent would be to turn on the Color Emphasis bits in the PPU, since those take effect immediately and there are 7 different combinations you can use (red, green, blue, magenta, yellow, cyan, and gray). Granted, on an NTSC/PAL PPU they might not be as easy to see, but with an RGB PPU they will be extremely visible.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Pokun
Posts: 2681
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: simple way to see visualize code timing?

Post by Pokun »

I toggle the monochrome flag (bit 0 of $2001) for a single scanline to do this, it's very easy to see unless your background uses a lot of white, grey or other bright colors, in that case the emphasis flags may be better to pick a color that is easier to see in your case.

Yeah there is no border color on the PPU, only the backdrop color.
User avatar
Ben Boldt
Posts: 1149
Joined: Tue Mar 22, 2016 8:27 pm
Location: Minnesota, USA

Re: simple way to see visualize code timing?

Post by Ben Boldt »

If you have an oscilloscope, you could toggle OUT2. You can trigger and take real measurements this way. You can include M2 on the scope so you can specifically count the cycles as well. This will also work for things in V-Blank, h-blank, and things not synchronized to the video. You could even include the video output on the scope; this lets you see your signal relative to V-blank. But there is some small amount of setup involved to use a scope and it requires real hardware.

Another option is to emulate and use breakpoints. This will count cycles for you VERY nicely, and not adding any overhead from toggling a signal (or toggling background color, etc.).
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: simple way to see visualize code timing?

Post by rainwarrior »

I've found using the monochrome bit + colour emphasis to be quite useful.

The monochrome flag is really helpful for making the emphasis colours more easily distinguishable, plus it turns black into grey so empty areas of the screen can still have a colour to emphasize.

I've used this for on-hardware profiling in a few of my projects. Article with example of how it looks.

Alternatively, the Mesen emulator has an "event viewer" debug feature that can display events or arbitrary breakpoints against the rendered picture in real-time. It also has a separate "performance profiler" tool that automatically gathers stats on a per-function-call basis.

Emulators with Lua support also give you a lot of options for writing something custom for measuring performance.
ninja_kun
Posts: 13
Joined: Fri Jun 17, 2022 5:02 am

Re: simple way to see visualize code timing?

Post by ninja_kun »

Thanks everyone, these are all great ideas!
Post Reply