*************************************
*				*
*	   "Y" - MODUL D		*
*				*
*   OVLADACE - DEMOVERZE!		*
*				*
*************************************

*** ZPG - SPOTREBA: 5
XZD	EPZ	248

***DEMO
CURPOS	EPZ	247

*** OBYC - SPOTREBA: 46
XOD	EQU	$B800

*** PROGRAM - DELKA: $426
XYD	EQU	$A800

*** PRENOSY A DEFINICE
XPMG	EQU	$B000
*XYE	EQU	$AF00

*************************************
	ORG	XYD,$A800

	JMP	DEMO

*** VEKTORY

	JMP	OSETFLG
	JMP	OINIT
	JMP	OVBI
	DFW	OIRQ

*** ZPG

OMFLG	EPZ	XZD
OMOLDX	EPZ	XZD+1
OMOLDY	EPZ	XZD+2
OXPOS	EPZ	XZD+3
OYPOS	EPZ	XZD+4

*** OBYC

OXC	EQU	XOD
OYC	EQU	XOD+1

OFLAGS	EQU	XOD+2

OKDEF	EQU	XOD+3	;5
OLKX	EQU	XOD+8
OLKY	EQU	XOD+9
OLTL	EQU	XOD+10

OKEN	EQU	XOD+11
OLEN	EQU	XOD+12
OMEN	EQU	XOD+13
OJEN	EQU	XOD+14

OKCNT	EQU	XOD+15
OLFLG	EQU	XOD+16
OPORT	EQU	XOD+17
OFIRE	EQU	XOD+18
OCP	EQU	XOD+19
OFBUF	EQU	XOD+20	;3
OPCNT	EQU	XOD+23
OLBUFX	EQU	XOD+24	;8
OLBUFY	EQU	XOD+32	;8
OXOLD	EQU	XOD+40
OYOLD	EQU	XOD+41
OCCNT	EQU	XOD+42
OJBLK	EQU	XOD+43

OBLOK	EQU	XOD+44
OFLSH	EQU	XOD+45

*** PMG

OPMG	EQU	XPMG+768

*** DOPOCET PRENOSU

*GOISET1	EQU	XYE
*GOISET2	EQU	XYE+2

*************************************
*	     OVLADACE		*
*************************************

*** INIT

OINIT	LDX	#$FF
	STX	OBLOK

	INX
	TXA
OI1	STA	OPMG,X
	DEX
	BNE	OI1

	STA	OXC
	STA	OYC

	STA	OMOLDX
	STA	OMOLDY
	STA	OCCNT

	LDA	#$E8
	STA	O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
	LDA	#0
OVE4	STA	OPMG,X
	INX
	DEY
	BPL	OVE4
*
	LDX	OYPOS
	STX	OYOLD
	LDY	#6
OVE5	LDA	OVET,Y
	STA	OPMG,X
	INX
	DEY
	BPL	OVE5
***
	LDA	#0
	STA	OBLOK
	RTS
***

OV5S	JSR	OV5SA
	INC	OV5SA+1
	BNE	OV5SA
	INC	OV5SA+2

OV5SA	STA	$FFFF
	RTS
***

* ASL,LSR
OV5T	DFB 10,74

* SIPKA
OVET	DFB 19,23,62,60,120,124,252

****IRQ PRO MYS

OITAB	DFB 0,1,255,0
	DFB 255,0,0,1
	DFB 1,0,0,255
	DFB 0,255,1,0
***

OIRQ	PHA

DEMOIRQ	LDA	#0	;IRQ RESET
	STA	$D20E
	LDA	#4
	STA	$D20E

	LDA	OMFLG
	BEQ	OIRQX

	TXA
	PHA
*X
	LDA	$D300
OISET	LSR
	LSR
	PHA

	AND	#12
	ORA	OMOLDX
	TAX
	LSR
	LSR
	STA	OMOLDX

	LDA	OITAB,X
	CLC
	ADC	OXPOS
	STA	OXPOS
