	;RTC DRIVER FOR USE UNDER SOS
	;WRITTEN BY DONALD G. WULFF
	;RTC "DEVICE" MUST BE OPENED AND CLOSED AS
	;WITH OTHER DEVICES.
	;THIS DRIVER USES THE CASSETTE DRIVER POSITION
	;IN SOS, MUST BE TRIGGERED AND LOADED AHEAD
	;OF SOS LIBRARY.
	;TIME IS KEPT IN 4 WORDS, 0,1,2 AND 3, AND
	;1 IS ADDED TO THE LOW ORDER BIT POSITION OF
	;WORD 3 EVERY 100MS.  WHEN WORD 3
	;OVERFLOWS 1 IS ADDED TO WORD 2, ETC ETC.

	;THE CLOCK IS STARTED BY A COMMAND WHICH SETS
	;THE TIMER VALUE.  IT MAY BE INSPECTED BY A
	;"GET TIME" COMND AT ANY TIME. SEE FORMAT BELOW.

	;TO START CLOCK AND SET TIME...
	;	SUB	AC0,AC0	;INDICATE SET TIME COMND
	;	LDA	AC1,ATMTB	;ADDRSS OF TIME
	;	.SYSTM
	;	.WRS	30	;CTU0
	;	JMP	ERR	;STANDARD OPN ERR RETRN
	;	...		;NORMAL RETURN
	;	...
	;	...
	;ATMTB: TIMTB
	;TIMTB:	4 WORDS CONTAINING THE DESIRED
	;	START VALUE. TIME WDS 0,1,2,3.

	;TO READ CLOCK...
	;SAME AS START CLOCK EXCEPT SET AC0=1.
	;THE CURRENT TIME VALUE WILL BE MOVED TO THE
	;4 WORD AREA POINTED TO BY AC1.
	.TITL	RTCDR
	.ENT	.CTAD,.CTU0
	.EXTN	.DISM
	.NREL

	; -D-E-V-I-C-E-----C-O-N-T-R-O-L-----T-A-B-L-E-
.CTAD:	RTC	;DCTCD 0DEVICE CODE
	MSPTP+MSTTI+MSTTO	;DCTMS 1MASK
	0	;DCTCH 2ACTIVE DEVICE CHARACTERISTICS
	0	;DCTLK 3LINK TO NEXT PRIORITY DCT
	INTRT	;DCTIS 4ADDRSS OF INTRPT SERVCE RTN
	0	;DCTIL 5DCT OF LAST INTRPTD DEVCE PNTR
	DSPTB	;DCTDT 6ADDRSS OF COMMND DSPATCH TABLE
	STRTR	;DCTST 7ADDRSS OF DEVICE START RTN
	STPRT	;DCTSP10ADDRSS OF DEVICE STOP RTN
	0	;DCTFL11DEVICE FLAGS
	0	;DCTBS12BUF SIZ BYTES=2*LENGTH(WRDS)
	0	;DCTBF13BYTE ADDRSS OF BUFFER START
	0	;DCTBL14BYTE ADDRSS OF BUFFER END
	0	;DCTIP15BUFFER CURRENT INPUT POINTER
	0	;DCTOP16BUFFER CURRENT OUTPUT POINTER
	0	;DCTCN17COUNT OF ACTV DATA IN BUFFER
	0	;DCTTO20TIMEOUT CONSTANT APPRX 
	0	;DCTRC21RESTART CONSTANT
	3	;DCTAT22DEVICE ATTRIBUTES, SEE .GTATR
	0	;DCTFC23DVCE CHARCTRSTCS, SEE .OPEN
		;24-32 DEVICE INTERRUPT FRAME
	0	;IAC0 24SAVED AC0
	0	;IAC1 25SAVED AC1
	0	;IAC2 26SAVED AC2
	0	;IAC3 27SAVED AC3
	0	;IPC  30PC (LOC 0 WHEN INTRPT TAKEN)
	0	;IRLOC31VOLATILE SOS LINKAGE CELL
	0	;IMSK 32INTRPT MSK WHEN INTRPT TAKEN


		;INTERRUPT ROUTINE
