#include "p16F76.inc"

	__CONFIG _WDTE_OFF & _PWRTE_OFF & _BOREN_OFF & _CP_OFF & _FOSC_HS
	    
    errorlevel    -302    


;PRO MASTERA:
;------------
; nastavit PACKET_NUMBER_START			equ .100
; zmenit v MAIN:
; 	movlw PLAYER_MASTER
;	movwf GAME_player

;PRO SLAVEA:
;------------
; nastavit PACKET_NUMBER_START			equ .200
; zmenit v MAIN:
; 	movlw PLAYER_SLAVE
;	movwf GAME_player


; HC-12 setup:
; FU1, B4800, C0007, P1

;*****************************************
;TODO
;*****************************************

;1) sendPacket - aby pri prijeti MAX_TRANSMIT_* packetu i receiver
;zahlasil chybu spojeni (mozna kontrolovat stejny obsah packetu a
;pokud jsou posledni a aktualni stejny, zvysit transmitCount

;******************************************************************
;****************** K O N S T A N T Y *****************************
;******************************************************************
; Konstanty
;{

FALSE						equ 0x00
TRUE						equ 0x01

PLAYER_MASTER				equ .0
PLAYER_SLAVE				equ .1

UART_2400BPS				equ .207
UART_4800BPS				equ .103

;Packet
PACKET_SIZE					equ .8
PACKET_MAX_TRANSMITS_GAME	equ .5			;Pro Check/Init dotazy
PACKET_MAX_TRANSMITS_TEST	equ .30			;Pro prvni test spojeni
PACKET_ACTION_CHECK			equ 'C'			;dotaz na pozici
PACKET_ACTION_INIT			equ 'I'			;druhy hrac polozil lode
PACKET_ACTION_TEST			equ 'T'			;uvodni test spojeni
PACKET_START_BYTE			equ 0xFE
PACKET_CHECK_RESP_BYTE		equ 0xFF		;Check request, resp byte
PACKET_INIT_RESP_BYTE		equ 0x88		;Init request, resp byte
PACKET_TEST_RESP_BYTE		equ 0xAB		;Test request, resp byte
PACKET_STOP_BYTE			equ 0xFD
PACKET_NUMBER_START			equ .100		;100-master, 200-slave
PACKET_TIMEOUT_MS			equ .2000		;2secs

;Joystick
JOY1_X_ADC					equ b'11000001' ;ADC Frc clock, AN0, ADC ON
JOY1_Y_ADC					equ b'11001001' ;ADC Frc clock, AN1, ADC ON

;HRACI PLOCHA
BOARD_SIZE					equ .10			;10x10 plocha
TILE_SIZE					equ .18 		;px
TILE_PADDING				equ .1			;px, na kazde strane
PIECE_SIZE					equ TILE_SIZE - 2*TILE_PADDING

GAME_TOTAL_HITS				equ .5 + .4 + .3 + .3 + .2 ; 17 casti lode
GAME_TOTAL_SHIPS			equ .5

STATUS_INIT_RECVD			equ .0	;(1) bit 0
STATUS_LAYING_DOWN			equ .1	;(2) pokladam (set) nebo kreslim board (unset)
STATUS_LAYING_MARK			equ .2  ;(4) pokladam znacku
STATUS_DRAWING_BOARD		equ .3	;(8) zobrazuju BOARDy
STATUS_SHOWING_COLOUR		equ .4	;(16) zobrazuju potopene lode barevne
STATUS_CHECK_RCVD			equ .5  ;(32) prisla PASS response?
STATUS_TEST_RCVD			equ .6  ;(64) mame konec hry?
STATUS_GAME_OVER			equ .7  ;(128) mame konec hry?

;Informace o poli na desce je v dolnich 4 bitech
BOARD_EMPTY					equ .0		;empty
BOARD_SHIP_5				equ .5		;lod delky 5
BOARD_SHIP_5_LEN			equ .5 
BOARD_SHIP_4				equ .4		;lod delky 4
BOARD_SHIP_4_LEN			equ .4
BOARD_SHIP_3A				equ .3		;lod delky 3 (prvni)
BOARD_SHIP_3A_LEN			equ .3
BOARD_SHIP_3B				equ .2		;lod delky 3 (druha)
BOARD_SHIP_3B_LEN			equ .3		;delka lode v polickach
BOARD_SHIP_2				equ .1		;lod delky 2
BOARD_SHIP_2_LEN			equ .2		;delka lode v polickach
BOARD_CROSS					equ .6		;obsazene misto
BOARD_MISS					equ .7		;znacka pro MISS

;HITy maji nastaveny bit 3
BOARD_SHIP_HIT_BIT			equ .3
BOARD_SHIP_HIT				equ 0x08	;znacka pro HIT (bez urceni lode)
BOARD_SHIP_5_HIT			equ BOARD_SHIP_HIT + BOARD_SHIP_5  ; 13d 
BOARD_SHIP_4_HIT			equ BOARD_SHIP_HIT + BOARD_SHIP_4  ; 12d 
BOARD_SHIP_3A_HIT			equ BOARD_SHIP_HIT + BOARD_SHIP_3A ; 11d
BOARD_SHIP_3B_HIT			equ BOARD_SHIP_HIT + BOARD_SHIP_3B ; 10d
BOARD_SHIP_2_HIT			equ BOARD_SHIP_HIT + BOARD_SHIP_2  ; 9d


TFT_HEIGHT 					equ .240 
TFT_WIDTH 					equ .320

TFT_BLACK       			equ 0x0000
TFT_WHITE       			equ 0xFFFF
TFT_RED         			equ 0xF800
TFT_GREEN       			equ 0x07E0
TFT_LIGHTGREY   			equ 0xC618      
TFT_DARKGREY    			equ 0x5ACB	
TFT_ORANGE					equ 0xFC21
TFT_VIOLET					equ 0xF81F
TFT_CYAN					equ 0x87FF
TFT_BLUE					equ 0x1032
TFT_YELLOW					equ 0xFFE0

FONT_CHAR_WIDTH				equ .8	;px
FONT_CHAR_HEIGHT			equ .16 ;px


;Pozice pro textove instrukce a informace
;*Hra Lode*
TEXT_LOGO2_X				equ .64
TEXT_LOGO2_Y				equ .8

;Nepouzito
TEXT_LOGO3_X				equ .0
TEXT_LOGO3_Y				equ .4 * FONT_CHAR_HEIGHT;

;Elektronicka hra
TEXT_LOGO4_X				equ .80
TEXT_LOGO4_Y				equ .6 * FONT_CHAR_HEIGHT + .13;

;Powered by PIC16F76
TEXT_LOGO5_X				equ .84
TEXT_LOGO5_Y				equ .8 * FONT_CHAR_HEIGHT + .13;

;Stiskni joystick
TEXT_LOGO6_X				equ .97
TEXT_LOGO6_Y				equ .13 * FONT_CHAR_HEIGHT;


TEXT_INSTR_X				equ .0
TEXT_INSTR_Y				equ (BOARD_SIZE * TILE_SIZE) + ( (TFT_HEIGHT - BOARD_SIZE * TILE_SIZE - 2*FONT_CHAR_HEIGHT) / 2)

;Pozice pro statistiky
;Stats1 = ** Me Lode **  / ** Me zasahy **
TEXT_STATS1_X				equ BOARD_SIZE * TILE_SIZE + FONT_CHAR_WIDTH
TEXT_STATS1_Y				equ .2
;Stats2 = Zasahy:			
TEXT_STATS2_X				equ BOARD_SIZE * TILE_SIZE + FONT_CHAR_WIDTH
TEXT_STATS2_Y				equ .3 * TILE_SIZE
;Stats4 = Potopeno:
TEXT_STATS4_X				equ BOARD_SIZE * TILE_SIZE + FONT_CHAR_WIDTH
TEXT_STATS4_Y				equ .6 * TILE_SIZE

;WT588D
SOUND_00_GUNSHOT			equ 0x00
SOUND_01_MOVE1				equ 0x01
SOUND_02_MOVE2				equ 0x02
SOUND_03_CHANGE1			equ 0x03
SOUND_04_CHANGE2			equ 0x04
SOUND_05_GAME_OVER			equ 0x05
SOUND_06_ERROR				equ 0x06
SOUND_07_EXPLOSION			equ 0x07
SOUND_08_SPLASH				equ 0x08
SOUND_09_CANNON				equ 0x09
SOUND_0A_SHIP_SUNKEN		equ 0x0A
SOUND_0B_FANFARE1			equ 0x0B
SOUND_0C_FANFARE2			equ 0x0C
SOUND_0D_BOMB_FALL			equ 0x0D
SOUND_0E_BEEP				equ 0x0E
SOUND_0F_CONNECTED			equ 0x0F
SOUND_10_CONNECTING			equ 0x10

;}

;******************************************************************
;****************** P R O M E N N E *******************************
;******************************************************************
    ;BANK 0
    cblock 0x20
;{    
	status_temp
	pclath_temp
	fsr_temp
		
	del1			;registry pro cekaci subrutiny
	del2
	del3
	
	asciiChar:.2	;bin2hex / 	bin2ascii
	asciiTmp		;bin2hex

	hiByte
	loByte	
	numSecs
	numMilis
	i
	j
	k
	x			;TFT_drawShip
	y			;TFT_drawShip
	tmp

	TFT_putChar
	TFT_textZoom	;Zoomuj Y/XY FALSE/TRUE	
	TFT_width		;sirka okna pro pismeno (TFT_drawText)
	TFT_height		;vyska okna pro pismeno (TFT_drawText)
	TFT_FGColor:.2	;foreground
	TFT_BGColor:.2	;background


	TFT_i
	TFT_j
	TFT_k
	TFT_byte		;8bit cislo
	TFT_tmp
	TFT_increment	;board vykreslovani	
	TFT_X:.2		;Pozice pro display window
	TFT_X_MAX:.2
	TFT_Y:.2
	TFT_Y_MAX:.2
	TFT_pieceFGColor:.2		;foreground
	TFT_pieceBGColor:.2		;background
	TFT_layoutDirection		;0/1 - horizontal/vertical
	TFT_drawingOld			;0/1 - drawShip / drawShipOld 
	TFT_pieceType
	TFT_pieceType_save

	JOY_ADresult	
	JOY_increment	;255=-1
	JOY_tmp			;8 -> 0, -1 -> 7
	JOY_whichAdjust ;0-BOARD_x, 1-BOARD_y

	BOARD_x			;pozice [x,y] na boardu [0,0]-[9,9]
	BOARD_y
	BOARD_x_old		;Predchozi pozice
	BOARD_y_old		
	BOARD_x_tmp
	BOARD_y_tmp
	BOARD_i
	BOARD_j
	BOARD_piece		;prave pokladany kus
	BOARD_pieceSave ;tmp	
	BOARD_tmp
	BOARD_shipLen	;Delka lode (5,4,3,2)
	BOARD_whichBoard ;0 - myBoard, 1 - hisBoard
	
	GAME_player		;0-MASTER, 1-SLAVE
	GAME_wasMove
	GAME_hitsCount	 ;pocet zasahu na danem boardu (my or his)
	GAME_sunkenBits  ;bity, ktere lode jsou potopeny
	GAME_lastSunkenCount ;Potopene v predchozim kole (kvuli hlasce Lod potopena)
	GAME_actSunkenCount ;Aktualne potopene 

	GAME_status		;flagy pro stav hry:
	; 0x00-starting
	; 0x01-init done
	; 0x02-laying down ship/mark
	
	dataPointer:.2	;Ukazatel do grafickych dat
	textPointer:.2	;Ukazatel do textu
	
	rxByte
	rxOffset		;Kam ukladame nacteny byte v rxBufferu
	rxPacketReady	;Mam nacteny v rxBufferu full 8B packet?
	;Format packetu - 8 Bytes:
	;0 - start byte 0xFE
	;1 - packet nr. 0x00 - 0xFF
	;2 - action ('I' - init, 'C' - check position)
	;3 - response - 0x00 (MISS) or BOARD_SHIP* (HIT)
	;4 - X
	;5 - Y
	;6 - XOR bytes 1,2,3,4,5
	;7 - end byte 0xFD	

	UART_timerH
	UART_timerL
	UART_isTimeout
	UART_transmitCount
	UART_maxTransmits
	UART_expPacketAction ; Ocekavana akce v packetu
	
	SWUART_whichPacket	; 0-tx buffer, 1-rx	
	
	wt588d_i
	wt588d_tmp
    endc
    
    cblock 0x7F
    w_temp			; spolecne misto pro vsechny banky
    endc
    
    ;BANK1
    cblock 0x0A0
	rxBuffer: PACKET_SIZE			; UART receive buffer	
	txBuffer: PACKET_SIZE			; UART send buffer
	txPacketNumber					; Packet numbering
	xorByte
	buf_i
    endc
    
    ;BANK2
    cblock 0x120
    myBoard: ((BOARD_SIZE * BOARD_SIZE) / 2)		;50B, pouzit pulbyte na kazdy kamen
    endc
    
    ;BANK3
    cblock 0x1A0
    hisBoard: ((BOARD_SIZE * BOARD_SIZE) / 2)		;50B, pouzit pulbyte na kazdy kamen
    endc    
         
;}

	

;******************************************************************
;****************** D E F I N I C E *******************************
;******************************************************************
; Definice
;{   
	
;TFT 2.8" ILI9341
#define TFT_SCK			PORTA, RA7;		(OUTPUT)
#define TFT_MOSI		PORTA, RA6;		(OUTPUT)
#define TFT_DC			PORTC, RC0;		(OUTPUT)
#define TFT_RESET		PORTC, RC1;		(OUTPUT)
#define TFT_CS			PORTC, RC2;		(OUTPUT)

;Joystick vstupy
#define JOY1_VRX		PORTA, RA0
#define JOY1_VRY		PORTA, RA1
#define JOY1_SW			PORTB, RB0
#define B_ROTATE		PORTB, RB1

;SW UART FTDI USB TO SERIAL
#define SWUART_TX		PORTB, RB7
#define SWUART_RX		PORTB, RB6

;DEBUG LED
#define LED_PACKET		PORTA, RA2
#define LED_CORRECT		PORTA, RA5 ; RA3 je ANALOG!

;WT588D   
#define WT588D_SCK		PORTB, RB5;
#define WT588D_CS		PORTB, RB4;
#define WT588D_SDA		PORTB, RB3;

;}

;******************************************************************
;****************** M A K R A *************************************
;******************************************************************
; Makra
;{
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   
    
PAGE0 macro
	clrf PCLATH ;Select page 0 (0000h-07FFh)
	endm
	
PAGE1 macro
	BSF PCLATH, 3 ;Select page 1 (0800h-FFFh)
	endm
;}

;******************************************************************    
;*************************** S T A R T ****************************
;******************************************************************
    
			org 0x00		
 			goto MAIN
			
;******************************************************************    
;********************** I N T E R R U P T *************************
;******************************************************************			
			
			org 0x04
INTERRUPT
;{	
	movwf w_temp 		;copy W to temp register, could be in either bank
	swapf STATUS, w		;swap status to be saved into W	
	BANK0
	movwf status_temp 	;save status to bank 0 register
	movf  FSR, w 		;save FSR
    movwf fsr_temp
    movf  PCLATH, w     ;move pclath register into w register
    movwf pclath_temp   ;save off contents of PCLATH register

	PAGE0
	
;                 start packet# action resp   X     Y     XOR, stop
;byte toSend[] = { 0xFE, 0x00,  'I',  0x00, 0x00, 0x00, byte  0xFD };	

INTERRUPT_MAIN

	btfss PIR1, RCIF	;Mame neco na UART RX?
	goto INTERRUPT_TIMER1	; Mame Timer1 interrupt?
	
	movf RCREG, w
	movwf rxByte
		
	;Cekej na offsetu 0 START_BYTE, pokud neni, nacteny byte zahod
	movf rxOffset, f
	btfss STATUS, Z
	goto INTERRUPT_NEXT
	
	movlw PACKET_START_BYTE
	xorwf rxByte, w
	btfss STATUS, Z		;rxByte == START_BYTE?
	goto INTERRUPT_TIMER1	;jeste zkontroluj Timer1 overflow
	
INTERRUPT_NEXT
	
	movlw rxBuffer
	movwf FSR
	movf rxOffset, w
	addwf FSR, f		;posun se v rx bufferu na spravnou pozici
	movf rxByte, w
	movwf INDF			;a uloz tam nacteny byte
	incf rxOffset, f	;posun na dalsi misto v poli
	movlw PACKET_SIZE
	xorwf rxOffset, w	;jsme na konci bufferu?
	btfss STATUS, Z
	goto INTERRUPT_TIMER1
	clrf rxOffset		;ano, posun na zacatek rxBufferu
	bsf rxPacketReady,0 ;muzeme zpracovat
	;jeste zkontroluj Timer1
	
INTERRUPT_TIMER1	

	btfss PIR1, TMR1IF 
	goto INTERRUPT_FINISH
	
	bcf PIR1, TMR1IF	;Vycisti Timer1 interrupt flag
	incf UART_timerL, f	;pricti 1ms
	btfsc STATUS, Z
	incf UART_timerH, f
	
	;Mame 2 secs?
	movlw HIGH PACKET_TIMEOUT_MS
	xorwf UART_timerH, w
	btfss STATUS, Z
	goto INTERRUPT_TIMER1_RESET
	movlw LOW PACKET_TIMEOUT_MS
	xorwf UART_timerL, w
	btfss STATUS, Z
	goto INTERRUPT_TIMER1_RESET
	
	bsf UART_isTimeout, 0		;Timeout
	
	clrf UART_timerH
	clrf UART_timerL
		
INTERRUPT_TIMER1_RESET
	
	;reset TMR1
	movlw 0xF8
	movwf TMR1H
	movlw 0x56
	movwf TMR1L
		
INTERRUPT_FINISH

	btfss RCSTA, OERR	;overrun?
	goto INTERRUPT_SKIP
	bcf RCSTA, CREN
	bsf RCSTA, CREN
		
INTERRUPT_SKIP
    movf  pclath_temp,w ;retrieve copy of PCLATH register
    movwf PCLATH        ;restore pre-isr PCLATH register contents
    movf  fsr_temp, w
    movwf FSR			;restore FSR
	swapf status_temp,w	;swap status_temp register into W, sets bank to original state
	movwf STATUS 		;move W into STATUS register
	swapf w_temp, f		;swap w_temp
	swapf w_temp, w		;swap w_temp into W
	retfie
;}

; ******************************************************************
; ************************** M A I N *******************************
; ******************************************************************

MAIN
;{			
	movlw PLAYER_MASTER
	movwf GAME_player
	
	PAGE1
	call SETUP
	PAGE0
;}

;*******************************************************************
;******************* G A M E   S T A R T ***************************
;*******************************************************************

GAME_START
;{						
	call BOARDs_init
	
	call GAME_init
	
	call UART_init
	
	call GAME_enableInterrupt
	
	PAGE1
	call TFT_init	
	call TFT_clearScreen
	
	PAGE0
	call WT588D_playConnecting
	call TFT_showInfoTestingComms
	
	;MASTER - posila test packet
	btfsc GAME_player, 0
	goto GAME_slaveTest
	
	call UART_sendTestPacket
	goto GAME_testFinished
		
	;SLAVE - prijima test packet
GAME_slaveTest

	call GAME_waitForMasterTestPacket

GAME_testFinished

	call WT588D_playConnected
	
	call TFT_showInfoTestingOK
	
	PAGE1
	call TFT_clearScreen
	PAGE0
	call TFT_showLogo
	call WT588D_playIntro 		;Bullets + shots
	
	;Zakomentovano - Slave muze rychle umistit sve lode, zatimco
	;master bude jeste na uvodni obrazovce, tzn. slave uz odesila
	;Init packet a Master jeste neprijima.
	;call JOY_waitButton
	
	PAGE1
	call wait1s
	call wait1s
	call wait1s
	
	call TFT_clearScreen		
	call TFT_drawBoardGrid
	PAGE0
			
	call TFT_showStats
	
	call TFT_showInfoPlaceShips
						
	call TFT_layDownShips
	
	;MASTER - ceka na Init packet
	btfsc GAME_player, 0
	goto GAME_slaveInit
				
	call TFT_showInfoWaitingOpponent
	
	call GAME_waitForOpponentInit
	
	goto GAME_MASTER_PLAYS

	;SLAVE - posila Init packet	
GAME_slaveInit

	call UART_sendInitPacket	; Odesli a pockej na odpoved
	
	goto GAME_SLAVE_PLAYS
		
GAME_MASTER_PLAYS
	
	; Hraju ja
	call GAME_toggleBoard

GAME_I_PLAY					; Pri zasahu hraju znovu ja

	call TFT_showStats
	
	call TFT_drawHisBoard
	
	call TFT_showInfoIPlay
	
	call TFT_layDownMark
	
	call TFT_showInfoSendingMove
	
	call UART_sendMoveToOpponent
	
	call GAME_handleResponse	;Voda nebo Zasah, vykresli potrebne
	
	call GAME_checkGameOver
	
	btfsc GAME_status, STATUS_GAME_OVER
	goto GAME_OVER
	
	;Pri zasahu opakuj tah
	btfsc BOARD_piece, BOARD_SHIP_HIT_BIT
	goto GAME_I_PLAY




	; Hraje druhy hrac	
	call GAME_toggleBoard
	
GAME_SLAVE_PLAYS			;Nesmi se prepnout board, kdyz zacina SLAVE
	
	call TFT_showStats
	
	call TFT_drawMyBoard
		
	call TFT_showInfoHePlays
	
	call GAME_waitForCheckPacket
	
	call GAME_sendCheckResponse
	
	call BOARD_setPlaceValue	; Uloz hit nebo miss na muj board
	
	call WT588D_playHitOrMiss
	
	call TFT_showInfoHitOrMiss ; Souper: Zasah nebo Souper: Voda
		
	call GAME_drawHitOrMiss		; Zablikej, jak to dopadlo
	
	call GAME_checkGameOver
	
	btfsc GAME_status, STATUS_GAME_OVER
	goto GAME_OVER
	
	btfsc BOARD_piece, BOARD_SHIP_HIT_BIT
	goto GAME_SLAVE_PLAYS
			
	goto GAME_MASTER_PLAYS				; Protihrac nedal zasah, hraju ja

GAME_OVER

	PAGE1
	movlw .250
	call waitMilis
	movlw .250
	call waitMilis	
	PAGE0

	call TFT_showInfoGameOver
	
	call WT588D_playGameOver
	
	PAGE1
	call wait1s
	call wait1s
	PAGE0	
	
	call TFT_showWinner

	call WT588D_playWinner
	
	PAGE1
	call wait1s
	call wait1s
	call wait1s
	PAGE0
	
	;Po skonceni hry ukaz vzdy svuj board,
	;aby bylo videt, kdo se kam netrefil.
	clrf BOARD_whichBoard
	call TFT_showStats
	call TFT_drawMyBoard
	
	
	call JOY_waitButton
	
	goto GAME_START
	
GAME_CONNECTION_LOST

	call SWUART_debug

	call GAME_disableInterrupt

	call WT588D_playError

	call TFT_showInfoConnectionLost
	
	goto $						; Cykli
					
;}
	
	
;*******************************************************************
;********************    W T 5 8 8 D *******************************
;*******************************************************************

;Vstup: W-REG - cislo zvuku
WT588D_playSound
;{
	movwf tmp
	PAGE1
	movf tmp, w
	call WT588D_doSoundFX
	PAGE0
	return
;}

WT588D_playClick
;{
	movlw SOUND_02_MOVE2
	goto WT588D_playSound
;}

WT588D_playRotate
;{
	movlw SOUND_01_MOVE1
	goto WT588D_playSound
;}

WT588D_playWrongPlace
;{
	movlw SOUND_06_ERROR
	goto WT588D_playSound
;}
WT588D_playError
;{
	movlw SOUND_0E_BEEP
	goto WT588D_playSound
;}

WT588D_playLaidDown
;{
	movlw SOUND_03_CHANGE1
	goto WT588D_playSound
;}

WT588D_playHitOrMiss
;{
	;Zvuk padajici bomby jen kdyz strili souper
	btfsc BOARD_whichBoard, 0
	goto WT588D_playHMNext	
	call WT588D_playBombFall	
	PAGE1
	call wait1s	
WT588D_playHMNext
	PAGE1
	movlw .250
	call waitMilis
	PAGE0
	
	btfsc BOARD_piece, BOARD_SHIP_HIT_BIT
	goto WT588D_playExplosion
	goto WT588D_playSplash
;}

WT588D_playBombFall
;{
	movlw SOUND_0D_BOMB_FALL
	goto WT588D_playSound
;}
WT588D_playExplosion
;{
	movlw SOUND_07_EXPLOSION
	goto WT588D_playSound
;}
WT588D_playSplash
;{
	movlw SOUND_08_SPLASH
	goto WT588D_playSound
;}

WT588D_playCannon
;{
	movlw SOUND_09_CANNON
	goto WT588D_playSound
;}

WT588D_playGameOver
;{
	movlw SOUND_05_GAME_OVER
	goto WT588D_playSound
;}


WT588D_playConnecting
;{
	movlw SOUND_10_CONNECTING
	call WT588D_playSound
	PAGE1
	movlw .100
	call waitMilis
	PAGE0
	movlw 0xF2			;loop aktualne prehravaneho zvuku
	goto WT588D_playSound
;}

WT588D_playConnected
;{
	movlw SOUND_0F_CONNECTED
	goto WT588D_playSound
;}

WT588D_playFanfare1
;{
	movlw SOUND_0B_FANFARE1
	goto WT588D_playSound
;}

WT588D_playFanfare2
;{
	movlw SOUND_0C_FANFARE2
	goto WT588D_playSound
;}

WT588D_playShipSunken
;{
	PAGE1
	movlw .250
	call waitMilis
	PAGE0
	movlw SOUND_0A_SHIP_SUNKEN
	call WT588D_playSound
	PAGE1
	call wait1s
	PAGE0
	return
;}

WT588D_playIntro
;{
	call TFT_setBlackOnBlue
	
	PAGE1
	movlw SOUND_00_GUNSHOT
	call WT588D_doSoundFX
	PAGE0
	call TFT_showBullet1
	movlw .250
	call waitMilis
	movlw SOUND_00_GUNSHOT
	call WT588D_doSoundFX
	PAGE0
	call TFT_showBullet2
	movlw .250
	call waitMilis
	movlw SOUND_00_GUNSHOT
	call WT588D_doSoundFX
	PAGE0
	call TFT_showBullet3
	movlw .250
	call waitMilis	
	movlw SOUND_07_EXPLOSION
	call WT588D_doSoundFX
	PAGE0
	call TFT_showBullet4
	movlw .250
	call waitMilis	
	movlw .250
	call waitMilis	
	movlw .100
	call waitMilis	
	PAGE0
	return
;}

WT588D_playWinner
;{
	btfss BOARD_whichBoard, 0
	return
	goto WT588D_playFanfare1
;}



;*******************************************************************
;******************* G A M E  R U T I N Y **************************
;*******************************************************************

GAME_disableInterrupt
;{	
	clrf rxOffset
	clrf rxPacketReady
	
	BANK1
	clrf PIE1				;Zakaz UART receive interrupt
	BANK0	
	bcf INTCON, GIE			;Zakaz vsechna preruseni
	bcf INTCON, PEIE		;vcetne peripheral		
	bcf RCSTA,  CREN		;disable reception
	return
;}

GAME_enableInterrupt
;{
	clrf rxOffset
	clrf rxPacketReady	
	
	BANK1	
	bsf PIE1, RCIE 			;enable RX IRQ

	BANK0	
	bsf RCSTA,  CREN		;enable reception
	bsf INTCON, PEIE		;enable global and peripheral interrupt
	bsf INTCON, GIE
	return
;}

		
GAME_init
;{
	
	clrf GAME_status
	clrf SWUART_whichPacket
	
	clrf GAME_lastSunkenCount
	clrf GAME_actSunkenCount
		
	movlw BOARD_SHIP_5
	movwf BOARD_piece
	call TFT_copyBoardPiece
	
	clrf TFT_layoutDirection
	movlw BOARD_SHIP_5_LEN
	movwf BOARD_shipLen
	
	goto GAME_disableInterrupt	
	
;}

GAME_sendCheckResponse
;{
	bcf GAME_status, STATUS_CHECK_RCVD	;reset pro dalsi kolo
		
	;Ziskej hodnotu na x, y a vrat zpet
	call BOARD_getPlaceValue
	movwf BOARD_piece	
	iorlw BOARD_EMPTY	;pokud je neprazdno, priORuj HIT_BIT
	btfss STATUS, Z		;pokud je prazdno, nastav MISS
	goto GAME_sendCheckRespHit
	
	movlw BOARD_MISS	
	movwf BOARD_piece
	goto GAME_sendCheckRespNext
	
GAME_sendCheckRespHit	
	
	bsf BOARD_piece, BOARD_SHIP_HIT_BIT
	
GAME_sendCheckRespNext
	
	movf BOARD_piece, w	;uloz do txBufferu na pozici resp.
	BANK1
	movwf txBuffer + .3
	BANK0
	
	call UART_sendTxBuffer
	
	bcf LED_CORRECT
	
	return
;}

;Vstup: nastaveny BOARD_x, BOARD_y a BOARD_piece podle response	
GAME_handleResponse
;{	
	btfss BOARD_piece, BOARD_SHIP_HIT_BIT
	goto GAME_noHit
		
	call TFT_showInfoResponseHit
	goto GAME_handleRespNext
	
GAME_noHit
	call TFT_showInfoResponseMiss	
			
GAME_handleRespNext

	call WT588D_playHitOrMiss
				
	;Na desku ulozim spravnou hodnotu, ale pokud prisel nejaky hit,
	;tak nezobrazuju barvu lode, ale jen hit kamen.
	call BOARD_setPlaceValue		;uloz na desku, co jsme dostali
	
	movlw BOARD_SHIP_HIT			;je-li hit, nezobrazuj barvu lode
	btfsc BOARD_piece, BOARD_SHIP_HIT_BIT
	movwf BOARD_piece
	
GAME_drawHitOrMiss
		
	movf BOARD_piece, w
	movwf BOARD_pieceSave		
	
	movlw .3
	movwf i
GAME_drawBlink
	
	movf BOARD_pieceSave, w
	movwf BOARD_piece
	call TFT_copyBoardPiece
	PAGE1
	call TFT_drawPiece
	
	movlw .200
	call waitMilis
	
	bcf LED_CORRECT
	
	movlw .200
	call waitMilis
	PAGE0
	
	;Pri Miss blikej mezi modrym a miss znackou
	;Pri Hit blikej mezi barvou lode a cervenym polem
	movlw BOARD_CROSS		
	btfss BOARD_pieceSave, BOARD_SHIP_HIT_BIT
	movlw BOARD_EMPTY
	movwf BOARD_piece
	call TFT_copyBoardPiece
	PAGE1
	call TFT_drawPiece
	
	movlw .200
	call waitMilis
	movlw .200
	call waitMilis
	PAGE0
	
	decfsz i, f
	goto GAME_drawBlink

	movf BOARD_pieceSave, w
	movwf BOARD_piece
	call TFT_copyBoardPiece
	PAGE1
	call TFT_drawPiece	
	PAGE0

	;Pri trefe zobraz aktualizovane statistiky
	btfsc BOARD_piece, BOARD_SHIP_HIT_BIT
	call TFT_showStats2		;Bez cisteni boxu
				
	btfss BOARD_whichBoard, 0	;Zajima nas dale jen souperuv board
	return
	
	;Zjisti, jestli jsme potopili novou lod (pouze hisBoard)
	PAGE1	
	call BOARD_getSunkenCount
	PAGE0	
	btfsc BOARD_whichBoard, 0
	movwf GAME_actSunkenCount
	
	xorwf GAME_lastSunkenCount, w
	btfss STATUS, Z
	
	;last != act ---> nova potopena lod, zobraz hlaseni
	call TFT_showInfoResponseHitNewSunken

GAME_handleRespNoNewSunk
	
	movf GAME_actSunkenCount, w
	movwf GAME_lastSunkenCount
		
	return
;}

;Pockej az prijde inicializacni packet od protihrace	
GAME_waitForOpponentInit
;{	
	btfss GAME_status, STATUS_INIT_RECVD
	goto GAME_waitForOpponentInitNext
	
	;Po Init packetu uz budou chodit jen 'C'heck packety
	movlw PACKET_ACTION_CHECK
	movwf UART_expPacketAction
	
	PAGE1
	movlw .200
	call waitMilis
	PAGE0
	
	bcf LED_CORRECT
	
	return

GAME_waitForOpponentInitNext
	
	; STATUS_INIT_RECVD == 1, pokud mame korektni init packet
	call UART_waitForInitPacket
	
	goto GAME_waitForOpponentInit
;}

;Cekej na Test packet od Mastera
GAME_waitForMasterTestPacket
;{
	movlw PACKET_ACTION_TEST
	movwf UART_expPacketAction
	
	btfss GAME_status, STATUS_TEST_RCVD
	goto GAME_waitForMasterTestPacketNext
	
	movlw PACKET_ACTION_INIT
	movwf UART_expPacketAction


	;Aby zeleny blik byl chvili videt
	PAGE1
	movlw .250
	call waitMilis
	PAGE0
		
	bcf LED_CORRECT
	
	return
	
GAME_waitForMasterTestPacketNext
	
	call UART_waitForTestPacket
	
	goto GAME_waitForMasterTestPacket
;}

;Pockej az prijde Check packet od protihrace
GAME_waitForCheckPacket
;{
	btfsc GAME_status, STATUS_CHECK_RCVD
	return
	
	call UART_waitForCheckPacket
	
	goto GAME_waitForCheckPacket
;}

;Prepni horizontal / vertical layout, pokud bylo stisknutou tlacitko
GAME_handleLayoutSwitch
;{
	btfss B_ROTATE
	return
	
	PAGE1
	movlw .10
	call waitMilis
	PAGE0
	
	btfss B_ROTATE
	return
	
GAME_handleLayoutSw
	btfsc B_ROTATE
	goto GAME_handleLayoutSw

	;Zjistime, jestli muzeme rotovat o +/- 90 stupnu, jestli se zrotovana
	;lod z bodu [BOARD_x, BOARD_y] vleze cela na desku.
	
	movf TFT_layoutDirection, w		;Ulozime si pred testem akt. stav
	movwf TFT_tmp
	
	call TFT_toggleLayoutDirection	;Prepni do ciloveho modu
		
	;-----------
	movf BOARD_shipLen, w
	addlw .255			;dec W	
	btfsc TFT_layoutDirection, 0
	goto GAME_handleLV
	addwf BOARD_x, w	
	goto GAME_handleLNext
GAME_handleLV
	addwf BOARD_y, w
GAME_handleLNext	
	sublw BOARD_SIZE - .1
	btfss STATUS, C				;C = OK, vlezeme se v horiz. smeru
	;-----------
	goto GAME_handleLSwitchFinish ; Nedelej nic
	
	call TFT_toggleLayoutDirection ;Uklid na stare pozici a smeru
	call TFT_drawShipOld
	call TFT_toggleLayoutDirection ;A na novou vykresli zrotovanou lod
	call GAME_setPieceTypeAfterMove	;Zjisti obsazenost/hratelnost noveho mista
	call WT588D_playRotate
	goto TFT_drawShip		;HOTOVO

GAME_handleLSwitchFinish

	call WT588D_playWrongPlace
	
	movf TFT_tmp, w
	movwf TFT_layoutDirection	
	return
	
;}

;Muzeme ulozit lod na misto na desce?
;Vstup: TFT_pieceType
;Vystup: ZERO - NEmuzeme
;Vystup: NON_ZERO - muzeme
GAME_canWePlaceOnBoard
;{
	movlw BOARD_CROSS
	subwf TFT_pieceType, w
	return
;}

;Vstup: BOARD_x, BOARD_y
;Vystup: TFT_pieceType - ponecham, je-li misto prazdne, BOARD_CROSS,
;pokud je obsazeno nektere z poli
GAME_setPieceTypeAfterMove
;{	
	call BOARD_saveBoardXY
			
	movf BOARD_piece, w			;Tohle chcu ukladat, pokud je prazdno
	movwf TFT_pieceType
	
	movf BOARD_shipLen, w
	movwf i
	
GAME_setAfterMoveLoop
	
	call BOARD_getPlaceValue
		
	iorlw BOARD_EMPTY
	btfss STATUS, Z				;Prazdne misto?
	goto GAME_setAfterNotEmpty	;Ne, koncime, nelze pokladat 
		
	btfss TFT_layoutDirection, 0	;kterym smerem pokracujeme?
	goto GAME_setAfterIncX
	incf BOARD_y, f	
	goto GAME_setAfterCommon

GAME_setAfterNotEmpty
	movlw BOARD_CROSS			;Pri obsazenem ukaz krizek
	movwf TFT_pieceType
	goto GAME_setAfterFinish	

GAME_setAfterIncX
	incf BOARD_x, f
	
GAME_setAfterCommon
	decfsz i, f
	goto GAME_setAfterMoveLoop

GAME_setAfterFinish

	goto BOARD_restoreBoardXY
	
;}

	
;Nastavi TFT_pieceType na stare pozici
;Vstup - BOARD_x_old, BOARD_y_old
GAME_setPieceOldPosition
;{
	call BOARD_saveXY	
	call GAME_restorePosition ;x_old,y_old -> x,y
	call BOARD_restoreXY
	return
;}

		
;Uloz aktualni pozici X,Y kurzoru a aktualni typ zobrazovani
GAME_savePosition
;{
	movf BOARD_x, w
	movwf BOARD_x_old
	movf BOARD_y, w
	movwf BOARD_y_old
	return
;}

;Obnov aktualni pozici X,Y kurzoru a aktualni typ zobrazovani
GAME_restorePosition
;{
	movf BOARD_x_old, w
	movwf BOARD_x
	movf BOARD_y_old, w
	movwf BOARD_y
	return
;}

		
;Vystup: GAME_wasMove -  0-kurzorem se nepohlo, 1-byl pohyb
GAME_checkMove
;{
	bsf GAME_wasMove, 0	; Predpokladame, ze byl
	
	movf BOARD_x_old, w
	xorwf BOARD_x, w
	btfss STATUS, Z
	return
	movf BOARD_y_old, w
	xorwf BOARD_y, w
	btfss STATUS, Z
	return
	movlw FALSE
	bcf GAME_wasMove, 0
	return
;}
	
;Vstup: BOARD_piece
;Vystup: nastavim STATUS_GAME_OVER, pokud je pocet zasahu == pocet kousku lodi
GAME_checkGameOver	
;{	
	PAGE1		
	call BOARD_getHitsCount
	PAGE0
	xorlw GAME_TOTAL_HITS	; Vsechno zasazeno?
	btfss STATUS, Z
	return
	
	bsf GAME_status, STATUS_GAME_OVER
	
	;Prekresli board, aby se cervene zasahy prekreslili spravne barvou lodi
	goto TFT_drawBoard	
		
;}
	
;Prepinej mezi boardy
GAME_toggleBoard
;{
	movlw 0x01
	xorwf BOARD_whichBoard, f
	return
;}	

;*******************************************************************
;******************* ZAPINANI/VYPINANI TIMERU1 pro odpocet *********
;*******************************************************************

; *** START TIMER1
startTimer1
;{

	BANK1
	bsf PIE1, TMR1IE
	BANK0

	clrf UART_isTimeout
	clrf UART_timerH
	clrf UART_timerL
	
	; *** INIT TIMER1 - nastav na 2000 = 1ms (pri 8MHz clocku)
	;initTimer 0xF856 - 63574 (1962 cyklu )
	movlw 0xF8
	movwf TMR1H
	movlw 0x56
	movwf TMR1L
	
	movlw b'00000101'		;Prescaler 1:1, internal, Timer1 ON
	movwf T1CON

	return
;}

; *** STOP TIMER1
stopTimer1
;{
	movlw b'00000100'		;Prescaler 1:1, internal, Timer1 OFF
	movwf T1CON
	
	BANK1
	bcf PIE1, TMR1IE
	BANK0
	return
;}

;*******************************************************************
;*************************** U A R T  ******************************
;*******************************************************************	

;Nastav ocekavanou packet action na Init
UART_init
;{
	
	clrf UART_transmitCount
	
	movlw PACKET_MAX_TRANSMITS_TEST
	movwf UART_maxTransmits

	BANK1
	movlw PACKET_NUMBER_START
	movwf txPacketNumber
	BANK0	

	;Testovaci vypis, z nejakeho duvodu je na FTDI TTL USB prvni zapis
	;poskozen, od druheho uz funguje, proto tento kod.
	goto SWUART_debug
;}

;Input: W-reg = character/byte
UART_send
;{ 	
	movwf TXREG
	BANK1
UART_wait
	btfss TXSTA, TRMT 	;check if TRMT is empty
	goto UART_wait
	BANK0
	return
;}


;Odesli txBuffer ven
UART_sendTxBuffer
;{
	clrf SWUART_whichPacket		;TX packet
	call SWUART_debugPacket		;debugni, co jde na vystup
	
	call UART_computeXORByte
	
	;Odesli packet
	movlw LOW txBuffer
	movwf FSR
	
	movlw PACKET_SIZE
	movwf i
UART_sendTxBufferLoop
	movf INDF, w
	call UART_send
	incf FSR, f
	decfsz i, f
	goto UART_sendTxBufferLoop
	
	return
;}


;Nastavi v tx bufferu XOR byte
UART_computeXORByte
;{	
	BANK1
	
	clrf xorByte				;XOR byte	

	movlw LOW txBuffer
	movwf FSR
	incf FSR, f					; od byte 1
	
	movlw .5
	movwf buf_i
UART_computeLoop
	movf INDF, w
	xorwf xorByte, f
	incf FSR, f
	decfsz buf_i, f
	goto UART_computeLoop
	
	movf xorByte, w				; na byte 6
	movwf INDF	
	
	BANK0
	
	return
	
;}

;Odesli txBuffer do sveta
UART_sendPacket
;{
	clrf UART_transmitCount
	
UART_sendPacket2	
	
	call UART_sendTxBuffer
				
	;Dokud nedostanu spravnou response, cyklim.
	;Pokud nedostanu response do 2s timeoutu, posilam znovu stejny packet.		
	call startTimer1
				
UART_sendPacketLoop

	btfss rxPacketReady, 0
	goto UART_checkTimeout
	
	bsf LED_PACKET
	
	;Hack - pri zapnutem Timer1 interruptu dochazi k framing errorum
	;pri SWUART_transmit, spatna delka pulzu, proto na chvilku vypnu.
	BANK1
	bcf PIE1, TMR1IE
	BANK0

	bsf SWUART_whichPacket, 0	;RX packet
	call SWUART_debugPacket		;debuguj, co prislo		
	
	BANK1
	bsf PIE1, TMR1IE
	BANK0
		
	;Mame response packet, zkontroluj validitu
	call UART_checkPacketFormat
	btfss STATUS, Z
	goto UART_sendPacketCommon	   ;neplatny packet, cekam dalsi
		
	;Mame platny format packetu, zkontroluj cislo packetu TX
	BANK1
	movf rxBuffer + .1, w
	xorwf txPacketNumber, w
	BANK0
	btfss STATUS, Z
	goto UART_sendPacketCommon	; spatne cislo packetu, nemelo by nastat
	
	;Mame action byte == "C" nebo "T" nebo "I"?
	movf UART_expPacketAction, w
	BANK1
	xorwf rxBuffer + .2, w
	BANK0 
	btfss STATUS, Z
	goto UART_sendPacketCommon	;Neprisla response s ocekavanou akci
	
	call stopTimer1
	
	bsf LED_CORRECT
	
	movlw PACKET_ACTION_TEST
	xorwf UART_expPacketAction, w
	btfss STATUS, Z
	goto UART_sendPacketNextAction
	
	BANK1
	movlw PACKET_TEST_RESP_BYTE
	xorwf rxBuffer + .3, w
	BANK0
	btfss STATUS, Z
	goto UART_sendPacketCommon	
	goto UART_sendPacketFinish
	
UART_sendPacketNextAction	
	
	;Validace dat ruznych typu packetu
	movlw PACKET_ACTION_CHECK
	xorwf UART_expPacketAction, w
	btfss STATUS, Z
	goto UART_sendPacketFinish  ;Kod nize jen pro "C"heck packet
	
	;v BOARD_x a BOARD_y uz mame hodnoty
	;nacti, co za typ pole nam vratila response (rxBuffer[3])
	BANK1
	movf rxBuffer + .3, w
	BANK0
	movwf BOARD_piece		;tohle budeme ukladat na board
	
	goto UART_sendPacketFinish
		
UART_sendPacketCommon
			
	bcf LED_PACKET
	
	clrf rxPacketReady		; cekej na dalsi				
	
	goto UART_sendPacketLoop
				
UART_checkTimeout

	;Doslo k timeoutu? Posli txPacket znovu
	btfss UART_isTimeout, 0
	goto UART_sendPacketLoop
				
	call stopTimer1
	
	;Maximalne max retransmissions, pote mame ztracene spojeni
	incf UART_transmitCount, f
	movf UART_maxTransmits, w
	xorwf UART_transmitCount, w
	btfsc STATUS, Z
	goto GAME_CONNECTION_LOST
	
	goto UART_sendPacket2
	
UART_sendPacketFinish

	clrf rxPacketReady
	
	BANK1
	incf txPacketNumber, f	; dalsi packet s cislem +1
	BANK0
		
	bcf LED_PACKET
				
	return						
;}

;Odesli packet z dotazem 'C', jestli je pole obsazene lodi nebo prazdne
UART_sendMoveToOpponent
;{	
	call UART_populateTxCommonBytes	
	
	;Stale v BANK1
	
	;2
	movlw PACKET_ACTION_CHECK
	movwf txBuffer + .2
	;3	- response 0xFF
	movlw PACKET_CHECK_RESP_BYTE
	movwf txBuffer + .3
	;4
	BANK0
	movf BOARD_x, w
	BANK1
	movwf txBuffer + .4	
	;5
	BANK0
	movf BOARD_y, w
	BANK1
	movwf txBuffer + .5
	;6 - XOR
	;Bude spocitany pri posilani
	
	BANK0
	goto UART_sendPacket
		
;}


;Kod pro druheho hrace, ktery odesle Init packet Masterovi, jakmile
;ma polozeny sve lode. Tim indikuje, ze se muze zacit hrat a Master zacina.
UART_sendInitPacket
;{
	
	movlw PACKET_MAX_TRANSMITS_GAME
	movwf UART_maxTransmits	
	
	movlw PACKET_ACTION_INIT
	movwf UART_expPacketAction
	
	call UART_populateTxCommonBytes
	
	BANK1
	;2
	movlw PACKET_ACTION_INIT
	movwf txBuffer + .2
	;3	- response 0x88
	movlw PACKET_INIT_RESP_BYTE
	movwf txBuffer + .3
	;4
	BANK1
	clrf txBuffer + .4		;X, Y = 0, 0
	;5
	clrf txBuffer + .5
	;6 - XOR
	;Bude spocitany pri posilani
	
	BANK0
	call UART_sendPacket
	
	;pokud jsme tady, dostali jsme response
	bsf GAME_status, STATUS_INIT_RECVD
	
	PAGE1
	movlw .250
	call waitMilis
	PAGE0
	
	bcf LED_CORRECT
	
	;Odted uz jen Check packety
	movlw PACKET_ACTION_CHECK
	movwf UART_expPacketAction
	
	return
	
;}

;Test packet pred hrou
UART_sendTestPacket
;{
	movlw PACKET_ACTION_TEST
	movwf UART_expPacketAction
	
	call UART_populateTxCommonBytes

	BANK1		
	;2
	movlw PACKET_ACTION_TEST
	movwf txBuffer + .2
	;3	- response 0xAB
	movlw PACKET_TEST_RESP_BYTE
	movwf txBuffer + .3
	;4
	clrf txBuffer + .4	
	;5
	clrf txBuffer + .5
	;6 - XOR
	;Bude spocitany pri posilani
	
	BANK0
	call UART_sendPacket	
	
	;Hotovo, cekame na Init packet
	movlw PACKET_ACTION_INIT
	movwf UART_expPacketAction
	
	;Pauza pro bliknuti zelene diody
	PAGE1
	movlw .250
	call waitMilis
	PAGE0
	
	bcf LED_CORRECT
	
	return
;}


;Nastav pro txBuffer spolecne fieldy (start_byte, packet nr., stop_byte)
UART_populateTxCommonBytes
;{
	BANK1
	
	;0 - START BYTE
	movlw PACKET_START_BYTE
	movwf txBuffer + .0
	
	;1	
	movf txPacketNumber, w	
	movwf txBuffer + .1
	
	;7 - STOP BYTE
	movlw PACKET_STOP_BYTE
	movwf txBuffer + .7
	
	return	
	
;}

;Zkontroluj format packetu (start byte uz je zkontrolovany v ISR)
;Kontroluj CRC a pokud je vse OK, nastav BOARD_x, BOARD_y
;Vstup: rxBuffer
;Vystup: ZERO - packet valid
;Vystup: NON-ZERO - packet invalid 
UART_checkPacketFormat
;{
	BANK1 
	
	clrf xorByte
	
	;1) Kontrola STOP BYTE na konci bufferu
	movlw PACKET_STOP_BYTE
	xorwf rxBuffer + .7, w
	BANK0
	btfss STATUS, Z
	return						;Pozor, abych se vratil do BANK0!
		
	;2) Kontrola XOR byte
	BANK1		
	movf rxBuffer + .1, w		;packet nr.
	xorwf xorByte, f
	movf rxBuffer + .2, w		;akce
	xorwf xorByte, f
	movf rxBuffer + .3, w		;akce resp
	xorwf xorByte, f
	movf rxBuffer + .4, w		; X
	xorwf xorByte, f	
	movf rxBuffer + .5, w		; Y	
	xorwf xorByte, f			;tmp = [1] ^ [2] ^ [3] ^ [4] ^ [5]
	movf rxBuffer + .6, w
	xorwf xorByte, w
	BANK0		
	btfss STATUS, Z
	return						;Pozor, abych se vratil do BANK0!
	
	BANK1
	;kontrola akce 'I' nebo 'C' nebo 'T'
	movlw PACKET_ACTION_INIT
	xorwf rxBuffer + .2, w
	btfsc STATUS, Z
	goto UART_checkNext
	movlw PACKET_ACTION_CHECK
	xorwf rxBuffer + .2, w
	btfsc STATUS, Z
	goto UART_checkNext
	movlw PACKET_ACTION_TEST
	xorwf rxBuffer + .2, w
	btfsc STATUS, Z
	goto UART_checkNext			
	goto UART_checkInvalidPacket
	
