CTC.asm
flypie_Ace-2019/testrom/CTC.asm
	INCLUDE "IODEFS.H"
	INCLUDE "DEBUGMACROS.H"

	INCLUDE "SYSVARS.H"

	SEGMENT CODE ; MAKE IT THE CURRENT SECTION


	PUBLIC CTC0INT,CTC1INT,CTC2INT,CTC3INT,INIT_CTC, INCSYSCLOCK

	EXTERN	CTCINTTBL,INTTBL
	EXTERN  CTC2KIO

INTON		EQU 10000000B
INTOFF		EQU 00000000B

COUNTMODE	EQU 01000000B
TIMEMODE	EQU 00000000B

PRE256		EQU 00100000B
PRE16		EQU 00000000B

RISING		EQU 00010000B
FALLING		EQU 00000000B

PULSESTARTS	EQU 00001000B
LOADSTARTS	EQU 00000000B

TIMECONST	EQU 00000100B
NOTIMECONST	EQU 00000000B

RESET		EQU 00000010B
CONTINU		EQU 00000000B

CONTROL		EQU 00000001B
VCTOR		EQU 00000000B


INIT_CTC:
;SET UP THE INT VECTOR
	PUSH AF
	PUSH HL
	PUSH DE
	PUSH BC

INIT_CTC0:
	DOOUT CTCCH0,INTOFF|COUNTMODE|PRE256|RISING|LOADSTARTS|TIMECONST|CONTINU|CONTROL	;WRITE INTO WR0: ERROR RESET, SELECT WR0
	DOOUT CTCCH0, 0FFH	;TIME CONSTOUT. APPARENTLY CHANNEL WILL NOT WORK WITHOUT ONE

INIT_CTC1:
	DOOUT CTCCH1,INTOFF|COUNTMODE|PRE256|RISING|LOADSTARTS|TIMECONST|CONTINU|CONTROL 
	DOOUT CTCCH1, 0FFH	;TIME CONSTOUT. APPARENTLY CHANNEL WILL NOT WORK WITHOUT ONE

INIT_CTC2:
	DOOUT CTCCH2,INTOFF|TIMEMODE|PRE16|RISING|LOADSTARTS|TIMECONST|CONTINU|CONTROL

	;	Prescaler		BPS
	;1	0.0000048828125	204800
	;2	0.000009765625	102400
	;3	0.0000146484375	68266.6666666667
	;4	0.00001953125	51200
	;5	0.0000244140625	40960

	DOOUT CTCCH2, 001H	;Set Count size

INIT_CTC3:
	DOOUT CTCCH3,INTON|COUNTMODE|PRE16|RISING|LOADSTARTS|TIMECONST|CONTINU|CONTROL ;SET THE CTC 3 TO DECRMENT WHEN RISING SIGNAL RECIVED. INTS ON. TIME CONST FOLLOWS
	DOOUT CTCCH3, 01H	;TIME CONSTOUT. APPARENTLY CHANNEL WILL NOT WORK WITHOUT ONE

;SET THE INT VECTOR
	LD	HL,CTCINTTBL
	LD	DE,INTTBL
	SBC HL,DE  ; GET OFFSET TO CTC INT TABLE
	LD	A,L 
	AND	011111000B ;MASK STUFF WE DO NOT CARE
	OR  VCTOR

	DOOUT CTCCH0,A
	

	POP BC
	POP DE
	POP HL
	POP AF

	RET

INCSYSCLOCK:
	PUSH HL

	LD HL,SYSCLOCK
	INC	(HL)
	JR  NZ, CARRYON
	
	INC HL
	INC	(HL)
	JR  NZ, CARRYON

	INC HL
	INC	(HL)
	JR  NZ, CARRYON

	INC HL
	INC	(HL)

CARRYON:
	POP	HL

	ret

CTC0INT:
	PUSH AF
	PUSH HL

	LD HL,VIDSTARTLOC+40H
	INC (HL)

	POP	HL;
	POP AF
	EI
	RETI

CTC1INT:
	PUSH AF
	PUSH HL

	LD HL,VIDSTARTLOC+60H
	INC (HL)

	POP	HL;
	POP AF
	EI
	RETI

CTC2INT:
	PUSH AF
	PUSH HL

	LD HL,VIDSTARTLOC+80H
	INC (HL)

	POP	HL;
	POP AF
	EI
	RETI


CTC3INT:
	PUSH	AF
	PUSH	HL

	LD HL,VIDSTARTLOC+0A0H
	INC (HL)

	LD HL,VIDSTARTLOC+0A1H
	INC (HL)

	LD HL,VIDSTARTLOC+0A2H
	INC (HL)

	LD HL,VIDSTARTLOC+0A3H
	INC (HL)

	CALL INCSYSCLOCK

	POP		HL
	POP		AF
	EI
	RETI


