//======================================================== FX_HEADER
EFF_INI		JP	EFF_INI_
EFF_RUN		JP	EFF_RUN_
//======================================================== FX_CONST
BUFF	EQU	#9D00
COS_A	EQU	#C0
SIN_A	EQU	#C0 + 16
MDX0	EQU	0
MDY0	EQU	0

ROT_STEP	EQU	#0200
MOV_X_STEP	EQU	#0500
MOV_Y_STEP	EQU	#0400
//======================================================== FX_INIT
EFF_INI_
	LD	A,#17
	CALL	PAGER
	;--
	;CALL	MRTCLR
	;CALL	NEXT
	;--
	LD	HL,#4000
	LD	BC,256 * 24 + #FF
	LD	A,L
	LD	A,#FF
INI1	LD	(HL),A
	INC	L
	JP	NZ,INI1
	;XOR	C
	INC	H
	DJNZ	INI1
	;--
	LD	HL,#C000
	LD	BC,256 * 24 + #FF
	LD	A,C
	LD	A,#FF
INI2	LD	(HL),A
	INC	L
	JP	NZ,INI2
	;XOR	C
	INC	H
	DJNZ	INI2
	;----
	EI
	HALT
	LD	HL,FX_SWAP_PG
	LD	(IM2CALL),HL
	;---
	RET
;---------------------------------
FX_SWAP_PG
	LD	A,(PAGE)
	XOR	#08
	LD	(PAGE),A
	JP	IM2CALL_RET
//======================================================== FX_MAINLOOP