UART_checkNext
	
	;kontrola 0-9 pro X a Y
	movlw .256 - BOARD_SIZE
	addwf rxBuffer + .4, w
	btfsc STATUS, C
	goto UART_checkInvalidPacket
	
	movlw .256 - BOARD_SIZE
	addwf rxBuffer + .5, w
	btfsc STATUS, C
	goto UART_checkInvalidPacket	

	BANK0
	bsf STATUS, Z
	return
	
UART_checkInvalidPacket
	BANK0
	bcf STATUS, Z
	return
	
;}

;Handler na prichozi packety
;Vstup: UART_expPacketAction
UART_waitForInitPacket
;{
	btfss rxPacketReady, 0
	return	
	
	bsf LED_PACKET
			
	bsf SWUART_whichPacket, 0		;RX packet
	call SWUART_debugPacket			;debuguj, co prislo
					
	call UART_checkInitPacket
				
	bcf LED_PACKET
	
	clrf rxPacketReady				; cekej na dalsi
			
	return
	
;}

UART_waitForTestPacket
;{
	btfss rxPacketReady, 0
	return	
	
	bsf LED_PACKET
			
	bsf SWUART_whichPacket, 0		;RX packet
	call SWUART_debugPacket			;debuguj, co prislo
					
	call UART_checkTestPacket
				
	bcf LED_PACKET
	
	clrf rxPacketReady				; cekej na dalsi
			
	return
	
;}


