;  SMARTWATCH clock driver
;
;  R.Frantz 4/3/87
;
WRTC		equ $808010
RDRTC		equ WRTC+8
Pattern		equ $5CA33AC5
low_time	equ $01234500	; fake up for rtc
hi_time		equ $87061901	; my birthday


_OpenLibrary	EQU	-6*68
_CloseLibrary	EQU	-6*69

_Write		EQU	-6*8
_Output		EQU	-6*10
_Execute	EQU	-6*37
;
; Call AmigaDOS
;
	MOVEM   d2/d3,-(sp)
	LEA	DosName(PC),a1	; Name of the DOS
	MOVEQ	#0,d0		
	MOVEA.L	4,a6		; Address of EXEC
	JSR	_OpenLibrary(A6)
	MOVEA.L	d0,a5
	TST.L	d0		
	BNE	GetTime		;OK, read the RTC
; Something is wrong with the AMIGA , BOMB the system !
	DC.W	$4AFC		
;
; Here is the code that is HARDWARE dependant.
;
GetTime:
	BSR ReadRTC
;FAKE: 	MOVE.L low_time,d2
;	MOVE.L hi_time,d3
	BSR Decode

SetTime:
;
; Call AmigaDOS Execute entry
	MOVE.L	#ExecMe,d1
	MOVEQ	#0,d2		; NIL for Input()
	MOVEQ	#0,d3		; NIL for Output()
	JSR	_Execute(A5)	; Call the DOS
	MOVE.L	a5,a1
	JSR	_CloseLibrary(A6)
; Exit program
	MOVEQ	#0,d0
	MOVEM   (sp)+,d2/d3
	RTS

; Various string constants
Months	DC.B	'-jan-feb-mar-apr-may-jun-jul-aug-sep-oct-nov-dec'
ExecMe	DC.B	'date 00:00:00 00-jst-00',0
DosName	DC.B	'dos.library',0
;
; Subroutines
;

Decode:
	MOVE.L 	d2,d0		;get the low data
	ROR.L	#8,d0		;shitf 1 byte right to get secs.
	MOVE.l	#ExecMe+$C,a0	;location in ExecMe string
	BSR 	ASCII		;convert to ascii
	ROR.L	#4,d0		;10 sec
	MOVE.L  #ExecMe+$B,a0
	BSR	ASCII		set 10 sec
; Next get the min.
	ROR.L	#4,d0		;min
	move.l 	#ExecMe+$9,a0
	BSR	ASCII		;set min
	ROR.L	#4,d0		;10 min
	MOVE.L	#ExecMe+$8,a0
	BSR	ASCII		;set 10 min
; Get the Hr.
	ROR.L	#4,d0		;hr
	MOVE.L  #ExecMe+$6,a0
	BSR	ASCII		;set hr
	ROR.L	#4,d0		;10 hr
	MOVE.L  #ExecMe+$5,a0	
	BSR	ASCII		;set 10 hr
	MOVE.L	d3,d0		;get the high data
; Get the Date
	ROR.L	#8,d0		;get the date
	MOVE.L 	#ExecMe+$F,a0	
	BSR	ASCII		;set the date
	ROR.L	#4,d0		;10 date
	MOVE.L 	#ExecMe+$E,a0
	BSR	ASCII		;set 10 date
; The month has to be converted into words
	ROR.L 	#4,d0		;get the month
	MOVE.B	d0,d1
	AND.L	#$10,d1		;check 10s of months
	BNE	AddMonth	;add 10 to month
	MOVE.B	d0,d1
	BRA	AddMonth1	;goto 
AddMonth:
	MOVE.L 	d0,d1
 	ADD.B	#$A,d1		;add 10 months to the count		
AddMonth1:
	AND.L	#$F,d1		;mask all upper bits
	SUBQ.L  #1,d1		month now 0-11
	ASL.L	#2,d1		;multiply by 4
	AND.L	#$3F,d1		;set a limit
	MOVE.L	d1,a0		
	ADD.L #Months,a0		;set the offset
	MOVE.L (a0),d1	;get the name for the month
	MOVE.L #ExecMe+$10,a0	
	MOVE.L d1,(a0)		;set the month	
; Now the Year
	ROR.L	#8,d0		;get year
	MOVE.L	#ExecMe+$16,a0
	BSR	ASCII		;set yr
	ROR.L 	#4,d0		;get 10s yr
	MOVE.L 	#ExecMe+$15,a0
	BSR	ASCII		;set 10 yr
	RTS			;done	

ASCII:	MOVE.B	d0,d1
	AND.B	#$F,d1		;mask upper nibble
	OR.B 	#$30,d1		;add $30 for ascii
	MOVE.B	d1,(a0)		;store in "date" cmd
	RTS			;done
	
	
ReadRTC:
	MOVE.B  RDRTC,d0	;Claer Clock
	MOVE.L 	#Pattern,d0	;get the wake up pattern
	BSR 	SND32		;send it
	MOVE.L 	#Pattern,d0	;send it again
	BSR 	SND32
	BSR 	RD32		;get the first 4 bytes
	MOVE.L 	d0,d2		;store it
	BSR 	RD32		;get the rest of the data
	MOVE.L 	d0,d3		;store it
	RTS			;done

SetRTC:
	MOVE.B  RDRTC,d0	;Clear Clock
	MOVE.L 	#Pattern,d0	;get the wake up pattern
	BSR	 SND32		;send it
	MOVE.L 	#Pattern,d0	;send it again
	BSR	 SND32
	MOVE.L 	d2,d0		;get the first 4 bytes
	BSR	 SND32		;send it to the clock
	MOVE.L 	d3,d0		;get the rest of the data
	BSR	 SND32		;send it to the clock
	RTS			;done

SND32:	
	MOVE.L 	#32,d4	;load loop counter with 32
S1:
	BTST.L 	#0,d0	;is the bit = zero ?
	BNE 	S2	;send a "one"
	MOVE.B 	WRTC,d1	;send a "zero"
S3:
	ASR.L 	#1,d0	;get the next bit
	SUBQ.L 	#1,d4	;dec the count
	BNE 	S1	;send the next bit
	RTS		;done
S2:	
	MOVE.B 	WRTC+2,d1	
	BRA.S 	S3	;send the next bit

RD32:	MOVE.L 	#0,d0	;clr d0
	MOVE.L 	#32,d4	;set the counter to 32
R1:
	MOVE.B 	RDRTC,d1	;get a bit from the smart watch
	AND.L 	#1,d1	;mask all other bits
	OR.L 	d1,d0	;place it in d0
	ROR.L   #1,d0	;Rotate (d0 -> d31) 
	SUBQ.L 	#1,d4	;dec the counter
	BNE 	R1	;get the next bit
	RTS		;done

	END
