Hello, Blog!
As promised, in this post, I’ll share the small tweaks I made to modify the functionality of the screen colouring 6502 assembly program.
- Check out the previous post for complete breakdown of the base program's logic: 6502 Assembly: Calculating Code Performance
- Find the 6502 emulator I used here: 6502 Emulator
If you have just stumbled upon my SPO600 series of blog posts, it has been created to document and share my learnings as I progress through my Software Portability and Optimization college course.
Taking a look at the given program
The following code fills the emulator's display with the yellow colour:
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
Filling the display with a different colour
To fill the display with the colour of choice I just needed to load the value associated with the colour into the accumulator.
- You can find information about the colour values by clicking the "Notes" button in the emulator and scrolling down to the "Bitmapped Display" section.
We can see that light blue = $e
lda #$00
sta $40
lda #$02
sta $41
lda #$0e ; load $0e (light blue) instead of $07 (yellow) into the accumulator
ldy #$00
loop: sta ($40),y
iny
bne loop
inc $41
ldx $41
cpx #$06
bne loop
Filling the display with different colour on each page
To colour each of the four pages with different colours, I decided to increment the colour value stored in the accumulator each time a page gets filled.
lda #$00
sta $40
lda #$02
sta $41
lda #$07
ldy #$00
loop: sta ($40),y
iny
bne loop
inc $41
adc #$01 ; increment the value in the accumulator, so the next page is coloured with different colour
ldx $41
cpx #$06
bne loop
Making each pixel a random colour
Achieving this was a bit trickier, as getting random colour values involved using pseudo-random number generator located at address $FE
in the emulator's Zero Page.
lda #$00
sta $40
lda #$02
sta $41
ldy #$00
loop: lda $fe ; load colour value from the pseudo-random number generator on each loop iteration
sta ($40),y
iny
bne loop
inc $41
ldx $41
cpx #$06
bne loop
P. S.
Playing around with the Assembly code provided by the professor was fun. I found that the key to effectively tweaking the code was a solid understanding of the logic behind the original program.
Thank you for reading my post!