lab 1 mob programming

In this lab our focus was on the 6502 Emulator, a tool that simulates the classic 6502 microprocessor. The task involved filling a bitmapped display with different colors and optimizing the performance of the code. This blog post will outline our experiments, performance calculations, code modifications, and personal reflections on the process.

Initial Code: 
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

Performance Calculation: 
sta ($40),y: 6 cycles
iny: 2 cycles
bne loop: 3 cycles (since it branches most of the time)
Loop total for one pixel: 6 + 2 + 3 = 11 cycles
Total for 256 pixels: 256 * 11 = 2816 cycles
Increment page instructions: 13 cycles per page
Total for 6 pages: 6 * 2816 + 78 = 16974 cycles
Execution time at 1 MHz: 16974 cycles / 1,000,000 cycles per second = 0.016974 seconds

Memory Usage:
Total program bytes: 21 bytes

Optimized Code:
lda #$00
sta $40
lda #$02
sta $41
lda #$07
ldy #$00
loop:
    sta ($40),y
    iny
    sta ($40),y
    iny
    sta ($40),y
    iny
    sta ($40),y
    iny
    sta ($40),y
    iny
    sta ($40),y
    iny
    bne loop
    inc $41
    ldx $41
    cpx #$06
    bne loop

Modified code to display light blue:
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 #$e    ; 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

Modified code to display different colors per page:
lda #$00
sta $40
lda #$02
sta $41
lda #$05    ; starting color number
ldy #$00
loop:   sta ($40),y
    iny
    bne loop
    inc $41
    ldx $41
    cpx #$06
    bne change_color
    rts
change_color:
    clc
    adc #$01   ; increment color number
    jmp loop

Optional experiment with tya:
Effect: This code created a gradient effect because the tya instruction transferred the value of y into the accumulator before storing it, causing pixel values to vary with y.
Optional experiment with lsr: 
Effect: Shifting the bits right (lsr) changed the colors, creating a different gradient effect as the value was halved.
Optional experiment with multiple lsr instructions: 
Effect: Adding more lsr instructions progressively darkened the colors as bits were shifted out.
Optional experiment with asl instead of lsr: 
Effect: Using asl instead of lsr created brighter and more varied colors as bits were shifted left.

Reflections:
This lab was an insightful exploration into Assembly Language and the 6502 microprocessor. Mob Programming facilitated collaboration and shared learning. Understanding how to manipulate memory and registers at a low level provided a deeper appreciation for how high-level code is executed by hardware. The challenge of optimizing code for performance was particularly rewarding, showcasing the importance of efficiency in programming.

Comments

Popular posts from this blog

Project stage 3 Integrate, Tidy, & Wrap

Project stage 1 GCC build