*Y
	PLA
	LSR
	LSR

	AND	#12
	ORA	OMOLDY
	TAX
	LSR
	LSR
	STA	OMOLDY

	LDA	OITAB,X
	CLC
	ADC	OYPOS
	STA	OYPOS
*
	PLA
	TAX
OIRQX	PLA
	RTI

*************************************

CLPMG	LDX	#0
	TXA
INIP2	STA	XPMG+$400,X
	DEX
	BNE	INIP2
	RTS
***

INIPMG	JSR	CLPMG

	LDA	#62
	STA	$22F
	STA	$D400
	LDA	#3
	STA	53277
	LDA	#XPMG:H
	STA	$D407
	LDA	#17
	STA	623
	STA	$D01B

	LDA	#$FF
	STA	711
	STA	$D019
	LDA	#$BF
	STA	704
	STA	$D012

	RTS
***

PRINT	LDY	#239

P2	LDA	TEXT,Y
	STA	(88),Y
	DEY
	CPY	#$FF
	BNE	P2

	RTS
***

TEXT	ASC "QRRRRRRRR#onfigRRRRR"
	ASC "RRREQRRRRR,0%.RRRRRE"

TXTL	ASC %|KBD* LPEN* MOUSE* J%
	ASC %OY*||X+** Y+** TL=*|%

	ASC "ZRRRRRRRRRRRRRRRRRRR"
	ASC "RRRCZRRRRRRRRRRRRRRC"

	ASC % <Z> Vlevo   <X> Vpr%
	ASC %avo   <C>,<V> Zmena %

	ASC % Driver ovladacu - D%
	ASC %emoverze - BEWESOFT %

	ASC %                    %
	ASC %                    %
***

INVT1	DFB 3,4,5,3,2,2,3
INVT2	DFB 1,6,12,19,25,30,35

***

DEMOVBI	CLI
	JSR	OVBI
	JMP	$E462

***

INVERT	LDX	#39

INV2	LDA	TXTL,X
	AND	#127
	STA	TXTL,X
	DEX
	BPL	INV2
*
	LDX	CURPOS
	LDY	INVT1,X
	LDA	INVT2,X
	TAX

INV3	LDA	TXTL,X
	ORA	#128
	STA	TXTL,X
	INX
	DEY
	BNE	INV3

	RTS
****

UPDS1	LDX	#13
	LSR
	BCC	UPDS1X
	LDX	#11
UPDS1X	RTS

**

UPDAT	LDA	OFLAGS
	JSR	UPDS1
	STX	TXTL+4
	JSR	UPDS1
	STX	TXTL+10
	JSR	UPDS1
	STX	TXTL+17
	JSR	UPDS1
	STX	TXTL+22

	LDX	#$18
	LDA	OLTL
	BMI	UPD2
	LDX	#$12
UPD2	STX	TXTL+38

	LDX	#27
	LDA	OLKX
	JSR	UPD3

	LDX	#32
	LDA	OLKY
*

UPD3	PHA
	LSR
	LSR
	LSR
	LSR
	JSR	UPD4
	PLA
*

UPD4	AND	#15
	ORA	#$10
	CMP	#$1A
	BCC	UPD5
	ADC	#6

UPD5	STA	TXTL,X
	INX
	RTS
****

KEY	JSR	CLICK

	LDA	$D20F
	AND	#4
	BEQ	KEY

KEY1	JSR	CLICK

	LDA	$D20F
	AND	#4
	BNE	KEY1

	LDA	$D209
	AND	#$3F

	RTS

*****

DEMO	JSR	INIPMG
	JSR	OINIT
	JSR	INTINI

	LDA	#0
	STA	CURPOS

DEMO2	JSR	INVERT
	JSR	UPDAT
	JSR	PRINT

DEMO3	JSR	KE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	LDA	#0
	STA	$D200
	STA	$D201

	RTS
***

CLTAB	DFB 56,108,198,130
	DFB 198,108,56

***

�������������������������������������������������������������������������������������������