Introduction
For this weeks lab, we were supposed to play with the following code in the 6502 emulator.
lda #$00 ; set a pointer in memory location $40 to point to $0200
sta $40 ; ... low byte ($00) goes in address $40
lda #$02
sta $41 ; ... high byte ($02) goes into address $41
lda #$07 ; colour number
ldy #$00 ; set index to 0
loop: sta ($40),y ; set pixel colour at the address (pointer)+Y
iny ; increment index
bne loop ; continue until done the page (256 pixels)
inc $41 ; increment the page
ldx $41 ; get the current page number
cpx #$06 ; compare with 6
bne loop ; continue until done all pages
This code will fill the screen up yellow colour. The first task of the lab was to calculate the how long does this code take to execute assuming a 1MHz clock speed and calculate the memory usage. This code should take 0.11319 seconds and 26 bytes to execute. Here is my analysis
The fun part: Optimization
This is what my optimized code looks like. this literally slices the execution time by more half
lda #$07
ldy #$00
loop:
sta $0200,y
sta $0300,y
sta $0400,y
sta $0500,y
iny
bne loop
New and different colour
To change the colour from yellow to blue, we need to update whats on the accumulator.
lda #$07 ; old
lda #$06 ; new
To get different colours in every quater, we can explicitly specify the colour we want to use.
ldy #$00
loop:
lda #$07
sta $0200,y
lda #$08
sta $0300,y
lda #$09
sta $0400,y
lda #$02
sta $0500,y
iny
bne loop
To get random colour in each pixel, we can use the one-byte pseudo-random number generator (PRNG) at $fe.
ldy #$00
loop:
lda $fe
sta $0200,y
lda $fe
sta $0300,y
lda $fe
sta $0400,y
lda $fe
sta $0500,y
iny
bne loop
Fun
One of the tasks was the colour the screen with different colours on the edges. I was able to get the top and bottom, but could not figure out how to do the right and left.
lda #$07
ldy #$00
loop:
sta $0200,y
sta $0300,y
sta $0400,y
sta $0500,y
iny
bne loop
lda #$2
loop2:
sta $0200,y
iny
cpy #$20
bne loop2
lda #$5
ldy #$e0
loop3:
sta $0500,y
iny
bne loop3
lda #$6
ldy #$e0
loop4:
sta $0500,y
iny
bne loop3
Reflection
I enjoy doing this lab, there was a lot of trial and error. But this is the part I like about learning and programming. Low level languages like assembly defiantly have a learning curve to it. I am still having difficulties understanding wrapping my heads around some of the concepts. This makes me admire the languages like python and java that we use these days even more. I learned the basics of 6502 language and how the syntax can be used to make changes on the small screen. I am looking forward to whats coming next.