;PIC16F690 Configuration Bit Settings

; Assembly source line config statements

#include "p16F690.inc"

	;MCLR_ON pro reset button
	__CONFIG _FOSC_INTRCIO & _MCLRE_ON &_WDTE_OFF & _PWRTE_OFF & _BOREN_OFF & _CPD_OFF & _CP_OFF & _IESO_OFF & _FCMEN_OFF
    
    errorlevel    -302    


;******************************************************************
;****************** P R O M E N N E *******************************
;******************************************************************
;{   
    cblock 0x20
	d1			;registry pro cekaci subrutiny
	d2
	d3
	tmp
	
	hiByte
	loByte
	numMilis
	i				
	j				
	tens			;desitky - bin2ascii
	ones			;jednotky - bin2ascii
	number			;vypisovana cislice
	dataPointer:2	;pozice v grafickych datech
	textPointer:2	;Ukazatel do textu
	fontPointer:2	;Ukazatel do dat fontu
	
	;ST7735 TFT display variables
	TFT_skip
	TFT_i				;TFT_WriteByte
	TFT_j
	TFT_k
	TFT_len				;TFT_FillRectWithFGColor	
	TFT_tmp
	TFT_X				;Pozice pro display window
	TFT_X_MAX
	TFT_Y
	TFT_Y_copy			;TFT_drawText
	TFT_Y_MAX

	TFT_pieceFGColor:.2
	TFT_pieceBGColor:.2
	TFT_dataLen			;Pocet bytes graficky dat pro vykresleni	
	TFT_whichFont		;0-8x16, 1-8x9
	TFT_charWidth		;Vykreslovani fontu
	TFT_charHeight
	TFT_putChar			;Vykreslujeme pouze 1 znak, 0-ne, 1-ano

	;Joystick	
	JOY_ADresultH
	JOY_ADresultL	
	JOY_increment	;255=-1	
	JOY_whichAdjust ;0-BOARD_x, 1-BOARD_y
	JOY_buttonPressed
	
	;WT588D player	
	WT588D_i
	WT588D_tmp	
	
	BOARD_x_offset
	BOARD_y_offset
	BOARD_x			;pozice [x,y] na boardu [0,0]-[7,7]
	BOARD_y
	BOARD_x_old		;Predchozi pozice
	BOARD_y_old		
	BOARD_x_tmp
	BOARD_y_tmp
	BOARD_x_sel
	BOARD_y_sel
	BOARD_i
	BOARD_j
	BOARD_piece
	BOARD_piece_save  ;GAME_isGameOver
	BOARD_tmp
	BOARD_pegsLeft
	BOARD_selectOrder ;kterou pozici zrovna vybiram, 0-nic, 1-odkud, 2-kam
	BOARD_selectOrder_save
	BOARD_directions  ;bXXXXDCBA - A-West,B-East,C-North,D-South
	BOARD_someDirectionValid ;Lze v nekterem smeru provest skok
    endc

    cblock 0xA0
    board:.49				; 7x7 pole
    
    BOARD_x_vtmp			;BOARD_isPositionInValidPairs
    BOARD_y_vtmp
    BOARD_pairsFoundPos		;Flag W,E,N,S podle pozice
    BOARD_validToPairs:.8	;Zde budou ulozeny max 4 cilove pozice (X,Y)
    						;na ktere lze hrat z vybrane BOARD_x, BOARD_y
    						;pozice. Poradi je West,East,North,South.
    						;Pokud neni v nekterem smeru moznost hrat, bude
    						;na prislusne pozici tohoto pole 0xFF,0xFF.
    endc

;}
;******************************************************************
;****************** K O N S T A N T Y *****************************
;******************************************************************

FALSE				equ 0x00
TRUE				equ 0x01

;TFT 1.8" ST7735 defines
TFT_WIDTH			equ .128		;LCD w
TFT_HEIGHT			equ .160		;LCD h

SOUND_00_INTRO		equ 0x00
SOUND_01_MOVE		equ 0x01
SOUND_02_JUMP		equ 0x02
SOUND_03_INVALID	equ 0x03
SOUND_04_GAMEOVER	equ 0x04
SOUND_05_APPLAUSE	equ 0x05
SOUND_06_SELECT		equ 0x06
SOUND_07_CINK		equ 0x07
SOUND_08_MOVE2		equ 0x08
SOUND_09_MOVE3		equ 0x09


PIECE_SIZE			equ .16 		;px
PIECE_BYTES			equ PIECE_SIZE * 2
PEGS_TOTAL			equ .32

BOARD_WIDTH			equ .7
BOARD_HEIGHT		equ BOARD_WIDTH
BOARD_SIZE			equ BOARD_WIDTH*BOARD_HEIGHT
BOARD_X_OFFSET		equ (TFT_WIDTH-(BOARD_WIDTH*PIECE_SIZE))/2
BOARD_Y_OFFSET		equ .4
BOARD_Y_OFFSET_INTRO	equ BOARD_Y_OFFSET+.20	;Pro intro

;Pro BOARD_piece
BOARD_EMPTY			equ 0x00
BOARD_NOGO			equ 0x80
BOARD_PEG			equ 0x01
BOARD_SELECTED		equ 0x02	;bit 2, 0x04 - misto na boardu bylo oznaceno

BOARD_JUMPDIST		equ 0x02  	;Skok o 2 pole

;Pro BOARD_selectOrder
BOARD_NOSELECT		equ 0x00	
BOARD_FROM			equ 0x00	;bit 0, 0x01
BOARD_TO			equ 0x01	;bit 1, 0x02
BOARD_INVALID		equ 0x02	;bit 2, 0x04 value - invalid From/To
BOARD_TO_INVALID	equ 0xFF	;BOARD_validToPairs

BOARD_WEST			equ 0x00	;bit0
BOARD_EAST			equ 0x01	;bit1
BOARD_NORTH			equ 0x02
BOARD_SOUTH			equ 0x03

;AD limity
AD_LIMIT_LOWER_H	equ HIGH 	.342
AD_LIMIT_LOWER_L	equ LOW		.342
AD_LIMIT_UPPER_H	equ HIGH 	.682
AD_LIMIT_UPPER_L	equ LOW		.682

;Joystick
JOY_X_ADC			equ b'10000001' ;ADC Frc clock, right justify, AN0, ADC ON
JOY_Y_ADC			equ b'10000101' ;ADC Frc clock, right justify, AN1, ADC ON

;******************************************************************
;****************** D E F I N I C E *******************************
;******************************************************************

;{
;TFT 1.8" ST7735
#define TFT_SCK		PORTB, RB4;		(OUTPUT)
#define TFT_MOSI	PORTB, RB5;		(OUTPUT)
;#define TFT_DC		PORTB, RB6;		(OUTPUT)
#define TFT_DC		PORTC, RC7;		(OUTPUT) ;Ulomil jsem PIN :)
#define TFT_CS		PORTB, RB7;		(OUTPUT)

#define FONT_8x16					.0
#define FONT_8x16_CHAR_HEIGHT		.16		;8x16 = 1 znak
#define FONT_8x16_CHAR_WIDTH		.8

#define ST7735_NOP     		0x00
#define ST7735_SWRESET 		0x01
#define ST7735_RDDID   		0x04
#define ST7735_RDDST   		0x09
#define ST7735_SLPIN   		0x10
#define ST7735_SLPOUT  		0x11
#define ST7735_PTLON   		0x12
#define ST7735_NORON   		0x13
#define ST7735_INVOFF  		0x20
#define ST7735_INVON   		0x21
#define ST7735_DISPOFF 		0x28
#define ST7735_DISPON  		0x29
#define ST7735_CASET   		0x2A
#define ST7735_RASET   		0x2B
#define ST7735_RAMWR   		0x2C
#define ST7735_RAMRD   		0x2E
#define ST7735_PTLAR   		0x30
#define ST7735_VSCRDEF 		0x33
#define ST7735_COLMOD  		0x3A
#define ST7735_MADCTL  		0x36
#define ST7735_VSCRSADD 	0x37
#define ST7735_FRMCTR1 		0xB1
#define ST7735_FRMCTR2 		0xB2
#define ST7735_FRMCTR3 		0xB3
#define ST7735_INVCTR  		0xB4
#define ST7735_DISSET5 		0xB6
#define ST7735_PWCTR1  		0xC0
#define ST7735_PWCTR2  		0xC1
#define ST7735_PWCTR3  		0xC2
#define ST7735_PWCTR4  		0xC3
#define ST7735_PWCTR5  		0xC4
#define ST7735_VMCTR1  		0xC5
#define ST7735_RDID1   		0xDA
#define ST7735_RDID2   		0xDB
#define ST7735_RDID3   		0xDC
#define ST7735_RDID4   		0xDD
#define ST7735_PWCTR6  		0xFC
#define ST7735_GMCTRP1 		0xE0
#define ST7735_GMCTRN1 		0xE1
;Color definitions
#define ST7735_BLACK   		0x0000
#define ST7735_DARKBLUE    	0x00CF
#define ST7735_BLUE    		0x001F
#define ST7735_RED     		0xF800
#define ST7735_RED2			0xE041
#define ST7735_GREEN   		0x07A0
#define ST7735_CYAN    		0x07FF
#define ST7735_MAGENTA 		0xF81F
#define ST7735_YELLOW  		0xFFE0
#define ST7735_WHITE   		0xFFFF

   
;Joystick vstupy
#define JOY_VRX		PORTA, RA0
#define JOY_VRY		PORTA, RA1
#define JOY_SW		PORTA, RA5

;WT588D
#define WT588D_SCK	PORTC, RC2;
#define WT588D_CS	PORTC, RC1;
#define WT588D_SDA	PORTC, RC0;

;}

;******************************************************************
;****************** M A K R A *************************************
;******************************************************************
 
;{
BANK0 macro
    bcf STATUS, RP0 ;Select memory bank 0
    bcf STATUS, RP1 
    endm
    
BANK1 macro
    bsf STATUS, RP0 ;Select memory bank 1
    bcf STATUS, RP1 
    endm   
    
BANK2 macro
    bcf STATUS, RP0 ;Select memory bank 2
    bsf STATUS, RP1 
    endm   
    
BANK3 macro
    bsf STATUS, RP0 ;Select memory bank 3
    bsf STATUS, RP1 
    endm
;}    
   
;******************************************************************    
;*************************** S T A R T ****************************
;******************************************************************
    
	org 0x00
    goto main
	
; ******************************************************************
; ************************** M A I N *******************************
; ******************************************************************

main

	clrwdt
	nop
	
	BANK1
	movlw b'01110100'	;8MHz internal
	movwf OSCCON
	movlw b'11001000'
	movwf OPTION_REG

	;*** PORTA NASTAVENI - joystick ***
	BANK1
	movlw b'00100011'; RA0,RA1-Joystick, RA5-Joystick switch
	movwf TRISA 

	BANK0 	
	clrf PORTA
	
	banksel ADCON1
	movlw b'01110000' ; ADC Frc clock
	movwf ADCON1	
		
	BANK2
	movlw b'00000011'; AN0-RA0, AN1-RA1 - Joystick - analog
	movwf ANSEL
	clrf ANSELH
	clrf CM1CON0
	clrf CM2CON0
	
	;*** PORTB NASTAVENI - TFT 1.8" ***	
	BANK1
	clrf TRISB
	BANK0
	clrf PORTB   
	clrf CCP1CON 		; Turn CCP module off
	
	;*** PORTC NASTAVENI - WT588D player***	
	BANK1
	clrf TRISC		
	BANK0
	clrf PORTC
	
	;Inicializace Timer0
	clrf INTCON
			
