how to get the ROM address of vblank and main loop end?

Discussion of hardware and software development for Super NES and Super Famicom. See the SNESdev wiki for more information.

Moderator: Moderators

Forum rules
  • For making cartridges of your Super NES games, see Reproduction.
Post Reply
odelot
Posts: 6
Joined: Mon Aug 27, 2018 7:33 am

how to get the ROM address of vblank and main loop end?

Post by odelot »

I saw this video that uses lsnes emulator to run a script and show the CPU usage graphicly.

https://www.youtube.com/watch?v=tD6fCPPmBMA

The video creater shared the script, put you need to inform the ROM address of the end of vblank and the end of the main loop.

I am developing using pvsneslib and I would love to track the cpu usage, but I don't know how to identify this two address in the ROM.

Does anyone knows how to find these address? Thank you ;-)
User avatar
rainwarrior
Posts: 8735
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: how to get the ROM address of vblank and main loop end?

Post by rainwarrior »

Actual vblank starts and stops with a fixed timing, there is no specific ROM address for its end, that will vary depending on what code the CPU happens to be running at the time.

The NMI routine starts at the beginning of vblank if it's enabled, so in that sense the start of vblank has a specific ROM address.

The NMI routine ends with an RTI instruction. That instruction has a specific ROM address as well, and could be described as the end of the routine that runs in vblank... is that what you're looking for?

Finally the main loop has no end, which is why it's called a loop. It always keeps running. However, it will have a point where it's done all of its work for the frame and has to wait for an NMI to signal the next frame. This might be a WAI instruction on the 65C816, but could also be done with a short loop that repeats until some variable has changed (identifiable by the same instructions/addresses executing over and over).
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: how to get the ROM address of vblank and main loop end?

Post by dougeff »

Could you do something like flipping mosiac on at the end of the main game logic (and turning it off in nmi).
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: how to get the ROM address of vblank and main loop end?

Post by koitsu »

This exact subject has come up before -- https://forums.nesdev.com/viewtopic.php?f=12&t=12419 -- and we went over it in great detail. That said...

Important detail easily overlooked in this thread: video in question is discussing how to examine CPU usage through reverse-engineering an existing game, using the feature of an emulator and its native Lua plug-in to "do magical stuff". That "magical stuff" is often not stuff you normally can do in native 65816 or in game code; it's usually "added on" functionality at the emulator level. But it really varies depending on the implementation in the emulator. This is a problem because at first glance, things like YouTube videos seem "magical and neat OMG!" yet in reality they're doing stuff that really isn't in native code running on the console itself. This gives a false impression about what the console itself is doing, unless the video author is very explicit about it (they often aren't).

So... I looked at the Lua script. Several things:

I don't see how $2C3 matches up with $0082C3, but according to this disassembly $0082C3 is indeed the rti instruction at the end of NMI.

So I have to assume $75 means $8075, which is an stz $10 instruction; that direct page location ($10) has some relevance pertaining to "lag" in some way. What bugs me about this is that the little loop involving $8075 does a jsr $9322 (this is still in bank $00), yet the code at that address isn't included in the disassembly. Instead, if you dig around the disassembly, you'll find that the person who did it omitted the address and instead called $9322 GetGameMode -- how nice that they they didn't update the label in the jsr statement. The "guts" of the main loop are actually in a complicated routine called ExecutePtr. So, the individual keying off of $75 / stz $10 is basically his/her "best-effort guess" at where the "main loop" ends -- and it's close enough.

In short: those "magic values" are some emulator-specific nonsense, and the person who did that video / did the script explained them in a very nebulous way. This doesn't surprise me -- common in the "SMW hacking" community and the like, as many there are folks who like to fool with this stuff but aren't really programmers (i.e. have never written code on the console). That said: I don't see why the emulator didn't just let you use the explicit ROM address, ex. $0082C3, since NMI always starts/executes in bank $00. Whatever. (Sorry, this type of stuff annoys me to no end; magic numbers really tick me off, as it only takes the person writing the documentation a sentence or two to explain their purpose).

The Lua script also clearly is drawing rectangles and graphics, i.e. overlays, on top of the existing visuals. But achieve this in natively 65816 on the SNES itself is similar in implementation, yet very different when it comes to getting the actual visuals. That leads me to discuss how to do it natively:

I don't think mosaic ($2105) or things like screen brightness ($2100) will be "visual" enough to notice, particularly if the amount of CPU time being used is very low. It would work for things that take a long time, but for shorter things, nope. That's why in the thread I suggest using the PPU colour add/sub registers or screen sub capability, which I'm pretty sure is what the individual in the video is using for the varying-length "green overlay" on top of the actual playfield. But give them a try and report back. The previous thread I linked should give you some ideas of what to try.

Otherwise, for how to use Lua/etc. to "tie in" to your own game's code to achieve the same thing? You're going to have to look at a code listing, or a label listing (with addresses), and know where in 65816 address space things are located. Maybe your assembler or compiler can do this for you, I don't know. You can alternately disassemble your own code and figure it out.

You'll find that getting emulators to "tie in" with your own native code isn't usually possible, e.g. you can't magically make a label in your code called "EndOfNMI" and then in some Lua script say "do something at EndOfNMI". There's no connection between the two things, hence why actual addresses are needed. To my knowledge, there's no SNES emulator that is so "integrated" with development tools (assemblers, compilers, etc.) to do this natively.
Last edited by koitsu on Fri Sep 28, 2018 2:14 pm, edited 1 time in total.
User avatar
rainwarrior
Posts: 8735
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: how to get the ROM address of vblank and main loop end?

Post by rainwarrior »

koitsu wrote:You'll find that getting emulators to "tie in" with your own native code isn't usually possible, e.g. you can't magically make a label in your code called "EndOfNMI" and then in some Lua script say "do something at EndOfNMI". There's no connection between the two things, hence why actual addresses are needed. To my knowledge, there's no SNES emulator that is so "integrated" with development tools (assemblers, compilers, etc.) to do this natively.
I literally did this with Lizard. ;) I had a python program to parse debug labels and spit some of them out into a header for the lua scripts. (Already had to do this to anyway to get the labels into FCEUX, so it wasn't much to add.)

Lua aside, using colours for profiling timing works pretty well on the NES at least, and I imagine would be pretty good on SNES too. If it's part of the ROM code you can see it on any emulator or on the real thing too, no scripts required.
psycopathicteen
Posts: 3140
Joined: Wed May 19, 2010 6:12 pm

Re: how to get the ROM address of vblank and main loop end?

Post by psycopathicteen »

So it's true that the score decimal calculation effects the CPU usage by quite a bit.
odelot
Posts: 6
Joined: Mon Aug 27, 2018 7:33 am

Re: how to get the ROM address of vblank and main loop end?

Post by odelot »

hi guys! thank you for the answers.

I did as you said and used the palette color, in the end of vblank I change it to RED and in the end of main loop I change it to BLACK again.

I did a video of this working https://youtu.be/BlgGKXIcdYs now I can press a button and see how much CPU I am using.

Thank you guys!
Post Reply