Lab 2: drawing cloloured box on 6502 assembly



2 min read

Code could be tested on emulator below:

define GREEN $5
define BLUE  $6
define YELLOW $7
define PURPLE $8
;set screen start address list significant bit and most significant bit
define screen_start_lsb $00 ; low bit of pixel 1 address
define screen_start_msb $02 ; high bit of pixel 2 address
;pixel one is at address 0200

define pointer_to_pixel_1_lsb $0  ; RAM address 0, that will store current pixel
define pointer_to_pixel_1_msb $1 ; RAM address 1

define index_at_end_of_row $20 ; dec 32
; screen is 32 pixels wide

           ;will be using memory location $0 
           ; to store screen lsb, and $1 msb
           ;6502 is little-endian, so 16 bin (2 byte) values stored with lsb first
      lda #screen_start_lsb ; lda loads value into register 'A'
      sta pointer_to_pixel_1_lsb ; sta stores value of regiser 'A' into RAM
      lda #screen_start_msb
      sta pointer_to_pixel_1_msb  
           ;4 lines above represent 'zero addressing mode', since within first 265 bytes     ($0000-$00FF)

    ldy #$00 ; set index to 0

color_horisontal_lines: ; begining of a loop that colors
    lda #GREEN
    sta (pointer_to_pixel_1_lsb),y ; store value of 'A' (color green) 
           ; into address of first pixel plus index.
           ;this is example of pointer arythmetics, where 'Y' will be 
           ;an offset from pixel 1

    lda #PURPLE
    sta $05E0, y ;collor bottom left pixel + y offset

    cpy #index_at_end_of_row 
    bne color_horisontal_lines ; will continue to loop, untill finished line

    ldx #$04 ;four pages per screen
    lda #YELLOW
    sta (pointer_to_pixel_1_lsb),y
    lda pointer_to_pixel_1_lsb
    adc #$1f
    sta pointer_to_pixel_1_lsb ;now we incremented pointer,
         ; so it points to different pixel

    lda #BLUE
    sta (pointer_to_pixel_1_lsb),y
    ;inc pointer_to_pixel_1_lsb; trying to increase value in mem, did not work
    lda pointer_to_pixel_1_lsb ; then let's load it to 'A', and then increment
    adc #$01
    sta pointer_to_pixel_1_lsb    

    bcc color_sides

    inc pointer_to_pixel_1_msb
    bne color_sides