NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF sys/pm/pm.nsc
*--------------------------------------
				.INB inc/macros.i
				.INB inc/io.i
				.INB inc/monitor.i
				.INB inc/mli.i
*--------------------------------------
NSC.IOBASE		.EQ $C300
*--------------------------------------
TmpPtr1			.EQ	$0
TmpPtr2			.EQ	$2
offset			.EQ $4
*--------------------------------------
NSC.Init		>LDYAI NSC.MSG0
				jsr NSC.Print
				lda MACHID
				and #MACHID.CLK			Clock already installed ?
				beq .1

				>LDYAI NSC.MSG1
				bra NSC.Print

.1				jsr NSC.Detect
				bcc .2

				>LDYAI NSC.MSG2
				bra NSC.Print

.2				jsr NSC.Install
				lda #MACHID.CLK
				tsb MACHID

				>LDYAI NSC.MSG3
*				jmp NSC.Print
*--------------------------------------
NSC.Print		sty TmpPtr1
				sta TmpPtr1+1
				ldy #0

.1				lda (TmpPtr1),y
				beq .8

				jsr COUT
				iny
				bne .1

.8				jmp CROUT
*--------------------------------------
DS1216E.DATA1	.EQ $10
DS1216E.DATA2	.EQ $18
*--------------------------------------
NSC.Detect		php
				sei

				lda RDCXROM
				php
				
				sta SETCXROM

				sta NSC.IOBASE
*               lda $C00B               Workaround for Ultrawarp bug

				ldx #8

.1				ldy #8

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

				dex
				bne .1

				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 DS1216E.PATTERN-1,x
				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
*--------------------------------------
				ldx #8

.6				ldy #8

.7				lda NSC.IOBASE+4
				lsr
				ror DS1216E.DATA2-1,x
				dey
				bne .7

				dex
				bne .6

				plp
				bmi .8

				sta CLRCXROM

.8				plp
*--------------------------------------
				ldx #8

.81				lda DS1216E.DATA1-1,x
				cmp DS1216E.DATA2-1,x
				bne .9

				dex
				bne .81

*				sec						from CMP
				rts

.9				clc
				rts
*--------------------------------------
NSC.Install		lda DATETIME+1
				sta TmpPtr1
				sec
				sbc #NSCDRV.B.START
				sta offset
				lda DATETIME+2
				sta TmpPtr1+1
				sbc /NSCDRV.B.START
				sta offset+1

				ldy #1
				ldx #0

.1				lda NSCDRV.RELOC,x
				beq .2

				sta TmpPtr2
				inx
				lda NSCDRV.RELOC,x
				sta TmpPtr2+1
				lda (TmpPtr2)
				clc
				adc offset
				sta (TmpPtr2)
				lda (TmpPtr2),y
				adc offset+1
				sta (TmpPtr2),y
				inx
				bne .1

.2				lda RRAMWRAMBNK1
				lda RRAMWRAMBNK1
.3				ldy #NSCDRV.SIZE-1		always < 128

.7				lda NSCDRV.B.START,y
				sta (TmpPtr1),y
				dey
				bpl .7

				lda RROMBNK1
				lda #$4C
				sta DATETIME
				rts
*--------------------------------------
NSCDRV.RELOC	.DA NSCDRV.R1+1
*				.DA NSCDRV.R2+1
*				.DA NSCDRV.R3+1
				.DA NSCDRV.R4+1
				.DA NSCDRV.R5+1
				.DA NSCDRV.R6+1
				.DA NSCDRV.R7+1
				.DA NSCDRV.R8+1
*				.DA NSCDRV.R9+1
				.DA #0
*--------------------------------------
NSC.MSG0		.AZ -"NSC 'No-Slot-Clock'/DS1216E Driver For A2osX"
NSC.MSG1		.AZ -"Clock Already Present!"
NSC.MSG2		.AZ -"No DS1216E Detected!"
NSC.MSG3		.AZ -"DS1216E Driver Successfully Installed."
*--------------------------------------
* Driver For DS1216E
*--------------------------------------
DS1216E.DATA	.EQ $200				Reverted YY MM DD Day HH mm SS CS
*--------------------------------------
NSCDRV.B.START	.EQ *
NSCDRV			php
				sei

				lda RDCXROM
				php
				
				sta SETCXROM

				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...

NSCDRV.R1		lda DS1216E.PATTERN-1,y
				phy
				
				ldy #8					....of 8 bits

NSCDRV.2		ldx #0
				lsr
				bcc NSCDRV.3

				inx
				
NSCDRV.3		bit NSC.IOBASE,x		Write Pattern bit in A0, with A2=0

				dey
				bne NSCDRV.2

				ply
				dey
				bne NSCDRV.R1

				ldx #8

NSCDRV.4		ldy #8

NSCDRV.5		pha
				lda NSC.IOBASE+4		Read Byte...

				lsr
				pla
				ror

				dey
				bne NSCDRV.5

				pha
				lsr
				lsr
				lsr
				lsr
				tay
				pla
				and #$0F

				clc

				.HS	2C					Bit Absolute to skip adc #$0A

NSCDRV.6		adc #$0A
				dey
				bpl NSCDRV.6

NSCDRV.R4		sta DS1216E.DATA-1,x
				dex
				bne NSCDRV.4

				pha						DS1216E.DATA

NSCDRV.R7		lda	DS1216E.DATA+1		Get MM
				asl
				asl
				asl
				asl
				asl
NSCDRV.R8		ora	DS1216E.DATA+2		Get DD
				sta	DATELO

*NSCDRV.R9		lda	DS1216E.DATA		Get YY
				
				pla
				rol
				sta	DATELO+1

NSCDRV.R6		lda	DS1216E.DATA+5		Get mm
				sta	TIMELO

NSCDRV.R5		lda	DS1216E.DATA+4		Get HH
				sta	TIMELO+1

				plp
				bmi .8

				sta CLRCXROM

.8				plp
				rts
*--------------------------------------
DS1216E.PATTERN	.HS	5CA33AC55CA33AC5	Reverted 7->0
*--------------------------------------
				.LIST ON
NSCDRV.SIZE		.EQ *-NSCDRV.B.START
				.LIST OFF
*--------------------------------------
* CONTROL SECTION :
*--------------------------------------
				.DO NSCDRV.SIZE>125
				ERROR:NSCDRV.SIZE too big
				.FIN
*--------------------------------------
MAN
SAVE usr/src/sys/pm.nsc.s
ASM
