SPO600 Lab4(Option 4): Screen Colour Selector Part B
Tags: spo600, 6502, assembly
Published: 2021-12-14 19:42:25

## Summary

This dev blog is the second part of the [previous one](https://dev.to/qiwenyu/6502-lab4option-4-screen-colour-selector-2k25). In this part, I have figured out all the subroutines required and put together as the final scripts.

## Final Implementation

```
; 
  
 lda #$00 
 sta COLOUR
 sta COLOUR_INDEX 

 jsr initialize_print ; jump to subroutine

; = get key input =
getKey:
 lda $ff
 sty $ff

 cmp #UP_KEY
 beq decrement_key

 cmp #DOWN_KEY 
 beq increment_key

 jmp getKey

decrement_key:
 lda COLOUR
 cmp #$01
 bpl decrement_colour

 jmp getKey

decrement_colour:
 dec COLOUR
 
 jsr initialize_print
 jsr initialize_paint
 jmp getKey

increment_key:
 lda COLOUR
 cmp #$0f
 bmi increment_colour

 jmp getKey

increment_colour:
 inc COLOUR
 jsr initialize_print
 jsr initialize_paint
 jmp getKey

; = print screen (Output) =
initialize_print:
 jsr SCINIT
        ldy #$00

title_write:
 lda title,y
        beq title_done
        jsr CHROUT
        iny
        bne title_write

title_done:
 lda #$00
 sta COLOUR_INDEX

write_colourStart:
 ora #$00
 ldy #$00

write_colourName:
 jsr select_colour
 beq write_done
 jsr toggle_highlight

 jsr CHROUT

 iny
 bne write_colourName

write_done:
 inc COLOUR_INDEX
 lda COLOUR_INDEX
 cmp #$10
 bne write_colourStart

select_colour:
 lda COLOUR_INDEX
 
 cmp #$00
 beq print_colour0

 cmp #$01
 beq print_colour1

 cmp #$02
 beq print_colour2

 cmp #$03
 beq print_colour3

 cmp #$04
 beq print_colour4

 cmp #$05
 beq print_colour5

 cmp #$06
 beq print_colour6

 cmp #$07
 beq print_colour7

 cmp #$08
 beq print_colour8

 cmp #$09
 beq print_colour9

 cmp #$0a
 beq print_colour10

 cmp #$0b
 beq print_colour11

 cmp #$0c
 beq print_colour12

 cmp #$0d
 beq print_colour13

 cmp #$0e
 beq print_colour14

 cmp #$0f
 beq print_colour15

 rts

print_colour0:
 lda colour0,y
 rts

print_colour1:
 lda colour1,y
 rts

print_colour2:
 lda colour2,y
 rts

print_colour3:
 lda colour3,y
 rts

print_colour4:
 lda colour4,y
 rts

print_colour5:
 lda colour5,y
 rts

print_colour6:
 lda colour6,y
 rts

print_colour7:
 lda colour7,y
 rts

print_colour8:
 lda colour8,y
 rts

print_colour9:
 lda colour9,y
 rts

print_colour10:
 lda colour10,y
 rts

print_colour11:
 lda colour11,y
 rts

print_colour12:
 lda colour12,y
 rts

print_colour13:
 lda colour13,y
 rts

print_colour14:
 lda colour14,y
 rts

print_colour15: 
 lda colour15,y
 rts

toggle_highlight:
 ldx COLOUR_INDEX
 cpx COLOUR
 beq highlight

 ora #$00
 rts

highlight:
 ora #$80
 rts

; = Screen Paint =
; = Initialize =
initialize_paint:
 lda #$00         ; set a pointer at $40 to point to $0200
        sta POINTER
        lda #$02
        sta POINTER_H
 
 ldy #$00

 lda COLOUR

draw_screen:
  sta ($40), y     ; set pixel

        iny              ; increment index
        bne draw_screen  ; continue until done the page

        inc $41          ; increment the page
        ldx $41          ; get the page
        cpx #$06         ; compare with 6
        bne draw_screen  ; continue until done all pages

 rts

; = for screen printing =
title:
dcb "C","o","l","o","u","r","s",13,00

colour0:
dcb "0",".",32,"B","l","a","c","k",13,00

colour1:
dcb "1",".",32,"W","h","i","t","e",13,00

colour2:
dcb "2",".",32,"R","e","d",13,00

colour3:
dcb "3",".",32,"C","y","a","n",13,00

colour4:
dcb "4",".",32,"P","u","r","p","l","e",13,00

colour5:
dcb "5",".",32","G","r","e","e","n",13,00

colour6:
dcb "6",".",32,"B","l","u","e",13,00

colour7:
dcb "7",".",32,"Y","e","l","l","o","w",13,00

colour8:
dcb "8",".",32,"O","r","a","n","g","e",13,00

colour9:
dcb "9",".",32,"B","r","o","w","n",13,00

colour10:
dcb "1","0",".",32,"L","i","g","h","t",32,"r","e","d",13,00

colour11:
dcb "1","1",".",32,"D","a","r","k",32,"g","r","e","y",13,00

colour12:
dcb "1","2",".",32,"G","r","e","y",13,00

colour13:
dcb "1","3",".",32,"L","i","g","h","t",32,"g","r","e","e","n",13,00

colour14:
dcb "1","4",".",32,"L","i","g","h","t",32,"b","l","u","e",13,00

colour15:
dcb "1","5",".",32,"L","i","g","h","t",32,"g","r","e","y",13,00
```

## What I have Learned

From this lab, I have learned how to work with strings in assembly language. Moreover, one need to use ROM, input and output key, and implement highlight characters as well.