	TITLE "BIOS.KOS   HT68K BIOS: K-OS ONE LOW-LEVEL ROUTINES, CLOCK"
	PAGE
**************** LOW-LEVEL ROUTINES FOR K-OS ONE *******************
;------------- INITIALIZE TRAP REGISTER SAVE
OSINIT	MOVE.L	(A4)+,D0	;SYSTEM HEAP POINTER
        MOVE.L	(A4)+,SYSPC	;INIT REGISTER SAVE AREA
	MOVE.W	#SUPERSR,SYSSR
	MOVEM.L	#0FFFFH,SYSREG
	MOVE.L	#COMM_CODE,D1	;RESIDENT COMMAND PROCESSOR
	MOVE.L	#USER_CODE,D0	;USER PROGRAM SPACE
	MOVE.L	D1,-(A4)	;CMDSTRT  ( 0=NONRESIDENT )
	MOVE.L	D0,-(A4)	;USTART   ( >=SYSTEM HEAP )
	RTS
;------------- ENTER TRAP PROCESSOR
TRAP1	MOVE.W	(SP)+,USRSR	;SAVE USER STATE
	MOVE.L	(SP)+,USRPC
	MOVEM.L	#0FFFFH,USRREG
	MOVEM.L	SYSREG,#0FFFFH	;LOAD SYSTEM STATE
	MOVE.L	SYSPC,-(SP)
	MOVE.W	SYSSR,SR
	MOVE.L	USRA0,-(A4)	;LOAD PARAM POINTER
	CLR.L	D7		;BYTE CONVERTION REGISTER
	RTS			;GO TO TRAP PROCESSOR
;------------- ENTER USER PROGRAM
USER	MOVEM.L	USRREG,#0FFFFH	;LOAD USER STATE
	MOVE.L	USRPC,-(SP)
	MOVE.W	USRSR,-(SP)
	RTE			;GO TO USER PROGRAM
;------------- INITIALIZE EXCEPTION HANDLER
EXSET	MOVE.L	(A4)+,EXINFO	;ADDRESS OF EXCEPTION INFO SPACE
	MOVE.L	(A4)+,EXPC	;ADDRESS OF EXCEPTION HANDLER
	MOVE.W	#SUPERSR,EXSR
	MOVEM.L	#0FFFFH,EXREG
	MOVEQ	#254,D0
	SUB.L	D0,EXA4		;AVOID SMASHING OPSYS STACKS
	SUB.L	D0,EXA7		;WHEN EXCEPTION OCCURS IN OPSYS
	RTS
************************ REAL TIME CLOCK ****************************
;------------------------------- DELAY ROUTINE
WAIT				;WAIT TOS MILLISECONDS
	LEA	BIOS_DATA,A0
	MOVE.L	(A4)+,WAITCNT(A0)
WAIT10	TST.L	WAITCNT(A0)
	BNE.S	WAIT10
	RTS
;------------------------------- SET CLOCK IN K-OS DATA SPACE
CLOCKSET			;SET POINTERS TO SYSDATE,SYSTIME
	LEA	BIOS_DATA,A0
	MOVE.L	(A4)+,MSECPN(A0)
	MOVE.L	(A4)+,DATEPN(A0)
	MOVE.L	(A4)+,TIMEPN(A0)
	RTS
;------------------------------- UPDATE TIME AND DATE
CLOCK				;UPDATE TIME,DATE EVERY 10 MS
	TST.L	WAITCNT(A6)
	BEQ.S	NOWAIT
	MOVEQ	#9,D0
WAITE	SUBQ.L	#1,WAITCNT(A6)
	DBEQ	D0,WAITE
NOWAIT	MOVEA.L	MSECPN(A6),A0
	MOVEQ	#10,D0
	ADD.L	D0,(A0)		;ADD  10 mS  TO MILLISEC COUNTER
	ADDQ.L	#1,TICCNT(A6)
	CMPI.L	#100,TICCNT(A6)
	BLO.S	ENDOFTIME
	CLR.L	TICCNT(A6)	;A SECOND HAS PASSED
	MOVEA.L	TIMEPN(A6),A0
	ADDQ.L	#4,A0
	CLR.B	-(A0)		;TIME.L = HR.B, MIN.B, SEC.B, 0.B
	ADDQ.B	#1,-(A0)	;INCREMENT SECONDS
	CMPI.B	#60,(A0)
	BLO.S	ENDOFTIME
	CLR.B	(A0)
	ADDQ.B	#1,-(A0)	;INCREMENT MINUTES
	CMPI.B	#60,(A0)
	BLO.S	ENDOFTIME
	CLR.B	(A0)
	ADDQ.B	#1,-(A0)	;INCREMENT HOURS
	CMPI.B	#24,(A0)
	BLO.S	ENDOFTIME
	CLR.B	(A0)		;A DAY HAS PASSED
	MOVEA.L	DATEPN(A6),A0
	ADDQ.L	#4,A0
	CLR.B	-(A0)		;DATE.L = YY.B, MM.B, DD.B, 0.B
	ADDQ.B	#1,-(A0)	;INCREMENT DAY
	MOVE.B	-1(A0),D1	;SEE IF LAST DAY IN MONTH
	CMPI.B	#2,D1
	BNE.S	NOTFEB
	MOVEQ	#28,D0		;LAST = 28
	MOVE.B	-2(A0),D2
	ANDI.B	#3,D2
	BNE.S	CHKDAY
	MOVEQ	#29,D0		;LAST = 29
	BRA.S	CHKDAY
NOTFEB	CMPI.B	#7,D1
	BHI.S	FALL
	MOVEQ	#30,D0		;JAN,MAR..JUL
	ANDI.B	#1,D1
	ADD.B	D1,D0		;LAST = 30 + MONTH MODULO 2
	BRA.S	CHKDAY
FALL	MOVEQ	#31,D0		;AUG..DEC
	ANDI.B	#1,D1
	SUB.B	D1,D0		;LAST = 31 - MONTH MODULO 2
CHKDAY	CMP.B	(A0),D0
	BHS.S	ENDOFTIME
	MOVE.B	#1,(A0)		;A MONTH HAS PASSED
	ADDQ.B	#1,-(A0)	;INCREMENT MONTH
	CMPI.B	#13,(A0)
	BLO.S	ENDOFTIME
	MOVE.B	#1,(A0)		;A YEAR HAS PASSED
	ADDQ.B	#1,-(A0)	;INCREMENT YEAR
ENDOFTIME
	RTS

	END	;BIOS.KOS
