SPO600 String lab option 2: Color Selector
Published: 2021-11-24 05:05:23

#Introduction#
Hello my friend, welcome back to the second part of the string lab. In this lab, we decided to create 2 programs and split it into [part 1](https://dev.to/qzhang125/spo600-lab4-6502-string-lab-option-1-adding-calculator-17m8) and part 2 using 6502 assembly language. In the first part, we created an adding calculator and now what you are reading is the second part of this lab. In this part, we decided to create a color selector. This program allows the user to choose the color on the right side screen by using keystroke, then the program will print the certain color on the screen.

#Steps#
1. Create a ROM to print the color string on the screen.
2. Create the up key and down key then assign the functionality. It will allow users to select the color string on the screen.
3. Print the title of the color string.
4. Print 15 color strings using loop
5. Use a loop to highlight the name string that the user has selected.
6. Use draw_screen loop to change the color of the pixels.

#Code#
```
define SCINIT  $ff81 ; initialize/clear screen
define CHROUT  $ffd2 ; output character to screen
 
define COLOUR  $10
define COLOUR_INDEX $11
define POINTER  $40
define POINTER_H $41
define UP_KEY  $80
define DOWN_KEY $82
 
 lda #$00 
 sta COLOUR
 sta COLOUR_INDEX 
 
 jsr initializePrint
 
getKey:
 lda $ff
 sty $ff
 
 cmp #UP_KEY
 beq decrementKey
 
 cmp #DOWN_KEY 
 beq incrementKey
 
 jmp getKey
 
decrementKey:
 lda COLOUR
 cmp #$01
 bpl decrementColour
 
 jmp getKey
 
decrementColour:
 dec COLOUR
 
 jsr initializePrint
 jsr initializePaint
 jmp getKey
 
incrementKey:
 lda COLOUR
 cmp #$0f
 bmi incrementColour
 
 jmp getKey
 
incrementColour:
 inc COLOUR
 jsr initializePrint
 jsr initializePaint
 jmp getKey
 
initializePrint:
 jsr SCINIT
        ldy #$00
 
writeTitle:
 lda title,y
        beq titleDone
        jsr CHROUT
        iny
        bne writeTitle
 
titleDone:
 lda #$00
 sta COLOUR_INDEX
 
startColour:
 ora #$00
 ldy #$00
 
colourName:
 jsr selectedColour
 beq afterWriting
 jsr highlightLine
 
 jsr CHROUT
 
 iny
 bne colourName
 
afterWriting:
 inc COLOUR_INDEX
 lda COLOUR_INDEX
 cmp #$10
 bne startColour
 
selectedColour:
 lda COLOUR_INDEX
 
 cmp #$00
 beq printColour0
 
 cmp #$01
 beq printColour1
 
 cmp #$02
 beq printColour2
 
 cmp #$03
 beq printColour3
 
 cmp #$04
 beq printColour4
 
 cmp #$05
 beq printColour5
 
 cmp #$06
 beq printColour6
 
 cmp #$07
 beq printColour7
 
 cmp #$08
 beq printColour8
 
 cmp #$09
 beq printColour9
 
 cmp #$0a
 beq printColour10
 
 cmp #$0b
 beq printColour11
 
 cmp #$0c
 beq printColour12
 
 cmp #$0d
 beq printColour13
 
 cmp #$0e
 beq printColour14
 
 cmp #$0f
 beq printColour15
 
 rts
 
printColour0:
 lda colour0,y
 rts
 
printColour1:
 lda colour1,y
 rts
 
printColour2:
 lda colour2,y
 rts
 
printColour3:
 lda colour3,y
 rts
 
printColour4:
 lda colour4,y
 rts
 
printColour5:
 lda colour5,y
 rts
 
printColour6:
 lda colour6,y
 rts
 
printColour7:
 lda colour7,y
 rts
 
printColour8:
 lda colour8,y
 rts
 
printColour9:
 lda colour9,y
 rts
 
printColour10:
 lda colour10,y
 rts
 
printColour11:
 lda colour11,y
 rts
 
printColour12:
 lda colour12,y
 rts
 
printColour13:
 lda colour13,y
 rts
 
printColour14:
 lda colour14,y
 rts
 
printColour15: 
 lda colour15,y
 rts
 
highlightLine:
 ldx COLOUR_INDEX
 cpx COLOUR
 beq highlight
 
 ora #$00
 rts
 
highlight:
 ora #$80
 rts
 
initializePaint:
 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
 
title:
dcb "L","i","s","t",32,"o","f",32,"C","o","l","o","u","r","s",":",13
dcb 00
 
colour0:
dcb "B","l","a","c","k",13
dcb 00
 
colour1:
dcb "W","h","i","t","e",13
dcb 00
 
colour2:
dcb "R","e","d",13
dcb 00
 
colour3:
dcb "C","y","a","n",13
dcb 00
 
colour4:
dcb "P","u","r","p","l","e",13
dcb 00
 
colour5:
dcb "G","r","e","e","n",13
dcb 00
 
colour6:
dcb "B","l","u","e",13
dcb 00
 
colour7:
dcb "Y","e","l","l","o","w",13
dcb 00
 
colour8:
dcb "O","r","a","n","g","e",13
dcb 00
 
colour9:
dcb "B","r","o","w","n",13
dcb 00
 
colour10:
dcb "L","i","g","h","t",32,"r","e","d",13
dcb 00
 
colour11:
dcb "D","a","r","k",32,"g","r","e","y",13
dcb 00
 
colour12:
dcb "G","r","e","y",13
dcb 00
 
colour13:
dcb "L","i","g","h","t",32,"g","r","e","e","n",13
dcb 00
 
colour14:
dcb "L","i","g","h","t",32,"b","l","u","e",13
dcb 00
 
colour15:
dcb "L","i","g","h","t",32,"g","r","e","y",13
dcb 00
```
The sample results :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf3qdq2zyw43e89zwhcs.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fg81inopikg39y7cn5x2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qub8kpevrufg9l72sct5.png)
 
  
#Reflection#
In lab 4 part 2, we created a color selector using assembly language. It is a new challenge for us. The most tedious thing is to use different loops to print the color strings and highlight the user selections. Afterwards, we also have to use a loop to change the color pixels. The assembly language is hard for me as a beginner to learn and work with it in 4 different labs. I can not say I'm a master of it because I’m not hundred-percent familiar with the syntax and the coding logic like other higher programming languages. But I’m glad to learn and work on it for almost the whole semester and it shows me a view of a low level programming language that I never touched before, it also helps me to understand the higher-level programming language deeply on the storage and ram view.