How to set program counter?

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
EnigmaOverdrive
Posts: 9
Joined: Sat Jul 09, 2016 10:16 am

How to set program counter?

Post by EnigmaOverdrive »

I'm trying to make a back function in a system of menus so that if the player presses the B button, it will take them back to the title screen. Using a recursive function call seems to cause the screen to go black after a few iterations, so I wonder: is there a way I can set the program counter directly to accomplish this?
User avatar
dougeff
Posts: 3080
Joined: Fri May 08, 2015 7:17 pm

Re: How to set program counter?

Post by dougeff »

So, the program counter is what the CPU does to keep track of what line of code it's on while it executes. To explicitly change the line of code being executed (program count), you would use a JMP instruction (or GOTO in C).
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
tokumaru
Posts: 12535
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How to set program counter?

Post by tokumaru »

You normally wouldn't want to simply set the program count (which is really accomplished with a JMP instruction), because that would result in instantaneous (and possibly glitchy) transitions. Usually you'd maintain a set of state variables indicating the current screen/mode/etc., and pressing the button would merely change a state variable. The game's update routine would detect the state change and proceed to end the current state, possibly doing a fade out or any other sorry of "out" animation, before transferring control (JMP) to the initialization of the other state, which ideally will have an "in" animation.
User avatar
Memblers
Posts: 4100
Joined: Mon Sep 20, 2004 6:04 am
Location: Indianapolis
Contact:

Re: How to set program counter?

Post by Memblers »

What you want is called a "finite state machine", you can search that term if you want to find other descriptions. It is exactly what tokumaru described.
Post Reply