;*******************************************************************
;******************* M A I N   L O O P *****************************
;*******************************************************************
;{
	
GAME_SETUP

	call TFT_init
	
	call TFT_setFont8x16
	
	movlw SOUND_00_INTRO
	call WT588D_doSoundFX		
		
GAME_START

	call GAME_showIntro

	call GAME_init
	
	movlw SOUND_02_JUMP
	call WT588D_doSoundFX
	
	call TFT_clearScreen	
	
	call TFT_drawBoard
	
	call TFT_showPegsLeft
					
	;Vybiram From
	bsf BOARD_selectOrder, BOARD_FROM
	bsf BOARD_selectOrder, BOARD_INVALID ;Na inicialni pozici nelze skakat
	call BOARD_getPlaceValue
	call TFT_drawPiece					 ;Na pozici BOARD_x, BOARD_y
		
GAME_LOOP

	call BOARD_saveOldPosition	;uschovej x,y -> x_old, y_old
	
	call JOY_getReadings
	
	call JOY_wasMove
	btfsc STATUS, Z			;Z-nebyl pohyb
	goto GAME_checkButton	;Zadny pohyb od posledni kontroly
	
	movlw SOUND_08_MOVE2
	call WT588D_doSoundFX
		
	;Zajisti uklid na predchozi pozici
	call BOARD_savePosition			  ;_x, _y -> _x_tmp, _y_tmp
	call BOARD_restoreOldPosition ;_x_old, _y_old -> _x, _y	
	call BOARD_getPlaceValue ; nastav BOARD_piece
	movf BOARD_selectOrder, w
	movwf BOARD_selectOrder_save		
	
	bcf BOARD_selectOrder, BOARD_INVALID	;predchozi nemuze byt invalid
	
	bsf BOARD_selectOrder, BOARD_TO
	btfss BOARD_piece, BOARD_SELECTED		;predchozi pozice vybrana?	
											;Ano, oznac (zlute pozadi)
	clrf BOARD_selectOrder	;=0-NOSELECT	;Ne, bez oznaceni (zelene pozadi)
	
	call TFT_drawPiece
		
	;A vykreslime situaci na nove pozici
	movf BOARD_selectOrder_save, w	;obnov
	movwf BOARD_selectOrder	
	call BOARD_restorePosition		;_x_tmp, _y_tmp -> _x, _y
	call BOARD_getPlaceValue 		;nastav BOARD_piece
	
	bcf BOARD_selectOrder, BOARD_INVALID	;valid/invalid se nastavi nize
			
	;Pokud vybirame To, tak zjisti, jestli je souradnice
	;BOARD_x,BOARD_y v seznamu validnich (pro dane vybrane From X,Y).
	btfss BOARD_selectOrder, BOARD_TO
	goto GAME_handleFromPosition

GAME_handleToPosition
	call BOARD_isPositionInValidPairs	;Z-nelze na To pozici hrat, NZ-lze
	goto GAME_handlePositionCommon

GAME_handleFromPosition
	call BOARD_computeFromDirections	;Spocitej, kterymi smery lze skakat z dane pozice
	call BOARD_isFromPositionPlayable   ;Projdi smery, Z-z pozice lze hrat, NZ-nelze
										;Tato rutina je potrebna i pro To pozici, nebot 
										;naplnuje seznam validnich To pozic.
GAME_handlePositionCommon
	btfsc STATUS, Z
	bsf BOARD_selectOrder, BOARD_INVALID	;Nastav cervenou barvu, nelze hrat
		
	call TFT_drawPiece
	
	movlw .100
	call waitMilis
	movlw .50
	call waitMilis
			
GAME_checkButton

	call JOY_getSwitchReading	;Z-byl stisk
	btfss STATUS, Z				;ZERO set - byl stisk
	goto GAME_LOOP

	;Pokud jsme na validnim miste, oznac ho
	btfsc BOARD_selectOrder, BOARD_INVALID
	goto GAME_placeInvalid
	
	;Ohandluj stisk ve FROM a TO
	btfss BOARD_selectOrder, BOARD_TO
	goto GAME_checkFrom

;Skok z A na B
GAME_checkTo

	;Moznost zahodit tah vybranim stejne pozice
	;Je _sel_x,_sel_y stejna jako _x, _y? Pak odznač tah
	movf BOARD_x_sel, w
	xorwf BOARD_x, w
	btfss STATUS, Z
	goto GAME_checkToNext
	movf BOARD_y_sel, w
	xorwf BOARD_y, w
	btfss STATUS, Z
	goto GAME_checkToNext	
	goto GAME_discardMove		;Provadim vyber cil=zdroj? Ano, zahod tah.

GAME_checkToNext
	;Uloz aktualni souradnice
	call BOARD_savePosition
	movlw BOARD_NOSELECT
	movwf BOARD_selectOrder	;Pro oba A,B mista chceme neoznacena mista
	
	;Peg na misto B
	movlw BOARD_PEG
	movwf BOARD_piece
	call BOARD_setPlaceValue
	call TFT_drawPiece	;Vykresli ho
	
	;Empty na preskakovany Peg
	call BOARD_setPosForIntermediate	;Musim zjistit, kterym smerem od B je
										;preskoceny Peg.
	movlw BOARD_EMPTY
	movwf BOARD_piece				
	call BOARD_setPlaceValue
	call TFT_drawPiece	;Vykresli ho						
		
	;Empty na misto A
	movf BOARD_x_sel, w
	movwf BOARD_x
	movf BOARD_y_sel, w
	movwf BOARD_y
	movlw BOARD_EMPTY
	movwf BOARD_piece
	call BOARD_setPlaceValue	
	call TFT_drawPiece	;Vykresli ho
	;Obnov puvodni cilovou pozici
	call BOARD_restorePosition
	call BOARD_getPlaceValue	;Potrebujeme spravny BOARD_piece
	;Spocitej hratelnost noveho mista a pokracuj

	movlw SOUND_02_JUMP
	call WT588D_doSoundFX

	decf BOARD_pegsLeft, f		;Sniz skore
	call TFT_showPegsLeft
	
	;Konec hry?
	call GAME_isGameOver
	btfsc STATUS, Z
	goto GAME_moveFinished
	
	call TFT_showGameOver	
	goto GAME_OVER
	
GAME_moveFinished
	;Dale vybirame FROM
	bcf BOARD_selectOrder, BOARD_TO
	bsf BOARD_selectOrder, BOARD_FROM	

	;Pro misto B hned nastav barvu, jestli lze hrat
	call BOARD_computeFromDirections
	call BOARD_isFromPositionPlayable
	btfsc STATUS, Z
	bsf BOARD_selectOrder, BOARD_INVALID	;Nastav cervenou barvu, nelze hrat
		
	call TFT_drawPiece	
	
	goto GAME_skip
	
;Vybrano misto A	
GAME_checkFrom	

	bcf BOARD_selectOrder, BOARD_FROM
	bsf BOARD_selectOrder, BOARD_TO
	
	;Uloz vybranou pozici
	movf BOARD_x, w
	movwf BOARD_x_sel
	movf BOARD_y, w
	movwf BOARD_y_sel
	
	;Uloz "selected" flag i do pole
	call BOARD_getPlaceValue
	bsf BOARD_piece, BOARD_SELECTED
	call BOARD_setPlaceValue

	movlw SOUND_06_SELECT
	call WT588D_doSoundFX
	
	call TFT_drawPiece
	
	goto GAME_skip
	
GAME_placeInvalid

	movlw SOUND_03_INVALID
	call WT588D_doSoundFX

	;Pokud jsme ve vyberu From, tak pokracujeme. Pokud To, tak jeste
	;musime zkontrolovat, jestli jsme na stejne pozici. Pokud ano, tak
	;tah resetuje/zneplatni a vybirame znovu From.
	btfss BOARD_selectOrder, BOARD_TO
	goto GAME_skip
	
	;Je _sel_x,_sel_y stejna jako _x, _y? Pak odznač tah
	movf BOARD_x_sel, w
	xorwf BOARD_x, w
	btfss STATUS, Z
	goto GAME_skip
	movf BOARD_y_sel, w
	xorwf BOARD_y, w
	btfss STATUS, Z
	goto GAME_skip
	
GAME_discardMove		;Provadim vyberem cil=zdroj
	
	;Puvodne bylo misto na boardu s flagem Selected
	call BOARD_getPlaceValue
	bcf BOARD_piece, BOARD_SELECTED
	call BOARD_setPlaceValue
	
	goto GAME_moveFinished
	
GAME_skip

	movlw .200
	call waitMilis
	
	goto GAME_LOOP
	
GAME_OVER

	movlw SOUND_04_GAMEOVER
	call WT588D_doSoundFX

	call wait1s
	call wait1s
	call wait1s
	
	;Pokud <= 5 pegs, zahrej aplaus
	movf BOARD_pegsLeft, w
	sublw .5
	btfss STATUS, C
	goto GAME_OVER_skip
	
	movlw SOUND_05_APPLAUSE
	call WT588D_doSoundFX
	
GAME_OVER_skip	

	call JOY_waitButton
	
	goto GAME_START
;}
	
;*******************************************************************
;*******************  G A M E    R U T I N Y ***********************
;*******************************************************************

GAME_init
;{

	movlw PEGS_TOTAL
	movwf BOARD_pegsLeft

	movlw .3
	movwf BOARD_x
	movwf BOARD_y
	
	;Pro intro page potrebuji posunout board nize
	movlw BOARD_X_OFFSET
	movwf BOARD_x_offset
	movlw BOARD_Y_OFFSET
	movwf BOARD_y_offset
	
	call BOARD_init

	return	
;}

GAME_showIntro
;{	
	call TFT_clearScreen	
	
	call TFT_colorSetBlackFG
	
	call TFT_showTitle

	movlw SOUND_00_INTRO
	call WT588D_doSoundFX
	
	movlw BOARD_X_OFFSET
	movwf BOARD_x_offset
	movlw BOARD_Y_OFFSET_INTRO
	movwf BOARD_y_offset
	
	call BOARD_initDemo
	
	call TFT_drawBoard	
	
	call TFT_showPressButton

GAME_introRepeat
	
	;Animace1
	;Prazdne misto
	clrf BOARD_selectOrder
	bsf BOARD_selectOrder, BOARD_FROM
	bsf BOARD_selectOrder, BOARD_INVALID
	movlw .2
	movwf BOARD_x
	movlw .2
	movwf BOARD_y	
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	clrf BOARD_selectOrder
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	;Posun doleva, oznac modre	
	bsf BOARD_selectOrder, BOARD_FROM
	bcf BOARD_selectOrder, BOARD_INVALID
	decf BOARD_x, f
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return

	;Oznac zlute
	bcf BOARD_selectOrder, BOARD_FROM	
	bsf BOARD_selectOrder, BOARD_TO
	bsf BOARD_piece, BOARD_SELECTED
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	;Posun se o 1 dolu (invalid)
	incf BOARD_y, f
	bcf BOARD_selectOrder, BOARD_FROM	
	bsf BOARD_selectOrder, BOARD_INVALID
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	clrf BOARD_selectOrder
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	;Posun se o 1 dolu (hratelne), zlute
	incf BOARD_y, f
	bsf BOARD_selectOrder, BOARD_TO
	bcf BOARD_selectOrder, BOARD_INVALID
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	;Udelej preskok - Peg v cilove pozici
	movlw BOARD_PEG
	movwf BOARD_piece
	call BOARD_setPlaceValue
	clrf BOARD_selectOrder
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	;Posun o 1 vyse, nastav Empty (mezi)
	clrf BOARD_selectOrder
	decf BOARD_y, f
	movlw BOARD_EMPTY
	movwf BOARD_piece
	call BOARD_setPlaceValue
	call BOARD_getPlaceValue	
	call TFT_drawPiece
	;call TFT_getAndDrawPiece
	
	;Posun o 1 vyse, nastav Empty (zdroj)
	clrf BOARD_selectOrder
	decf BOARD_y, f
	movlw BOARD_EMPTY
	movwf BOARD_piece
	call BOARD_setPlaceValue	
	call BOARD_getPlaceValue	
	call TFT_drawPiece
	;call TFT_getAndDrawPiece
	
	call TFT_showMinusOneLeft
	call JOY_waitCheckButton
	btfsc JOY_buttonPressed, 0
	return
	call JOY_waitCheckButton
	btfsc JOY_buttonPressed, 0
	return
	call JOY_waitCheckButton
	btfsc JOY_buttonPressed, 0
	return
	
	call TFT_showMinusOneLeftClear
	
	;Animace2
	;Prazdne misto
	clrf BOARD_selectOrder
	bsf BOARD_selectOrder, BOARD_FROM
	bsf BOARD_selectOrder, BOARD_INVALID
	movlw .6
	movwf BOARD_x
	movlw .4
	movwf BOARD_y	
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	clrf BOARD_selectOrder
	call TFT_getAndDrawPiece		
	btfsc JOY_buttonPressed, 0
	return
	
	;Posun nahoru, oznac modre	
	bsf BOARD_selectOrder, BOARD_FROM
	bcf BOARD_selectOrder, BOARD_INVALID
	decf BOARD_y, f
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return	

	;Oznac zlute
	bcf BOARD_selectOrder, BOARD_FROM	
	bsf BOARD_selectOrder, BOARD_TO
	bsf BOARD_piece, BOARD_SELECTED
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	;Posun se o 1 doleva (invalid)
	decf BOARD_x, f
	bcf BOARD_selectOrder, BOARD_FROM	
	bsf BOARD_selectOrder, BOARD_INVALID
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return	
	
	clrf BOARD_selectOrder
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return	
	
	;Posun se o 1 doleva (hratelne), zlute
	decf BOARD_x, f
	bsf BOARD_selectOrder, BOARD_TO
	bcf BOARD_selectOrder, BOARD_INVALID
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return
	
	;Udelej preskok - Peg v cilove pozici
	movlw BOARD_PEG
	movwf BOARD_piece
	call BOARD_setPlaceValue
	clrf BOARD_selectOrder
	call TFT_getAndDrawPiece
	btfsc JOY_buttonPressed, 0
	return	
	
	;Posun o 1 doprava, nastav Empty (mezi)
	clrf BOARD_selectOrder
	incf BOARD_x, f
	movlw BOARD_EMPTY
	movwf BOARD_piece
	call BOARD_setPlaceValue	
	call BOARD_getPlaceValue	
	call TFT_drawPiece
	;call TFT_getAndDrawPiece
	
	;Posun o 1 doprava, nastav Empty (zdroj)
	clrf BOARD_selectOrder
	incf BOARD_x, f
	movlw BOARD_EMPTY
	movwf BOARD_piece
	call BOARD_setPlaceValue	
	call BOARD_getPlaceValue	
	call TFT_drawPiece
	;call TFT_getAndDrawPiece
	
	call TFT_showMinusOneRight
	call JOY_waitCheckButton
	btfsc JOY_buttonPressed, 0
	return
	call JOY_waitCheckButton
	btfsc JOY_buttonPressed, 0
	return		
	call TFT_showMinusOneRightClear
	
	call JOY_waitCheckButton
	btfsc JOY_buttonPressed, 0
	return		
	call JOY_waitCheckButton
	btfsc JOY_buttonPressed, 0
	return				

	;Vrat zpet vse, at muzeme opakovat animaci
	;Reset animace1
	movlw .1
	movwf BOARD_x
	movlw .2
	movwf BOARD_y
	movlw BOARD_PEG
	movwf BOARD_piece
	call BOARD_setPlaceValue
	clrf BOARD_selectOrder
	call TFT_drawPiece
	incf BOARD_y, f
	call BOARD_setPlaceValue
	call TFT_drawPiece
	incf BOARD_y, f
	movlw BOARD_EMPTY
	movwf BOARD_piece
	call BOARD_setPlaceValue
	call TFT_drawPiece
	
	btfss JOY_SW
	return		
	
	;Reset animace2
	movlw .6
	movwf BOARD_x
	movlw .3
	movwf BOARD_y
	movlw BOARD_PEG
	movwf BOARD_piece
	call BOARD_setPlaceValue
	clrf BOARD_selectOrder
	call TFT_drawPiece
	decf BOARD_x, f
	call BOARD_setPlaceValue
	call TFT_drawPiece
	decf BOARD_x, f
	movlw BOARD_EMPTY
	movwf BOARD_piece
	call BOARD_setPlaceValue
	call TFT_drawPiece
	
	btfss JOY_SW
	return			
	
	goto GAME_introRepeat
			
;}

