SPO600 Lab 2
Lab 2, where I have a given 6502 assembly code:
lda #$00 ; set a pointer at $40 to point to $0200 sta $40 lda #$02 sta $41 lda #$07 ; colour number ldy #$00 ; set index to 0 loop: sta ($40),y ; set pixel at the address (pointer)+Y iny ; increment index bne loop ; continue until done the page inc $41 ; increment the page ldx $41 ; get the current page number cpx #$06 ; compare with 6 bne loop ; continue until done all pages
My job is to play around with the code, calculate the performance of the given code, and try to optimize the code to have a better performance.
First, starting to run the code on http://6502.cdot.systems/ - a website that stimulates a 6502 emulator, I got a result of a yellow screen, which consists of 4 pages.
Let's look at the performance by calculating the number of cycles and cycle count. We will do this on Microsoft Excel to have a clear perspective.
After doing some calculations on Excel, I got a result of 0.012 seconds. How did I find the cycles and cycle count for each instruction? What I did was look at the instruction set document. For example, the first line of code, LDA #$00, looking at the instruction set, I found:
We can find the line of code would take 2 cycles because it is immediate addressing (with #). Additionally, it only takes one count since it is not in any loops. It is more advanced in counting the cycle count in the loop. The program has a nested loop. The parent loop loops 4 times since there are 4 pages on the screen. For each page, we will increase the y index 256 times.However, we can still optimize the code to have better performance. Instead of getting the current page and comparing it to 6 every time the page gets increased. We can load the index X with 6 outside the loop and compare it with the address $41 inside the loop. This way, the cycle count decreased by 3. However, we still can reduce the number of cycles. Another way is we can make 4 manual loops for each page. Or even more hard-coded, where we manually load the colour for each pixel. In this way, the cycles are significantly cut down. All we have to do is load the accumulator with the colour, then store it to each pixel on each page. So the program would only have 4098 cycles, compared to 12115 cycles in the first program.
To change the colour to blue, we only need to change the colour number to 6, as referred to in the 6502 emulator notes.
To make each page have a different colour, we can store the initial colour number to an address, such as $42. At the start of the loop, we load the accumulator to the number stored in the address and increase it after each page is filled.
Comments
Post a Comment