; time.s
; Provides a 1e-4 timer
; by N. Douglas
; Started: 01-04-1997
; Last updated: ?
R0 RN 0
R1 RN 1
R2 RN 2
R3 RN 3
R4 RN 4
R5 RN 5
R6 RN 6
R7 RN 7
R8 RN 8
R9 RN 9
R10 RN 10
R11 RN 11
R12 RN 12
R13 RN 13
R14 RN 14
PC RN 15

XOS_ClaimDeviceVector		EQU	&02004b
XOS_ReleaseDeviceVector		EQU	&02004c
XOS_EnterOS			EQU	&020016

	AREA |C$$code|, CODE, READONLY

	EXPORT	|_initnicetime|
	EXPORT	|_endnicetime|

|_initnicetime|
	STMFD	R13!,{R0-R12,R14}
	MOV	R0,#6
	ADR	R1,ioctimer1
	MOV	R2,#0
	MOV	R3,#0
	MOV	R4,#0
	SWI	XOS_ClaimDeviceVector

	SWI	XOS_EnterOS
	MOV	R1,#&3200000
	LDRB	R0,[R1,#&18]
	ORR	R0,R0,#1<<6
	STRB	R0,[R1,#&18]
	LDR	R0,timerinterval
	STRB	R0,[R1,#&50]
	MOV	R0,R0,LSR #8
	STRB	R0,[R1,#&54]
	TEQP	PC,#0
	MOV	R0,R0
	LDMFD	R13!,{R0-R12,PC}^
timerinterval
	DCD	2000000/1000

|_endnicetime|
	STMFD	R13!,{R0-R12,R14}
	MOV	R0,#6
	ADR	R1,ioctimer1
	MOV	R2,#0
	MOV	R3,#0
	MOV	R4,#0
	SWI	XOS_ReleaseDeviceVector
	LDMFD	R13!,{R0-R12,PC}^

ioctimer1
	MOV	R0,#1<<6
	STRB	R0,[R3,#&14]
	LDR	R0,nicetimea
	LDR	R1,[R0]
	ADD	R1,R1,#1
	STR	R1,[R0]
	MOVS	PC,R14

nicetimea
	DCD	|nicetime|

	AREA	|C$$data|,DATA
	EXPORT	|nicetime|
|nicetime|
	DCD	0

	END

