SPO600 Lab3 - Math Lab Update Kaleidoscope
Published: 2021-11-23 23:06:12

Hello my friend, good to see you again here. Today I'm going to update the lab 3 that I started a couple weeks ago. Unfortunately, my group mates and I are unable to solve the Pong game by using 6502 Assembly language. Our problem is we are not sure how to make it bounce on a wall. So we decided to start a new project, if you are still interested in the Pong game and want to explore it further, [click here](https://dev.to/qzhang125/lab3-math-lab-200b). But now the new project is Kaleidoscope.

For this project we needed to create a project to draw the same shape in each quarter of the screen when pressing the arrow keys. This is the code:

```
define POINTER     $10   	;ptr: start of row
 define POINTER_H   $11 
define ROW     $20 		;current row
 define COL     $21 		; current column
 define DOT     $01 		; dot colour location
 define CURSOR      $05 	; green colour
setup: lda #$0f    		; initial ROW,COL
    sta ROW
    sta COL
    LDA #$01
    STA DOT
 draw:  jsr draw_cursor
 getkey:    lda $ff     	; get the keystroke
    ldx #$00    		; clear the key buffer
    stx $ff
    cmp #$30
    bmi getkey
    cmp #$40
    bpl continue
    SEC
    sbc #$30
    tay
    lda color_pallete, y
    sta DOT
    jmp done
 
continue:   cmp #$43    
    beq clear
    cmp #$63
    beq clear
    cmp #$80    
    bmi getkey
    cmp #$84
    bpl getkey
    pha    
    lda DOT
    sta (POINTER),y
    jsr draw_on_quads
    pla
    cmp #$80
    bne check1
    dec ROW  
    jmp done
 
 check1:    cmp #$81    ; right key
    bne check2
    inc COL     ; increment COL
    jmp done
 
 check2:    cmp #$82    ; down key
    bne check3
    inc ROW     ; increment ROW
    jmp done
 
 check3:    cmp #$83    ; left key
    bne done
    dec COL     ; decrement COL
    clc
    bcc done
 
 clear: lda table_low   ; clear screen
    sta POINTER
    lda table_high
    sta POINTER_H
    ldy #$00
    tya
 
 c_loop:    sta (POINTER),y
    iny
    bne c_loop
    inc POINTER_H
    ldx POINTER_H
    cpx #$06
    bne c_loop
 
 done:  clc   
    bcc draw
 
 draw_cursor:
    lda ROW     
    and #$0f
    sta ROW
    lda COL     
    and #$0f
    sta COL
    ldy ROW     
    lda table_low,y
    sta POINTER
    lda table_high,y
    sta POINTER_H
    ldy COL     
    lda #CURSOR
    sta (POINTER),y
    rts
 
 draw_on_quads:     
    LDA POINTER 
    PHA         
    LDA POINTER_H
    PHA
    LDA #$10
    CLC
    SBC COL
    CLC
    ADC #$10
    TAY
    LDA DOT
    STA (POINTER),y
    TYA
    PHA 
    lda #$10    
    CLC
    SBC ROW
    CLC
    ADC #$10
    TAY
    lda table_low,y
    sta POINTER
    lda table_high,y
    sta POINTER_H
    ldy COL     
    lda DOT
    sta (POINTER),y
    PLA
    TAY
    lda DOT
    sta (POINTER),y
    PLA
    STA POINTER_H
    PLA
    STA POINTER
    RTS
 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
color_pallete:
dcb $01,$02,$03,$04,$05,$06,$07,$08,$09,$0a
```
This is a sample of the result:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3z8i35y70c7hu89vpcko.png)

##Reflection##
In this lab, we actually worked on a fully functional program, this program is available to receive user’s input and draw out a diagram. After I finished this lab, I learned how to work on subroutines and understand the syntax and logic of assembly language deeply. Assembly language is a low-level programming language which frequently and explicitly accesses the memory and registers.  I hope this practice will help me to work on 6502 assembly language easier in the future.