Search code examples
emulation6502

What is the logic behind 6502 instruction clock cycles?


Some questions to consider taken from page 10 of the 6502 datasheet:

http://archive.6502.org/datasheets/rockwell_r650x_r651x.pdf

  • What does page 10 by "Add 1 to N if page boundary is crossed"?
  • What does page 10 imply "Add 1 to N if branch occurs on the same page?"
  • What does it mean by add 2 to N if branch occurs to different page?
  • Does read and write to other devices ie.RAM cause any irregularities in clock cycles?
  • Are there any other factors that could affect clock cycles on the 6502 (more specifically the NES) ?

Solution

  • With regards to 6502 machine instructions. Instruction addresses are calculated and stored as two eight bit bytes. When doing address calculations such as with register indexed instructions, or for the target address for branching instructions - it's possible that there is an internal carry from the least significant byte to the most significant byte. This is what 'crossing a page boundary' means - a 'page' being 256 bytes. The internal carry process can impose a penalty of one cycle.

    To see it more clearly, if you encode your addresses in hexadecimal, then the lower byte of the address is the right-hand two digits. For example address $1234 hex, the lower byte would contain $34 hex and the upper $12. If the address you branch to or load or store from 'crosses the page boundary', by tipping the upper byte over by one, for example to address $1300, then a cycle penalty will be incurred.

    With branching instructions there is a further cycle added if the branch is 'taken' in other words the condition is satisfied and the program jumps to the new location. So if the branch happens to go into another page, then effectively 2 cycles will be added.