;PT3 Duration Calculator
;Author Sergey Bulba <vorobey@mail.khstu.ru>
;Support page http://bulba.at.kz/
;(c)2005 S.V.Bulba

;Based on Ay_Emul's GetTime function and
;on VT II PT3 player for ZX Spectrum

;Calculation delay is about from 0.1 to 0.5 second
;(depending of given PT3 module)

;Precision of calculation is 1 interrupt

;Limitations
;-----------
;No any checkings of module structure, so, can
;deadlock on damaged modules or on random data.
;If you want, you can add deadlock and crash
;checking from Ay_Emul (will decrease speed).

	ORG #8000

;TEST CODE TO CALL FROM BASIC
	LD HL,#C000

;Start of GetTime

;HL - PT3 MODULE ADDRESS
	DI ;SP is used, any interrupts are not allowed

	LD (PT3TMdAd),HL
	PUSH HL
	PUSH HL
	LD DE,100
	ADD HL,DE
	LD A,(HL)
	LD (PT3T_Dl),A
	LD E,3
	ADD HL,DE
	LD C,(HL)
	INC HL
	LD B,(HL)
	LD H,D
	LD L,D
	LD (PT3TIME),HL
	POP HL
	ADD HL,BC
	LD (PT3TPtPtr),HL
	POP HL
	LD E,201
	ADD HL,DE
	PUSH HL

PT3TLP POP HL
	LD A,(HL)
	INC HL
	INC A
	JP Z,PT3T_EXIT
	PUSH HL
	DEC A
	ADD A,A
	LD E,A
	LD D,0
	RL D
PT3TPtPtr EQU $+1
	LD HL,#2121
	ADD HL,DE
PT3TMdAd EQU $+1
	LD DE,#1111
	LD (PT3TSP_+1),SP
	LD SP,HL
	POP HL
	ADD HL,DE
	LD (PT3TAdInPtA),HL
	POP HL
	ADD HL,DE
	LD (PT3TAdInPtB),HL
	POP HL
	ADD HL,DE
	LD (PT3TAdInPtC),HL
PT3TSP_	LD SP,#3131
	LD A,1
	LD (PT3TSKACNT),A
	LD (PT3TSKBCNT),A
	LD (PT3TSKCCNT),A


PT3TLP2	LD HL,PT3TSKACNT
	DEC (HL)
	JR NZ,PT3TCHB
PT3TAdInPtA EQU $+1
	LD BC,#0101
	LD A,(BC)
	AND A
	JR Z,PT3TLP
PT3TSKPA EQU $+2
	LD IXL,#2E
	CALL PT3TPD
	LD A,IXL
	LD (PT3TSKPA),A
	LD (PT3TSKACNT),A
	LD (PT3TAdInPtA),BC

PT3TCHB	LD HL,PT3TSKBCNT
	DEC (HL)
	JR NZ,PT3TCHC
PT3TSKPB EQU $+2
	LD IXL,#2E
PT3TAdInPtB EQU $+1
	LD BC,#0101
	CALL PT3TPD
	LD A,IXL
	LD (PT3TSKPB),A
	LD (PT3TSKBCNT),A
	LD (PT3TAdInPtB),BC

PT3TCHC	LD HL,PT3TSKCCNT
	DEC (HL)
	JR NZ,EXTMP
PT3TSKPC EQU $+2
	LD IXL,#2E
PT3TAdInPtC EQU $+1
	LD BC,#0101
	CALL PT3TPD
	LD A,IXL
	LD (PT3TSKPC),A
	LD (PT3TSKCCNT),A
	LD (PT3TAdInPtC),BC

EXTMP	
PT3T_Dl	EQU $+1
	LD DE,#11
PT3TIME	EQU $+1
	LD HL,#2121
	ADD HL,DE
	LD (PT3TIME),HL
	JP PT3TLP2

PT3TSETSK LD A,(BC)
	LD IXL,A
	JR PT3TPD2
PT3TPD4	INC BC
PT3TPD3	INC BC
PT3TPD2	INC BC
PT3TPD1	LD A,(BC)
	INC BC
	ADD A,D
	JR C,PT3TPD2
	ADD A,E
	RET Z
	JR C,PT3TPD1
	ADD A,D
	RET Z
	JR C,PT3TPD1
	ADD A,14
	JR C,PT3TPD3
	INC A
	JR Z,PT3TSETSK
	INC A
	JR Z,PT3TPD1
	ADD A,96
	RET C
	ADD A,48
	JR C,PT3TPD1
	ADD A,15
	JR C,PT3TPD4
	INC A
	JR Z,PT3TPD2
	ADD A,A
	LD E,A
	LD HL,PT3TSPCCOMS+#FF20-#1000
	ADD HL,DE
	LD E,(HL)
	INC HL
	LD D,(HL)
	PUSH DE
PT3TPD	LD DE,#1020
	JR PT3TPD1


PT3TC_DELAY LD A,(BC)
	INC BC
	LD (PT3T_Dl),A
	RET

PT3TC_PORTM INC BC
	INC BC
PT3TC_GLISS
PT3TC_ENGLS INC BC
PT3TC_VIBRT INC BC
PT3TC_SMPOS
PT3TC_ORPOS INC BC
PT3TC_NOP RET

;ALL 16 ADDRESSES TO PROTECT FROM BROKEN PT3 MODULES
PT3TSPCCOMS DW PT3TC_NOP
	DW PT3TC_GLISS
	DW PT3TC_PORTM
	DW PT3TC_SMPOS
	DW PT3TC_ORPOS
	DW PT3TC_VIBRT
	DW PT3TC_NOP
	DW PT3TC_NOP
	DW PT3TC_ENGLS
	DW PT3TC_DELAY
	DW PT3TC_NOP
	DW PT3TC_NOP
	DW PT3TC_NOP
	DW PT3TC_NOP
	DW PT3TC_NOP
	DW PT3TC_NOP

PT3T_EXIT
;	LD HL,(PT3TIME)

;TEST CODE TO RETURN TO BASIC
	LD BC,(PT3TIME)
	EI ;REMOVE TO DISABLE INTERRUPTS

	RET 

PT3TSKACNT	EQU $
PT3TSKBCNT	EQU $+1
PT3TSKCCNT	EQU $+2