;Vrat Z-neni konec hry, NZ-je
;Pokud najdu 1 pozici, odkud je mozne skakat, ihned koncim.
GAME_isGameOver
;{
	call BOARD_saveOldPosition	
	movf BOARD_piece, w
	movwf BOARD_piece_save
	
	clrf BOARD_y
	
	movlw BOARD_HEIGHT
	movwf j

GAME_isGameOverLoopJ

	movlw BOARD_WIDTH
	movwf i
	
	clrf BOARD_x
	
GAME_isGameOverLoopI

	call BOARD_getPlaceValue		;Zjisti, co je na pozici na boardu? 
									;Nastaven BOARD_piece bez SELECTED flagu.
	movlw BOARD_PEG					;Zajimaji me pouze mista s Pegem.
	xorwf BOARD_piece, w
	btfss STATUS, Z
	goto GAME_isGameOverLoopINext
	
	call BOARD_computeFromDirections
	call BOARD_isFromPositionPlayable
	btfss STATUS, Z
	goto GAME_isNotGameOver			;NZ-lze hrat, koncime, neni konec hry.	

GAME_isGameOverLoopINext

	incf BOARD_x, f
	decfsz i, f
	goto GAME_isGameOverLoopI

	incf BOARD_y, f
	decfsz j, f
	goto GAME_isGameOverLoopJ
		
	call BOARD_restoreOldPosition
	movf BOARD_piece_save, w
	movwf BOARD_piece	
	bcf STATUS, Z		; Je konec hry	
	return
	
GAME_isNotGameOver
	call BOARD_restoreOldPosition	
	movf BOARD_piece_save, w
	movwf BOARD_piece
	bsf STATUS, Z
	return
;}

;*******************************************************************
;*************** B O A R D    R U T I N Y **************************
;*******************************************************************

BOARD_initCommon
;{
	;Fill with pegs
	movlw BOARD_SIZE
	movwf BOARD_i
	
	movlw board
	movwf FSR
	
	movlw BOARD_PEG
BOARD_iloop
	movwf INDF
	incf FSR, f
	decfsz BOARD_i, f
	goto BOARD_iloop
	
	;No go places
	BANK1
	movlw BOARD_NOGO
	movwf board+.0
	movwf board+.1
	movwf board+.5
	movwf board+.6	
	movwf board+.7
	movwf board+.8
	movwf board+.12
	movwf board+.13
	movwf board+.35
	movwf board+.36
	movwf board+.40
	movwf board+.41
	movwf board+.42
	movwf board+.43
	movwf board+.47
	movwf board+.48	
	return		
;}

BOARD_init
;{	

	call BOARD_initCommon
	
	;Empty starting place
	movlw BOARD_EMPTY
	movwf board+.24	
		
	BANK0
	return
;}

BOARD_initDemo
;{	

	call BOARD_initCommon
		
	;Empty starting place
	movlw BOARD_EMPTY

	;DEMO	
	movwf board+.2
	movwf board+.3

	movwf board+.9	
	movwf board+.10
	movwf board+.11
	
	movwf board+.14
	movwf board+.16
	
	movwf board+.17
	movwf board+.19
	
	movwf board+.20
	movwf board+.21
	movwf board+.23
	movwf board+.24
	movwf board+.25
	movwf board+.28
	movwf board+.29
	movwf board+.30
	movwf board+.32
	movwf board+.34
	movwf board+.37
	movwf board+.45
		
	BANK0
	return
;}



;Vraci hodnotu  board[BOARD_tmp]
;Vstup: BOARD_x, BOARD_y
;Vystup: WREG hodnota v poli
BOARD_getPlaceValue
;{
	call BOARD_setAndGetAddress
	movf INDF, w
	movwf BOARD_piece			;kopii sem
	return
;}

BOARD_setAndGetAddress
;{
	clrf BOARD_tmp
			
	bcf STATUS, C		;Vynasob 7 = 2+2+2+1		
	rlf BOARD_y, w    	;x 2	
	addwf BOARD_tmp, f	;+2
	addwf BOARD_tmp, f  ;+2
	addwf BOARD_tmp, f  ;+2
	movf BOARD_y, w		;+1
	addwf BOARD_tmp, f	;soucet
	
	movf BOARD_x, w
	addwf BOARD_tmp, f	
	movlw board
	movwf FSR
	movf BOARD_tmp, w
	addwf FSR, f
	return
;}

;Vystup: Z- pokud je misto na [BOARD_x, BOARD_y] prazdne
BOARD_isPlaceEmpty
;{
	call BOARD_getValue
	xorlw BOARD_EMPTY
	return
;}

;Vraci hodnotu BOARD[BOARD_tmp]
;Vstup: BOARD_x, BOARD_y
;Vystup: WREG hodnota v poli
BOARD_getValue
;{
	call BOARD_setAndGetAddress
	movf INDF, w
	movwf BOARD_tmp			;kopii sem
	return
;}



;Nastav hodnotu na BOARD_x, BOARD_y
;Vstup: BOARD_x, BOARD_y, W-REG s hodnotou
BOARD_restoreValue
;{
	call BOARD_setAndGetAddress
	movf BOARD_piece, w
	movwf INDF
	return	
;}

;Uloz aktualni pozici X,Y kurzoru
BOARD_savePosition
;{
	movf BOARD_x, w
	movwf BOARD_x_tmp
	movf BOARD_y, w
	movwf BOARD_y_tmp
	return
;}

;Obnov aktualni pozici X,Y kurzoru
BOARD_restorePosition
;{
	movf BOARD_x_tmp, w
	movwf BOARD_x
	movf BOARD_y_tmp, w
	movwf BOARD_y
	return
;}

;Uloz aktualni pozici X,Y kurzoru
;_x,_y -> _x_old, _y_old
BOARD_saveOldPosition
;{
	movf BOARD_x, w
	movwf BOARD_x_old
	movf BOARD_y, w
	movwf BOARD_y_old
	return
;}

;Obnov aktualni pozici X,Y kurzoru
BOARD_restoreOldPosition
;{
	movf BOARD_x_old, w
	movwf BOARD_x
	movf BOARD_y_old, w
	movwf BOARD_y
	return
;}

;Uloz kamen na pozici x,y - podle toho, kdo hraje
;Vstup: BOARD_x, BOARD_y
BOARD_setPlaceValue
;{
	call BOARD_setAndGetAddress
	movf BOARD_piece, w	
	movwf INDF
	return
;}

;Vstup: BOARD_x, BOARD_y
;Vystup nastavene dolni 4 bity BOARD_directions
;BOARD_directions: b0000DCBA - A-West,B-East,C-North,D-South
;Nastav bit, pokud je mozne v tomto smeru hledat platny tah (preskoceni)
;Tato subrutina bude pouzita pro test, zda lze z daneho mista provest skok.
BOARD_computeFromDirections
;{
	clrf BOARD_directions
	
	;X - smer West a East
	;Pro kazdy smer zjisti vzdalenost, jak daleko jsme od dolni / horni meze,
	;kam lze jit. Pokud je vzdalenost < 2, tak tim smerem nelze provest preskoceni.
	
	;West
	bcf JOY_whichAdjust, 0			;X
	
	movlw HIGH JOY_getLowerBound
	movwf PCLATH
	call JOY_getLowerBound		;W-reg obsahuje mez
	addlw BOARD_JUMPDIST
	subwf BOARD_x, w			;je vzd. X od meze aspon 2?
	btfsc STATUS, C				;Carry = ANO
	bsf BOARD_directions, BOARD_WEST
	
	;East	
	;upperlimit >= 2 + BOARD_x
	movlw HIGH JOY_getUpperBound
	movwf PCLATH
	call JOY_getUpperBound		;W-reg obsahuje mez
	movwf tmp
	
	movlw BOARD_JUMPDIST
	addwf BOARD_x, w
	subwf tmp, w
	btfsc STATUS, C				;Carry = ANO
	bsf BOARD_directions, BOARD_EAST
	
	;Y - smer North a South
	;North
	bsf JOY_whichAdjust, 0			;Y
	
	movlw HIGH JOY_getLowerBound
	movwf PCLATH
	call JOY_getLowerBound		;W-reg obsahuje mez
	addlw BOARD_JUMPDIST
	subwf BOARD_y, w			;je vzd. Y od meze aspon 2?
	btfsc STATUS, C				;Carry = ANO
	bsf BOARD_directions, BOARD_NORTH
	
	;South
	;upperlimit >= 2 + BOARD_y
	movlw HIGH JOY_getUpperBound
	movwf PCLATH
	call JOY_getUpperBound		;W-reg obsahuje mez
	movwf tmp
	
	movlw BOARD_JUMPDIST
	addwf BOARD_y, w
	subwf tmp, w
	btfsc STATUS, C				;Carry = ANO
	bsf BOARD_directions, BOARD_SOUTH
	
	return
;}