;Vstup: rxBuffer
;Vstup UART_expPacketAction - jakou akci v packetu ocekavam
;Vystup: GAME_status nastaven init_done bit, pokud je packet v poradku
UART_checkTestPacket
;{
	call UART_checkPacketFormat
	btfss STATUS, Z					
	return							;Invalid packet format
		
	movf UART_expPacketAction, w	;Dorazil ocekavany packet?	
	BANK1
	xorwf rxBuffer + .2, w
	BANK0
	btfss STATUS, Z
	return
	
	BANK1
	movlw PACKET_TEST_RESP_BYTE 	;Test packet ma 0xAB jako resp byte	
	xorwf rxBuffer + .3, w
	BANK0
	btfss STATUS, Z
	return

	call UART_sendBackPacket ; Vrat stejny packet zpet jako potvrzeni
	
	;Pokud porad chodi packety z druhe strany a nedari se
	;je potvrzovat, pak po 5 pokusech zahlas chybu spojeni.
	incf UART_transmitCount, f
	movf UART_maxTransmits, w
	xorwf UART_transmitCount, w
	btfsc STATUS, Z
	goto GAME_CONNECTION_LOST

	bsf GAME_status, STATUS_TEST_RCVD
	
	bsf LED_CORRECT
	
	clrf UART_transmitCount	;pro dalsi pouziti
	
	return
	
;}



;Vstup: rxBuffer
;Vstup UART_expPacketAction - jakou akci v packetu ocekavam
;Vystup: GAME_status nastaven init_done bit, pokud je packet v poradku
UART_checkInitPacket
;{
	call UART_checkPacketFormat
	btfss STATUS, Z					
	return							;Invalid packet format
		
	movf UART_expPacketAction, w	;Dorazil ocekavany packet?	
	BANK1
	xorwf rxBuffer + .2, w
	BANK0
	btfss STATUS, Z
	return
	
	movlw PACKET_INIT_RESP_BYTE		;Init packet ma 0x88 jako resp byte
	BANK1
	xorwf rxBuffer + .3, w
	BANK0
	btfss STATUS, Z
	return
	
	call UART_sendBackPacket ; Vrat stejny packet zpet jako potvrzeni
	
	;Pokud porad chodi packety z druhe strany a nedari se
	;je potvrzovat, pak po 5 pokusech zahlas chybu spojeni.
	incf UART_transmitCount, f
	movf UART_maxTransmits, w
	xorwf UART_transmitCount, w
	btfsc STATUS, Z
	goto GAME_CONNECTION_LOST

	bsf GAME_status, STATUS_INIT_RECVD
	
	bsf LED_CORRECT
	
	return
	
;}


;Vstup: rxBuffer - prijaty packet
;Vrat stejny packet zpet odesilateli
UART_sendBackPacket
;{
	BANK1
	movf rxBuffer + .0, w
	movwf txBuffer + .0
	movf rxBuffer + .1, w
	movwf txBuffer + .1
	movf rxBuffer + .2, w
	movwf txBuffer + .2
	movf rxBuffer + .3, w
	movwf txBuffer + .3
	movf rxBuffer + .4, w
	movwf txBuffer + .4
	movf rxBuffer + .5, w
	movwf txBuffer + .5
	movf rxBuffer + .6, w
	movwf txBuffer + .6			
	movf rxBuffer + .7, w
	movwf txBuffer + .7	
	BANK0
	
	goto UART_sendTxBuffer			
;}

;Handler na prichozi Check packet
UART_waitForCheckPacket
;{
	btfss rxPacketReady, 0
	return	
	
	bcf GAME_status, STATUS_CHECK_RCVD
	
	bsf LED_PACKET	
	
	bsf SWUART_whichPacket, 0		;RX packet
	call SWUART_debugPacket			;debuguj, co prislo	
		
	call UART_checkPacketFormat		
	btfss STATUS, Z					
	goto UART_waitForCheckPacketInv	;Invalid packet format
		
	movlw PACKET_ACTION_CHECK		;Dorazil packet 'C'?
	BANK1
	xorwf rxBuffer + .2, w
	BANK0
	btfss STATUS, Z
	goto UART_waitForCheckPacketInv
	
	;Kontroluj resp byte pro odliseni predchoziho packetu s Check response
	;of noveho Check requestu
	movlw PACKET_CHECK_RESP_BYTE		;Musi prijit request (akce==0xFF)
	BANK1
	xorwf rxBuffer + .3, w
	BANK0
	btfss STATUS, Z
	goto UART_waitForCheckPacketInv
	
	bsf LED_CORRECT
	
	;Nakopiruj uz do txBufferu pro response
	BANK1
	movf rxBuffer + .1, w		;zachovej cislo packetu
	movwf txBuffer + .1
	
	movf rxBuffer + .2, w		;akce 'C'
	movwf txBuffer + .2
		
	movf rxBuffer + .4, w		;X
	movwf txBuffer + .4
	BANK0
	movwf BOARD_x
	
	BANK1
	movf rxBuffer + .5, w		;Y
	movwf txBuffer + .5
	BANK0
	movwf BOARD_y
	
	bsf GAME_status, STATUS_CHECK_RCVD
	
UART_waitForCheckPacketInv
	
	bcf LED_PACKET
	
	clrf rxPacketReady				; cekej na dalsi	
	
	return
	
;}




;9600 baud rate (104uS for 1 bit)
;Vstup: W-reg - byte to send
SWUART_Transmit
;{
	movwf tmp
	
	;1. Start bit 
	bcf SWUART_TX	
	call Delay_104us

	;2. Send bits, 8bits, no parity, no flow control
	movlw .8
	movwf i	
SWUART_TxLoop	
	btfss tmp, 0			; bit 0 nastaven?
	goto SWUART_bitNotSet
	bsf SWUART_TX		 	; ano, posli log 1
	goto SWUART_tested
SWUART_bitNotSet
	bcf SWUART_TX			; ne, posli log 0 na vystup
SWUART_tested
	call Delay_104us	
	rrf tmp, f
	decfsz i, f
	goto SWUART_TxLoop

	;3. Stop bit
	bsf SWUART_TX
	call Delay_104us
	
	return
;}



;Posli na debug konzoli 8Bytes packetu
;Vstup: SWUART_whichPacket - ktery buffer vypisovat
SWUART_debugPacket
;{
	
	;Tx >, Rx <
	movlw '<'
	btfss SWUART_whichPacket, 0
	movlw '>'			
	call SWUART_Transmit
		
	movlw '['
	call SWUART_Transmit
	
	movlw ' '
	call SWUART_Transmit

	movlw PACKET_SIZE
	movwf j
	
	movlw LOW rxBuffer
	btfss SWUART_whichPacket, 0		
	movlw LOW txBuffer
	movwf FSR
	
UART_debugPacketLoop

	PAGE1
	movf INDF, w
	call bin2hex				;bin -> 2 characters '0'-'F'
	PAGE0
	movf asciiChar, w
	call SWUART_Transmit
	movf asciiChar+1, w
	call SWUART_Transmit
	movlw ' '
	call SWUART_Transmit
	
	incf FSR, f
	decfsz j, f
	goto UART_debugPacketLoop
		
	movlw ']'
	call SWUART_Transmit
	
	;CR/LF
	movlw 0x0D
	call SWUART_Transmit	
	movlw 0x0A
	goto SWUART_Transmit	
		
;}

SWUART_debug
;{
	movlw 'D'
	call SWUART_Transmit
	movlw 'B'
	call SWUART_Transmit
	movlw 'G'
	call SWUART_Transmit
	movlw ':'
	call SWUART_Transmit
	
	;RCSTA
	PAGE1
	movf RCSTA, w
	call bin2hex
	PAGE0
	movf asciiChar, w
	call SWUART_Transmit
	movf asciiChar+1, w
	call SWUART_Transmit
	movlw ' '
	call SWUART_Transmit
	
	;CR/LF	
	movlw 0x0D
	call SWUART_Transmit	
	movlw 0x0A
	call SWUART_Transmit		
	return
;}

;*******************************************************************
;********************** J O Y S T I C K Y **************************
;*******************************************************************

