SPO600 - LAB 1: 6502 - Assembly Language Lab (Part 1)
In this lab 1, I have learnt basic 6502 assembly language and used an emulator to manipulate a bitmap display. I learnt how to calculate program execution time, optimize performance, and change code to achieve various visual effects.
Bitmap Code
In this step, I configure the emulator and run the initial code, which fills the screen with yellow pixels.
2. I copied and pasted the following assembly code into the 6502 Emulator:
3. Then I pressed the "Assemble" button and "Run" to assemble and execute the code, which successfully filled the screen with yellow pixels.
- The code fills the bitmapped display with yellow by storing the yellow colour code (#07) at sequential memory addresses that correspond to pixels.
- It employs indirect indexed addressing (sta ($40),y). with memory locations $40 and $41 pointing to the beginning of the display memory ($0200).
- The iny instruction raises the index, looping through each pixel in a page, while bne keeps the loop running until all pages are filled.
Calculating Performance
4. The next step is to calculate the program's performance in terms of execution time. We assume the system runs at 1 MHz clock speed.
lda #$00
and similar instructions for setting up initial values (address and color):
- These are initialization instructions executed only once. Each lda
takes 2 cycles.
Inner Loop Instructions (per pixel, 256 pixels per page):
- sta ($40),y
: Store accumulator to memory using indirect indexed addressing. This takes 6 cycles per execution.
- iny
: Increment Y register. This takes 2 cycles.
- bne loop
: Branch if not equal (loop back to sta
until Y reaches 256). This takes 3 cycles when the branch is taken and 2 cycles when it isn’t.
Outer Loop Instructions (per page, executed 5 times to cover all 6 pages):
- inc $41
: Increment high byte of the memory address (to move to the next page). This takes 5 cycles.
- ldx $41
: Load X register from memory (getting the current page). This takes 3 cycles.
- cpx #$06
: Compare X register with 6 (check if we’ve reached the last page). This takes 3 cycles.
- bne loop
: Branch if not equal (repeat for the next page). This takes 3 cycles when the branch is taken and 2 when it isn’t.
Total Cycles per Page (Inner Loop):
For each page, there are 256 iterations of the inner loop:
- sta ($40),y
: 256 * 6 cycles = 1536 cycles.
- iny
: 256 * 2 cycles = 512 cycles.
- bne loop
: 255 times it takes 3 cycles, and once it takes 2 cycles (when it exits the loop):
So, the total cycles per page for the inner loop are:
Total Cycles for All Pages (Outer Loop):
The outer loop increments the page and checks if the last page has been reached:
- inc $41
: 5 cycles.
- ldx $41
: 3 cycles.
- cpx #$06
: 3 cycles.
- bne loop
: 3 cycles (5 times, since the final time doesn't branch).
So, for 5 page transitions (from page 1 to page 6):
Grand Total Cycles:
Inner loop cycles for 6 pages:
cycles.Outer loop cycles: 70 cycles.
Thus, the grand total is:
Execution Time:
With a 1 MHz clock, the total execution time is calculated as:
5. Memory Usage
We will calculate the total memory usage for the program, including the program code and any pointers or variables.
Each instructions typically occupies 2 bytes. The program consists of around 14 instructions, so the total size of the program code is approximately:
14 instructions x 2 bytes/instruction = 28 bytes.
The pointer to the memory address (stored in $40 and $41) uses 2 bytes.
Thus, the total memory usage is:
28 bytes + 2 bytes for pointers = 30 bytes.
6. Optimizing the Code to Reduce Execution Time
We need to fine one or more ways to decrease the time taken to fill the screen with a solid color. The goal is to make the execution time as fast as possible.
lda #$07 ; color number (yellow)
ldy #$00 ; start index at 0
lda #$00 ; low byte for base address
sta $40
lda #$02 ; high byte for base address
sta $41
fill_loop:
sta ($40),y ; store the color at the address (pointer) + Y
iny ; increment index
sta ($40),y ; store again
iny
sta ($40),y ; store again
iny
sta ($40),y ; store again
iny
sta ($40),y ; store again
iny
bne fill_loop ; loop for 256 pixels
inc $41 ; increment page
ldx $41 ; get page number
cpx #$06 ; compare with 6
bne fill_loop ; loop for 6 pages
Nhận xét
Đăng nhận xét