;Vstup: BOARD_x, BOARD_y, BOARD_directions
;Vystup: NZ-je mozne z toho mista skakat, Z-nelze
;Zaroven naplnime pole BOARD_validToPairs cilovymi pozicemi X,Y, na ktere lze
;z [BOARD_x, BOARD_y] skakat. Takto budu mit seznam platnych cilovych pozic.
BOARD_isFromPositionPlayable
;{
	
	clrf BOARD_someDirectionValid			;Zadny platny smer
	
	call BOARD_initValidToPairs				;Vycisti cilove pozice
	
	bcf BOARD_selectOrder, BOARD_INVALID	;nova pozice muze byt validni
	
	movf BOARD_directions, f		;==0 ? Nelze se hybat v zadnem smeru
	btfsc STATUS, Z
	return

	call BOARD_savePosition	;_x, _y -> _x_tmp, _y_tmp
		
	;Na pozici X musi byt peg, X-1 peg, X-2 empty
	btfss BOARD_directions, BOARD_WEST
	goto BOARD_isPosEast
	call BOARD_getValue		;v BOARD_tmp
	xorlw BOARD_PEG			;X=peg?
	btfss STATUS, Z
	goto BOARD_isPosEast	;Zkus smer EAST
	decf BOARD_x, f
	call BOARD_getValue
	xorlw BOARD_PEG			;X-1=peg?
	btfss STATUS, Z
	goto BOARD_isPosEast
	decf BOARD_x, f
	call BOARD_getValue		;kopie v BOARD_tmp, abych mohl zavolat restore a return
    xorlw BOARD_EMPTY		;X-2=empty?
    btfss STATUS, Z			
    goto BOARD_isPosEast	;nelze, dalsi smer
    	
	;Z-nastaveno, da se z X,Y hrat, ulozime do BOARD_validToPairs[0]
   	movf BOARD_x, w
   	BANK1
   	movwf BOARD_validToPairs+.0
   	BANK0
   	movf BOARD_y, w
   	BANK1
   	movwf BOARD_validToPairs+.1	
	BANK0
	incf BOARD_someDirectionValid, f
					
BOARD_isPosEast

	call BOARD_restorePosition	
	
	call BOARD_savePosition	;_x, _y -> _x_tmp, _y_tmp

	;Na pozici X musi byt peg, X+1 peg, X+2 empty
	btfss BOARD_directions, BOARD_EAST
	goto BOARD_isPosNorth	
	call BOARD_getValue		;v BOARD_tmp
	xorlw BOARD_PEG			;X=peg?
	btfss STATUS, Z
	goto BOARD_isPosNorth	;Zkus smer NORTH
	incf BOARD_x, f
	call BOARD_getValue
	xorlw BOARD_PEG			;X+1=peg?
	btfss STATUS, Z
	goto BOARD_isPosNorth
	incf BOARD_x, f
	call BOARD_getValue		;kopie v BOARD_tmp, abych mohl zavolat restore a return
	xorlw BOARD_EMPTY		;X+2=empty?
	btfss STATUS, Z
	goto BOARD_isPosNorth	;nelze, dalsi smer
	
	;Z-nastaveno, da se na X,Y hrat, ulozime do BOARD_validToPairs[2-3]
   	movf BOARD_x, w
   	BANK1
   	movwf BOARD_validToPairs+.2
   	BANK0
   	movf BOARD_y, w
   	BANK1
   	movwf BOARD_validToPairs+.3	
	BANK0
	incf BOARD_someDirectionValid, f

BOARD_isPosNorth

	call BOARD_restorePosition	
	
	call BOARD_savePosition	;_x, _y -> _x_tmp, _y_tmp

	;Na pozici Y musi byt peg, Y-1 peg, Y-2 empty
	btfss BOARD_directions, BOARD_NORTH
	goto BOARD_isPosSouth
	call BOARD_getValue		;v BOARD_tmp
	xorlw BOARD_PEG			;Y=peg?
	btfss STATUS, Z
	goto BOARD_isPosSouth	;Zkus smer SOUTH
	decf BOARD_y, f
	call BOARD_getValue
	xorlw BOARD_PEG			;Y-1=peg?
	btfss STATUS, Z
	goto BOARD_isPosSouth
	decf BOARD_y, f
	call BOARD_getValue		;kopie v BOARD_tmp, abych mohl zavolat restore a return
	xorlw BOARD_EMPTY		;Y-2=empty?
	btfss STATUS, Z
	goto BOARD_isPosSouth
	
	;Z-nastaveno, da se na X,Y hrat, ulozime do BOARD_validToPairs[4-5]
   	movf BOARD_x, w
   	BANK1
   	movwf BOARD_validToPairs+.4
   	BANK0
   	movf BOARD_y, w
   	BANK1
   	movwf BOARD_validToPairs+.5	
	BANK0	
	incf BOARD_someDirectionValid, f

BOARD_isPosSouth

	call BOARD_restorePosition	
	
	call BOARD_savePosition	;_x, _y -> _x_tmp, _y_tmp

	;Na pozici Y musi byt peg, Y+1 peg, Y+2 empty
	btfss BOARD_directions, BOARD_SOUTH
	goto BOARD_isPosFinish
	call BOARD_getValue		;v BOARD_tmp
	xorlw BOARD_PEG			;Y=peg?
	btfss STATUS, Z
	goto BOARD_isPosFinish	;NZ=nelze hrat
	incf BOARD_y, f
	call BOARD_getValue
	xorlw BOARD_PEG			;Y+1=peg?
	btfss STATUS, Z
	goto BOARD_isPosFinish
	incf BOARD_y, f
	call BOARD_getValue		;kopie v BOARD_tmp, abych mohl zavolat restore a return
	xorlw BOARD_EMPTY		;Y+2=empty?
	btfss STATUS, Z
	goto BOARD_isPosFinish
	
	;Z-nastaveno, da se na X,Y hrat, ulozime do BOARD_validToPairs[4-5]
   	movf BOARD_x, w
   	BANK1
   	movwf BOARD_validToPairs+.6
   	BANK0
   	movf BOARD_y, w
   	BANK1
   	movwf BOARD_validToPairs+.7	
	BANK0
	incf BOARD_someDirectionValid, f	
		
BOARD_isPosFinish
	call BOARD_restorePosition
	
	movf BOARD_someDirectionValid, f	;==0? Z/NZ nastaven podle toho
	return	
;}

;Nastav/vyresetuj seznam hratelnych cilovych pozic
BOARD_initValidToPairs
;{
	
	;Fill with 0xFF
	movlw .8		;4x2 pozice x,y
	movwf BOARD_i
	
	movlw BOARD_validToPairs
	movwf FSR
	
	movlw BOARD_TO_INVALID
BOARD_initToPairsLoop
	movwf INDF
	incf FSR, f
	decfsz BOARD_i, f
	goto BOARD_initToPairsLoop
	
	return	
	
;}

;Vstup: BOARD_x, BOARD_y
;Vystup: Z-nelze z BOARD_x_sel,BOARD_y_sel na BOARD_x,BOARD_y provest skok
;        NZ-lze
;Vystup: BOARD_pairFoundPos - nastaven flag W,E,N,S, na ktere pozici jsem nasel
;Jednoduse projdu vsechny x,y v poli a srovnam
BOARD_isPositionInValidPairs
;{	
	
	;Nejprve si zkopiruju, at nemusim porad prepinat mezi bankami
	movf BOARD_x, w
	BANK1
	movwf BOARD_x_vtmp
	BANK0
	movf BOARD_y, w
	BANK1					;Zustavam v BANK1
	movwf BOARD_y_vtmp
	
	clrf BOARD_pairsFoundPos	;Potrebuju vedet jakym validnim smerem jsem nakonec hral,
								;Abych mohl vymazat Peg, ktery jsem preskocil, na spravne
								;pozici.	
		
	;Prohledej BOARD_x_vtmp,BOARD_y_vtmp v poli BOARD_validToPairs	
	movf BOARD_validToPairs+.0, w
	xorwf BOARD_x_vtmp, w
	btfss STATUS, Z
	goto BOARD_isInValid2		;Zkus dalsi pozici
	movf BOARD_validToPairs+.1, w
	xorwf BOARD_y_vtmp, w
	btfss STATUS, Z
	goto BOARD_isInValid2		;Zkus dalsi pozici
	bsf BOARD_pairsFoundPos, BOARD_WEST
	goto BOARD_isPosInValidEnd	;Z flag zustava nastaven, jsme hotovi
	
BOARD_isInValid2

	movf BOARD_validToPairs+.2, w
	xorwf BOARD_x_vtmp, w
	btfss STATUS, Z
	goto BOARD_isInValid3		;Zkus dalsi pozici
	movf BOARD_validToPairs+.3, w
	xorwf BOARD_y_vtmp, w
	btfss STATUS, Z
	goto BOARD_isInValid3		;Zkus dalsi pozici
	bsf BOARD_pairsFoundPos, BOARD_EAST
	goto BOARD_isPosInValidEnd	;Z flag zustava nastaven, jsme hotovi
	
BOARD_isInValid3

	movf BOARD_validToPairs+.4, w
	xorwf BOARD_x_vtmp, w
	btfss STATUS, Z
	goto BOARD_isInValid4		;Zkus dalsi pozici
	movf BOARD_validToPairs+.5, w
	xorwf BOARD_y_vtmp, w
	btfss STATUS, Z
	goto BOARD_isInValid4		;Zkus dalsi pozici
	bsf BOARD_pairsFoundPos, BOARD_NORTH
	goto BOARD_isPosInValidEnd	;Z flag zustava nastaven, jsme hotovi

BOARD_isInValid4	

	movf BOARD_validToPairs+.6, w
	xorwf BOARD_x_vtmp, w
	btfss STATUS, Z
	goto BOARD_isPosInValidEnd	;Koncime NZ
	movf BOARD_validToPairs+.7, w
	xorwf BOARD_y_vtmp, w		;Z/NZ
	btfsc STATUS, Z
	bsf BOARD_pairsFoundPos, BOARD_SOUTH
	
BOARD_isPosInValidEnd
	BANK0						;nemeni nastaveni Z flagu
	
	;Invertujeme hodnotu Z flagu
	btfsc STATUS, Z
	goto BOARD_isPosInFound
	clrf tmp		;Nastav Zero	
	return
BOARD_isPosInFound
	bcf STATUS, Z	;Non-Zero
	return	
;}

;Vstup: BOARD_x, BOARD_y, BOARD_pairsFoundPos
;Vystup: Nastavi BOARD_x, BOARD_y pro preskakovany Peg
BOARD_setPosForIntermediate
;{
	;At nemusim porad prepinat
	BANK1
	movf BOARD_pairsFoundPos, w
	BANK0
	movwf BOARD_tmp
	
	;V BOARD_pairsFoundPos mam nastaveny nektery z W,E,N,S flagu,
	;tj. smer, jakym se provedl skok do bodu B. Pozor, souradnice
	; musim posunout v opacnem smeru k bodu A, ze ktereho jsem skakal.
	
BOARD_setIntmediateWest	
	btfss BOARD_tmp, BOARD_WEST
	goto BOARD_setIntmediateEast
	incf BOARD_x, f				;opacny smer!
	return
	
BOARD_setIntmediateEast

	btfss BOARD_tmp, BOARD_EAST
	goto BOARD_setIntmediateNorth
	decf BOARD_x, f				;opacny smer!
	return

BOARD_setIntmediateNorth

	btfss BOARD_tmp, BOARD_NORTH
	goto BOARD_setIntmediateSouth
	incf BOARD_y, f				;opacny smer!
	return

BOARD_setIntmediateSouth

	btfsc BOARD_tmp, BOARD_SOUTH
	decf BOARD_y, f				;opacny smer!
	return
;}

;*******************************************************************
;*************** J O Y S T I C K   R U T I N Y *********************
;*******************************************************************

;Cekej na stisk joystick button - pozor obracena logika
;Vrat se, az nejaky byl
JOY_waitButton
;{
	btfsc JOY_SW
	goto JOY_waitButton
	
	movlw .10
	call waitMilis
	
JOY_waitB
	btfss JOY_SW	
	goto JOY_waitB
	
	return
;}


;50x10ms s cekanim na button, pokud byl stisk, nastav flag a vrat se.
JOY_waitCheckButton
;{
	
	clrf JOY_buttonPressed
	
	movlw .50
	movwf i
	
JOY_wait1sLoop

	btfss JOY_SW
	goto JOY_wait1sEnd

	movlw .10
	call waitMilis
	
	decfsz i, f
	goto JOY_wait1sLoop
	return
	
JOY_wait1sEnd
	bsf JOY_buttonPressed, 0	;true, button pressed
	return
	
;}

;Vraci Z-nebyl pohyb, NZ-byl
JOY_wasMove
;{
	movf BOARD_x_old, w
	xorwf BOARD_x, w
	btfss STATUS, Z
	return
	movf BOARD_y_old, w
	xorwf BOARD_y, w
	return
;}

JOY_getReadings
;{
	clrf JOY_whichAdjust	;BOARD_x
	call JOY_getAnalogX		
	call JOY_handleMoveX
	
	incf JOY_whichAdjust, f	;BOARD_y
	call JOY_getAnalogY
	call JOY_handleMoveY
		
	call JOY_stopADC

	return
;}

;Byl stisk joysticku - pozor obracena logika!
;Vystup: ZERO set: byl, ZERO unset nebyl
JOY_getSwitchReading
;{
	bcf STATUS, Z
	btfsc JOY_SW
	return
	
	movlw .10
	call waitMilis
	
	bcf STATUS, Z
	btfsc JOY_SW
	return
	
JOY_getSwitchL
	btfss JOY_SW
	goto JOY_getSwitchL
	
	bsf STATUS, Z
	return
;}
	
JOY_stopADC
;{
	banksel ADCON0
	movlw b'10000000'		;ADC Frc clock, AN0, ADC OFF	
	movwf ADCON0
	BANK0
	return
;}

