; UpdateTimer.asm -- O.S. code for updating timers
; 			P. Deutsch  31-DEC-74
; Copyright Xerox Corporation 1979
; last modified December 7, 1978  10:58 PM by Taft

; By extreme good fortune, this code also works on the Alto II,
; even though the details of the clock differ a bit.

.ent UpdateTimer, ClockSecond

	.srel

UpdateTimer:	.UpdateTimer
ClockSecond:	SECHI

	.nrel

; Move excess time from hardware clock to calendar.
; Must be called at least once per second,
; preferably from vertical field interrupt.
;
;	JSRII [ UpdateTimer ]
;	 0		(not read)
;	..returns here.. AC2 preserved

.UpdateTimer:
	sta 3 ret
	rclk
	lda 1 @.RTB0
	sub 1 0		; Time since base
	lda 3 SECHI
	adcz# 3 0 snc
	 jmp exit	; Hasn't reached 1 second yet
	add 3 1
	sta 1 @.RTB0	; Update base by 1 second
	lda 3 .RTB0	; Get pointer for displacement addressing
	lda 1 RTB1-b 3
	lda 0 SECLO
	addz 0 1 szc	; 32-bit add
	 isz RTB0-b 3
	 nop
	sta 1 RTB1-b 3
	lda 1 CMIL1-b 3	; Update ms calendar by 1000
	lda 0 THOU
	addz 0 1 szc	; 32+16 bit add
	 isz CMIL0-b 3
	 nop
	sta 1 CMIL1-b 3
	isz CSEC1-b 3	; Update seconds calendar by 1
	 jmp exit
	isz CSEC0-b 3
	 nop
exit:	lda 3 ret
	jmp 1 3

ret:	.blk 1

; Addresses of reserved page 1 locations
CSEC0 = 572	; Seconds calendar
CSEC1 = 573
CMIL0 = 574	; Milliseconds calendar
CMIL1 = 575
RTB0 = 576	; Base for RTC
RTB1 = 577

b = RTB0	; Base for displacement addressing
.RTB0:	RTB0

; Funny constants
SECHI:	25.	; 1 second in units of .595 microseconds
SECLO:	41600.

THOU:	1000.

.END