Program counter

The program counter is the part of the 6502 microprocessor that keeps track of where it is during the execution of a game.

Understanding the Program Counter

The program counter contains the memory address of the next opcode to be executed. The 6502 fetches that opcode and then executes it. After the execution, the program counter is incremented to where the next opcode should be. For example, look at this table of memory :

$0200 : INC $d020
$0203 : INC $d020
$0206 : INC $d020

If the program started out with the memory address $0200, the processor would :

  • Fetch the opcode in $0200, which is INC (increment the value in a memory address)
  • Fetch it's operands, in this case $20 and $d0, which is the address of the memory address to increment. (The 6502 is a little endian processor).
  • Execute the code. The value of $d020 is incremented by one.
  • Finally, the program is incremented by 3, where the next opcode should be in address $0203.

Instructions that Change the Program Counter

The program counter can be changed by quite a few instructions.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.