JOY_getAnalogX
;{
	movlw JOY_X_ADC
	goto JOY_readAnalog
;}
JOY_getAnalogY
;{
	movlw JOY_Y_ADC
	goto JOY_readAnalog
;}		
JOY_readAnalog
;{		
	banksel ADCON0
	movwf ADCON0   ; Vdd Vref, On
	call wait30us  ; Acquisiton delay
	bsf ADCON0, GO ; Start conversion
	btfsc ADCON0, GO ;Is conversion done?
	goto $-1 ;No, test again
	banksel ADRESH
	movf ADRESH, w ;Read upper 2 bits
	BANK0
	movwf JOY_ADresultH
	banksel ADRESL
	movf ADRESL, w ;Read lower 8 bits
	BANK0
	movwf JOY_ADresultL	
	return
;}

;Nastavi JOY_increment podle JOY_ADresultH:JOY_AdresultL
;Vystup:
;JOY_increment = 0		  AD result <342,  681>
;JOY_increment = 255 (-1) AD result <  0,  341>
;JOY_increment = 1		  AD result <681, 1023>
;Vystup: uprav BOARD_x, BOARD_y podle pohybu joysticku

JOY_handleMoveX
;{
	movlw .1			 	;Default +1
	movwf JOY_increment		
	
	;1) AD result >= 342?
    movlw AD_LIMIT_LOWER_L  ;move low byte of constant to wreg
    subwf JOY_ADresultL, w  ;subtract low bytes  
    movlw AD_LIMIT_LOWER_H  ;move high byte of constant to wreg
    btfss STATUS,C     	    ;borrow from the low bytes subtraction?
    addlw .1           	    ;yes, add one
    subwf JOY_ADresultH, w  ;subtract high bytes
    
    btfss STATUS, C    	    ;AD result >= 342
    goto JOY_checkBoundaries  ;<342, nothing more to compare 
 	
 	decf JOY_increment, f	;0 now

	;2) AD result >= 682?
    movlw AD_LIMIT_UPPER_L  ;move low byte of constant to wreg
    subwf JOY_ADresultL, w  ;subtract low bytes  
    movlw AD_LIMIT_UPPER_H  ;move high byte of constant to wreg
    btfss STATUS,C     	    ;borrow from the low bytes subtraction?
    addlw .1           	    ;yes, add one
    subwf JOY_ADresultH, w  ;subtract high bytes
    
    btfss STATUS, C    	    ;AD result >= 682
    return 					;>=342 && <682, we are done, no roll-over possible
 	
	decf JOY_increment, f	;-1
	goto JOY_checkBoundaries
;}
;Tady pozor na decf/incf, joystick je otoceny o 90 stupnu	
JOY_handleMoveY	
;{
	movlw .255				
	movwf JOY_increment		;Default -1
	
	;1) AD result >= 342?
    movlw AD_LIMIT_LOWER_L  ;move low byte of constant to wreg
    subwf JOY_ADresultL, w  ;subtract low bytes  
    movlw AD_LIMIT_LOWER_H  ;move high byte of constant to wreg
    btfss STATUS,C     	    ;borrow from the low bytes subtraction?
    addlw .1           	    ;yes, add one
    subwf JOY_ADresultH, w  ;subtract high bytes
    
    btfss STATUS, C    	     ;AD result >= 342
    goto JOY_checkBoundaries ;<342, nothing more to compare 
 	
 	incf JOY_increment, f	;0 now

	;2) AD result => 682?
    movlw AD_LIMIT_UPPER_L  ;move low byte of constant to wreg
    subwf JOY_ADresultL, w  ;subtract low bytes  
    movlw AD_LIMIT_UPPER_H  ;move high byte of constant to wreg
    btfss STATUS,C     	    ;borrow from the low bytes subtraction?
    addlw .1           	    ;yes, add one
    subwf JOY_ADresultH, w  ;subtract high bytes
    
    btfss STATUS, C    	    ;AD result >= 682
    return 					;<=342 && <682, we are done, no roll-over possible
 	
	incf JOY_increment, f	;+1
	goto JOY_checkBoundaries
;}
	
;Zkontroluj meze boardu po zmene pozice X, Y
;Vstup JOY_increment - 255(-1) nebo +1
;Nastavi BOARD_x nebo BOARD_y (podle JOY_whichAdjust)
JOY_checkRollOver
;{
	movf JOY_increment, w
	btfss JOY_whichAdjust, 0		;BOARD_x nebo BOARD_y?
	goto JOY_adjustX	
	addwf BOARD_y, f
	goto JOY_checkBoundaries
JOY_adjustX							;BOARD_x
	addwf BOARD_x, f
;}
	
;Nastav meze, kam se lze pohybovat
;Na vstupu jsou aktualni souradnice BOARD_x a BOARD_y
;a JOY_increment (-1 [255] / +1)
;Postup:
;1) Pro X nebo Y zjisti (podle JOY_increment), jestli se souradnice snizuje (255) ci zvysuje (+1).
;2) Pokud snizuje, ziskej dolni mez, jinak ziskej horni mez
;3) Pote porovnej mez s aktualni X nebo Y a pokud jsme na mezi, souradnice neupravuj.
JOY_checkBoundaries
;{
	;Snizeni souradnice (-1)
	movlw .255
	xorwf JOY_increment, w
	btfss STATUS, Z
	goto JOY_checkBoundInc
	
	;Snizujeme, zjisti dolni mez
	movlw HIGH JOY_getLowerBound
	movwf PCLATH
	call JOY_getLowerBound
	movwf tmp
	
	;Zjisti, jestli BOARD_x nebo BOARD_y == dolni mez pro X nebo Y osu
	
	movf BOARD_y, w
	btfss JOY_whichAdjust, 0
	movf BOARD_x, w
	xorwf tmp, w
	btfsc STATUS, Z		;Jsme na dolni mezi?
	return				;Jsme = souradnice dal neupravuj

	;Jsme nad dolni mezi, muzeme dekrementovat
	btfss JOY_whichAdjust, 0	;vyber, co dekrementovat
	goto JOY_chkDecX
	decf BOARD_y, f
	return
JOY_chkDecX
	decf BOARD_x, f
	return

JOY_checkBoundInc

	;Zvysujeme, zjisti horni mez
	movlw HIGH JOY_getUpperBound
	movwf PCLATH
	call JOY_getUpperBound
	movwf tmp
	
	;Zjisti, jestli BOARD_x nebo BOARD_y == horni mez pro X nebo Y osu
	
	movf BOARD_y, w
	btfss JOY_whichAdjust, 0
	movf BOARD_x, w
	xorwf tmp, w
	btfsc STATUS, Z		;Jsme na horni mezi?
	return				;Jsme = souradnice dal neupravuj

	;Jsme pod horni mezi, muzeme inkrementovat
	btfss JOY_whichAdjust, 0	;vyber, co inkrementovat
	goto JOY_chkIncX
	incf BOARD_y, f
	return
JOY_chkIncX
	incf BOARD_x, f
	return
	
;}

;Vstup: JOY_whichAdjust - 0=x, 1=y
;Vystup: W-REG s dolni mezi bud pro X anebo Y.
;Jelikoz je hraci plocha symetricka, dolni meze jsou shodne pro X a Y
JOY_getLowerBound
;{
	movf BOARD_x, w
	btfss JOY_whichAdjust, 0
	movf BOARD_y, w
	addwf PCL, f

	retlw .2
	retlw .2	
	retlw .0
	retlw .0
	retlw .0
	retlw .2
	retlw .2
;}
	
JOY_getUpperBound
;{
	movf BOARD_x, w
	btfss JOY_whichAdjust, 0
	movf BOARD_y, w
	addwf PCL, f

	retlw .4
	retlw .4	
	retlw BOARD_WIDTH-.1
	retlw BOARD_WIDTH-.1
	retlw BOARD_WIDTH-.1
	retlw .4
	retlw .4	
;}
		
; *********************************************************************
; ************************ 1.8" TFT ST7735 DRIVER *********************
; *********************************************************************


TFT_SendCmd
;{
		bcf TFT_CS
		bcf TFT_DC		;Command
		
		call TFT_WriteByte
		
		bsf TFT_CS
		return
;}
		
TFT_SendData
;{
		bcf TFT_CS
		bsf TFT_DC		;Data
		
		call TFT_WriteByte
		
		bsf TFT_CS
		return
;}

TFT_WriteByte				;SW SPI
;{
		movwf TFT_tmp

		movlw .8			;8 bits transfer
		movwf TFT_i

TFT_cycle	
		btfss TFT_tmp, 7		
		goto TFT_bitNotSet
		bcf TFT_SCK	
		bsf TFT_MOSI
		goto TFT_tested
TFT_bitNotSet 		
		bcf TFT_SCK
		bcf TFT_MOSI
TFT_tested
		bsf TFT_SCK

		rlf TFT_tmp, f
		decfsz TFT_i, f
		goto TFT_cycle
	
		bcf TFT_SCK
		return			
;}
		

TFT_init
;{

  		bsf TFT_CS
  		bcf TFT_DC
  
  		bcf TFT_SCK
    	bcf TFT_MOSI
    
		movlw ST7735_SWRESET
		call TFT_SendCmd
		movlw .150
		call waitMilis
		movlw ST7735_SLPOUT
		call TFT_SendCmd
		movlw .250
		call waitMilis
		movlw .250
		call waitMilis
		movlw ST7735_FRMCTR1
		call TFT_SendCmd
		movlw 0x01
		call TFT_SendData
		movlw 0x2C
		call TFT_SendData
		movlw 0x2D
		call TFT_SendData
		movlw ST7735_FRMCTR2
		call TFT_SendCmd
		movlw 0x01
		call TFT_SendData
		movlw 0x2C
		call TFT_SendData
		movlw 0x2D
		call TFT_SendData
		movlw ST7735_FRMCTR3
		call TFT_SendCmd
		movlw 0x01
		call TFT_SendData 
		movlw 0x2C
		call TFT_SendData 
		movlw 0x2D
		call TFT_SendData
		movlw 0x01
		call TFT_SendData 
		movlw 0x2C
		call TFT_SendData 
		movlw 0x2D
		call TFT_SendData
		movlw ST7735_INVCTR
		call TFT_SendCmd
		movlw 0x07
		call TFT_SendData
		movlw ST7735_PWCTR1
		call TFT_SendCmd
		movlw 0xA2
		call TFT_SendData
		movlw 0x02
		call TFT_SendData
		movlw 0x84
		call TFT_SendData
		movlw ST7735_PWCTR2
		call TFT_SendCmd
		movlw 0xC5
		call TFT_SendData
		movlw ST7735_PWCTR3
		call TFT_SendCmd
		movlw 0x0A
		call TFT_SendData
		movlw 0x00
		call TFT_SendData
		movlw ST7735_PWCTR4
		call TFT_SendCmd
		movlw 0x8A
		call TFT_SendData
		movlw 0x2A
		call TFT_SendData
		movlw ST7735_PWCTR5
		call TFT_SendCmd
		movlw 0x8A
		call TFT_SendData
		movlw 0xEE
		call TFT_SendData
		movlw ST7735_VMCTR1
		call TFT_SendCmd
		movlw 0x0E
		call TFT_SendData
		movlw ST7735_INVOFF
		call TFT_SendCmd
		movlw ST7735_MADCTL
		call TFT_SendCmd
		movlw 0x03	;portrait RGB	
		;movlw 0xC8
		;movlw 0xA8 ;landscape BRG
		;movlw 0xA0  ;landscape RGB OK
		call TFT_SendData
		movlw ST7735_COLMOD
		call TFT_SendCmd
		movlw 0x05
		call TFT_SendData 
		movlw ST7735_CASET
		call TFT_SendCmd
		movlw 0x00
		call TFT_SendData 
		movlw 0x00
		call TFT_SendData
		movlw 0x00
		call TFT_SendData 
		movlw 0x7F
		call TFT_SendData
		movlw ST7735_RASET
		call TFT_SendCmd
		movlw 0x00
		call TFT_SendData 
		movlw 0x00
		call TFT_SendData
		movlw 0x00
		call TFT_SendData 
		movlw 0x9F
		call TFT_SendData
		movlw ST7735_GMCTRP1
		call TFT_SendCmd
		movlw 0x02
		call TFT_SendData
		movlw 0x1C
		call TFT_SendData 
		movlw 0x07
		call TFT_SendData 
		movlw 0x12
		call TFT_SendData
		movlw 0x37
		call TFT_SendData 
		movlw 0x32
		call TFT_SendData 
		movlw 0x29
		call TFT_SendData 
		movlw 0x2D
		call TFT_SendData
		movlw 0x29
		call TFT_SendData 
		movlw 0x25
		call TFT_SendData 
		movlw 0x2B
		call TFT_SendData 
		movlw 0x39
		call TFT_SendData
		movlw 0x00
		call TFT_SendData 
		movlw 0x01
		call TFT_SendData 
		movlw 0x03
		call TFT_SendData 
		movlw 0x10
		call TFT_SendData
		movlw ST7735_GMCTRN1
		call TFT_SendCmd
		movlw 0x03
		call TFT_SendData 
		movlw 0x1D
		call TFT_SendData 
		movlw 0x07
		call TFT_SendData 
		movlw 0x06
		call TFT_SendData
		movlw 0x2E
		call TFT_SendData 
		movlw 0x2C
		call TFT_SendData 
		movlw 0x29
		call TFT_SendData 
		movlw 0x2D
		call TFT_SendData
		movlw 0x2E
		call TFT_SendData 
		movlw 0x2E
		call TFT_SendData 
		movlw 0x37
		call TFT_SendData 
		movlw 0x3F
		call TFT_SendData
		movlw 0x00
		call TFT_SendData 
		movlw 0x00
		call TFT_SendData 
		movlw 0x02
		call TFT_SendData 
		movlw 0x10
		call TFT_SendData
		movlw ST7735_NORON
		call TFT_SendCmd
		movlw .10
		call waitMilis
		movlw ST7735_DISPON
		call TFT_SendCmd
		movlw .100
		call waitMilis   
    		
		return
;}		


