DEV Community

Krinskumar Vaghasia
Krinskumar Vaghasia

Posted on

SPO Week 1.2 - learnings

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

Enter fullscreen mode Exit fullscreen mode

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

Image description

Image description

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Image description

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
Enter fullscreen mode Exit fullscreen mode

Conclusion

I enjoy doing this lab, there was a lot of trial and error. But this is the part I like about learning and programming.

Top comments (0)