;Cekej na stisk nektereho buttonu
;Vrat se, az nejaky byl
JOY_waitButton
;{
	btfss JOY1_SW
	return
	goto JOY_waitButton
;}

;Byl stisk aktualniho hrace?
;Vystup: ZERO set: byl, ZERO unset nebyl
JOY_getSwitchReading
;{
	btfsc JOY1_SW
	goto JOY_SwReadWasNotPushed	;Neni stisk

JOY_SwReadNext
	PAGE1
	movlw .10					;Debounce test
	call waitMilis	
	PAGE0

JOY1_SwReadTest2	
	btfsc JOY1_SW
	goto JOY_SwReadWasNotPushed

JOY1_SwReadTest3
	btfss JOY1_SW
	goto JOY1_SwReadTest3
	goto JOY_SwReadWasPushed
	
JOY_SwReadWasPushed
	bsf STATUS, Z
	return
	
JOY_SwReadWasNotPushed
	bcf STATUS, Z
	return
;}

JOY_getReadings
;{
	clrf JOY_whichAdjust	;BOARD_x
	call JOY_getAnalogX		

	PAGE1
	call JOY_handleMoveX
	PAGE0

	incf JOY_whichAdjust, f	;BOARD_y
	call JOY_getAnalogY
	
	PAGE1
	call JOY_handleMoveY
	PAGE0
	
	call JOY_stopADC
	return
;}




JOY_stopADC
;{
	banksel ADCON0 
	movlw b'11000000' 			;ADC Frc clock, movwf ADCON0 ;AN0, ADC OFF	
	return
;}

JOY_getAnalogX
;{
	movlw JOY1_X_ADC				
	goto JOY_readAnalog
;}
JOY_getAnalogY
;{
	movlw JOY1_Y_ADC				
	goto JOY_readAnalog
;}		
JOY_readAnalog
;{		
	banksel ADCON0
	movwf ADCON0 ;ADC On		
	PAGE1
	call wait30us ;Acquisiton delay
	PAGE0
	bsf ADCON0, GO ;Start conversion
	btfsc ADCON0, GO ;Is conversion done?
	goto $-1 ;No, test again
	banksel ADRES
	movf ADRES, w ;Read upper 2 bits
	BANK0
	movwf JOY_ADresult ;store in GPR space
	return
;}
	
	
; *******************************************************************
; ************ 2.8" TFT ILI9341   R U T I N Y ***********************
; *******************************************************************

TFT_showWinner
;{
	btfss BOARD_whichBoard, 0
	goto TFT_showInfoOpponentWon		
	goto TFT_showInfoIWon
;}

;Vykresli statistiky k danemu boardu (pocet zasahu, potopenych lodi)
;Vstup: BOARD_whichBoard
TFT_showStats
;{	
	PAGE1
	call TFT_clearBox
	PAGE0

TFT_showStats2
	
	movlw FALSE
	movwf TFT_textZoom	
	
	call TFT_setYellowOnBlue
		
	btfsc BOARD_whichBoard, 0
	goto TFT_showHis
	
	;** Me Lode **
	movlw HIGH Table_text3
	movwf textPointer+0
	movlw LOW Table_text3
	movwf textPointer+1
	goto TFT_showMy

TFT_showHis
	
	;** Me zasahy **		
	movlw HIGH Table_text4
	movwf textPointer+0
	movlw LOW Table_text4
	movwf textPointer+1

TFT_showMy

	movlw HIGH TEXT_STATS1_X
	movwf TFT_X+0
	movlw LOW TEXT_STATS1_X
	movwf TFT_X+1
	movlw HIGH TEXT_STATS1_Y
	movwf TFT_Y+0
	movlw LOW TEXT_STATS1_Y
	movwf TFT_Y+1
	call TFT_drawText	
	
	;Nechci zobrazovat zasahy na protihracove desce, aby
	;se mi vlezly potopene lode
	
	btfsc BOARD_whichBoard, 0
	goto TFT_skipMy
	;Zasahy:		
	call TFT_setGreenFG
	
	movlw HIGH Table_text5
	movwf textPointer+0
	movlw LOW Table_text5
	movwf textPointer+1

	movlw HIGH TEXT_STATS2_X
	movwf TFT_X+0
	movlw LOW TEXT_STATS2_X
	movwf TFT_X+1
	movlw HIGH TEXT_STATS2_Y
	movwf TFT_Y+0
	movlw LOW TEXT_STATS2_Y
	movwf TFT_Y+1
	call TFT_drawText

	call TFT_setWhiteFG	
		
	;Vypis pocet zasahu - "XX/17"
	PAGE1		
	call BOARD_getHitsCount
	call bin2ascii
	PAGE0
	movf asciiChar, w
	call TFT_drawChar
	movf asciiChar+1, w
	call TFT_drawChar
	movlw '/'
	call TFT_drawChar
			
	PAGE1
	movlw GAME_TOTAL_HITS
	call bin2ascii
	PAGE0
	movf asciiChar, w
	call TFT_drawChar
	movf asciiChar+1, w
	call TFT_drawChar		
		
TFT_skipMy	

	;Potopeno:	
	call TFT_setCyanFG
	
	movlw HIGH Table_text6
	movwf textPointer+0
	movlw LOW Table_text6
	movwf textPointer+1

	;Pro desku protihrace posun Potopeno: o radek vyse
	btfsc BOARD_whichBoard, 0
	goto TFT_shift
	movlw HIGH TEXT_STATS4_X
	movwf TFT_X+0
	movlw LOW TEXT_STATS4_X
	movwf TFT_X+1
	movlw HIGH TEXT_STATS4_Y
	movwf TFT_Y+0
	movlw LOW TEXT_STATS4_Y
	movwf TFT_Y+1
	goto TFT_showNext
	
TFT_shift	

	movlw HIGH TEXT_STATS2_X
	movwf TFT_X+0
	movlw LOW TEXT_STATS2_X
	movwf TFT_X+1
	movlw HIGH TEXT_STATS2_Y
	movwf TFT_Y+0
	movlw LOW TEXT_STATS2_Y
	movwf TFT_Y+1
	
TFT_showNext
	
	call TFT_drawText
	
	call TFT_setWhiteFG
	
	;Vypis pocet potopenych lodi "X/5"
	PAGE1
	call BOARD_getSunkenCount
	call bin2ascii
	PAGE0
	movf asciiChar+1, w
	call TFT_drawChar	
	movlw '/'
	call TFT_drawChar
		
	PAGE1
	movlw GAME_TOTAL_SHIPS
	call bin2ascii
	PAGE0
	movf asciiChar+1, w
	call TFT_drawChar
	
	;Vycisti misto po Potopeno: X/5 u protihrace, budou tam potopene lode
	btfss BOARD_whichBoard, 0
	return
	
	;Zobraz potopene lode od nejvetsi, pouze na hracove obrazovce	
	bsf GAME_status, STATUS_SHOWING_COLOUR
	
	clrf TFT_layoutDirection

	movlw .11
	movwf BOARD_x
	movlw .5
	movwf BOARD_y
	
	btfss GAME_sunkenBits, BOARD_SHIP_5
	goto TFT_showSunken4
	movlw BOARD_SHIP_5_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_5_HIT
	movwf TFT_pieceType
	call TFT_drawShip
	incf BOARD_y, f		
	
TFT_showSunken4	

	btfss GAME_sunkenBits, BOARD_SHIP_4
	goto TFT_showSunken3A
	movlw BOARD_SHIP_4_LEN
	movwf BOARD_shipLen	
	movlw BOARD_SHIP_4_HIT
	movwf TFT_pieceType
	call TFT_drawShip
	incf BOARD_y, f
	
TFT_showSunken3A

	btfss GAME_sunkenBits, BOARD_SHIP_3A
	goto TFT_showSunken3B	
	movlw BOARD_SHIP_3A_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_3A_HIT
	movwf TFT_pieceType
	call TFT_drawShip
	incf BOARD_y, f
	
TFT_showSunken3B
	
	btfss GAME_sunkenBits, BOARD_SHIP_3B
	goto TFT_showSunken2
	movlw BOARD_SHIP_3B_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_3B_HIT
	movwf TFT_pieceType
	call TFT_drawShip
	incf BOARD_y, f
	
TFT_showSunken2

	btfss GAME_sunkenBits, BOARD_SHIP_2
	goto TFT_showSunkenDone	
	movlw BOARD_SHIP_2_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_2_HIT
	movwf TFT_pieceType
	call TFT_drawShip		

TFT_showSunkenDone

	bcf GAME_status, STATUS_SHOWING_COLOUR
		
	return
;}

;Umisti sve lode.
TFT_showInfoPlaceShips
;{
	movlw HIGH Table_text1
	movwf textPointer+0
	movlw LOW Table_text1
	movwf textPointer+1

	goto TFT_showInstruction
;}

;Cekame na protihrace
TFT_showInfoWaitingOpponent
;{
	movlw HIGH Table_text2
	movwf textPointer+0
	movlw LOW Table_text2
	movwf textPointer+1
	goto TFT_showInstruction

;}

;Hrajes, poloz znacku
TFT_showInfoIPlay
;{
	movlw HIGH Table_text8
	movwf textPointer+0
	movlw LOW Table_text8
	movwf textPointer+1
	goto TFT_showInstruction
;}

;Hraje souper, cekame
TFT_showInfoHePlays
;{
	movlw HIGH Table_text9
	movwf textPointer+0
	movlw LOW Table_text9
	movwf textPointer+1
	goto TFT_showInstruction
;}


; Souper: Zasah nebo Souper: Voda
TFT_showInfoHitOrMiss
;{	
	btfsc BOARD_piece, BOARD_SHIP_HIT_BIT	
	goto TFT_showInfoOpponentHit
	goto TFT_showInfoOpponentMiss
;}

;Odesilam tah...
TFT_showInfoSendingMove
;{
	movlw HIGH Table_text12
	movwf textPointer+0
	movlw LOW Table_text12
	movwf textPointer+1
	goto TFT_showInstruction
;}

;Odpoved: Zasah
TFT_showInfoResponseHit
;{
	movlw HIGH Table_text13
	movwf textPointer+0
	movlw LOW Table_text13
	movwf textPointer+1
	goto TFT_showInstruction
;}


;Lod potopena!
TFT_showInfoResponseHitNewSunken
;{
	call WT588D_playShipSunken
	
	movlw HIGH Table_text14
	movwf textPointer+0
	movlw LOW Table_text14
	movwf textPointer+1
	goto TFT_showInstruction
;}

;Odpoved: Voda
TFT_showInfoResponseMiss
;{
	movlw HIGH Table_text15
	movwf textPointer+0
	movlw LOW Table_text15
	movwf textPointer+1
	goto TFT_showInstruction
;}

;Spojeni preruseno! Spust hru znovu
TFT_showInfoConnectionLost
;{
	movlw HIGH Table_text16
	movwf textPointer+0
	movlw LOW Table_text16
	movwf textPointer+1
	call TFT_showInstruction
	PAGE1
	call wait1s
	call wait1s
	PAGE0
	bcf LED_PACKET
	movlw HIGH Table_text17
	movwf textPointer+0
	movlw LOW Table_text17
	movwf textPointer+1
	goto TFT_showInstruction	
;}

;Souper: Zasah!
TFT_showInfoOpponentHit
;{
	movlw HIGH Table_text18
	movwf textPointer+0
	movlw LOW Table_text18
	movwf textPointer+1
	goto TFT_showInstruction
;}

;Souper: Voda...
TFT_showInfoOpponentMiss
;{
	movlw HIGH Table_text19
	movwf textPointer+0
	movlw LOW Table_text19
	movwf textPointer+1
	goto TFT_showInstruction
;}

;Konec hry
TFT_showInfoGameOver
;{
	movlw HIGH Table_text10
	movwf textPointer+0
	movlw LOW Table_text10
	movwf textPointer+1
	goto TFT_showInstruction
;}

;Vyhral jsi!
TFT_showInfoIWon
;{
	movlw HIGH Table_text20
	movwf textPointer+0
	movlw LOW Table_text20
	movwf textPointer+1
	goto TFT_showInstruction
;}

;Vyhral souper.
TFT_showInfoOpponentWon
;{
	movlw HIGH Table_text21
	movwf textPointer+0
	movlw LOW Table_text21
	movwf textPointer+1
	goto TFT_showInstruction
;}



TFT_showInfoTestingComms
;{
	bcf TFT_textZoom, 0
	
	movlw HIGH Table_text27
	movwf textPointer+0
	movlw LOW Table_text27
	movwf textPointer+1	
	
	movlw HIGH TEXT_INSTR_X
	movwf TFT_X+0
	movlw LOW TEXT_INSTR_X
	movwf TFT_X+1
	movlw HIGH (TEXT_INSTR_Y - .40)
	movwf TFT_Y+0
	movlw LOW (TEXT_INSTR_Y - .40)
	movwf TFT_Y+1
	call TFT_setCyanOnBlue

	goto TFT_drawText	
;}

TFT_showInfoTestingOK
;{
	
	movlw HIGH Table_text28
	movwf textPointer+0
	movlw LOW Table_text28
	movwf textPointer+1	
	
	goto TFT_drawText
;}

TFT_showInfoTestingError
;{
	
	movlw HIGH Table_text29
	movwf textPointer+0
	movlw LOW Table_text29
	movwf textPointer+1	
	
	goto TFT_drawText
;}


TFT_showInstruction
;{
	bsf TFT_textZoom, 0
	
	movlw HIGH TEXT_INSTR_X
	movwf TFT_X+0
	movlw LOW TEXT_INSTR_X
	movwf TFT_X+1
	movlw HIGH TEXT_INSTR_Y
	movwf TFT_Y+0
	movlw LOW TEXT_INSTR_Y
	movwf TFT_Y+1
	call TFT_setYellowOnBlue

	goto TFT_drawText
;}

TFT_showLogo
;{
	movlw HIGH Table_text22
	movwf textPointer+0
	movlw LOW Table_text22
	movwf textPointer+1
	
	movlw HIGH TEXT_LOGO2_X
	movwf TFT_X+0
	movlw LOW TEXT_LOGO2_X
	movwf TFT_X+1
	movlw HIGH TEXT_LOGO2_Y
	movwf TFT_Y+0
	movlw LOW TEXT_LOGO2_Y
	movwf TFT_Y+1
	call TFT_setYellowOnBlue
	bsf TFT_textZoom, 0
	call TFT_drawText
	
	
	call TFT_showShipLogo
	
	
	movlw HIGH Table_text24
	movwf textPointer+0
	movlw LOW Table_text24
	movwf textPointer+1
	
	movlw HIGH TEXT_LOGO4_X
	movwf TFT_X+0
	movlw LOW TEXT_LOGO4_X
	movwf TFT_X+1
	movlw HIGH TEXT_LOGO4_Y
	movwf TFT_Y+0
	movlw LOW TEXT_LOGO4_Y
	movwf TFT_Y+1
	call TFT_setCyanFG
	bcf TFT_textZoom, 0
	call TFT_drawText		
	
	movlw HIGH Table_text25
	movwf textPointer+0
	movlw LOW Table_text25
	movwf textPointer+1
	
	movlw HIGH TEXT_LOGO5_X
	movwf TFT_X+0
	movlw LOW TEXT_LOGO5_X
	movwf TFT_X+1
	movlw HIGH TEXT_LOGO5_Y
	movwf TFT_Y+0
	movlw LOW TEXT_LOGO5_Y
	movwf TFT_Y+1
	call TFT_setGreenFG
	bcf TFT_textZoom, 0
	goto TFT_drawText		
;}


TFT_showBullet1
;{
	PAGE1
	;X
	movlw HIGH .20
    movwf TFT_X+.0
    movlw LOW .20
    movwf TFT_X+.1
    movlw HIGH (.20 + .32 - .1)
    movwf TFT_X_MAX+.0
    movlw LOW (.20 + .32 - .1)
    movwf TFT_X_MAX+.1    
    ;Y
    movlw HIGH .70
    movwf TFT_Y+.0
    movlw LOW .70
    movwf TFT_Y+.1
    movlw HIGH (.70 + .30 - .1)
    movwf TFT_Y_MAX+0
    movlw LOW (.70 + .30 - .1)
    movwf TFT_Y_MAX+1	
	
    call TFT_setXY2
    
    movlw HIGH Table_gunshot_data
    movwf dataPointer+.0
    movlw LOW Table_gunshot_data
    movwf dataPointer+.1
    movlw Table_gunshot_data_len

    goto TFT_drawBox    


TFT_showBullet2    

	PAGE1
	;X
	movlw HIGH .260
    movwf TFT_X+0
    movlw LOW .260
    movwf TFT_X+1
    movlw HIGH (.260 + .32 - .1)
    movwf TFT_X_MAX+0
    movlw LOW (.260 + .32 - .1)
    movwf TFT_X_MAX+1
    
    ;Y
    movlw HIGH .100
    movwf TFT_Y+0
    movlw LOW .100
    movwf TFT_Y+1
    movlw HIGH (.100 + .30 - .1)
    movwf TFT_Y_MAX+0
    movlw LOW (.100 + .30 - .1)
    movwf TFT_Y_MAX+1	
	
    call TFT_setXY2
    
    movlw HIGH Table_gunshot_data
    movwf dataPointer+0
    movlw LOW Table_gunshot_data
    movwf dataPointer+1
    movlw Table_gunshot_data_len

    goto TFT_drawBox
    
TFT_showBullet3    

	PAGE1
	
	;X
	movlw HIGH .160
    movwf TFT_X+0
    movlw LOW .160
    movwf TFT_X+1
    movlw HIGH (.160 + .32 - .1)
    movwf TFT_X_MAX+0
    movlw LOW (.160 + .32 - .1)
    movwf TFT_X_MAX+1
    
    ;Y
    movlw HIGH .175
    movwf TFT_Y+0
    movlw LOW .175
    movwf TFT_Y+1
    movlw HIGH (.175 + .30 - .1)
    movwf TFT_Y_MAX+0
    movlw LOW (.175 + .30 - .1)
    movwf TFT_Y_MAX+1	
	
    call TFT_setXY2
    
    movlw HIGH Table_gunshot_data
    movwf dataPointer+0
    movlw LOW Table_gunshot_data
    movwf dataPointer+1
    movlw Table_gunshot_data_len
    
    goto TFT_drawBox        
 ;} 

TFT_showBullet4    
;{
	
	call TFT_setRedFG
	call TFT_setBlackBG
	
	PAGE1
	
	;X
	movlw HIGH .153
    movwf TFT_X+0
    movlw LOW .153
    movwf TFT_X+1
    movlw HIGH (.153 + .32 - .1)
    movwf TFT_X_MAX+0
    movlw LOW (.153 + .32 - .1)
    movwf TFT_X_MAX+1
    
    ;Y
    movlw HIGH .72
    movwf TFT_Y+0
    movlw LOW .72
    movwf TFT_Y+1
    movlw HIGH (.72 + .30 - .1)
    movwf TFT_Y_MAX+0
    movlw LOW (.72 + .30 - .1)
    movwf TFT_Y_MAX+1	
	
    call TFT_setXY2
    
    movlw HIGH Table_gunshot_data
    movwf dataPointer+0
    movlw LOW Table_gunshot_data
    movwf dataPointer+1
    movlw Table_gunshot_data_len
    
    goto TFT_drawBox        
 ;} 