EFF_RUN_
		IF SHOW_FRAMES=1
		LD	A,(FRAMES_QTY)
		PUSH	AF
		XOR	A
		LD	(FRAMES_QTY),A
		ENDIF
	EI
	HALT
		IF SHOW_FRAMES=1
		POP AF:OUT (#FE),A
		ENDIF
	CALL	MRTOUT
	CALL	MRTCLR
	CALL	NEXT
	CALL	SYNC
	CALL	MRT
		IF SHOW_FRAMES=1
		XOR A:OUT (#FE),A
		ENDIF
	RET
//======================================================== FX_PROCEDURES
SYNC
	XOR	A
	LD	(BORDER),A
	;---
	LD	HL,SYNC_TBL
SYNC_ADR EQU $-2
	LD	C,(HL)
	INC	HL
	LD	B,(HL)
	INC	HL
	EX	DE,HL
	LD	HL,(MUSPOS)
	AND	A
	SBC	HL,BC
	RET	C
	;---
	LD	(SYNC_ADR),DE
	LD	A,7
	LD	(BORDER),A
	RET
;-----------------------------
SYNC_TBL
	DW	1872-1
	DW	2080-1
	DW	2288-1
	DW	#FFFF
;-----------------------------
NEXT	
	LD	HL,0
ROTHL	EQU	$-2
	LD	BC,ROT_STEP
	ADD	HL,BC
	LD	(ROTHL),HL
	LD	L,H
	LD	H,TBL_ROT / 256
	LD	A,(HL)
	LD	(ROTA),A
	;---
	LD	HL,0
MOV_X	EQU	$-2
	LD	BC,MOV_X_STEP
	ADD	HL,BC
	LD	(MOV_X),HL
	LD	L,H
	LD	H,TBL_MOV_X / 256
	LD	A,(HL)
	LD	(MDX),A
	;---
	LD	HL,0
MOV_Y	EQU	$-2
	LD	BC,MOV_Y_STEP
	ADD	HL,BC
	LD	(MOV_Y),HL
	LD	L,H
	LD	H,TBL_MOV_Y / 256
	LD	A,(HL)
	LD	(MDY),A
	;---
	RET
;--------------------------------------
FXBLINK
	LD	HL,#5800
	LD	DE,#5801
	LD	BC,767
	LD	(HL),7*8+7
	LDIR
	LD	HL,#5800
	LD	DE,#D800
	LD	BC,768
	LDIR
	RET
;--------------------------------------
MRTOUT	LD	A,(PAGE)
	OR	#07
	CALL	PAGER	
	;---
	LD	A,7
BORDER	EQU	$-1
	OUT	(#FE),A
	CP	7
	JP	Z,FXBLINK
	;---
	LD	D,PAL_A / 256
	LD	BC,#5800
	EXX
	LD	D,PAL_B / 256
	LD	BC,#D800
	;--
	LD	A,(PAGE)
	AND	#08
	JR	NZ,MRTOUT2
	EXX
	;--
MRTOUT2
	CALL	MRTOUT0
	EXX
	;----
MRTOUT0	LD	LX,24
	LD	L,low BUFF + 5
MRTOUT1	;---
	LD	H,high BUFF
	DUP	31
	LD	E,(HL)	;7
	LD	A,(DE)	;7
	LD	(BC),A	;7
	INC	C	;4
	INC	H	;4 /29
	EDUP
	LD	E,(HL)	;7
	LD	A,(DE)	;7
	LD	(BC),A	;7
	INC	BC	;6
	INC	L	;4 /31
	;--
	DEC	LX	   ;8
	JP	NZ,MRTOUT1 ;10
	;--
	RET
;-------------------------------------------
MRTCLR
	LD	(MRTCLR_SOHR_SP),SP
	;---
	LD	B,32
	LD	DE,0
	LD	HL,BUFF+24+5
MRTCLR1	LD	SP,HL
	DUP	12
	PUSH	DE
	EDUP
	INC	H
	DJNZ	MRTCLR1
	;---
	LD	SP,0
MRTCLR_SOHR_SP EQU $-2
	RET

;tx = x*cos(a)-y*sin(a);
;ty = x*sin(a)+y*cos(a);
MRT	
	LD	A,(PAGE)
	AND	#18
	CALL	PAGER
	;---
	LD	BC,0-MRT1_SZ
	LD	A,0
ROTA	EQU	$-1
	OR	#C0
	LD	(ROTH+1),A
	;--
	LD	HL,COS1+1
	DUP	32
	LD	(HL),A
	ADD	HL,BC
	EDUP
	;--
	LD	HL,COS2+1
	DUP	32
	LD	(HL),A
	ADD	HL,BC
	EDUP
	;--
	ADD	A,16
	OR	#C0
	;--
	LD	HL,SIN1+1
	DUP	32
	LD	(HL),A
	ADD	HL,BC
	EDUP
	;---
	LD	A,(MDX)
	LD	HL,MDX-MRT1_SZ
	DUP	31
	LD	(HL),A
	ADD	HL,BC
	EDUP
	;---
	LD	A,(MDY)
	LD	HL,MDY-MRT1_SZ
	DUP	31
	LD	(HL),A
	ADD	HL,BC
	EDUP
	;---
	LD	DE,BUFF
ROTH	LD	H,0
	EXX
	LD	DE,ROMBSMAP
	LD	LX,30
MRT2	;----------------
	DUP	31
	;----------------
	EX	DE,HL
	LD	C,(HL)
	INC	L
	LD	B,(HL)
	INC	L	
	EX	DE,HL
	; -- координаты C=x B=y
	LD	H,COS_A	;7
	LD	L,C	;4
	LD	A,(HL)	;7 a=(hl)=x*cos(a)
	LD	H,SIN_A	;7
	LD	L,B	;4 (hl)=y*sin(a)
	SUB	(HL)	;7 a=y`
	ADD	A,MDX0	;7
	EX	AF,AF	;4
	;--
	LD	L,C	;4
	LD	A,(HL)	;7 a=(hl)=x*sin(a)
	LD	H,COS_A	;7
	LD	L,B	;4
	ADD	A,(HL)	;7
	ADD	A,MDY0	;7
	OR	#C0	;7
	LD	B,A	;4
	;--
	EX	AF,AF	;4
	LD	L,A	;4
	LD	H,high TXCOR ;7
	LD	C,(HL)	;7
	;--
	LD	A,(BC)	;7
	;--------
	EXX		;4
	;--------
	OR	A	;4
	JP	Z,1F	;10 /14
	;--
	LD	L,A	;7
	LD	A,E	;4
	;-
	DUP	6
	LDI		;16*6=96
	EDUP
	;-
	LD	E,A	;4
1	INC	D	;4
	EXX		;4 /122
	;----------------
	EDUP
MRT1	;----------------
	EX	DE,HL
	LD	C,(HL)
	INC	L
	LD	B,(HL)
	INC	HL	
	EX	DE,HL
	; -- координаты C=x B=y
COS1	LD	H,COS_A	;7
	LD	L,C	;4
	LD	A,(HL)	;7 a=(hl)=x*cos(a)
SIN1	LD	H,SIN_A	;7
	LD	L,B	;4 (hl)=y*sin(a)
	SUB	(HL)	;7 a=y`
	ADD	A,MDX0	;7
MDX	EQU	$-1
	EX	AF,AF	;4
	;--
	LD	L,C	;4
	LD	A,(HL)	;7 a=(hl)=x*sin(a)
COS2	LD	H,COS_A	;7
	LD	L,B	;4
	ADD	A,(HL)	;7
	ADD	A,MDY0	;7
MDY	EQU	$-1
	OR	#C0	;7
	LD	B,A	;4
	;--
	EX	AF,AF	;4
	LD	L,A	;4
	LD	H,high TXCOR ;7
	LD	C,(HL)	;7
	;--
	LD	A,(BC)	;7
	;--------
	EXX		;4
	;--------
	OR	A	;4
	JP	Z,MRT1_END ;10 /14
	;--
	LD	L,A	;7
	LD	A,E	;4
	;-
	DUP	6
	LDI		;16*6=96
	EDUP
	;-
	LD	E,A	;4
MRT1_END ;----
MRT1_SZ	EQU (MRT1_END - MRT1 + 2)
	LD	D,high BUFF
	INC	E
	EXX
	;------------------
	DEC	LX	;8
	JP	NZ,MRT2 ;10 /18
	;------------------
	RET

//======================================================== FX_INCLUDE_DATA
	ALIGN	256
ROMBSMAP	INCLUDE "bin/rombs_map.asm"
//======================================================== FX_TABLES
	ALIGN	256
TXCOR	INCLUDE "bin/rombs_txcor.asm"
	ALIGN	256
TBL_ROT INCLUDE "bin/tbl_rot.asm"
TBL_MOV_X INCLUDE "bin/tbl_mov_x.asm"
TBL_MOV_Y INCLUDE "bin/tbl_mov_y.asm"

	ALIGN	256
PAL_A
	DB	0
	DB	#01,#41,#41,#41,#41,#05,#45,#45+#07*8
	DB	#02,#42,#42,#42,#46,#46,#46,#46+#07*8
	DB	#03,#03,#03,#43,#43,#47,#43,#43+#07*8
	DB	#04,#44,#44,#44,#44,#44,#44,#44+#07*8
	ALIGN	256
PAL_B
	DB	0
	DB	#00,#00,#01,#05,#45,#45,#47,#47+#05*8
	DB	#00,#00,#02,#06,#02,#42,#47,#47+#06*8
	DB	#00,#01,#03,#03,#43,#03,#47,#47+#03*8
	DB	#00,#00,#04,#05,#44,#45,#47,#47+#04*8