DEV Community

Tecca Yu
Tecca Yu

Posted on

A simple drawing program using 6502 Emulator - LAB3 part 1

Hi this is Tecca, in this post I'll be working on writing a drawing program in assembly language that runs on 6502 Emulator.

The user will be able to draw pixels on the screen using arrow keys on keyboard.

Source code

; zero-page variable locations
define  ROW     $20 ; current row
define  COL     $21 ; current column
define  POINTER     $10 ; ptr: start of row
define  POINTER_H   $11

; constants
define  DOT     $03 
define  CURSOR      $04 


setup:      lda #$0f    ; set initial ROW,COL
        sta ROW
        sta COL

draw:       lda ROW     ; ensure ROW is in range 0:31
        and #$1f
        sta ROW

        lda COL     ; ensure COL is in range 0:31
        and #$1f
        sta COL

        ldy ROW     ; load POINTER with start-of-row
        lda table_low,y
        sta POINTER
        lda table_high,y
        sta POINTER_H

        ldy COL     ; store CURSOR at POINTER plus COL
        lda #CURSOR
        sta (POINTER),y

getkey:     lda $ff     ; get a keystroke
        beq getkey

        ldx #$00    ; clear out the key buffer
        stx $ff

        cmp #$43    ; handle C or c
        beq clear
        cmp #$63
        beq clear

        cmp #$80    ; if not a cursor key, ignore
        bmi getkey
        cmp #$84
        bpl getkey

        pha     ; save A

        lda #DOT    ; set current position to DOT
        sta (POINTER),y

        pla     ; restore A

        cmp #$80    ; check key == up
        bne check1

        dec ROW     ; ... if yes, decrement ROW
        jmp done

check1:     cmp #$81    ; check key == right
        bne check2

        inc COL     ; ... if yes, increment COL
        jmp done

check2:     cmp #$82    ; check if key == down
        bne check3

        inc ROW     ; ... if yes, increment ROW
        jmp done

check3:     cmp #$83    ; check if key == left
        bne done

        dec COL     ; ... if yes, decrement COL
        clc
        bcc done

clear:      lda table_low   ; clear the screen
        sta POINTER
        lda table_high
        sta POINTER_H

        ldy #$00
        tya


done:       clc     ; repeat
        bcc draw


; these two tables contain the high and low bytes
; of the addresses of the start of each row

table_high:
dcb $02,$02,$02,$02,$02,$02,$02,$02
dcb $03,$03,$03,$03,$03,$03,$03,$03
dcb $04,$04,$04,$04,$04,$04,$04,$04
dcb $05,$05,$05,$05,$05,$05,$05,$05,

table_low:
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0


Enter fullscreen mode Exit fullscreen mode

Result:
At start it will show the cursor at the center of the bitmap indicating where the drawing begins.
Image description

Then it is waiting for user's keyboard action and checks which keystroke did the user press on to make drawing.

Image description
Image description

This is a simple drawing program that runs on 6502 emulator and I plan on to create a kaleidoscope drawing program base on this in the next post....to be continued..

Top comments (1)

Collapse
 
andypiper profile image
Andy Piper

did you mean to include some content in your post here?