TFT_showShipLogo
;{
	
	movlw HIGH TFT_BLACK
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_BLACK
    movwf TFT_pieceFGColor+1
    
	movlw HIGH TFT_BLUE
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_BLUE
    movwf TFT_pieceBGColor+1    
    			
	PAGE1
	;X
	movlw HIGH .64
    movwf TFT_X+.0
    movlw LOW .64
    movwf TFT_X+.1
    movlw HIGH (.64 + .192 - .1)
    movwf TFT_X_MAX+.0
    movlw LOW (.64 + .192 - .1)
    movwf TFT_X_MAX+.1    
    ;Y
    movlw HIGH .40
    movwf TFT_Y+.0
    movlw LOW .40
    movwf TFT_Y+.1
    movlw HIGH (.40 + .64 - .1)
    movwf TFT_Y_MAX+.0
    movlw LOW (.40 + .64 - .1)
    movwf TFT_Y_MAX+.1	
	
    call TFT_setXY2
    
    movlw HIGH Table_logo_data
    movwf dataPointer+.0
    movlw LOW Table_logo_data
    movwf dataPointer+.1
    movlw .0	; 256
    call TFT_drawBox    
    
    movlw HIGH (Table_logo_data + .256)
    movwf dataPointer+.0
    movlw LOW (Table_logo_data + .256)
    movwf dataPointer+.1
    movlw .0	; 256
    call TFT_drawBox     
    
    movlw HIGH (Table_logo_data + .512)
    movwf dataPointer+.0
    movlw LOW (Table_logo_data + .512)
    movwf dataPointer+.1
    movlw .0	; 256
    call TFT_drawBox         
    
    movlw HIGH (Table_logo_data + .768)
    movwf dataPointer+.0
    movlw LOW (Table_logo_data + .768)
    movwf dataPointer+.1
    movlw .0	; 256
    call TFT_drawBox         
    
    movlw HIGH (Table_logo_data + .1024)
    movwf dataPointer+.0
    movlw LOW (Table_logo_data + .1024)
    movwf dataPointer+.1
    movlw .0	; 256
    call TFT_drawBox         
    
    movlw HIGH (Table_logo_data + .1280)
    movwf dataPointer+.0
    movlw LOW (Table_logo_data + .1280)
    movwf dataPointer+.1
    movlw .0	; 256
    call TFT_drawBox         
       
    PAGE0
    
    return
;}

;Vykresli box na obrazovku
;Vstup: dataPointer - ukazatel na data
;Vstup: WREG - delka v bytech (max 255B)
;Vstup: Predpocitane TFT_X, TFT_Y, TFT_X_MAX, TFT_Y_MAX
TFT_drawBoxZoom
;{
	movwf TFT_i

TFT_drawZ_loop

	PAGE1
	call TFT_getNextByte
	PAGE0
    movwf TFT_tmp
    movwf TFT_byte

	movlw .2
	movwf TFT_k

TFT_drawZ

    movf TFT_byte, w
    movwf TFT_tmp

    movlw .8
    movwf TFT_j

TFT_drawZ_loopB

    btfss TFT_tmp, 7        ;nejvyssi bit set?
    goto TFT_ZbitNotSet
	
	PAGE1
    movf TFT_FGColor+0, w
    call TFT_sendData
    movf TFT_FGColor+1, w
    call TFT_sendData
    PAGE0

    btfss TFT_textZoom, 0	;zoomuji i X? - posli pixel 2x
    goto TFT_ZbitCommon		;nezoomuji X

	PAGE1
    movf TFT_FGColor+0, w
    call TFT_sendData
    movf TFT_FGColor+1, w
    call TFT_sendData
    PAGE0

    goto TFT_ZbitCommon

TFT_ZbitNotSet

	PAGE1
    movf TFT_BGColor+0, w
    call TFT_sendData
    movf TFT_BGColor+1, w
    call TFT_sendData
    PAGE0

    btfss TFT_textZoom, 0	;zoomuji X?
    goto TFT_ZbitCommon

	PAGE1
    movf TFT_BGColor+0, w
    call TFT_sendData
    movf TFT_BGColor+1, w
    call TFT_sendData
    PAGE0

TFT_ZbitCommon
    rlf TFT_tmp, f

    decfsz TFT_j, f
    goto TFT_drawZ_loopB

	decfsz TFT_k, f
	goto TFT_drawZ

	decfsz TFT_i, f
    goto TFT_drawZ_loop

    return
;}


