SPO600 - Lab 2

This blog post is for my SPO600 class I am participating at Seneca College, this is related to Lab 2 detailed here.

Beginning Lab 2 we are given a piece of assembly code to run on our 6502 Emulator online. The code is as follows:

This code simply colours the entirety of the 32x32 graphics display with a dull yellow. We are tasked with calculating the performance, finding a way to reduce the runtime of this program, as well as modifying the colour(s) that appear in the display.

Calculating Performance

Shown above are all of the instructions involved in the program, with the number of cycles the instruction takes (Cycles), and the number of times the instruction should execute (Cycle Count). The conclusion is easy to see, runtime is approximately 11325 microseconds, 11.325 milliseconds, or 0.011325 seconds.

The Breakdown

To break this down, I can start with the instructions above the loop point. Each of these instructions only execute once. Simple enough, their total cycles are equal to the cycles the instruction takes.

Next are the instructions in the first part of the loop: 'STA($40),y' down to the first instance of 'BNE loop'. How many times will each of these instructions execute? The answer is in the instruction 'BNE loop'. BNE is waiting for the Y register to equal 0, and that will happen after 256 increments during the loop. Y begins at 0 and will be incremented all the way to 255 and after the next increment will wrap back around to 0. After this point the loop will no longer branch and continue to the next section. Knowing this we can say that each instruction in this section will run 256 times. Note that BNE has alt cycle counts, the final time BNE executes it does not branch and therefore the amount of cycles it takes changes.

You may have noticed now that the cycle counts for instructions in that first section do NOT equal 256 like they should. Why don't they? The answer lies once again in the instruction 'BNE loop'.

In this next section the instructions are basically incrementing the value that is responsible for addressing the current page in the display. The display has four pages, addressed by memory locations $0200 through $05FF. Four pages, each with 256 pixels.

Knowing this, and what the purpose of the program is, we can begin to see why the first loop would have a higher counts than 256. The value for addressing the page begins at 02, once we reach this part of the loop it is incremented. The next instructions store that value in the X register and check if it equals 06. If this does not evaluate to true, 'BNE loop' branches all the way back to the top of the loop. Now back at the top, y is now back to 0. So the process will repeat, this portion of the loop will run 256 times, this time painting the $0300 to $03FF section of the display. That's already double the executions for the first section of the loop. This portion of the loop will branch 3 times before continuing, resulting in the first portion of the loop executing fully (256 loops) 4 times, for 1024 loops total.

The totals of the cycle counts in the first half of the loop reflect this, and the 2nd half of the loop will execute 4 times total.

There we have it! The breakdown of calculating the execution time of this program painting our fancy 32x32 pixel display. Since this post ended up being so wordy I will continue lab 2 in another post, detailing the modifications that are requested.

Comments