

NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/nscutil
*--------------------------------------
NSC.IOBASE		.EQ $C300
*--------------------------------------
				.INB inc/macros.i
				.INB inc/io.i
				.INB inc/a2osx.i
				.INB inc/mli.e.i
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
DS121x.DATA0	.BS 8
DS121x.DATA1	.BS 8					Reverted YY MM DD Day HH mm SS CS
ZPPtr1			.BS 2
TmpByte			.BS 1
ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #0					S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data Segment Size
				.DA #16					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.MSG.READ.KO	.DA MSG.READ.KO
L.MSG.READ.OK1	.DA MSG.READ.OK1
L.MSG.READ.OK2.12	.DA MSG.READ.OK2.12
L.MSG.READ.OK2.24	.DA MSG.READ.OK2.24
L.MSG.READ.OK3	.DA MSG.READ.OK3
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.CRLF		.DA MSG.CRLF
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			ldy #S.PS.ARGC
				lda (pPS),y
				cmp #1
				bcs CS.RUN.SET

CS.RUN.READ		jsr NSC.Init
				jsr NSC.Read
				ldx #7

.1				lda DS121x.DATA0,x
				eor DS121x.DATA1,x
				bne .8

				dex
				bpl .1

				>PUSHW L.MSG.READ.KO
				>PUSHBI 0
				>SYSCALL PrintF
				lda #MLI.E.NODEV
				sec
				rts

.8				jsr NSC.Dump

				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL PrintF
				lda #0					tell TSKMGR that all done ok, but
				sec						we do not want to stay in memory
				rts

CS.RUN.SET		lda #1
				>SYSCALL ArgV
				>STYA ZPPtr1
				>SYSCALL StrLen
				cpy #17
				bne .9

				ldy #0
				stz DS121x.DATA1+7
				jsr GetBCD
				bcs .9

				sta DS121x.DATA1
				lda (ZPPtr1),y
				cmp #'/'
				bne .9

				iny

				jsr GetBCD
				bcs .9

				sta DS121x.DATA1+1

				lda (ZPPtr1),y
				cmp #'/'
				bne .9

				iny

				jsr GetBCD
				bcs .9

				sta DS121x.DATA1+2

				lda (ZPPtr1),y
				cmp #','
				bne .9

				iny

				jsr GetBCD
				bcs .9
				sta DS121x.DATA1+4

				lda (ZPPtr1),y
				cmp #':'
				bne .9

				iny

				jsr GetBCD
				bcs .9

				sta DS121x.DATA1+5

				lda (ZPPtr1),y
				cmp #':'
				bne .9

				iny

				jsr GetBCD
				bcs .9

				sta DS121x.DATA1+6
				stz DS121x.DATA1+7
				jsr NSC.Write

				lda #0					tell TSKMGR that all done ok, but
				sec						we do not want to stay in memory
				rts

.9				lda #E.SYN
				sec
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			clc
				rts
*--------------------------------------
NSC.Init		php
				sei

				lda IO.RDCXROM
				php

				sta IO.SETCXROM

				ldx #8

.1				ldy #8

.2				lda NSC.IOBASE+4
				lsr
				ror DS121x.DATA0-1,x
				dey
				bne .2

				dex
				bne .1

				plp
				bmi .8

				sta IO.CLRCXROM

.8				plp
				rts
*--------------------------------------
NSC.Read		php
				sei

				lda IO.RDCXROM
				php

				sta IO.SETCXROM

				jsr NSC.Select

				ldx #8					Read 8 bytes...

.1				ldy #8

.2				lda NSC.IOBASE+4
				lsr
				ror DS121x.DATA1-1,x
				dey
				bne .2

				dex
				bne .1

				plp
				bmi .8

				sta IO.CLRCXROM

.8				plp
				rts
*--------------------------------------
NSC.Write		php
				sei

				lda IO.RDCXROM
				php

				sta IO.SETCXROM

				jsr NSC.Select

				ldx #8					Write 8 bytes...

.3				lda DS121x.DATA1-1,x
				phx

				ldx #8					....of 8 bits

.4				ldy #0
				lsr
				bcc .5

				iny