;Vykresli text
;Vstup:
; textPointer:2 - zacatek textu
; TFT_X, TFT_Y - pozice kam
TFT_drawChar
;{
		movwf tmp		;uloz pro vypis 1 znaku

		bsf TFT_putChar, 0
		goto TFT_drawTextBegin

TFT_drawText

		bcf TFT_putChar, 0

TFT_drawTextBegin

		;Zvetsime jen Y-ovou (FALSE), nebo obe (TRUE)?
		movlw FONT_CHAR_WIDTH - .1		;8x16 -> 16x32
		btfsc TFT_textZoom, 0
		movlw .2 * FONT_CHAR_WIDTH - .1	;8x16 -> 8x32
		movwf TFT_width

		movlw .2 * FONT_CHAR_HEIGHT - .1	;Zoomujeme - kazdy radek vykreslim 2x
		movwf TFT_height

		movf tmp, w				;pouze 1 znak
		btfsc TFT_putChar, 0
		goto TFT_processChar

TFT_drawTextLoop
		call TFT_getNextChar
		iorlw 0x00
		btfsc STATUS, Z
		return
TFT_processChar
		movwf tmp
		movlw .44				;Zaciname az od ascii code 46
		subwf tmp, w			;x 1

		movwf loByte
		movwf tmp				;x1 uschovej
		clrf hiByte

		;Najdi graficka data pro pismeno (x 16 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

		movlw HIGH Table_font8x16	;8x16
		movwf dataPointer+0
		movlw LOW Table_font8x16
		movwf dataPointer+1		;k dataPointer musime pricit 16x base

		movf loByte, w
		addwf dataPointer+1, f
		btfsc STATUS, C			;soucet >256?
		incf dataPointer+0, f
		movf hiByte, w
		addwf dataPointer+0, f


	;TFT_calculateBoundaries
	
	;TFT_X -> TFT_X_MAX
	movf TFT_X+0, w
	movwf TFT_X_MAX+0
	movf TFT_X+1, w
	movwf TFT_X_MAX+1

	;TFT_Y -> TFT_Y_MAX
	movf TFT_Y+0, w
	movwf TFT_Y_MAX+0
	movf TFT_Y+1, w
	movwf TFT_Y_MAX+1

	movf TFT_width, w
	addwf TFT_X_MAX+1, f
	btfsc STATUS, C
	incf TFT_X_MAX+0, f

	movf TFT_height, w
	addwf TFT_Y_MAX+1, f
	btfsc STATUS, C
	incf TFT_Y_MAX+0, f

	PAGE1
	call TFT_setXY2
	PAGE0

;		call TFT_calculateBoundaries

		movlw FONT_CHAR_HEIGHT
		call TFT_drawBoxZoom

		;Posun se o znak doprava na obrazovce (podle zoomu)
		movlw FONT_CHAR_WIDTH
		btfsc TFT_textZoom, 0
		movlw .2 * FONT_CHAR_WIDTH
		addwf TFT_X+1, f			;vlezeme se do 256?
		btfsc STATUS, C				;soucet >256?
		incf TFT_X+0, f

		btfss TFT_putChar, 0		;0-ne, 1-ano
		goto TFT_drawTextLoop
		return

;}

TFT_getNextChar
;{
	call TFT_GetNextChar2 		;To je patch kvuli strankovani
	PAGE0
	incfsz textPointer+1, f ;Inkrementuj pointer
	return
	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_layDownShips
;{	
	clrf UART_transmitCount	;pocitadlo vracenych Init packetu

	;bcf nize jsem presunul z UART_checkInitPacket sem kvuli tomu,
	;ze kdyz prijde aspon 1 validni Init packet, tak aby pripadne dalsi
	;nahodne packety nezrusily ten STATUS_INIT_RECVD	flag
	bcf GAME_status, STATUS_INIT_RECVD
	
	bsf GAME_status, STATUS_LAYING_DOWN
	
	movlw BOARD_SHIP_5_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_5	
	call TFT_layDownShip
	
	movlw BOARD_SHIP_4_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_4	
	call TFT_layDownShip	
	
	movlw BOARD_SHIP_3A_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_3A
	call TFT_layDownShip
	
	movlw BOARD_SHIP_3B_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_3B
	call TFT_layDownShip
	
	movlw BOARD_SHIP_2_LEN
	movwf BOARD_shipLen
	movlw BOARD_SHIP_2
	call TFT_layDownShip
	
	bcf GAME_status, STATUS_LAYING_DOWN
			
	return
;}

;Poloz znacku, kde chces zkusit zasah
TFT_layDownMark
;{
	bsf GAME_status, STATUS_LAYING_DOWN
	bsf GAME_status, STATUS_LAYING_MARK
			
	movlw .1
	movwf BOARD_shipLen
	movlw BOARD_MISS
	call TFT_layDownShip
	
	bcf GAME_status, STATUS_LAYING_DOWN
	
	bcf GAME_status, STATUS_LAYING_MARK
	
	goto WT588D_playCannon
	
;}
	
;Poloz lod na desku anebo znacku na desku
;Vstup: nastaveny BOARD_shipLen
;Vstup: W-reg - typ lode (5,4,3a,3b,2) anebo Miss
TFT_layDownShip
;{	
	movwf BOARD_piece
	call TFT_copyBoardPiece	
	clrf TFT_layoutDirection
	
	clrf BOARD_x
	clrf BOARD_y
	
	;Pri pokladani nove lode se defaultne umisti na 0,0, takze
	;potrebuju u druhe a dalsich pokladanych lodi zjistit, jestli
	;uz na 0,0 neco nelezi.
	call GAME_setPieceTypeAfterMove
	
TFT_layDownShipLoop

	;Prijimej packety az do polozeni svych lodi, druha strana
	;muze mit problemy a posila packety znovu.
	btfss BOARD_whichBoard, 0		;neprijimej cizi packety, pokud zrovna pokladam znacku ja
	call UART_waitForInitPacket
	
	call GAME_savePosition	;uschovej x,y -> x_old, y_old

	call TFT_drawShip		;na pozici BOARD_x, BOARD_y
		
	call JOY_getReadings	;uprav BOARD_x, BOARD_y podle pohybu

	;Byl pohyb joysticku?	
	call GAME_checkMove		;Zjisti, jestli byl pohyb, nastav GAME_wasMove
	btfss GAME_wasMove, 0	;[x, y] vs [x_old, y_old]
	goto GAME_checkJoySwitch
	
	;Zajisti uklid na predchozi pozici				
	;call GAME_setPieceOldPosition ;nastav, co se bude kreslit		
	call BOARD_saveXY		  ;x -> x_tmp, y -> y_tmp
	call GAME_restorePosition ;x_old, y_old -> x,y	
	call TFT_drawShipOld	  ;na pozici BOARD_x_old, BOARD_y_old
	call BOARD_restoreXY	  ;x_tmp -> x, y_tmp -> y
			
	;A vykreslime lod na nove pozici
	call GAME_setPieceTypeAfterMove	;Zjisti obsazenost/hratelnost noveho mista
	call TFT_drawShip		;a vykresli novou pozici BOARD_x, BOARD_y
	
	call WT588D_playClick
		
GAME_checkJoySwitch

	call JOY_getSwitchReading
	btfss STATUS, Z			;ZERO set - byl stisk u aktualniho hrace
	goto GAME_checkLayoutSwitch
	
	;TFT_pieceType obsahuje aktualni stav pozice (lze hrat, nelze)
	call GAME_canWePlaceOnBoard	;uloz kamen na desku, pokud je OK
	btfsc STATUS, Z			;Da se kamen na pozici odehrat?
	goto GAME_wrongPlace	;Ne, hrac musi najit jine misto
	call BOARD_putShipOnBoard
	call TFT_drawShip
	
	btfsc BOARD_whichBoard, 0
	return
	goto WT588D_playLaidDown	; HOTOVO

GAME_wrongPlace

	call WT588D_playWrongPlace
	
GAME_checkLayoutSwitch		;Prepinac horizontal / vertical pokladani
	
	;Prepinac funguje jen pri pokladani lodi, ne pri pokladani znacky
	btfss GAME_status, STATUS_LAYING_MARK
	call GAME_handleLayoutSwitch	

	PAGE1
	movlw .100
	call waitMilis		
	PAGE0
	
	bcf LED_CORRECT

	goto TFT_layDownShipLoop
;}

;Prepina mezi horizontal / vertical
TFT_toggleLayoutDirection
;{
	movlw .1
	xorwf TFT_layoutDirection, f
	return
;}

;Uloz TFT_pieceType
TFT_savePieceType
;{
	movf TFT_pieceType, w
	movwf TFT_pieceType_save
	return
;}

;Obnov TFT_pieceType
TFT_restorePieceType
;{
	movf TFT_pieceType_save, w
	movwf TFT_pieceType
	return
;}

;V BOARD_piece je to co, je ulozeno na desce (lode, miss, hity),
;v TFT_pieceType pak to, co se bude vykreslovat, tedy i dalsi typy
; (kurzor, krizek).
TFT_copyBoardPiece
;{
	movf BOARD_piece, w
	movwf TFT_pieceType
	return
;}

;Black on Blue
TFT_setBlackOnBlue
;{
	movlw HIGH TFT_BLUE
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_BLUE
    movwf TFT_pieceBGColor+1
    movlw HIGH TFT_BLACK
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_BLACK
    movwf TFT_pieceFGColor+1
    return
;}


;Vykresli moji desku
TFT_drawMyBoard
;{
	clrf BOARD_whichBoard
	goto TFT_drawBoard
;}

;Vykresli desku protihrace
TFT_drawHisBoard
;{
	bsf BOARD_whichBoard, 0
	goto TFT_drawBoard
;}

;Vykresli lode na me desce
TFT_drawBoard
;{
	;reseni odbarveni HITu v piecePrep pro moje zasahy
	bsf GAME_status, STATUS_DRAWING_BOARD
	
	clrf BOARD_x
	clrf BOARD_y
	movlw .1
	movwf BOARD_shipLen
	clrf TFT_layoutDirection
	
	movlw BOARD_SIZE
	movwf i	
TFT_drawBoardRow	
	clrf BOARD_x
	movlw BOARD_SIZE
	movwf j
TFT_drawBoardColumn
	call BOARD_getPlaceValue	;Pro kazde misto na boardu
	movwf TFT_pieceType	
	PAGE1
	call TFT_drawPiece
	PAGE0
	
	incf BOARD_x, f
	decfsz j, f
	goto TFT_drawBoardColumn
	
	incf BOARD_y, f
	
	decfsz i, f
	goto TFT_drawBoardRow
	
	bcf GAME_status, STATUS_DRAWING_BOARD
	
	return	
;}



;Vykresli lod
;Vstup: BOARD_x, BOARD_y, TFT_layoutDirection, BOARD_shipLen
TFT_drawShip
;{	
	movlw FALSE
	movwf TFT_drawingOld
	
TFT_drawShip2

	call BOARD_saveBoardXY

	movf BOARD_shipLen, w
	movwf i
TFT_drShipLoop

	btfss TFT_drawingOld, 0		;Uklizime na predchozi pozici?
	goto TFT_drawShipNotOld
	call BOARD_getPlaceValue	;Pro kazde misto na boardu
	movwf TFT_pieceType	
	
TFT_drawShipNotOld
	PAGE1
	call TFT_drawPiece
	PAGE0
	
	btfsc TFT_layoutDirection, 0	;Horiz 0 / Vertic 1?
	goto TFT_drawIncY

	incf BOARD_x, f
	goto TFT_drawShipCommon
	
TFT_drawIncY
	incf BOARD_y, f	

TFT_drawShipCommon
	decfsz i, f
	goto TFT_drShipLoop
	
	goto BOARD_restoreBoardXY		
;}

;Obnovujeme
TFT_drawShipOld
;{
	;Hack pouze pri zobrazovani MISS znacky pri pokladani u soupere
	bcf GAME_status, STATUS_LAYING_DOWN
	bcf GAME_status, STATUS_SHOWING_COLOUR	;pro zasahy nechceme odkryt barvu
	movlw TRUE
	movwf TFT_drawingOld
	call TFT_drawShip2
	bsf GAME_status, STATUS_LAYING_DOWN
	bsf GAME_status, STATUS_SHOWING_COLOUR
	return
;}

TFT_setGreenBG
;{
	movlw HIGH TFT_GREEN
    movwf TFT_BGColor+0
    movlw LOW TFT_GREEN
    movwf TFT_BGColor+1
    return
;}

TFT_setBlackBG
;{
	movlw HIGH TFT_BLACK
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_BLACK
    movwf TFT_pieceBGColor+1
    return
;}

TFT_setBlueBG
;{
	movlw HIGH TFT_BLUE
    movwf TFT_BGColor+0
    movlw LOW TFT_BLUE
    movwf TFT_BGColor+1
    return
;}

TFT_setWhiteFG
;{
	movlw HIGH TFT_WHITE
    movwf TFT_FGColor+0
    movlw LOW TFT_WHITE
    movwf TFT_FGColor+1
    return
;}

TFT_setCyanFG
;{
	movlw HIGH TFT_CYAN
    movwf TFT_FGColor+0
    movlw LOW TFT_CYAN
    movwf TFT_FGColor+1
    return
;}

TFT_setRedFG
;{
	movlw HIGH TFT_RED
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_RED
    movwf TFT_pieceFGColor+1
    return
;}

TFT_setBlackFG
;{
	movlw HIGH TFT_BLACK
    movwf TFT_FGColor+0
    movlw LOW TFT_BLACK
    movwf TFT_FGColor+1
    return
;}

TFT_setGreenFG
;{
	movlw HIGH TFT_GREEN
    movwf TFT_FGColor+0
    movlw LOW TFT_GREEN
    movwf TFT_FGColor+1
    return
;}

TFT_setYellowFG
;{
	movlw HIGH TFT_YELLOW
    movwf TFT_FGColor+0
    movlw LOW TFT_YELLOW
    movwf TFT_FGColor+1
    return
;}

;Yellow on Blue
TFT_setYellowOnBlue
;{
	call TFT_setBlueBG
	goto TFT_setYellowFG
;}

TFT_setCyanOnBlue
;{
	call TFT_setBlueBG
	goto TFT_setCyanFG
;}


;*******************************************************************
;************************** B O A R D  *****************************
;*******************************************************************

;Vycisti oba boardy
BOARDs_init
;{
	clrf BOARD_whichBoard		;Moje lode
	
	PAGE1
	call BOARD_init
	PAGE0
	
	bsf BOARD_whichBoard, 0
	
	PAGE1
	call BOARD_init				;Moje zasahy 
	PAGE0
	
	clrf BOARD_whichBoard
	
	return
;}


;Docasne uloz BOARD_x a BOARD_y. Nemuzeme pouzit BOARD_x_old nebo BOARD_x_tmp...
BOARD_saveBoardXY
;{
	movf BOARD_x, w
	movwf x
	movf BOARD_y, w
	movwf y	
	return
;}

;Obnov BOARD_x a BOARD_y. Nemuzeme pouzit BOARD_x_old nebo BOARD_x_tmp...
BOARD_restoreBoardXY
;{
	movf x, w
	movwf BOARD_x
	movf y, w
	movwf BOARD_y
	
	return
;}


;Ulozi BOARD_x a BOARD_y do BOARD_x_tmp a BOARD_y_tmp
BOARD_saveXY
;{
	movf  BOARD_x, w
	movwf BOARD_x_tmp
	movf  BOARD_y, w
	movwf BOARD_y_tmp
	return
;}


;Obnovi BOARD_x a BOARD_y
BOARD_restoreXY
;{
	movf  BOARD_x_tmp, w
	movwf BOARD_x
	movf  BOARD_y_tmp, w
	movwf BOARD_y
	return
;}


;Vraci hodnotu  board[BOARD_tmp]
;Vstup: BOARD_x, BOARD_y
;Vystup: WREG hodnota v poli
BOARD_getPlaceValue
;{
	PAGE1
	call BOARD_setAndGetAddress	
	PAGE0
	
	;BOARD_tmp lichy?
	movf INDF, w
	btfss BOARD_tmp, 0
	swapf INDF, w		;vymen nibbly
	andlw b'00001111'	;Pouze dolni 4 bity			
		
	bcf STATUS, IRP
	return
;}

;Uloz kamen na pozici x,y
;Vstup: BOARD_x, BOARD_y
;Vstup: BOARD_piece
BOARD_setPlaceValue
;{
	PAGE1
	call BOARD_setAndGetAddress
	PAGE0
	
	;Spocitej do ktereho pulbyte
	movf BOARD_piece, w	
	btfss BOARD_tmp, 0		;Pujdem na horni / dolni nibble?
	swapf BOARD_piece, w	;Sudy = horni byte
	iorwf INDF, f			;A prioruj ke zbytku
		
	bcf STATUS, IRP
	return
;}



;Uloz na board lod aktualni delky a dane orientaci pokladani
;Vstup: BOARD_x, BOARD_y
;Vstup: BOARD_shipLen
;Vstup: TFT_layoutDirection
BOARD_putShipOnBoard
;{
	;Neukladej znacku na board, jen lode
	btfsc GAME_status, STATUS_LAYING_MARK
	return
	
	call BOARD_saveBoardXY
	
	movf BOARD_shipLen, w
	movwf i
	
BOARD_putShipLoop

	call BOARD_setPlaceValue

	btfss TFT_layoutDirection, 0	;kterym smerem pokracujeme?
	goto BOARD_putShipIncX
	incf BOARD_y, f	
	goto BOARD_putShipCommon
	
BOARD_putShipIncX
	incf BOARD_x, f

BOARD_putShipCommon
	decfsz i, f
	goto BOARD_putShipLoop
		
	goto BOARD_restoreBoardXY
;}



; Delay = 0.000104 seconds
; Clock frequency = 8 MHz

; Actual delay = 0.000104 seconds = 208 cycles
; Error = 0 %
Delay_104us
;{
			;202 cycles
	movlw	0x43
	movwf	del1
Delay_104us_0
	decfsz	del1, f
	goto	Delay_104us_0

			;2 cycles
	goto	$+1

			;4 cycles (including call)
	return
;}	

 

	 
			org 0x900
			
;Tile 16x16px - lod	 
Table_ship_data
;{
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
	dt 0xFF, 0xFF
Table_ship_data_end
Table_ship_data_len	equ Table_ship_data_end - Table_ship_data
;}	

Table_miss_data
;{
	dt 	0x00, 0x00 ;                 
	dt 	0x00, 0x00 ;                 
	dt 	0x00, 0x00 ;                 
	dt 	0x00, 0x00 ;                 
	dt 	0x03, 0xC0 ;       ####      
	dt 	0x07, 0xE0 ;      ######     
	dt 	0x06, 0x60 ;      ##  ##     
	dt 	0x06, 0x60 ;      ##  ##     
	dt 	0x07, 0xE0 ;      ######     
	dt 	0x03, 0xC0 ;       ####      
	dt 	0x03, 0xC0 ;       ####      
	dt 	0x01, 0x80 ;        ##       
	dt 	0x01, 0x80 ;        ##       
	dt 	0x00, 0x00 ;                 
	dt 	0x00, 0x00 ;                 
	dt 	0x00, 0x00 ;
Table_miss_data_end
Table_miss_data_len	equ Table_miss_data_end - Table_miss_data	
;}
	 
Table_hit_data	
;{ 
	dt 	0x00, 0x00;                 
	dt 	0x40, 0x02;  #            # 
	dt 	0x20, 0x04;   #          #  
	dt 	0x10, 0x08;    #        #   
	dt 	0x08, 0x10;     #      #    
	dt 	0x04, 0x20;      #    #     
	dt 	0x02, 0x40;       #  #      
	dt 	0x01, 0x80;        ##       
	dt 	0x01, 0x80;        ##       
	dt 	0x02, 0x40;       #  #      
	dt 	0x04, 0x20;      #    #     
	dt 	0x08, 0x10;     #      #    
	dt 	0x10, 0x08;    #        #   
	dt 	0x20, 0x04;   #          #  
	dt 	0x40, 0x02;  #            # 
	dt 	0x00, 0x00;                 	 
Table_hit_data_end
Table_hit_data_len	equ Table_hit_data_end - Table_hit_data	 
;}	 
	 
	 		org 0x0A00
	
BOARD_init
;{

	clrf BOARD_x
	clrf BOARD_y
	
	movlw (BOARD_SIZE * BOARD_SIZE) / .2
	movwf BOARD_i
	
	bsf STATUS, IRP
	movlw LOW hisBoard
	btfss BOARD_whichBoard, 0
	movlw LOW myBoard

	movwf FSR
		
	movlw BOARD_EMPTY	
	
B_clear
	movwf INDF
	incf FSR, f
	decfsz BOARD_i, f
	goto B_clear

	bcf STATUS, IRP	
	return
;}

	 		
; *** SUBRUTINA pro prevod 8bit binary to 2 chars hex ********************************	
	
bin2hex	
;{
		movwf	asciiTmp
		CLRF	asciiChar	;Clear output registers
		CLRF	asciiChar+1
		SWAPF	asciiTmp,W	;Get upper nibble
		ANDLW	0x0F		;Mask off unwanted bits
		MOVWF	asciiChar	;Save
		MOVLW 	.10			;For BCD lower nibble only (lower 4 bits)
		SUBWF 	asciiChar,W
		MOVLW 	0x30		;If numerical add ascii value (-10)
		BTFSC 	STATUS,C
		MOVLW 	0x37		;If alphabetical, then add a different ascii value (-10)
		ADDWF 	asciiChar,1	;save in output reg
		MOVF	asciiTmp,W	;get lower nibble of input byte
		ANDLW	0x0F		;mask off unwanted data
		MOVWF	asciiChar+1
		MOVLW 	.10		
		SUBWF 	asciiChar+1,W
		MOVLW 	0x30
		BTFSC 	STATUS,C
		MOVLW 	0x37
		ADDWF 	asciiChar+1,1	;and save in output reg
		retlw   .0
;}
	 	 		

; *** SUBRUTINA pro prevod 6bit binary to BCD ********************************

bin2ascii
;{
	clrf asciiChar
	addlw .256 - .100
digit100
	addlw .20
	btfss STATUS,C
	goto  digit40
	bsf   asciiChar,3
	addlw .256 - .20
	goto digit10
digit40
	addlw .40
	btfss STATUS,C
	goto  $+3
	bsf   asciiChar,2
	addlw .256 - .40
	addlw .20
	btfss STATUS,C
	goto  $+3
	bsf   asciiChar,1
	addlw .256 - .20
digit10
	addlw .10
	btfss STATUS,C
	goto  $+3
	bsf   asciiChar,0
	addlw .256 - .10

	addlw .10
	movwf asciiChar+1
	
	;Udelej z nich ascii cislice
	movlw '0'
	addwf asciiChar, f
	addwf asciiChar+1, f
	return
;}

;Vraci pocet vsech hitu pro dany board. Projdu 25 Bytes
;a v kazdem nibblu kontroluju 3. bit.
;Vstup: BOARD_whichBoard
;Vystup: W-reg
BOARD_getHitsCount
;{
	clrf tmp
	clrf j			;ukladani INDF, 2 pulbytes
	
	bsf STATUS, IRP
	
	movlw LOW myBoard
	btfsc BOARD_whichBoard, 0
	movlw LOW hisBoard
	movwf FSR
	
	movlw ((BOARD_SIZE * BOARD_SIZE) / 2)
	movwf i
BOARD_getHitsLoop

	btfsc INDF, 3 		;0x08 - nastaveny hit bit (dolni pulbyte)
	incf tmp, f
	
	swapf INDF, w		;nechci prijit o hodnotu na BOARDu
	movwf j		
	btfsc j, 3			;horni pulbyte v j
	incf tmp, f
	
	incf FSR, f
	decfsz i, f
	goto BOARD_getHitsLoop
		
	bcf STATUS, IRP
	
	movf tmp, w
	
	return
	
;}

;Spocita na danem boardu pocet potopenych lodi. Pro kazdy typ lode
;najdu pocet jejich hitu. Pokud je pocet == delka lode, prictu +1 k
;celkovemu souctu.
;Vstup: BOARD_whichBoard
;Vystup W-reg 
BOARD_getSunkenCount
;{
	clrf GAME_sunkenBits		;bit 5, 4, 3, 2, 1 nastaven, pokud
	                            ;je prislusna lod potopena.
			
	clrf BOARD_i				;ukladany pocet
	
	movlw BOARD_SHIP_5_HIT
	call BOARD_getCountByValue	;V tmp je porad pocet
	movlw .5
	subwf tmp, w				
	btfss STATUS, Z				;pocet hitu = delka lode 5?
	goto BOARD_checkSunk4
	incf BOARD_i, f				;ano, lod potopena
	bsf GAME_sunkenBits, BOARD_SHIP_5
		
BOARD_checkSunk4
					
	movlw BOARD_SHIP_4_HIT
	call BOARD_getCountByValue	
	movlw .4
	subwf tmp, w				
	btfss STATUS, Z
	goto BOARD_checkSunk3A
	incf BOARD_i, f
	bsf GAME_sunkenBits, BOARD_SHIP_4

BOARD_checkSunk3A
	
	movlw BOARD_SHIP_3A_HIT
	call BOARD_getCountByValue	
	movlw .3
	subwf tmp, w				
	btfss STATUS, Z
	goto BOARD_checkSunk3B
	incf BOARD_i, f	
	bsf GAME_sunkenBits, BOARD_SHIP_3A

BOARD_checkSunk3B
	
	movlw BOARD_SHIP_3B_HIT
	call BOARD_getCountByValue	
	movlw .3
	subwf tmp, w				
	btfss STATUS, Z
	goto BOARD_checkSunk2
	incf BOARD_i, f		
	bsf GAME_sunkenBits, BOARD_SHIP_3B
	
BOARD_checkSunk2
	
	movlw BOARD_SHIP_2_HIT
	call BOARD_getCountByValue	
	movlw BOARD_SHIP_2_HIT
	call BOARD_getCountByValue	
	movlw .2
	subwf tmp, w				
	btfss STATUS, Z
	goto BOARD_checkSunkFinish
	incf BOARD_i, f		
	bsf GAME_sunkenBits, BOARD_SHIP_2

BOARD_checkSunkFinish
	
	movf BOARD_i, w
	
	return	
;}

;Vstup W-REG - hledany typ lode
;Vystup W-REG - pocet nalezenych na boardu
BOARD_getCountByValue
;{
	movwf BOARD_tmp	;hledana hodnota
	
	clrf tmp
	
	bsf STATUS, IRP
	
	movlw LOW myBoard
	btfsc BOARD_whichBoard, 0
	movlw LOW hisBoard
	movwf FSR
	
	movlw ((BOARD_SIZE * BOARD_SIZE) / 2)
	movwf i
BOARD_getCountValueLoop

	;INDF obsahuje 2 pulbytes, prohledam oba
	movf INDF, w			
	andlw b'00001111'
	subwf BOARD_tmp, w
	btfsc STATUS, Z
	incf tmp, f
	
	swapf INDF, w
	andlw b'00001111'
	subwf BOARD_tmp, w
	btfsc STATUS, Z
	incf tmp, f
	
	incf FSR, f
	decfsz i, f
	goto BOARD_getCountValueLoop
		
	bcf STATUS, IRP
	
	movf tmp, w
			
	return
	
;}

;Vstup: W-REG
;Vystup: 18*W-REG + 1, ulozeno do hiByte, lowByte	
TFT_calculate18
;{	
	movwf loByte
	
	clrf hiByte
		
	;TILE_SIZE = 18, nasobime 18 - pozor, muzeme pretect 256!!!
	;x18 = x8 + x8 + x2
	bcf STATUS, C
	rlf loByte, f	;x2
	movf loByte, w
	movwf tmp		
	rlf loByte, f	;x4
	rlf loByte, f 	;x8	
	movf loByte, w
	movwf TFT_byte
	addwf loByte, f ;x16
	btfsc STATUS, C
	incf hiByte, f
	movf tmp, w		; + x2
	addlw TILE_PADDING
	addwf loByte, f
	btfsc STATUS, C
	incf hiByte, f
	return
;}	

;Podle typu kamene nastav barvy
;Vstup: TFT_piecePrep
TFT_drawPiecePrep
;{
	;Pokud zobrazuju zasahy, tak neukazuj barvy lodi pri zasahu
	btfss BOARD_whichBoard, 0
	goto TFT_drawPiecePrepNext

	;Cele potopene lode ukazeme barevne	
	btfss TFT_pieceType, BOARD_SHIP_HIT_BIT
	goto TFT_drawPiecePrepNextCheck

	movf TFT_pieceType, w
	andlw b'00000111'	;0-7
	addlw .1
	movwf k
	clrf tmp
	bsf STATUS, C	
TFT_drawPieceSunken
	rlf tmp, f
	decfsz k, f
	goto TFT_drawPieceSunken
	movf tmp, w
	andwf GAME_sunkenBits, w
	btfss STATUS, Z
	goto TFT_drawPiecePrepNext	;Potopena lod, zobraz barevne.

TFT_drawPiecePrepNextCheck
	
	;Zobrazuju potopene lode v showStats, ty chceme barevne
	btfsc GAME_status, STATUS_DRAWING_BOARD
	goto TFT_drawPieceUncolour				;zasahy odbarvime
	btfsc GAME_status, STATUS_SHOWING_COLOUR
	goto TFT_drawPiecePrepNext				;potopene lode barvime

TFT_drawPieceUncolour	
	movlw BOARD_SHIP_HIT
	btfsc TFT_pieceType, BOARD_SHIP_HIT_BIT		; je to hit lode?
	movwf TFT_pieceType		; nastav na BOARD_SHIP_HIT
	
TFT_drawPiecePrepNext
	
	movf TFT_pieceType, w
	movwf tmp
	
	;Defaultni modre pozadi
	movlw HIGH TFT_BLUE
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_BLUE
    movwf TFT_pieceBGColor+1
    		
	;Defaultne kreslime lod
	movlw HIGH Table_ship_data
	movwf dataPointer+0
	movlw LOW Table_ship_data
	movwf dataPointer+1	
	
  
    ;0 - Empty
    movf tmp, f
    btfss STATUS, Z
    goto Ship_2

	movlw HIGH TFT_BLUE		   ;prazdny
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_BLUE
    movwf TFT_pieceFGColor+1        
    return
    
    
Ship_2
    
    ;1 - Lod delky 2
    decfsz tmp, f
    goto Ship_3B
    
   	movlw HIGH TFT_YELLOW
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_YELLOW
    movwf TFT_pieceFGColor+1    
    return
    
Ship_3B
	
	;2 - Lod delky 3 (B)
	decfsz tmp, f
	goto Ship_3A
	
	movlw HIGH TFT_ORANGE
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_ORANGE
    movwf TFT_pieceFGColor+1    
    return
    
	
Ship_3A

	;3 - Lod delky 3 (A)
	decfsz tmp, f
	goto Ship_4
	
	movlw HIGH TFT_CYAN
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_CYAN
    movwf TFT_pieceFGColor+1    
    return


Ship_4

	;4 - Lod delky 4
	decfsz tmp, f
	goto Ship_5
	
	movlw HIGH TFT_VIOLET
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_VIOLET
    movwf TFT_pieceFGColor+1    
    return
    
Ship_5

	;5 - Lod delky 5
	decfsz tmp, f
	goto Piece_Cross
	
	movlw HIGH TFT_GREEN
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_GREEN
    movwf TFT_pieceFGColor+1    
    return    

Piece_Cross
	
	;6 - Obsazene misto 
	decfsz tmp, f
	goto Piece_Miss
	
	movlw HIGH TFT_RED
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_RED
    movwf TFT_pieceFGColor+1    		
	return
	
Piece_Miss

	;7 - Miss 
	decfsz tmp, f
	goto Piece_Hit
			
	movlw HIGH Table_miss_data
	movwf dataPointer+0
	movlw LOW Table_miss_data
	movwf dataPointer+1		
	
	movlw HIGH TFT_WHITE
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_WHITE
    movwf TFT_pieceFGColor+1    
    
    ;Pokud pokladam znacku, tak pokladana znacka bude mit cerne pozadi,
    ;at vidim, kde je zrovna pozice.
    btfss GAME_status, STATUS_LAYING_DOWN
    return
    
    movlw HIGH TFT_BLACK
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_BLACK
    movwf TFT_pieceBGColor+1    
    return
	
Piece_Hit

	;8 - Hit (bez barvy lode)
	decfsz tmp, f
	goto Ship_2_hit

	movlw HIGH TFT_WHITE
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_WHITE
    movwf TFT_pieceFGColor+1 
	
	movlw HIGH TFT_RED
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_RED
    movwf TFT_pieceBGColor+1
    
	movlw HIGH Table_hit_data
	movwf dataPointer+0
	movlw LOW Table_hit_data
	movwf dataPointer+1	     
	return	

Ship_2_hit

	;9 - Hit Lod 2
	decfsz tmp, f
	goto Ship_3B_hit
		
	movlw HIGH TFT_YELLOW
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_YELLOW
    movwf TFT_pieceBGColor+1	
	
    goto TFT_drawPiecePrep_hit

Ship_3B_hit

	;10 - Hit Lod 3B
	decfsz tmp, f
	goto Ship_3A_hit
	
	movlw HIGH TFT_ORANGE
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_ORANGE
    movwf TFT_pieceBGColor+1
    	
	goto TFT_drawPiecePrep_hit

Ship_3A_hit

	;11 - Hit Lod 3A
	decfsz tmp, f
	goto Ship_4_hit
	
	movlw HIGH TFT_CYAN
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_CYAN
    movwf TFT_pieceBGColor+1
    	
	goto TFT_drawPiecePrep_hit

Ship_4_hit

	;12 - Hit Lod 4
	decfsz tmp, f
	goto Ship_5_hit
	
	movlw HIGH TFT_VIOLET
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_VIOLET
    movwf TFT_pieceBGColor+1
    	
	goto TFT_drawPiecePrep_hit

Ship_5_hit

	;13 - Hit Lod 5
	movlw HIGH TFT_GREEN
    movwf TFT_pieceBGColor+0
    movlw LOW TFT_GREEN
    movwf TFT_pieceBGColor+1
    	
	goto TFT_drawPiecePrep_hit

TFT_drawPiecePrep_hit

	movlw HIGH Table_hit_data
	movwf dataPointer+0
	movlw LOW Table_hit_data
	movwf dataPointer+1			
	
	movlw HIGH TFT_BLACK
    movwf TFT_pieceFGColor+0
    movlw LOW TFT_BLACK
    movwf TFT_pieceFGColor+1    
	
	return
;}
	 		 
	 		 
;Vstup: BOARD_x, BOARD_y
;Vystup: BOARD_tmp = BOARD_y * 10 + BOARD_x
;Vystup: FSR nastaveny na BOARD_tmp / 2
;Pote vezmu byte INDF a vezmu bud horni pulbyte (BOARD_tmp sudy)
;Anebo dolni pro BOARD_tmp lichy)
BOARD_setAndGetAddress
;{
	movf BOARD_y, w
	movwf BOARD_tmp
	bcf STATUS, C		;Vynasob 10
	rlf BOARD_tmp, f	;x2
	movf BOARD_tmp, w
	movwf tmp
	rlf BOARD_tmp, f	;x4
	rlf BOARD_tmp, f	;x8
	movf tmp, w			
	addwf BOARD_tmp, f  ;x10
	movf BOARD_x, w
	addwf BOARD_tmp, f	;Y * 10 + X
	bsf STATUS, IRP		;Jsme ve druhe a treti bance
	movlw LOW hisBoard
	btfss BOARD_whichBoard, 0
	movlw LOW myBoard
	movwf FSR
	bcf STATUS, C
	rrf BOARD_tmp, w	;BOARD_tmp / 2 + [board]
	addwf FSR, f
	return
;}	 		 
	 	
;Nastavi JOY_increment podle JOY_ADresult 
;JOY_increment = 0		  AD result <64, 191>
;JOY_increment = 255 (-1) AD result <0, 63>
;JOY_increment = 1		  AD result <192, 255>
;Vystup: uprav BOARD_x, BOARD_y podle pohybu joysticku

JOY_handleMoveX
;{
	clrf JOY_increment		;Default - zadna zmena
	movlw .256 - .64		;192
	addwf JOY_ADresult, w	;1) <0,63>
	btfsc STATUS, C
	goto JOY_notZeroX
	incf JOY_increment, f	;Bylo doleva, 255 (-1)
	goto JOY_adjust_X
JOY_notZeroX	
	movlw .256 - .192		;64
	addwf JOY_ADresult, w	;2) <64,191>		
	btfss STATUS, C
	return					;3) else > 64 && < 191 tj. zadna zmena X
	decf JOY_increment, f	