INTRT:	NIOS	RTC	;SET RTC BUSY FLAG
	ISZ	TIM3	;INCREMENT TIME WORD 3
	JMP	DISMS	;JMP IF NO OVRFLOW FROM WORD 3
	ISZ	TIM2	;INCREMENT TIME WORD 2
	JMP	DISMS	;JMP IF NO OVRFLOW FROM WORD 2
	ISZ	TIM1	;INCREMENT TIME WORD 1
	JMP	DISMS	;JMP IF NO OVRFLOW FROM WORD 1
	ISZ	TIM0	;INCREMENT TIME WORD 0
	JMP	.	;LOOP IF TIMER OVERFLOW
DISMS:	JMP	@.+1	;RETURN TO SOS
	.DISM		;RETURN TO SOS INTRPT MODULE

		;DEVICE START ROUTINE
STRTR:	JMP	.	;SHOULD NOT OCCUR

		;DEVICE STOP ROUTINE
STPRT:	NIOC	RTC	;SEND CLEAR PULSE
	JMP	0,AC3	;RETURN

		;READ/WRITE SEQ BY CHARACTERS ROUTINE
RDWRS:	SKPBZ	CPU	;SKIP IF INTRPTS DSABLD
	JMP	DIS
TSTSG:	LDA	AC0,CAC0	;FETCH USER AC0
	MOV	AC0,AC0,SZR	;TEST AC0,0=SET, 1=GET
	JMP	READ	;JMP IF GET TIME COMMAND

		;SET TIME COMMAND
	LDA	AC2,CAC1	;FETCH USER AC1
	LDA	AC1,0,AC2	;FETCH USER TIME WORD 0
	STA	AC1,TIM0	;SET TIME WORD 0
	LDA	AC1,1,AC2	;FETCH USER TIME WORD 1
	STA	AC1,TIM1	;SET TIME WORD 1
	LDA	AC1,2,AC2	;FETCH USER TIME WORD 2
	STA	AC1,TIM2	;SET TIME WORD 2
	LDA	AC1,3,AC2	;FETCH USER TIME WORD 3
	STA	AC1,TIM3	;SET TIME WORD 3
	SUBZL	AC0,AC0	;AC0=1
	DOAS	AC0,RTC	;SET RTC TO 10HZ AND SET BUSY
CRETN:	LDA	AC0,DSSW	;FETCH DISABL SWTCH
	MOV	AC0,AC0,SNR	;TEST SWTCH
	JMP	RET	;JMP IF ION WAS NOT CHNGED
	INTEN		;TURN ION ON
RET:	JMP	1,AC3	;NORMAL RETURN TO SOS

DIS:	INTDS	;DISABL INTRPTS
	SUBZL	AC0,AC0	;AC0=1
	STA	AC0,DSSW	;INDICATE ION WAS RESET
	JMP	TSTSG	;CONTINUE

		;GET TIME COMMAND
READ:	LDA	AC2,CAC1	;FETCH USER AC1
	LDA	AC1,TIM0	;FETCH TIME WORD 0
	STA	AC1,0,AC2	;GIVE TO USER
	LDA	AC1,TIM1	;FETCH TIME WORD 1
	STA	AC1,1,AC2	;GIVE TO USER
	LDA	AC1,TIM2	;FETCH TIME WORD 2
	STA	AC1,2,AC2	;GIVE TO USER
	LDA	AC1,TIM3	;FETCH TIME WORD 3
	STA	AC1,3,AC2	;GIVE TO USER
	JMP	CRETN	;RETURN TO USER

		;READ/WRITE SEQUENTIAL LINE ROUTINE
RDWRL	=RDWRS	;SAME AS FOR SEQUENTIAL CHARACTERS

OPEN	=CRETN	;RETURN TO SOS
.CTU0	=.CTAD

CLOSE:	NIOC	RTC	;STOP CLOCK
	JMP	CRETN	;RETURN TO SOS

		;COMMAND DISPATCH TABLE
DSPTB:	OPEN	;SOS OPEN ROUTINE
	CLOSE	;CLOSE ROUTINE
	RDWRS	;READ/WRITE SEQUENTIAL CHARACTRS ROUTN
	RDWRL	;READ/WRITE SEQUENTIAL LINE ROUTINE

DSSW:	0	;SWITCH,  IF ON INTDS WAS ISSUED

TIM0	=.CTAD+DCTBF	;USE DCT TO HOLD TIME
TIM1	=TIM0+1
TIM2	=TIM1+1
TIM3	=TIM2+1
	.END