.5				pha
				lda NSC.IOBASE,y		Write Pattern bit in A0, with A2=0
				pla
				dex
				bne .4

				plx
				dex
				bne .3

				plp
				bmi .8

				sta IO.CLRCXROM

.8				plp
				rts
*--------------------------------------
NSC.Select		sta NSC.IOBASE
*                lda $C00B               Workaround for Ultrawarp bug

				lda NSC.IOBASE+4		Reset DS1216E comparison register with READ A2=1

				ldy #8					Read 8 bytes...

.3				lda DS121x.SIG-1,y
				phy

				ldy #8					....of 8 bits

.4				ldx #0
				lsr
				bcc .5

				inx

.5				bit NSC.IOBASE,x		Write Pattern bit in A0, with A2=0

				dey
				bne .4

				ply
				dey
				bne .3

				rts
*--------------------------------------
NSC.Dump		>PUSHW L.MSG.READ.OK1
				ldx #1
				ldy #1
				lda DS121x.DATA1+3
				bit #%00010000
				beq .1

				dex
.1				bit #%00100000

				beq .2
				dey

.2				tya
				>PUSHA
				txa
				>PUSHA

				>PUSHBI 2
				>SYSCALL PrintF

				>LDYA L.MSG.READ.OK2.24
				bit DS121x.DATA1+4
				bpl .3

				>LDYA L.MSG.READ.OK2.12

.3				>SYSCALL PutS

				>PUSHW L.MSG.READ.OK3
				>PUSHBI 0
				>SYSCALL PrintF

				lda DS121x.DATA1
				jsr PrintBCD

				lda #'/'
				>SYSCALL PutChar

				lda DS121x.DATA1+1
				jsr PrintBCD

				lda #'/'
				>SYSCALL PutChar

				lda DS121x.DATA1+2
				jsr PrintBCD

				lda #','
				>SYSCALL PutChar

				lda DS121x.DATA1+4
				bpl .4
				and #$1F

.4				and #$3F
				jsr PrintBCD

				lda #':'
				>SYSCALL PutChar

				lda DS121x.DATA1+5
				jsr PrintBCD

				lda #':'
				>SYSCALL PutChar

				lda DS121x.DATA1+6
				jsr PrintBCD

				lda #'.'
				>SYSCALL PutChar
				lda DS121x.DATA1+7
				jsr PrintBCD

				>PUSHW L.MSG.CRLF
				>PUSHBI 0
				>SYSCALL PrintF
				rts
*--------------------------------------
PrintBCD		pha
				lsr
				lsr
				lsr
				lsr
				ora #$30
				>SYSCALL PutChar

				pla
				and #$0f
				ora #$30
				>SYSCALL PutChar
				rts
*--------------------------------------
GetBCD			lda (ZPPtr1),y
				iny
				jsr IsDigit
				bcs .9

				asl
				asl
				asl
				asl
				sta TmpByte
				lda (ZPPtr1),y
				iny
				jsr IsDigit
				bcs .9

				and #$0f
				ora TmpByte

.9				rts
*--------------------------------------
IsDigit			cmp #'0'
				bcc .9

				cmp #'9'+1
				and #$0f
				rts						cc if ok, cs if not

.9				sec
				rts
*--------------------------------------
CS.END
MSG.READ.KO		.CZ "DS1216E Not Detected.\r\n"
MSG.READ.OK1	.CZ "DS1216E Detected : /OSC=%d, /RST=%d, mode="
MSG.READ.OK2.12 .CZ "am/pm"
MSG.READ.OK2.24	.CZ "24h"
MSG.READ.OK3	.CZ "\r\nCurrent Date/Time : "
MSG.USAGE		.CS "\r\nTo Reset the chip and set clock, type:\r\nNSCUTIL YY/MM/DD,hh:mm:ss"
MSG.CRLF		.CZ "\r\n"
DS121x.SIG		.HS	5CA33AC55CA33AC5	Reverted 7->0
*--------------------------------------
DS.START
DS.END
*--------------------------------------
MAN
SAVE usr/src/bin/nscutil.s
ASM