JOY_adjust_X
	goto JOY_checkRollOver	
;}

JOY_handleMoveY
;{
	clrf JOY_increment		;Default - zadna zmena
	movlw .256 - .64		;192
	addwf JOY_ADresult, w	;1) <0,63>
	btfsc STATUS, C
	goto JOY_notZeroY
	decf JOY_increment, f	;Bylo doleva, 255 (-1)
	goto JOY_adjust_Y
JOY_notZeroY	
	movlw .256 - .192		;64
	addwf JOY_ADresult, w	;2) <64,191>		
	btfss STATUS, C
	return					;3) else > 64 && < 191 tj. zadna zmena X
	incf JOY_increment, f	
JOY_adjust_Y
	goto JOY_checkRollOver	

;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
	
JOY_checkBoundaries	;Zkontroluj jestli jsme <0,BOARD_SIZE-BOARD_shipLen>
	
	btfsc TFT_layoutDirection, 0	;horiz / vertical?
	goto JOY_checkVertical
		
JOY_checkHorizontal
	movf BOARD_shipLen, w			; MAX(x) = BOARD_SIZE-BOARD_shipLen - 1
	sublw BOARD_SIZE + .1			; x = 11?
	btfss JOY_whichAdjust, 0		;BOARD_y
	goto JOY_checkBoundaryX
	movlw BOARD_SIZE				;v horizontalnim smeru je MAX(y) = BOARD_SIZE-1
	xorwf BOARD_y, w
	goto JOY_10done
	
JOY_checkVertical
	movlw BOARD_SIZE				; MAX(x) = BOARD_SIZE - 1	
	btfss JOY_whichAdjust, 0		;BOARD_y
	goto JOY_checkBoundaryX
	movf BOARD_shipLen, w
	sublw BOARD_SIZE + .1			; y = 11?	
	xorwf BOARD_y, w
	goto JOY_10done
		
JOY_checkBoundaryX
	xorwf BOARD_x, w
JOY_10done	
	btfss STATUS, Z
	goto check_255
	btfss JOY_whichAdjust, 0		;snizime BOARD_x nebo BOARD_y?		
	goto JOY_checkDecX
	decf BOARD_y, f
	return
JOY_checkDecX
	decf BOARD_x, f	
	return
	
check_255							;levy (x) a horni(x) roh
	movlw .255
	btfss JOY_whichAdjust, 0		;BOARD_y
	goto JOY_checkBoundaryX2
	xorwf BOARD_y, w
	goto JOY_255done
JOY_checkBoundaryX2
	xorwf BOARD_x, w
JOY_255done
	btfss STATUS, Z
	return							;Nic nemenime
	btfss JOY_whichAdjust, 0		;zvysime BOARD_x nebo BOARD_y?		
	goto JOY_checkIncX
	incf BOARD_y, f
	return
JOY_checkIncX
	incf BOARD_x, f	
	return
;}	 	
	 	
	 	
WT588D_doSoundFX
;{
		movwf wt588d_tmp
					
		movlw .22
		call waitMilis
	
		;ORIGINAL CODE
		;bsf WT588D_SCK
		;nop
		;bsf WT588D_CS
		;nop	
		
		;movlw .17
		;call waitMilis

		bcf	WT588D_CS		;Slave Select = LOW (Initiating transmission)
		
		
		movlw .5			;WAS 2 ORIGINALLY
		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 wait40us

		bsf WT588D_SCK		;Clock na log. 1, skoncili jsme prenos 1 bitu	
		call wait40us

		rrf wt588d_tmp, f
		decfsz wt588d_i, f
		goto cycle
		
		;CS i SCK na high
		bsf WT588D_SCK		;Slave Select = HI
		
		movlw .20
		call waitMilis
		
		bsf WT588D_CS		;Pro WT588D

		return
;}
	 
wait30us
;{			
			nop
			movlw .18
			movwf del1
wait30l		decfsz del1, f
			goto wait30l																			

			return;			ta je za 2 cykly
;}

wait40us
;{
			;76 cycles
	movlw	0x19
	movwf	del1
wait40us_0
	decfsz	del1, f
	goto	wait40us_0

			;4 cycles (including call)
	return
;}
	 		 
; Delay = 0.001 seconds
; Clock frequency = 8 MHz
wait1ms
;{
			;1993 cycles
	movlw	0x8E
	movwf	del1
	movlw	0x02
	movwf	del2
wait1ms_0
	decfsz	del1, f
	goto	$+2
	decfsz	del2, 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
;}
	 	
;********************************************************************
;************************ S E T U P *********************************
;********************************************************************
SETUP
;{
	BANK1
	movlw b'11001000'
	movwf OPTION_REG

	;***********************
	;*** PORTA NASTAVENI ***
	;***********************
	
	banksel ADCON1
	movlw b'00000100' 	;RA0-RA1 Analog
	movwf ADCON1
	
	;Set RA0 - joy X to analog
	;Set RA1 - joy Y
	
	banksel ADCON0
	movlw b'11000000' ;ADC Frc clock ;AN0, ADC OFF

	BANK1	
	movlw b'00000011' ;RA0, RA1 = JOY
	movwf TRISA
		
	movlw UART_4800BPS		;baud rate 4800bps
	movwf SPBRG				;8MHz
		 
	movlw b'00100100'
	movwf TXSTA		;8 bit transmission, transmit enabled, async mode, high speed baud rate	
	
	BANK0
	clrf PORTA
	
	movlw b'10000000'	;Set up RC7 and RC6 for serial port 
	movwf RCSTA	

	;***********************
	;*** PORTB NASTAVENI ***
	;***********************

	BANK0	
	clrf CCP1CON 		;Turn CCP module off
	clrf PORTB   	
	
	BANK1
	movlw b'01000011'	; RB0 (Joy switch) a RB1 (layout switch), RB6-RX SWUART
	movwf TRISB
		
	;***********************
	;*** PORTC NASTAVENI ***
	;***********************
	
	BANK0
	clrf PORTC
	clrf INTCON			;Vypni interrupty

	;TFT 2.8" SPI	
	clrf SSPCON  		; Inicializace MSSP modulu
	movlw b'00110000'	; 00, SSPEN=1,CKP=1, 0001 SPI Master Mode = FOSC/4
	movwf SSPCON	
		
	BANK1
	movlw b'10000000'	;UART RX as input
	movwf TRISC 
		
	clrf SSPSTAT
  	movlw 0x80			; set up spi port, SPI master,
	movwf SSPSTAT		; CKE = 0 (mode 1,1)
	
	BANK0
	clrf T2CON			; Timer2 nepouzivame
	
	;WT588D				;Nastav SCK a CS na log. 1
	bsf WT588D_SCK		;Pro WT588D
	bsf WT588D_CS	
	return
;}			 		
	 		

TFT_init
;{
		bsf TFT_RESET
		
		movlw .200
		call waitMilis
		bcf TFT_RESET
		movlw .200
		call waitMilis		
		bsf TFT_RESET
		movlw .200
		call waitMilis		
				
		;Soft RESET command
		movlw 0x01		
		call TFT_sendCmd
		movlw .1
		call waitSecs
			
		;power control A
		movlw 0xCB
		call TFT_sendCmd
		movlw 0x39
		call TFT_sendData
		movlw 0x2C
		call TFT_sendData
		movlw 0x00
		call TFT_sendData
		movlw 0x34
		call TFT_sendData
		movlw 0x02
		call TFT_sendData
	
	
		;power control B
		movlw 0xCF
		call TFT_sendCmd
		movlw 0x00
		call TFT_sendData
		movlw 0xC1
		call TFT_sendData
		movlw 0x30
		call TFT_sendData
	
		;driver timing control A
		movlw 0xE8
		call TFT_sendCmd
		movlw 0x85
		call TFT_sendData
		movlw 0x00
		call TFT_sendData
		movlw 0x78
		call TFT_sendData
	
		;driver timing control B
		movlw 0xEA
		call TFT_sendCmd
		movlw 0x00
		call TFT_sendData
		movlw 0x00
		call TFT_sendData
	
		;power on sequence control
		movlw 0xED
		call TFT_sendCmd
		movlw 0x64
		call TFT_sendData
		movlw 0x03
		call TFT_sendData
		movlw 0x12
		call TFT_sendData
		movlw 0x81
		call TFT_sendData
	
		;pump ratio control
		movlw 0xF7
		call TFT_sendCmd
		movlw 0x20
		call TFT_sendData
	
		;power control,VRH[5:0]
		movlw 0xC0
		call TFT_sendCmd
		movlw 0x23
		call TFT_sendData
	
		;Power control,SAP[2:0];BT[3:0]
		movlw 0xC1
		call TFT_sendCmd
		movlw 0x10
		call TFT_sendData
	
		;vcm control
		movlw 0xC5
		call TFT_sendCmd
		movlw 0x3E
		call TFT_sendData
		movlw 0x28
		call TFT_sendData
	
		;vcm control 2
		movlw 0xC7
		call TFT_sendCmd
		movlw 0x86
		call TFT_sendData
	
		;memory access control
		movlw 0x36
		call TFT_sendCmd
		;movlw 0x48  ORIG
		movlw 0x28 			;W=320, H=240
		call TFT_sendData
	
		;pixel format
		movlw 0x3A
		call TFT_sendCmd
		movlw 0x55
		call TFT_sendData
	
		;frameration control,normal mode full colours
		movlw 0xB1
		call TFT_sendCmd
		movlw 0x00
		call TFT_sendData
		movlw 0x18
		call TFT_sendData
	
		;display function control
		movlw 0xB6
		call TFT_sendCmd
		movlw 0x08
		call TFT_sendData
		movlw 0x82
		call TFT_sendData
		movlw 0x27
		call TFT_sendData
	
		;3gamma function disable
		movlw 0xF2
		call TFT_sendCmd
		movlw 0x00
		call TFT_sendData
	
		;gamma curve selected
		movlw 0x26
		call TFT_sendCmd
		movlw 0x01
		call TFT_sendData
	
		;set positive gamma correction
		movlw 0xE0
		call TFT_sendCmd
		movlw 0x0F
		call TFT_sendData
		movlw 0x31
		call TFT_sendData
		movlw 0x2B
		call TFT_sendData
		movlw 0x0C
		call TFT_sendData
		movlw 0x0E
		call TFT_sendData
		movlw 0x08
		call TFT_sendData
		movlw 0x4E
		call TFT_sendData
		movlw 0xF1
		call TFT_sendData
		movlw 0x37
		call TFT_sendData
		movlw 0x07
		call TFT_sendData
		movlw 0x10
		call TFT_sendData
		movlw 0x03
		call TFT_sendData
		movlw 0x0E
		call TFT_sendData
		movlw 0x09
		call TFT_sendData
		movlw 0x00
		call TFT_sendData
	
		;set negative gamma correction
		movlw 0xE1
		call TFT_sendCmd
		movlw 0x00
		call TFT_sendData
		movlw 0x0E
		call TFT_sendData
		movlw 0x14
		call TFT_sendData
		movlw 0x03
		call TFT_sendData
		movlw 0x11
		call TFT_sendData
		movlw 0x07
		call TFT_sendData
		movlw 0x31
		call TFT_sendData
		movlw 0xC1
		call TFT_sendData
		movlw 0x48
		call TFT_sendData
		movlw 0x08
		call TFT_sendData
		movlw 0x0F
		call TFT_sendData
		movlw 0x0C
		call TFT_sendData
		movlw 0x31
		call TFT_sendData
		movlw 0x36
		call TFT_sendData
		movlw 0x0F
		call TFT_sendData
	
		;exit sleep
		movlw 0x11
		call TFT_sendCmd
		
		movlw .120
		call waitMilis
			
		;display on
		movlw 0x29
		call TFT_sendCmd
		
		return
;}	 	

;Nastavi v displeji okno pro vykresleni kamene, podle BOARD_x a BOARD_y
TFT_setXY
;{
	clrf TFT_X+0
	clrf TFT_X+1
	clrf TFT_X_MAX+0
	clrf TFT_X_MAX+1
	clrf TFT_Y+0
	clrf TFT_Y+1
	clrf TFT_Y_MAX+0
	clrf TFT_Y_MAX+1	
	
	;X
	movf BOARD_x, w
	call TFT_calculate18
			
	movf hiByte, w
	addwf TFT_X, f
	addwf TFT_X_MAX, f
	movf loByte, w			
	addwf TFT_X+1, f
	addwf TFT_X_MAX+1, f
			
	movlw PIECE_SIZE - .1
	addwf TFT_X_MAX+1, f		;+velikost kamene
	btfsc STATUS, C
	incf TFT_X_MAX, f			;>256
	
	;Y	
	movf BOARD_y, w
	call TFT_calculate18
	
	movf hiByte, w
	addwf TFT_Y, f
	addwf TFT_Y_MAX, f
	movf loByte, w			
	addwf TFT_Y+1, f
	addwf TFT_Y_MAX+1, f
			
	movlw PIECE_SIZE - .1
	addwf TFT_Y_MAX+1, f		;+velikost kamene
	btfsc STATUS, C
	incf TFT_Y_MAX, f			;>256		
		
TFT_setXY2						;Pro skore
	;Column Set
	
	movlw 0x2A
	call TFT_sendCmd
	movf TFT_X+0, w
	call TFT_sendData
	movf TFT_X+1, w
	call TFT_sendData
	movf TFT_X_MAX+0, w
	call TFT_sendData
	movf TFT_X_MAX+1, w
	call TFT_sendData

	;Page Set
	movlw 0x2B
	call TFT_sendCmd
	movf TFT_Y+0, w
	call TFT_sendData
	movf TFT_Y+1, w
	call TFT_sendData
	movf TFT_Y_MAX+0, w
	call TFT_sendData
	movf TFT_Y_MAX+1, w
	call TFT_sendData
	
	;memory write
	movlw 0x2C
	goto TFT_sendCmd

;}
	
	 		
TFT_sendCmd
;{
	bcf TFT_CS
	
	bcf TFT_DC		;Command
	
	goto TFT_writeByte
	
;}
		
TFT_sendData
;{
	bcf TFT_CS

	bsf TFT_DC		;Data
	
	goto TFT_writeByte
	
;}

TFT_writeByte				;HW SPI
;{
	movwf SSPBUF			
	banksel SSPSTAT
TFT_wait
    btfss SSPSTAT, BF		; Has data been transmitted?        
	goto TFT_wait			; Loop if not transmitted yet
	banksel SSPBUF
    movf SSPBUF, w			; Empty the receive buffer
    
    bsf TFT_CS
    
	return
;}	 		 

TFT_drawBoardGrid
;{
	;1) Vycisti plochu zelene
	;2) Horizontalni linie
	;3) Vertikalni linie

	call TFT_drawHorizontalLines
	call TFT_drawVerticalLines
	
	return
;}

TFT_drawHorizontalLines
;{
	movlw TILE_SIZE
TFT_drawHorizontalLines2	
	movwf TFT_increment
	
	movlw BOARD_SIZE+1
	movwf TFT_i					;9 linek
	
	clrf TFT_j					;Y-osa

TFT_drawHorizontalLinesR

	;Column Set
	movlw 0x2A
	call TFT_sendCmd
	movlw .0
	call TFT_sendData
	movlw .0
	call TFT_sendData
	movlw HIGH BOARD_SIZE * TILE_SIZE	;Jen ctverec
	call TFT_sendData
	movlw LOW BOARD_SIZE * TILE_SIZE
	call TFT_sendData

	;Page Set
	movlw 0x2B
	call TFT_sendCmd
	movlw .0
	call TFT_sendData
	movf TFT_j, w
	call TFT_sendData
	movlw .0
	call TFT_sendData
	movf TFT_j, w
	addlw .1
	call TFT_sendData

	;memory write
	movlw 0x2C
	call TFT_sendCmd
	
	movlw BOARD_SIZE * TILE_SIZE
	movwf TFT_k

TFT_drawHorizontalLinesC	


	movlw HIGH TFT_WHITE
	call TFT_sendData
	movlw LOW TFT_WHITE
	call TFT_sendData

	
	decfsz TFT_k, f
	goto TFT_drawHorizontalLinesC

	movf TFT_increment, w			;Dalsi linka
	addwf TFT_j, f
	
	decfsz TFT_i, f
	goto TFT_drawHorizontalLinesR
	
	return
;}



TFT_drawVerticalLines
;{
	movlw TILE_SIZE
TFT_drawVerticalLines2			;custom TILE_SIZE
	movwf TFT_increment
	
	movlw BOARD_SIZE+1
	movwf TFT_i					;9 linek
	
	clrf TFT_j					;X-osa

TFT_drawVerticalLinesR

	;Column Set
	movlw 0x2A
	call TFT_sendCmd
	movlw .0
	call TFT_sendData
	movf TFT_j, w
	call TFT_sendData
	movlw .0
	call TFT_sendData
	movf TFT_j, w
	call TFT_sendData

	;Page Set
	movlw 0x2B
	call TFT_sendCmd
	movlw .0
	call TFT_sendData
	movlw .0
	call TFT_sendData
	movlw HIGH BOARD_SIZE * TILE_SIZE
	call TFT_sendData
	movlw LOW BOARD_SIZE * TILE_SIZE
	call TFT_sendData
	
	;memory write
	movlw 0x2C
	call TFT_sendCmd
	
	movlw BOARD_SIZE * TILE_SIZE
	movwf TFT_k

TFT_drawVerticalLinesC	

	movlw HIGH TFT_WHITE
	call TFT_sendData
	movlw LOW TFT_WHITE
	call TFT_sendData
	
	decfsz TFT_k, f
	goto TFT_drawVerticalLinesC

	movf TFT_increment, w			;Dalsi linka
	addwf TFT_j, f
	
	decfsz TFT_i, f
	goto TFT_drawVerticalLinesR
	
	return
;}

TFT_clearScreen
;{
	
	;Column Set
	movlw 0x2A
	call TFT_sendCmd
	movlw .0
	call TFT_sendData
	movlw .0
	call TFT_sendData
	movlw HIGH TFT_WIDTH
	call TFT_sendData
	movlw LOW TFT_WIDTH
	call TFT_sendData

	;Page Set
	movlw 0x2B
	call TFT_sendCmd
	movlw .0
	call TFT_sendData
	movlw .0
	call TFT_sendData
	movlw .0
	call TFT_sendData
	movlw TFT_HEIGHT
	call TFT_sendData
	
	;memory write
	movlw 0x2C
	call TFT_sendCmd
	
	bcf TFT_CS		;Zacatek write operace
	nop
	bsf TFT_DC		;Data
	nop
		
	movlw HIGH TFT_WIDTH	
	movwf hiByte
	movlw LOW TFT_WIDTH	
	movwf loByte
TFT_fillR					; Rows
	movlw TFT_HEIGHT
	movwf TFT_i	
TFT_fillC					; Columns
	movlw HIGH TFT_BLUE
	movwf SSPBUF
	banksel SSPSTAT
TFT_fillScreenH
    btfss SSPSTAT, BF		; Has data been transmitted?
	goto TFT_fillScreenH	; Loop if not transmitted yet
	banksel SSPBUF
    movf SSPBUF, w			; Empty the receive buffer

	movlw LOW TFT_BLUE
	movwf SSPBUF
	banksel SSPSTAT
TFT_fillScreenL
    btfss SSPSTAT, BF		; Has data been transmitted?
	goto TFT_fillScreenL	; Loop if not transmitted yet
	banksel SSPBUF
    movf SSPBUF, w			; Empty the receive buffer
    
    decfsz TFT_i, f
    goto TFT_fillC			
    
    call decHL
    btfss STATUS, Z
    goto TFT_fillR
	
	bsf TFT_CS				; Konec write operace			
	return
;}	

