; Title - Color Picker
; By    - Michael Garber
; Rev   - 1.1 - March 17, 2011
;
; About this Program
; ------------------ 
; This program lets the user change the background color using the knob
; and displays the value of the color in hex
;
; To Compile:
; zmac -i -o colorpicker.bin colorpicker.asm
; 
; Revisions:
;      1.1 - March 17, 2011 (Update by Adam Trionfo)
;            - Fixed to work with all versions of MESS Emulator
;              Changed Interrupt Mode from $18 to $08.
;              Thanks to Richard Degler for figuring this out a short
;              while back and providing the fix via private email exchanges.  
;      1.0 - May 5, 2007
;            - First Public Release




INCLUDE "HVGLIB.H"      ; Home Video Game Library Header

VERTLN	EQU	90			; number of rows used for screen data

        ORG    FIRSTC   ; First byte of Cartridge
        DB     $55      ; User Cartridge Sentinel

; Menu Selection Choice #1
        DW     MENUST   ; Next Menu Link
        DW     PRGNAM   ; Address of Program Name text
        DW     PRGST    ; Jump here if selected
 
; Program Start
PRGST:	DI                ; Disable interrupts

        SYSTEM (INTPC)    ; Start System Interpreter 

        DO     (SETOUT)   ; Set Display Ports
        DB     (VERTLN*2) ; Vertical Blanking Line (90 lines)
        DB     44         ; Left/Right Color Boundary 
        DB     $08        ; Interrupt Mode 

        DO     (COLSET)   ; Set Color Registers
        DW     COLTAB     ; Color Table

        DO     (FILL)     ; Screen Fill
        DW     $4000      ; Destination
        DW     (VERTLN*40)    ; Bytes to move
        DB     00000000B  ; Fill with

        DO     (STRDIS)   ; Display String
		DB     48         ; X
		DB     42         ; Y
		DB     00001101B  ; options
		DW     PRGNAM     ; COLOR PICK

        EXIT              ; Exit System Interpreter

        EI                ; Enable interrupts 

GAME:   IN   A,(POT0)     ; Get Pot 0 value (Player 1 knob)
        OUT  (COL0L),A    ; Left Color Register
		CALL DISPHEX      ; display hex val
		JR	GAME

; ----------------------------------------------------------------

DISPHEX:
		DI                ; Disable interrupts

		PUSH   AF

		LD     C,A
		SRL    C
		SRL    C
		SRL    C
		SRL    C
		LD     B,0
		LD     HL,HEXDIG
		ADD    HL,BC
		LD     A,(HL)
		LD     C, 00001101B  ; options
		LD     D,50       ; y
		LD     E,52       ; x
        SYSTEM (CHRDIS)   ; Display Char

		POP    AF

		AND    $f
		LD     C,A
		LD     B,0
		LD     HL,HEXDIG
		ADD    HL,BC
		LD     A,(HL)
		LD     C, 00001101B  ; options
		LD     D,50       ; y
		LD     E,60       ; x
        SYSTEM (CHRDIS)   ; Display Char

        EI                ; Enable interrupts

        RET

; ----------------------------------------------------------------

; Color Table #1

COLTAB: DB     $07        ; Color 3 Left 
        DB     $07        ; Color 2 Left
        DB     $03        ; Color 1 Left 
        DB     $00        ; Color 0 Left
        DB     $07        ; Color 3 Right
        DB     $07        ; Color 2 Right 
        DB     $03        ; Color 1 Right 
        DB     $00        ; Color 0 Right

; Program Name
PRGNAM: DB     'C','O','L','O','R',' ','P','I','C','K',$00

HEXDIG: DB     '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'

; End of Program
