DEV Community


Posted on • Updated on

Calculating 6502 Execution Time - Part 1

Today I'm working on lab 2 for my software portability and optimization class, wherein we're looking at writing and analyzing assembly code written for the MOS 6502 as emulated here. This is the first in a series of posts about writing assembly for the 6502, part 2 of which you can find here and part 3 of which you can find here. Because the emulator isn't a 1:1 recreation of the 6502 and execution time may vary from one to the other, as well as manual calculation being fairly simple, I'll be manually determining execution time of a program and going through the steps to do so here.

First off is breaking down the program into individual instructions, finding the cycles it takes to run an instruction by its mnemonic here.

Using a table like this one, we can look up the machine cycles it takes to run each instruction and compile these to calculate the total time.

Image description

Once we find the relevant instruction in the instruction set, we need to check what kind of memory is being accessed and enter the appropriate cycle count in our table. For example the first instruction, LDA #$00, references an immediate value as evidenced by the preceding # symbol.

Image description

Other possible options include:

  • Memory on the zero (first - $0000 - $00FF) page, or zero page with the value of the X register added.

Image description

These are faster than accessing other absolute memory locations as the addresses are only 8 bits.

Image description

  • Absolute addresses
  • Absolute addresses plus X
  • Absolute addresses plus Y

The latter two operations come with the condition that if the page boundary is crossed, 1 cycle should be added to the count.

Image description

  • Address plus X dereferenced
  • Address dereferenced plus Y

The latter comes with the aforementioned condition that a cycle is added if a page boundary is crossed.

In the next blog post I'll start using the info I've gone through in this post to measure the runtime of the program and begin optimizing it.

Top comments (0)