TFT_clearBox
;{
	;Column Set
	movlw 0x2A
	call TFT_sendCmd
	movlw .0
	call TFT_sendData
	movlw .181
	call TFT_sendData
	movlw HIGH .300
	call TFT_sendData
	movlw LOW .300
	call TFT_sendData

	;Page Set
	movlw 0x2B
	call TFT_sendCmd
	movlw .0
	call TFT_sendData
	movlw .91
	call TFT_sendData
	movlw .0
	call TFT_sendData
	movlw .180
	call TFT_sendData
	
	;memory write
	movlw 0x2C
	call TFT_sendCmd
	
	bcf TFT_CS		;Zacatek write operace
	nop
	nop
	bsf TFT_DC		;Data
	nop
	nop
		
	movlw HIGH .119
	movwf hiByte
	movlw LOW .119
	movwf loByte
TFT_boxR					; Rows
	movlw .89
	movwf TFT_i	
TFT_boxC					; Columns
	movlw HIGH TFT_BLUE
	movwf SSPBUF
	banksel SSPSTAT
TFT_boxScreenH
    btfss SSPSTAT, BF		; Has data been transmitted?
	goto TFT_boxScreenH	; Loop if not transmitted yet
	banksel SSPBUF
    movf SSPBUF, w			; Empty the receive buffer

	movlw LOW TFT_BLUE
	movwf SSPBUF
	banksel SSPSTAT
TFT_boxScreenL
    btfss SSPSTAT, BF		; Has data been transmitted?
	goto TFT_boxScreenL	; Loop if not transmitted yet
	banksel SSPBUF
    movf SSPBUF, w			; Empty the receive buffer
    
    decfsz TFT_i, f
    goto TFT_boxC			
    
    call decHL
    btfss STATUS, Z
    goto TFT_boxR
	
	bsf TFT_CS				; Konec write operace			
	return
;}

;Vykresli kamen na pozici [BOARD_x, BOARD_y] v barve TFT_pieceType
;Vstup: BOARD_x, BOARD_y
TFT_drawPiece
;{
	call TFT_drawPiecePrep 	;Nastav barvy a gfx data pro dany 

    call TFT_setXY				;Spocitej a nastav pozici

TFT_drawPiece2					;Vykresli 
    movlw Table_ship_data_len	;
TFT_drawBox						;Vykreslovani obecnych obdelniku
    movwf TFT_i

TFT_draw_loop

	call TFT_getNextByte
    movwf tmp

    movlw .8
    movwf TFT_j

TFT_draw_loopB
    
    btfss tmp, 7        ;nejvyssi bit set?
    goto TFT_bitNotSet
    movf TFT_pieceFGColor+0, w
    call TFT_sendData
    movf TFT_pieceFGColor+1, w
    call TFT_sendData
    goto TFT_bitCommon

TFT_bitNotSet
    movf TFT_pieceBGColor+0, w
    call TFT_sendData
    movf TFT_pieceBGColor+1, w
    call TFT_sendData

TFT_bitCommon            
    rlf tmp, f

    decfsz TFT_j, f
    goto TFT_draw_loopB
    
    incf FSR, f
    
    decfsz TFT_i, f
    goto TFT_draw_loop
    
    return
;}

TFT_getNextByte
;{
	call TFT_getNextByte2 	;To je patch kvuli strankovani
	PAGE1
	bcf PCLATH, 4
	incfsz dataPointer+1, f ;Inkrementuj pointer
	return
	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		
;}

; Delay = 1 seconds
; Clock frequency = 8 MHz
wait1s
;{
			;1999996 cycles
	movlw	0x11
	movwf	del1
	movlw	0x5D
	movwf	del2
	movlw	0x05
	movwf	del3
wait1s_0
	decfsz	del1, f
	goto	$+2
	decfsz	del2, f
	goto	$+2
	decfsz	del3, f
	goto	wait1s_0

			;4 cycles (including call)
	return
;}

waitSecs
;{
		movwf numSecs
waitS	call wait1s
		decfsz numSecs, f
		goto waitS
		return
;}

;*** SUBRUTINY pro 16bitovy counter ********************************	
; *** Nastavi Zero, pokud jsme na nule
;{
decHL	movf loByte, f
		btfsc STATUS, Z ;je loByte na nule?
		goto decHL2
		decf loByte, w	;kopie pro nasledny OR (test na 0)
		decf loByte, f
		iorwf hiByte, w ;Z flag nastaven pri 0 (loByte OR hiByte)
		return
decHL2	decf loByte, f
		decf hiByte, f
		bcf STATUS, Z	;Potrebuju vynulovat Zero (aby mi pri lo/hi 0/1 predchozi decf Z nenastavil)
		return
;}
	 		org 0x0F00
	 		
Table_text1
	dt "ROZM[STI;SV\\;LOD^.;;", 0x00
Table_text2
	dt "_EKaME;NA;PROTIHRa_E", 0x00
Table_text3
	dt "@@;M\\;LOD^;@@", 0x00
Table_text4
	dt "@@;M\\;ZaSAHY;@@", 0x00
Table_text5
	dt "ZaSAHY:;", 0x00
Table_text6
	dt "POTOPENO:;", 0x00	
Table_text7
	dt "00;/;17", 0x00
Table_text8
	dt "HRAJE`,VYBER;POZICI.", 0x00
Table_text9
	dt "HRAJE;SOUPEb,;_EKaM.", 0x00
Table_text10
	dt ";;;;;KONEC;HRY.;;;;;", 0x00		 
Table_text11
	dt ";;;;;;;;;;;;;;;;;;;;", 0x00
Table_text12
	dt "ODES[LaM;TAH;...;;;;", 0x00
Table_text13
	dt "ODPOV^]:;ZaSAH?;;;;;", 0x00	
Table_text14
	dt "LO];POTOPENA?;;;;;;;", 0x00
	
;91 - [ - 
;92 - \ - 
;93 - ] - 
;94 - ^ - 
;95 - _ - 
;96 - ` - 
;97 - a - 
;98 - b - 
;99 - c - 	
	
		org 0x1000
Table_text15
	dt "ODPOV^]:;VODA;;;;;;;", 0x00	
Table_text16
	dt "SPOJEN[;NEFUNGUJE?;;", 0x00
Table_text17
	dt "SPUScTE;HRU;ZNOVU.;;", 0x00
Table_text18
	dt "SOUPEb:;ZaSAH?;;;;;;", 0x00
Table_text19
	dt "SOUPEb:;VODA;;;;;;;;", 0x00	
Table_text20
	dt ";;;;VYHRaL;JSI?;;;;;", 0x00
Table_text21
	dt ";;VYHRaL;SOUPEb.;;;;", 0x00
Table_text22
	dt "@;HRA;LOD^;@", 0x00
Table_text24
	dt ";;ELEKTRONICKa;HRA;;", 0x00
Table_text25
	dt "POWERED;BY;PIC16F76;", 0x00
Table_text26
	dt "STISKNI;JOYSTICK", 0x00
Table_text27
	dt "TEST;SPOJEN[...", 0x00
Table_text28
	dt "OK", 0x00
Table_text29
	dt "CHYBA", 0x00
	 
	 		org 0x1100
	 
Table_font8x16

;{
	 dt 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x18,0x30,0x30; // Code for char num 44
	 dt 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00; // Code for char num 45
	 dt 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x18; // Code for char num 46
	 dt 0x03,0x03,0x06,0x06,0x0C,0x0C,0x18,0x18,0x30,0x30,0x60,0x60,0xC0,0xC0,0x80,0x80; // Code for char num 47
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x6E,0x6E,0x7E,0x7E,0x76,0x76,0x66,0x66,0x3C,0x3C; // Code for char num 48
	 dt 0x00,0x00,0x18,0x18,0x38,0x38,0x78,0x78,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18; // Code for char num 49
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x06,0x06,0x0C,0x0C,0x18,0x18,0x30,0x30,0x7E,0x7E; // Code for char num 50
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x06,0x06,0x1C,0x1C,0x06,0x06,0x66,0x66,0x3C,0x3C; // Code for char num 51
	 dt 0x00,0x00,0x1C,0x1C,0x3C,0x3C,0x6C,0x6C,0xCC,0xCC,0xFE,0xFE,0x0C,0x0C,0x0C,0x0C; // Code for char num 52
	 dt 0x00,0x00,0x7E,0x7E,0x60,0x60,0x7C,0x7C,0x06,0x06,0x06,0x06,0x66,0x66,0x3C,0x3C; // Code for char num 53
	 dt 0x00,0x00,0x1C,0x1C,0x30,0x30,0x60,0x60,0x7C,0x7C,0x66,0x66,0x66,0x66,0x3C,0x3C; // Code for char num 54
	 dt 0x00,0x00,0x7E,0x7E,0x06,0x06,0x06,0x06,0x0C,0x0C,0x18,0x18,0x18,0x18,0x18,0x18; // Code for char num 55
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x66,0x66,0x3C,0x3C,0x66,0x66,0x66,0x66,0x3C,0x3C; // Code for char num 56
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x66,0x66,0x3E,0x3E,0x06,0x06,0x0C,0x0C,0x38,0x38; // Code for char num 57
	 dt 0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x18; // Code for char num 58
	 dt 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00; // Code for char num 59  ' misto ';'	
	 dt 0x00,0x00,0x03,0x03,0x06,0x06,0x0C,0x0C,0x18,0x18,0x0C,0x0C,0x06,0x06,0x03,0x03; // Code for char num 60
	 dt 0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00; // Code for char num 61
	 dt 0x00,0x00,0xC0,0xC0,0x60,0x60,0x30,0x30,0x18,0x18,0x30,0x30,0x60,0x60,0xC0,0xC0; // Code for char num 62
	 dt 0x00,0x00,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x18,0x18; // Code for char num 63  '!' misto '?'
	 dt 0x00,0x00,0x00,0x66,0x66,0x3C,0x3C,0xFF,0xFF,0x3C,0x3C,0x66,0x66,0x00,0x00,0x00; // Code for char num 64 '*' misto '@'
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x66,0x66,0x7E,0x7E,0x66,0x66,0x66,0x66,0x66,0x66; // Code for char num 65
	 dt 0x00,0x00,0x7C,0x7C,0x66,0x66,0x66,0x66,0x7C,0x7C,0x66,0x66,0x66,0x66,0x7C,0x7C; // Code for char num 66
	 dt 0x00,0x00,0x1E,0x1E,0x30,0x30,0x60,0x60,0x60,0x60,0x60,0x60,0x30,0x30,0x1E,0x1E; // Code for char num 67
	 dt 0x00,0x00,0x78,0x78,0x6C,0x6C,0x66,0x66,0x66,0x66,0x66,0x66,0x6C,0x6C,0x78,0x78; // Code for char num 68
	 dt 0x00,0x00,0x7E,0x7E,0x60,0x60,0x60,0x60,0x78,0x78,0x60,0x60,0x60,0x60,0x7E,0x7E; // Code for char num 69
	 dt 0x00,0x00,0x7E,0x7E,0x60,0x60,0x60,0x60,0x78,0x78,0x60,0x60,0x60,0x60,0x60,0x60; // Code for char num 70
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x60,0x60,0x6E,0x6E,0x66,0x66,0x66,0x66,0x3E,0x3E; // Code for char num 71
	 dt 0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x66,0x7E,0x7E,0x66,0x66,0x66,0x66,0x66,0x66; // Code for char num 72
	 dt 0x00,0x00,0x3C,0x3C,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x3C; // Code for char num 73
	 dt 0x00,0x00,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x06,0x66,0x66,0x3C,0x3C; // Code for char num 74
	 dt 0x00,0x00,0xC6,0xC6,0xCC,0xCC,0xD8,0xD8,0xF0,0xF0,0xD8,0xD8,0xCC,0xCC,0xC6,0xC6; // Code for char num 75
	 dt 0x00,0x00,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x60,0x7E,0x7E; // Code for char num 76
	 dt 0x00,0x00,0xC6,0xC6,0xEE,0xEE,0xFE,0xFE,0xD6,0xD6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6; // Code for char num 77
	 dt 0x00,0x00,0xC6,0xC6,0xE6,0xE6,0xF6,0xF6,0xDE,0xDE,0xCE,0xCE,0xC6,0xC6,0xC6,0xC6; // Code for char num 78
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x3C,0x3C; // Code for char num 79
	 dt 0x00,0x00,0x7C,0x7C,0x66,0x66,0x66,0x66,0x7C,0x7C,0x60,0x60,0x60,0x60,0x60,0x60; // Code for char num 80
	 dt 0x00,0x00,0x78,0x78,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xDC,0xDC,0x7E,0x7E; // Code for char num 81
	 dt 0x00,0x00,0x7C,0x7C,0x66,0x66,0x66,0x66,0x7C,0x7C,0x6C,0x6C,0x66,0x66,0x66,0x66; // Code for char num 82
	 dt 0x00,0x00,0x3C,0x3C,0x66,0x66,0x70,0x70,0x3C,0x3C,0x0E,0x0E,0x66,0x66,0x3C,0x3C; // Code for char num 83
	 dt 0x00,0x00,0x7E,0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18; // Code for char num 84
	 dt 0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x3C,0x3C; // Code for char num 85
	 dt 0x00,0x00,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x66,0x3C,0x3C,0x3C,0x3C,0x18,0x18; // Code for char num 86
	 dt 0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xD6,0xD6,0xFE,0xFE,0xEE,0xEE,0xC6,0xC6; // Code for char num 87
	 dt 0x00,0x00,0xC3,0xC3,0x66,0x66,0x3C,0x3C,0x18,0x18,0x3C,0x3C,0x66,0x66,0xC3,0xC3; // Code for char num 88
	 dt 0x00,0x00,0xC3,0xC3,0x66,0x66,0x3C,0x3C,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18; // Code for char num 89
	 dt 0x00,0x00,0xFE,0xFE,0x0C,0x0C,0x18,0x18,0x30,0x30,0x60,0x60,0xC0,0xC0,0xFE,0xFE; // Code for char num 90
	 dt 0x0C,0x18,0x00,0x3C,0x3C,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x3C; // Code for char num 91
	 dt 0x0C,0x18,0x00,0x7E,0x7E,0x60,0x60,0x60,0x78,0x78,0x60,0x60,0x60,0x60,0x7E,0x7E; // Code for char num 92
	 dt 0x28,0x10,0x78,0x78,0x6C,0x6C,0x66,0x66,0x66,0x66,0x66,0x66,0x6C,0x6C,0x78,0x78; // Code for char num 93
	 dt 0x24,0x18,0x7E,0x7E,0x60,0x60,0x60,0x60,0x78,0x78,0x60,0x60,0x60,0x60,0x7E,0x7E; // Code for char num 94
	 dt 0x0A,0x04,0x1E,0x1E,0x30,0x30,0x60,0x60,0x60,0x60,0x60,0x60,0x30,0x30,0x1E,0x1E; // Code for char num 95
	 dt 0x14,0x08,0x3C,0x3C,0x66,0x66,0x70,0x70,0x3C,0x3C,0x0E,0x0E,0x66,0x66,0x3C,0x3C; // Code for char num 96
	 dt 0x04,0x08,0x3C,0x3C,0x66,0x66,0x66,0x66,0x7E,0x7E,0x66,0x66,0x66,0x66,0x66,0x66; // Code for char num 97
	 dt 0x14,0x08,0x7C,0x7C,0x66,0x66,0x66,0x66,0x7C,0x7C,0x6C,0x6C,0x66,0x66,0x66,0x66; // Code for char num 98
	 dt 0x14,0x08,0x7E,0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18; // Code for char num 99
	 
	 
	 	org 0x1500
	 	
Table_gunshot_data
;{
	 dt 0x00, 0x03, 0x0C, 0x00 ;               ##    ##          
	 dt 0x00, 0x03, 0x0C, 0x00 ;               ##    ##          
	 dt 0x00, 0x03, 0x9E, 0x00 ;               ###  ####         
	 dt 0x0C, 0x0E, 0xFE, 0x00 ;     ##      ### #######         
	 dt 0x07, 0xF8, 0x22, 0x00 ;      ########     #   #         
	 dt 0x03, 0x63, 0xC7, 0x00 ;       ## ##   ####   ###        
	 dt 0x01, 0x87, 0xF3, 0x80 ;        ##    #######  ###       
	 dt 0x01, 0x9F, 0xF8, 0x80 ;        ##  ##########   #       
	 dt 0x01, 0xBF, 0xFC, 0xC0 ;        ## ############  ##      
	 dt 0x07, 0x3F, 0xFE, 0x40 ;      ###  #############  #      
	 dt 0x02, 0x7F, 0xFE, 0x40 ;       #  ##############  #      
	 dt 0x02, 0x7F, 0xFE, 0x40 ;       #  ##############  #      
	 dt 0x02, 0x7F, 0xFE, 0x40 ;       #  ##############  #      
	 dt 0x0C, 0x7F, 0xFE, 0x78 ;     ##   ##############  ####   
	 dt 0x38, 0x3F, 0xFC, 0x7E ;   ###     ############   ###### 
	 dt 0x3E, 0x3F, 0xFC, 0x40 ;   #####   ############   #      
	 dt 0x03, 0x1F, 0xFC, 0xE0 ;       ##   ###########  ###     
	 dt 0x01, 0x9F, 0xF9, 0x80 ;        ##  ##########  ##       
	 dt 0x01, 0x8F, 0xF3, 0x00 ;        ##   ########  ##        
	 dt 0x01, 0x23, 0xE3, 0x00 ;        #  #   #####   ##        
	 dt 0x03, 0xF8, 0x1F, 0x00 ;       #######      #####        
	 dt 0x07, 0x8C, 0x17, 0x80 ;      ####   ##     # ####       
	 dt 0x00, 0x07, 0xD3, 0x80 ;              ##### #  ###       
	 dt 0x00, 0x07, 0xF1, 0x80 ;              #######   ##       
	 dt 0x00, 0x06, 0x70, 0x00 ;              ##  ###            
	 dt 0x00, 0x00, 0x30, 0x00 ;                   ##            
	 dt 0x00, 0x00, 0x20, 0x00 ;                   #             
Table_gunshot_data_end
Table_gunshot_data_len	equ Table_gunshot_data_end - Table_gunshot_data
;}	 
		
		org 0x1600
		
Table_logo_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;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x32, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x31, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0xA2, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x2A, 0x00, 0x80, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x32, 0xFE, 0x60, 0x80, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEC, 0x7D, 0xF0, 0x80, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x22, 0x39, 0xF0, 0x80, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xFD, 0x19, 0xF0, 0x80, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xA5, 0x28, 0xE2, 0x80, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x22, 0x09, 0xFA, 0xB8, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x30, 0x89, 0xF2, 0xB8, 0x00
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x09, 0xF2, 0xBB, 0x80
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x19, 0xF2, 0xBB, 0x80
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1D, 0x22, 0x7F, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x7F, 0xFF, 0xFF, 0xFF, 0xFC
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x7F, 0xBF, 0xFF, 0xFF, 0xFC
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF0, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xF0, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x3F, 0xFF, 0xF0, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x01, 0xF0, 0x3F, 0xFF, 0xF0, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x7F, 0xF8, 0x3F, 0xFF, 0xF8, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x03, 0xFC, 0x7F, 0xFF, 0xF8, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x03, 0xFE, 0x7F, 0xFF, 0xF8, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E;
	 dt 0x00, 0x00, 0x00, 0x00, 0x03, 0xFE, 0x7F, 0xFF, 0xF8, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFC;
	 dt 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xF8;
	 dt 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xE0;
	 dt 0x00, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0;
	 dt 0x00, 0x00, 0x0C, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80;
	 dt 0x00, 0x00, 0x3E, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00;
	 dt 0x00, 0x07, 0xFF, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00;
	 dt 0x00, 0x00, 0x7F, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00;
	 dt 0x00, 0x00, 0x7F, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
	 dt 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00;
	 dt 0x00, 0x7D, 0xFF, 0xF3, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00;
	 dt 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0x00;
	 dt 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x00;
	 dt 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00;
	 dt 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00;
	 dt 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00;
	 dt 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00;
	 dt 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00;
	 dt 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0x00, 0x00;
	 dt 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x00;
	 dt 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x00;
	 dt 0x00, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0x00, 0x00, 0x00;
	 dt 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00, 0x00;
	 dt 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	 dt 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x00;
	 dt 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	 dt 0x00, 0x00, 0x7F, 0xFF, 0xF0, 0x00, 0x00, 0x00;
	 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;


Table_logo_data_end
Table_logo_data_len	equ Table_logo_data_end - Table_logo_data	
	 
; *** KONEC SOUBORU *************************************************	
	end