;Vykresli kameny na boardu
TFT_drawPieces
;{
	clrf BOARD_y
	movlw BOARD_HEIGHT
	movwf BOARD_j

TFT_pieceTestY	
	clrf BOARD_x
	movlw BOARD_WIDTH
	movwf BOARD_i
	
TFT_pieceTestX
	call BOARD_getPlaceValue		;BOARD_piece nastaven
		
	call TFT_drawPiece
	incf BOARD_x, f
	decfsz BOARD_i, f
	goto TFT_pieceTestX
		
	incf BOARD_y, f
	decfsz BOARD_j, f
	goto TFT_pieceTestY
	return
;}		

TFT_drawBoard
;{
	call BOARD_savePosition
	
	;Nic nevybiram
	movlw BOARD_NOSELECT
	movwf BOARD_selectOrder	
	
	call TFT_drawPieces
	
	call BOARD_restorePosition

	return
;}


;Vstup: BOARD_selectOrder
;Nastavi BG a FG barvy
TFT_setPieceColor
;{
	;FG je spolecne pro vsechny
	call TFT_colorSetBlackFG
	
	;Nastaven Invalid flag?
	btfsc BOARD_selectOrder, BOARD_INVALID
	goto TFT_colorSetRedBG
	
	;0x00 - Nothing
	movf BOARD_selectOrder, f
	btfsc STATUS, Z
	goto TFT_colorSetGreenBG
		
	;0x01 - From
	btfsc BOARD_selectOrder, BOARD_FROM
	goto TFT_colorSetBlueBG
	
	;0x02 - To
	btfsc BOARD_selectOrder, BOARD_TO
	goto TFT_colorSetYellowBG
	
	return
;}
	
;Nastavi v displeji okno pro vykresleni kamene, podle BOARD_x a BOARD_y
TFT_setBoardBoundaries
;{

	clrf TFT_X
	clrf TFT_X_MAX
	clrf TFT_Y
	clrf TFT_Y_MAX
		
	movf BOARD_x, w
	movwf tmp
	
	bcf STATUS, C
	rlf tmp, f
	rlf tmp, f
	rlf tmp, f
	rlf tmp, w		;x 16
	addwf TFT_X, f

	movf BOARD_x_offset, w
	;movlw BOARD_X_OFFSET		;posun o X offset
	addwf TFT_X, f
	
	movf TFT_X, w
	movwf TFT_X_MAX
	
	movf BOARD_y, w
	movwf tmp
	
	bcf STATUS, C
	rlf tmp, f
	rlf tmp, f
	rlf tmp, f
	rlf tmp, w		;x 16
	addwf TFT_Y, f
	
	movf BOARD_y_offset, w
	;movlw BOARD_Y_OFFSET		;posun v Y
	addwf TFT_Y, f
	
	movf TFT_Y, w
	movwf TFT_Y_MAX
	
	movlw PIECE_SIZE - .1
	addwf TFT_X_MAX, f
	addwf TFT_Y_MAX, f
		
	return
;}


TFT_setGfxDataPointer
;{
	bcf BOARD_piece, BOARD_SELECTED		;Tento bit zde nesmi byt nastaveny
	
	movlw BOARD_EMPTY
	xorwf BOARD_piece, w
	btfsc STATUS, Z
	goto TFT_setPtrEmpty

	movlw BOARD_NOGO	
	xorwf BOARD_piece, w
	btfsc STATUS, Z
	goto TFT_setPtrNoGo
	
	movlw BOARD_PEG
	xorwf BOARD_piece, w	
	btfss STATUS, Z
	return
	
	movlw HIGH Table_peg_data
	movwf dataPointer+0
	movlw LOW Table_peg_data
	movwf dataPointer+1
	return		
	
TFT_setPtrNoGo
	movlw HIGH Table_nogo_data
	movwf dataPointer+0
	movlw LOW Table_nogo_data
	movwf dataPointer+1
	return	
	
TFT_setPtrEmpty
	movlw HIGH Table_empty_data
	movwf dataPointer+0
	movlw LOW Table_empty_data
	movwf dataPointer+1
	return
	
;}

;Vykresli kamen na pozici [BOARD_x, BOARD_y] v barve BOARD_piece
TFT_drawPiece
;{	
	call TFT_setPieceColor
	
    call TFT_setBoardBoundaries	;Spocitej a nastav pozici obdelniku
    
    call TFT_setGfxDataPointer	;Nastav gfx data pro dany BOARD_piece
    
    movlw PIECE_BYTES
    movwf TFT_dataLen

    goto TFT_draw
          
;}

TFT_getAndDrawPiece	
;{
	call BOARD_getPlaceValue
	call TFT_drawPiece
	goto JOY_waitCheckButton
;}

TFT_getNextByte
;{
	call TFT_getNextByte2 	;To je patch kvuli strankovani
	clrf PCLATH
	incfsz dataPointer+1, f ;Inkrementuj pointer
	goto $+2
	incf dataPointer+0, f
	return 					;Vrat hodnotu (je stale ve W)
TFT_getNextByte2
	;Zde se pouze skoci na dvoubajtovou adresu v dataPointer	
	movf dataPointer+0, w 	;Zkopiruj HI byte do PCLATHu
	movwf PCLATH
	movf dataPointer+1, w 	;Dej LO byte do PCL
	movwf PCL 				;Skoci na adresu, kam ukazuje textPointer		
;}


TFT_colorSetBlackFG
;{
    movlw HIGH ST7735_BLACK
    movwf TFT_pieceFGColor+0
    movlw LOW ST7735_BLACK
    movwf TFT_pieceFGColor+1
    return
;}

TFT_colorSetBlackBG
;{
    movlw HIGH ST7735_BLACK
    movwf TFT_pieceBGColor+0
    movlw LOW ST7735_BLACK
    movwf TFT_pieceBGColor+1
    return
;}

TFT_colorSetWhiteBG
;{
    movlw HIGH ST7735_WHITE
    movwf TFT_pieceBGColor+0
    movlw LOW ST7735_WHITE
    movwf TFT_pieceBGColor+1
    return
;}

TFT_colorSetGreenBG
;{
    movlw HIGH ST7735_GREEN
    movwf TFT_pieceBGColor+0
    movlw LOW ST7735_GREEN
    movwf TFT_pieceBGColor+1
    return
;}

TFT_colorSetBlueBG
;{
    movlw HIGH ST7735_BLUE
    movwf TFT_pieceBGColor+0
    movlw LOW ST7735_BLUE
    movwf TFT_pieceBGColor+1
    return
;}

TFT_colorSetRedBG
;{
    movlw HIGH ST7735_RED
    movwf TFT_pieceBGColor+0
    movlw LOW ST7735_RED
    movwf TFT_pieceBGColor+1
    return
;}

TFT_colorSetYellowBG
;{
    movlw HIGH ST7735_YELLOW
    movwf TFT_pieceBGColor+0
    movlw LOW ST7735_YELLOW
    movwf TFT_pieceBGColor+1
    return
;}

TFT_colorSetWhiteFG
;{
    movlw HIGH ST7735_WHITE
    movwf TFT_pieceFGColor+0
    movlw LOW ST7735_WHITE
    movwf TFT_pieceFGColor+1
    return
;}

TFT_colorSetBlueFG
;{
    movlw HIGH ST7735_BLUE
    movwf TFT_pieceFGColor+0
    movlw LOW ST7735_BLUE
    movwf TFT_pieceFGColor+1
    return
;}

TFT_colorSetRedFG
;{
    movlw HIGH ST7735_RED
    movwf TFT_pieceFGColor+0
    movlw LOW ST7735_RED
    movwf TFT_pieceFGColor+1
    return
;}


; Left-Top, [X,Y] -> [X_MAX, Y_MAX]
TFT_setWindow
;{
	movlw ST7735_CASET
	call TFT_SendCmd
	movlw 0
	call TFT_SendData
	movf TFT_X, w
	call TFT_SendData
	movlw 0
	call TFT_SendData
	movf TFT_X_MAX, w
	call TFT_SendData
	movlw ST7735_RASET
	call TFT_SendCmd
	movlw 0
	call TFT_SendData
	movf TFT_Y, w
	call TFT_SendData
	movlw 0
	call TFT_SendData
	movf TFT_Y_MAX, w
	call TFT_SendData
	movlw ST7735_RAMWR
	call TFT_SendCmd
	return
;}		


;Vykresli data do obdelniku [X,Y] -> [X_MAX, Y_MAX]
;TFT_dataLen - pocet bytes
TFT_draw
;{
    call TFT_setWindow			;Spocitej a nastav pozici

    bcf TFT_CS		;Zacatek write operace
	nop
	nop
	bsf TFT_DC		;Pouze data
	nop
	nop
    	
    movf TFT_dataLen, w		
TFT_drawBox					;Vykreslovani obecnych obdelniku
    movwf i

TFT_draw_loop

	call TFT_getNextByte
    movwf tmp

    movlw .8				
    movwf j

TFT_draw_loopB    
    btfss tmp, 7        ;nejvyssi bit set?
    goto TFT_drawBitNotSet
    movf TFT_pieceFGColor+0, w
    call TFT_WriteByte
    movf TFT_pieceFGColor+1, w
    call TFT_WriteByte
    goto TFT_drawBitCommon

TFT_drawBitNotSet
    movf TFT_pieceBGColor+0, w
    call TFT_WriteByte
    movf TFT_pieceBGColor+1, w
    call TFT_WriteByte

TFT_drawBitCommon            
    rlf tmp, f
	
    decfsz j, f
    goto TFT_draw_loopB
        
    decfsz i, f
    goto TFT_draw_loop
    
    bsf TFT_CS				; Konec write operace
    
    return
;}


TFT_getNextChar
;{
		call TFT_getNextChar2 		;To je patch kvuli strankovani
		clrf PCLATH
		incfsz textPointer+1, f ;Inkrementuj pointer
		goto $+2
		incf textPointer+0, f
		return 					;Vrat hodnotu (je stale ve W)
TFT_getNextChar2
		;Zde se pouze skoci na dvoubajtovou adresu v textPointer
		movf textPointer+0,w 	;Zkopiruj HI byte do PCLATHu
		movwf PCLATH
		movf textPointer+1,w 	;Dej LO byte do PCL
		movwf PCL 				;Skoci na adresu, kam ukazuje textPointer
;}



TFT_fillRectWithFGColor
;{
	call TFT_setWindow
	
	movf TFT_len, w
	movwf TFT_j
	
	bcf TFT_CS		;Zacatek write operace
	nop
	nop
	bsf TFT_DC		;Data
	nop
	nop
	
TFT_FillRectLoop


	movf TFT_pieceFGColor+0, w
	call TFT_WriteByte
	
	movf TFT_pieceFGColor+1, w
	call TFT_WriteByte
        
	decfsz TFT_j, f
    goto TFT_FillRectLoop
	
	bsf TFT_CS				; Konec write operace
		
	return
	
;}

TFT_drawBorder
;{	
	
	call TFT_colorSetBlackFG
	
	;line 0,0 - BOARD_SIZE*TILE_SIZE-1,0
	movlw .0
	movwf TFT_X	
	
	movlw .0
	movwf TFT_Y
	
	movlw TFT_WIDTH - .1
	movwf TFT_X_MAX
	
	movlw .0
	movwf TFT_Y_MAX		
	
	movlw TFT_WIDTH - .1
	movwf TFT_len	
	call TFT_fillRectWithFGColor
	
	;line 0,0 - 0,127
	movlw .0
	movwf TFT_X	

	movlw .0
	movwf TFT_Y

	movlw .0
	movwf TFT_X_MAX

	movlw TFT_WIDTH - .1	
	movwf TFT_Y_MAX		
	
	movlw TFT_WIDTH - .1
	movwf TFT_len	
	call TFT_fillRectWithFGColor	
		
	;line 0,127 - 127,127
	movlw .0
	movwf TFT_X	

	movlw TFT_WIDTH - .1	
	movwf TFT_Y

	movlw TFT_WIDTH - .1	
	movwf TFT_X_MAX

	movlw TFT_WIDTH - .1	
	movwf TFT_Y_MAX		
	
	movlw TFT_WIDTH - .1
	movwf TFT_len	
	call TFT_fillRectWithFGColor

	;line 127,0 - 127,127
	movlw TFT_WIDTH - .1	
	movwf TFT_X	

	movlw .0
	movwf TFT_Y

	movlw TFT_WIDTH - .1	
	movwf TFT_X_MAX

	movlw TFT_WIDTH - .1	
	movwf TFT_Y_MAX		
	
	movlw TFT_WIDTH - .1
	movwf TFT_len	
	call TFT_fillRectWithFGColor
	
	
				
	return	
;}
		
TFT_clearScreen
;{	
	movlw .0
	movwf TFT_X
	
	movlw .0
	movwf TFT_Y

	movlw TFT_WIDTH - .1
	movwf TFT_X_MAX

	movlw TFT_HEIGHT - .1
	movwf TFT_Y_MAX
	
	call TFT_setWindow
			
	bcf TFT_CS		;Zacatek write operace
	nop
	nop
	bsf TFT_DC		;Data
	nop
	nop
		
	movlw TFT_HEIGHT	
	movwf j

TFT_fillR					; Rows
	movlw TFT_WIDTH
	movwf i	
TFT_fillC					; Columns

	movlw HIGH ST7735_GREEN	;Use selected color
	call TFT_WriteByte
	movlw LOW ST7735_GREEN
	call TFT_WriteByte
    
    decfsz i, f
    goto TFT_fillC			
    
	decfsz j, f
    goto TFT_fillR
	
	bsf TFT_CS				; Konec write operace			
	return
;}
				



;Vykresli text
;Vstup:
; textPointer:2 - zacatek textu
; TFT_X, TFT_Y - pozice kam
TFT_drawText
;{
		movlw FALSE				;Vypisuj cele retezce
		movwf TFT_putChar
		
TFT_drawTextLoop
		call TFT_getNextChar
		iorlw 0x00 
		btfsc STATUS, Z
		return
TFT_DrawChar
		movwf tmp
		movlw .32
		subwf tmp, w			;x 1
		
		movwf loByte
		movwf tmp				;x1 uschovej
		clrf hiByte
	
TFT_Mult8x16
		;Najdi graficka data pro pismeno (x16 pro 8x16 font)
		bcf STATUS, C
		rlf loByte, f
		rlf hiByte, f
		rlf loByte, f
		rlf hiByte, f
		rlf	loByte, f
		rlf hiByte, f				
		rlf loByte, f
		rlf hiByte, f

TFT_MultFinished		

		movlw HIGH Table_font8x16	;8x16
		movwf dataPointer+.0
		movlw LOW Table_font8x16
		movwf dataPointer+.1		;k dataPointer musime pricit 16x base
		
TFT_DrawFontOK		
		movf loByte, w
		addwf dataPointer+.1, f
		btfsc STATUS, C			;soucet >256?
		incf dataPointer+.0, f		
		movf hiByte, w
		addwf dataPointer+.0, f			
		
		movf TFT_X, w
		movwf TFT_X_MAX	
		
		movlw .1
		subwf TFT_charWidth, w		;W = CHAR_WIDTH - 1
	
		addwf TFT_X_MAX, f
		
		movf TFT_Y, w
		movwf TFT_Y_MAX
		
		movlw .1
		subwf TFT_charHeight, w		;W = CHAR_HEIGHT - 1		
		
		addwf TFT_Y_MAX, f

		;Vykresli graficka data znaku na pozici
		movf TFT_charHeight, w
		
		movwf TFT_dataLen
					
		call TFT_draw
			
		movf TFT_Y_copy, w
		movwf TFT_Y
		
		;Posun se o znak doprava na obrazovce
		movf TFT_charWidth, w
		addwf TFT_X, f
		
		btfss TFT_putChar, 0		;0-ne, 1-ano		
		goto TFT_drawTextLoop
		return

;}				


TFT_setFont8x16
;{
	movlw FONT_8x16
	movwf TFT_whichFont		;8x16 herni texty
	
	movlw FONT_8x16_CHAR_HEIGHT
	movwf TFT_charHeight
	movlw FONT_8x16_CHAR_WIDTH
	movwf TFT_charWidth	
	return
;}


TFT_showGameOver
;{     

	call TFT_colorSetWhiteBG
	
	movlw HIGH T_GAMEOVER
	movwf textPointer+.0
	movlw LOW T_GAMEOVER
	movwf textPointer+.1
	
	movlw .28
	movwf TFT_X
	movlw .120
	movwf TFT_Y
	movwf TFT_Y_copy
		
	goto TFT_drawText	
	
;}

TFT_showTitle
;{
	call TFT_colorSetWhiteBG
	
	movlw HIGH T_SOLITAIRE
	movwf textPointer+.0
	movlw LOW T_SOLITAIRE
	movwf textPointer+.1
	
	movlw .28
	movwf TFT_X
	movlw .4
	movwf TFT_Y
	movwf TFT_Y_copy
		
	goto TFT_drawText	
	
;}

TFT_showPressButton
;{
	
	call TFT_colorSetWhiteBG
		
	movlw HIGH T_PRESS_BUTTON
	movwf textPointer+.0
	movlw LOW T_PRESS_BUTTON
	movwf textPointer+.1
	
	movlw .16
	movwf TFT_X
	movlw .140
	movwf TFT_Y
	movwf TFT_Y_copy
		
	call TFT_drawText
	return	
;}

TFT_showMinusOneLeftClear
;{
	movlw HIGH T_MINUS_ONE_CLEAR
	movwf textPointer+.0
	movlw LOW T_MINUS_ONE_CLEAR	
	movwf textPointer+.1
	goto TFT_showMinusOneLC
;}

TFT_showMinusOneRightClear
;{
	movlw HIGH T_MINUS_ONE_CLEAR
	movwf textPointer+.0
	movlw LOW T_MINUS_ONE_CLEAR	
	movwf textPointer+.1
	goto TFT_showMinusOneRC
;}

TFT_showMinusOneLeft
;{
	movlw HIGH T_MINUS_ONE
	movwf textPointer+.0
	movlw LOW T_MINUS_ONE
	movwf textPointer+.1

TFT_showMinusOneLC

	call TFT_colorSetGreenBG
	call TFT_colorSetBlueFG
		
	movlw .16
	movwf TFT_X
	movlw .110
	movwf TFT_Y
	movwf TFT_Y_copy
		
	call TFT_drawText
	return	
;}
TFT_showMinusOneRight
;{
	movlw HIGH T_MINUS_ONE
	movwf textPointer+.0
	movlw LOW T_MINUS_ONE
	movwf textPointer+.1

TFT_showMinusOneRC
	
	call TFT_colorSetGreenBG	
	call TFT_colorSetBlueFG
	
	movlw .100
	movwf TFT_X
	movlw .110
	movwf TFT_Y
	movwf TFT_Y_copy
		
	call TFT_drawText
	return	
;}

TFT_showPegsLeft
;{

	movf BOARD_pegsLeft, w
	call bin2ascii   
	
	movlw HIGH T_PEGS_LEFT
	movwf textPointer+.0
	movlw LOW T_PEGS_LEFT
	movwf textPointer+.1
	
	movlw .16
	movwf TFT_X
	movlw .140
	movwf TFT_Y
	movwf TFT_Y_copy
		
	call TFT_drawText
	
	bsf TFT_putChar, 0	;true
	movf tens, w
	call TFT_DrawChar
	movf ones, w
	call TFT_DrawChar		
	bcf TFT_putChar, 0 ;false
	return	
;}

; *********************************************************************
; ************************ W T 5 8 8 D ********************************
; *********************************************************************

WT588D_doSoundFX
;{
	
		movwf WT588D_tmp
	
		;Nastav SCK a CS na log. 1
		movlw b'00000110'		;SCK log. 1, CS log. 1
		movwf PORTC
		
		movlw .17
		call waitMilis
		
		movlw b'00000100'		;SCK log. 1, CS log. 0
		movwf PORTC				;Slave Select = LOW (Initiating transmission)
		
		movlw .2
		call waitMilis

		;Ted projdu postupne bit 0, bit 1, bit 2, bit 3 a nastavuju podle toho log 1 nebo log 0

		movlw .8
		movwf WT588D_i
		
cycle	bcf WT588D_SCK		;Clock na log. 0, zaciname prenos dat			

		btfss WT588D_tmp, 0;  bit 0 nastaven?
		goto btNotSet
		bsf WT588D_SDA		 ; ano, posli log 1
		goto tsted
btNotSet bcf WT588D_SDA		; ne, posli log 0 na vystup
tsted	call WT588D_wait30us

		bsf WT588D_SCK		;Clock na log. 1, skoncili jsme prenos 1 bitu	
		call WT588D_wait30us

		rrf WT588D_tmp, f
		decfsz WT588D_i, f
		goto cycle

		;CS i SCK na high
		movlw b'00000110'	;SDA log. 0, CS log. 1, SCK log. 1
		movwf PORTC			;Slave Select = HI

		return
;}


WT588D_wait30us
;{			
			nop
			movlw .18
			movwf d1
wait30l		decfsz d1, f
			goto wait30l																			

			return;			ta je za 2 cykly
;}

WT588D_wait40us
;{
			;76 cycles
	movlw	0x19
	movwf	d1
wait40us_0
	decfsz	d1, f
	goto	wait40us_0

			;4 cycles (including call)
	return
;}





getNextChar
;{
	call getNextChar2 		;To je patch kvuli strankovani
	clrf PCLATH
	incfsz textPointer+0,f 	;Inkrementuj pointer
	goto $+2
	incf textPointer+1,f
	return 					;Vrat hodnotu (je stale ve W)
getNextChar2
	;Zde se pouze skoci na dvoubajtovou adresu v textPointer
	movf textPointer+1,w 	;Zkopiruj HI byte do PCLATHu
	movwf PCLATH
	movf textPointer+0,w 	;Dej LO byte do PCL
	movwf PCL 				;Skoci na adresu, kam ukazuje textPointer
		
getNextFontByte
	call getNextFontByte2 	;To je patch kvuli strankovani
	;movwf tmp				;Posunu byte doleva, posune pismeno smerem dolu o 1px
	clrf PCLATH
	;bcf STATUS, C			;Nuluj carry, at nam neco neprirotuje zprava
	;rlf tmp, w
	incfsz fontPointer+0,f 	;Inkrementuj pointer
	goto $+2
	incf fontPointer+1,f
	return 					;Vrat hodnotu (je stale ve W)
getNextFontByte2
	;Zde se pouze skoci na dvoubajtovou adresu v fontPointer
	movf fontPointer+1,w 	;Zkopiruj HI byte do PCLATHu
	movwf PCLATH
	movf fontPointer+0,w 	;Dej LO byte do PCL
	movwf PCL 				;Skoci na adresu, kam ukazuje fontPointer
;}

; *** SUBRUTINA, CEKEJ nuMilis milisekund ***************************


; *** SUBRUTINA, CEKEJ 30us (=30 jednotaktovych instrukci)
wait30us	
;{
			nop		;26, 2 na zacatku sezere volani call
			nop
			nop
			nop
			nop
		 	nop
			nop
			nop
			nop
			nop
wait20us	nop
			nop
			nop
			nop
			nop
		 	nop
			nop
			nop
			nop
			nop
wait10us	nop
			nop
			nop
			nop
			nop
wait5us	 	nop
			return;		ta je za 2 cykly
;}

; Delay = 1 seconds
; Clock frequency = 4 MHz

; Actual delay = 1 seconds = 1000000 cycles
; Error = 0 %

wait1s
;{
			;999990 cycles
	movlw	0x07
	movwf	d1
	movlw	0x2F
	movwf	d2
	movlw	0x03
	movwf	d3
wait1s_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	wait1s_0

	;6 cycles
	goto	$+1
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return
;}

; Delay = 0.001 seconds
; Clock frequency = 8 MHz
wait1ms
;{
			;1993 cycles
	movlw	0x8E
	movwf	d1
	movlw	0x02
	movwf	d2
wait1ms_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	wait1ms_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return
;}

waitMilis
;{
		movwf numMilis
waitM	call wait1ms
		decfsz numMilis, f
		goto waitM
		return
;}

	
; *** SUBRUTINA pro prevod 6bit binary to BCD ********************************	
;Vstup: W-REG	
bin2ascii
;{
	clrf tens
	addlw .256 - .100
digit100
	addlw .20
	btfss STATUS,C
	goto  digit40
	bsf   tens,3
	addlw .256 - .20
	goto digit10
digit40
	addlw .40
	btfss STATUS,C
	goto  $+3
	bsf   tens,2
	addlw .256 - .40
	addlw .20
	btfss STATUS,C
	goto  $+3
	bsf   tens,1
	addlw .256 - .20
digit10
	addlw .10
	btfss STATUS,C
	goto  $+3
	bsf   tens,0
	addlw .256 - .10
	
	addlw .10
	movwf ones
	
	movlw '0'
	addwf tens, f
	addwf ones, f
	return	
;}

; ******************************************************************************
; ****************************** T E X T Y *************************************
; ******************************************************************************

T_GAMEOVER
	dt "GAME OVER", 0x00
T_PEGS_LEFT
	dt "PEGS LEFT:", 0x00
T_SOLITAIRE
	dt "SOLITAIRE", 0x00
T_PRESS_BUTTON
	dt "PRESS BUTTON", 0x00
T_MINUS_ONE
	dt "-1", 0x00
T_MINUS_ONE_CLEAR
	dt "  ", 0x00
	

; ******************************************************************************
; ****************************** F O N T Y *************************************
; ******************************************************************************	
	
				org 0x0800
;16 radku x 16bytes = 256Bytes = 1 page
Table_font8x16
;{
Table_Font1	
				dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ; " " (dec 32)
				dt 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00 
				dt 0x6c, 0x6c, 0x6c, 0x6c, 0x24, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
				dt 0x6c, 0x6c, 0x6c, 0x6c, 0xfe, 0xfe, 0x6c, 0x6c, 0xfe, 0xfe, 0x6c, 0x6c, 0x6c, 0x6c, 0x00, 0x00 
				dt 0x10, 0x10, 0x7c, 0x7c, 0xd0, 0xd0, 0x7c, 0x7c, 0x16, 0x16, 0x16, 0x16, 0x7c, 0x7c, 0x10, 0x10 
				dt 0x60, 0x60, 0x96, 0x96, 0x6c, 0x6c, 0x18, 0x18, 0x30, 0x30, 0x6c, 0x6c, 0xd2, 0xd2, 0x0c, 0x0c 
				dt 0x70, 0x70, 0xd8, 0xd8, 0x70, 0x70, 0xf6, 0xf6, 0xdc, 0xdc, 0xd8, 0xd8, 0x7c, 0x7c, 0x06, 0x06 
				dt 0x18, 0x18, 0x18, 0x18, 0x30, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
				dt 0x03, 0x07, 0x0e, 0x0c, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x0c, 0x0e, 0x07, 0x03, 0x00, 0x00 
				dt 0xc0, 0xe0, 0x70, 0x30, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x30, 0x70, 0xe0, 0xc0, 0x00, 0x00 
				dt 0x00, 0x00, 0x6c, 0x6c, 0x38, 0x38, 0xfe, 0xfe, 0x38, 0x38, 0x6c, 0x6c, 0x00, 0x00, 0x00, 0x00 
				dt 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x00 
				dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x30, 0x30 
				dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 
				dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00 
				dt 0x03, 0x03, 0x06, 0x06, 0x0c, 0x0c, 0x18, 0x18, 0x30, 0x30, 0x60, 0x60, 0xc0, 0xc0, 0x80, 0x80 
Table_Font2				
				dt 0x00, 0x00, 0x78, 0x78, 0xcc, 0xcc, 0xde, 0xde, 0xf6, 0xf6, 0xe6, 0xe6, 0x7c, 0x7c, 0x00, 0x00 
				dt 0x18, 0x18, 0x18, 0x18, 0x38, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x7e, 0x00, 0x00 
				dt 0x7c, 0x7c, 0x06, 0x06, 0x3c, 0x3c, 0x60, 0x60, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xfe, 0x00, 0x00 
				dt 0x3c, 0x3c, 0x06, 0x06, 0x1c, 0x1c, 0x06, 0x06, 0x46, 0x46, 0xc6, 0xc6, 0x7c, 0x7c, 0x00, 0x00 
				dt 0x18, 0x18, 0x18, 0x18, 0x30, 0x30, 0x6c, 0x6c, 0xcc, 0xcc, 0xfe, 0xfe, 0x0c, 0x0c, 0x00, 0x00 
				dt 0xf8, 0xf8, 0xc0, 0xc0, 0xfc, 0xfc, 0x06, 0x06, 0x46, 0x46, 0xcc, 0xcc, 0x78, 0x78, 0x00, 0x00 
				dt 0x70, 0x70, 0xc0, 0xc0, 0xfc, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0xcc, 0xcc, 0x78, 0x78, 0x00, 0x00 
				dt 0xfe, 0xfe, 0x06, 0x06, 0x0c, 0x0c, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00 
				dt 0x78, 0x78, 0xcc, 0xcc, 0x7c, 0x7c, 0xc6, 0xc6, 0xc6, 0xc6, 0xcc, 0xcc, 0x78, 0x78, 0x00, 0x00 
				dt 0x78, 0x78, 0xcc, 0xcc, 0xc6, 0xc6, 0xc6, 0xc6, 0x7e, 0x7e, 0x06, 0x06, 0x1c, 0x1c, 0x00, 0x00 
				dt 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00 
				dt 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x30, 0x30 
				dt 0x03, 0x03, 0x06, 0x06, 0x0c, 0x0c, 0x18, 0x18, 0x0c, 0x0c, 0x06, 0x06, 0x03, 0x03, 0x00, 0x00 
				dt 0x00, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x00, 0x00, 0x00, 0x00 
				dt 0xc0, 0xc0, 0x60, 0x60, 0x30, 0x30, 0x18, 0x18, 0x30, 0x30, 0x60, 0x60, 0xc0, 0xc0, 0x00, 0x00 
				dt 0x7c, 0x7c, 0xc6, 0xc6, 0x06, 0x06, 0x3c, 0x3c, 0x30, 0x30, 0x00, 0x00, 0x30, 0x30, 0x00, 0x00 
Table_Font3		
				dt 0x7c, 0x7c, 0xc6, 0xc6, 0xde, 0xde, 0xd6, 0xd6, 0xde, 0xde, 0xc0, 0xc0, 0x7e, 0x7e, 0x00, 0x00 
				dt 0x78, 0x78, 0xcc, 0xcc, 0xc6, 0xc6, 0xfe, 0xfe, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00 
				dt 0xf8, 0xf8, 0xcc, 0xcc, 0xfc, 0xfc, 0xc6, 0xc6, 0xc6, 0xc6, 0xcc, 0xcc, 0xf8, 0xf8, 0x00, 0x00 
				dt 0x78, 0x78, 0xcc, 0xcc, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc6, 0xc6, 0x7c, 0x7c, 0x00, 0x00 
				dt 0xf8, 0xf8, 0xcc, 0xcc, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xfc, 0xfc, 0x00, 0x00 
				dt 0xfe, 0xfe, 0xc0, 0xc0, 0xfc, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xfe, 0x00, 0x00 
				dt 0xfe, 0xfe, 0xc0, 0xc0, 0xfc, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00 
				dt 0x38, 0x38, 0x60, 0x60, 0xc0, 0xc0, 0xce, 0xce, 0xc6, 0xc6, 0xc6, 0xc6, 0x7e, 0x7e, 0x06, 0x06 
				dt 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xfe, 0xfe, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00 
				dt 0x7e, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x7e, 0x7e, 0x00, 0x00 
				dt 0x0e, 0x0e, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x7c, 0x00, 0x00 
				dt 0xc6, 0xc6, 0xcc, 0xcc, 0xd8, 0xd8, 0xf0, 0xf0, 0xd8, 0xd8, 0xcc, 0xcc, 0xc6, 0xc6, 0x00, 0x00 
				dt 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfe, 0xfe, 0x00, 0x00 
				dt 0xc6, 0xc6, 0xee, 0xee, 0xfe, 0xfe, 0xd6, 0xd6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00 
				dt 0xc6, 0xc6, 0xe6, 0xe6, 0xf6, 0xf6, 0xde, 0xde, 0xce, 0xce, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00 
				dt 0x78, 0x78, 0xcc, 0xcc, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x7c, 0x00, 0x00 
				
Table_Font4				
				dt 0xf8, 0xf8, 0xcc, 0xcc, 0xc6, 0xc6, 0xc6, 0xc6, 0xfc, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00 
				dt 0x78, 0x78, 0xcc, 0xcc, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xd6, 0xd6, 0x7c, 0x7c, 0x0c, 0x0c 
				dt 0xf8, 0xf8, 0xcc, 0xcc, 0xc6, 0xc6, 0xc6, 0xc6, 0xfc, 0xfc, 0xd8, 0xd8, 0xcc, 0xcc, 0x06, 0x06 
				dt 0x78, 0x78, 0xc0, 0xc0, 0x7c, 0x7c, 0x06, 0x06, 0x46, 0x46, 0xc6, 0xc6, 0x7c, 0x7c, 0x00, 0x00 
				dt 0x7e, 0x7e, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00 
				dt 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x7c, 0x00, 0x00 
				dt 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x6c, 0x6c, 0x6c, 0x6c, 0x38, 0x38, 0x38, 0x38, 0x00, 0x00 
				dt 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xd6, 0xd6, 0xfe, 0xfe, 0xee, 0xee, 0xc6, 0xc6, 0x00, 0x00 
				dt 0xc6, 0xc6, 0x6c, 0x6c, 0x38, 0x38, 0x38, 0x38, 0x6c, 0x6c, 0xc6, 0xc6, 0xc6, 0xc6, 0x00, 0x00 
				dt 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0xc6, 0x7c, 0x7c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x00, 0x00 
				dt 0xfe, 0xfe, 0x0c, 0x0c, 0x18, 0x18, 0x30, 0x30, 0x60, 0x60, 0xc0, 0xc0, 0xfe, 0xfe, 0x00, 0x00 ; "Z" (dec 90)				
;}



				org 0x0C00
				
Table_peg_data
;{
	dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x0F, 0xF0, 0x0F, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8
	dt 0x1F, 0xF8, 0x1F, 0xF8, 0x0F, 0xF0, 0x0F, 0xF0, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
;}

Table_empty_data
;{

	dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x0C, 0x30, 0x08, 0x10, 0x10, 0x08, 0x10, 0x08
	dt 0x10, 0x08, 0x10, 0x08, 0x08, 0x10, 0x0C, 0x30, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
;}

Table_nogo_data
;{

	dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
;}
				
Table_kamen_data
;{
	dt 0x00, 0x00, 0x00 ;                         
	dt 0x00, 0x7E, 0x00 ;          ######         
	dt 0x03, 0xFF, 0xC0 ;       ############      
	dt 0x07, 0xFF, 0xE0 ;      ##############     
	dt 0x0F, 0xFF, 0xF0 ;     ################    
	dt 0x1F, 0xFF, 0xF8 ;    ##################   
	dt 0x3F, 0xFF, 0xFC ;   ####################  
	dt 0x3F, 0xFF, 0xFC ;   ####################  
	dt 0x3F, 0xFF, 0xFC ;   ####################  
	dt 0x7F, 0xFF, 0xFE ;  ###################### 
	dt 0x7F, 0xFF, 0xFE ;  ###################### 
	dt 0x7F, 0xFF, 0xFE ;  ###################### 
	dt 0x7F, 0xFF, 0xFE ;  ###################### 
	dt 0x7F, 0xFF, 0xFE ;  ###################### 
	dt 0x7F, 0xFF, 0xFE ;  ###################### 
	dt 0x3F, 0xFF, 0xFC ;   ####################  
	dt 0x3F, 0xFF, 0xFC ;   ####################  
	dt 0x3F, 0xFF, 0xFC ;   ####################  
	dt 0x1F, 0xFF, 0xF8 ;    ##################   
	dt 0x0F, 0xFF, 0xF0 ;     ################    
	dt 0x07, 0xFF, 0xE0 ;      ##############     
	dt 0x03, 0xFF, 0xC0 ;       ############      
	dt 0x00, 0x7E, 0x00 ;          ######         
	dt 0x00, 0x00, 0x00 ;                    					
Table_kamen_data_end
Table_kamen_data_len	equ Table_kamen_data_end - Table_kamen_data
;}


; *** KONEC SOUBORU *************************************************	
	end