NEW
  AUTO 3,1
*/--------------------------------------
* # GetDevByName
*  Y,A = Ptr to device name (C-String)
* ## RETURN VALUE
* CC = OK, CS = ERROR
* Y,A = FD
* X = hFD
*\--------------------------------------
K.GetDevByName	>STYA ZPPtr1			requested dev name

				ldx #1

.1				ldy FDs.pHI-1,x
				beq .7

				cpy #$C0
				bcc .7

				stx IO.hFD
				
				sty pFD+1
				lda FDs.pLO-1,x
				sta pFD

				clc
				adc #S.FD.DEV
				sta ZPPtr2

				tya
				adc /S.FD.DEV
				sta ZPPtr2+1

				ldy #$ff

.4				iny
				lda (ZPPtr1),y
				cmp (ZPPtr2),y
				bne .7

				eor #0
				bne .4

				>LDYA pFD
				ldx IO.hFD
				clc
				rts

.7				inx
				cpx #K.FD.MAX+1
				bcc .1

				lda #MLI.E.NODEV
*				sec

K.GetDev.9		rts
*/--------------------------------------
* # MkFD
* ## C
* `short int mkfd(short int type, );`
* ## ASM
* `>PUSHB DevID`
* `>PUSHW S.DIB`
* `>SYSCALL MkFD`
* ## RETURN VALUE
*\--------------------------------------


*				clc
*				rts
*/--------------------------------------
* # MKDev
* Create a hDEV
* ## C
* `hDEV mkdev (S.FD *fd, const char *devname)`
* ## ASM
* `>PUSHW fd`
* `>PUSHW devname`
* `>SYSCALL mkdev`
* ## RETURN VALUE
*  A = hDEV
*\--------------------------------------
K.MKDev			jsr SHARED.PullP2P1		ptr2=devname,ptr1=fd

				jsr DEV.GetFreeFD
				bcs .9
				
				stx IO.hFD

				ldy #$ff
				ldx #S.FD.DEV

.1				iny						Get DEV name length
				inx						compute FD+NAME+\0 in X
				lda (ZPPtr2),y
				bne .1

				txa
				tay
				lda #0

				jsr MEM.GetKBuf
				bcs .9

				>STYA pFD
				>STYA .4+1
				
				ldx IO.hFD
				sta FDs.pHI-1,x
				tya
				sta FDs.pLO-1,x

				ldy #S.FD.DEV-1

.2				lda (ZPPtr1),y
				sta (pFD),y
				dey
				bpl .2

				ldy #$ff
				ldx #S.FD.DEV-1

.3				iny
				inx
				lda (ZPPtr2),y

.4				sta $ffff,x
				bne .3

				>LDYAI K.IOBuf
				>STYA K.S.IOCTL+S.IOCTL.BUFPTR

				ldy #S.FD.DEV.DRVPTR+1
				lda (pFD),y
				bne .5
				
				lda /DRV.SP
				sta (pFD),y
				dey
				lda #DRV.SP
				sta (pFD),y

.5				jsr DEV.GetPDrv

				lda #S.IOCTL.S.STATUS
				jsr DEV.GetStatus
				bcs .9

				lda K.IOBuf				Get Status byte
				bit #S.DIB.S.IRQ
				beq .8

				ldx #0

.6				lda	IRQ.Vectors+1,x
				beq .7

				inx
				inx
				cpx #K.IRQDEV.MAX*2
				bcc .6

				lda #E.OOH

*				sec

.9				rts

.7				ldy #IOCTL.IRQ+4
				lda (pDRV),y
				sta IRQ.Vectors,x
				iny
				lda (pDRV),y
				sta IRQ.Vectors+1,x

.8				lda IO.hFD

*				clc

				rts
*/--------------------------------------
* # IOCTL
* ## C
* `int ioctl(short int hFD, short int request, void *param);`
* ## ASM
* `>PUSHB hDEV`
* `>PUSHB request`
* `>PUSHW param`
* `>SYSCALL IOCTL`
* ## RETURN VALUE
*  Y,A = ...
*\--------------------------------------
K.IOCTL			ldy #3					hFD
				lda (pStack),y

				jsr DEV.GetPFD

				ldy #2					request
				lda (pStack),y
				tax

				>PULLYA					param
				inc pStack
				inc pStack

				jmp (pDrv)
*--------------------------------------
DEV.OPEN		ldx #IOCTL.OPEN
				lda IO.hFD
				jsr DEV.pDrvJmp.1
				bcs .9

				lda IO.hFD

.9				rts
*--------------------------------------
DEV.READ		ldx #IOCTL.READ
				.HS 2C					BIT ABS
*--------------------------------------
DEV.WRITE		ldx #IOCTL.WRITE

				>PULLW K.S.IOCTL+S.IOCTL.BYTECNT
				>PULLW K.S.IOCTL+S.IOCTL.BUFPTR

				inc pStack				pop hFD

				jsr DEV.pDrvJmp
				bcs .9

				>LDYA K.S.IOCTL+S.IOCTL.BYTECNT

.9				rts
*--------------------------------------
DEV.EOF			lda #S.IOCTL.S.EOF
*--------------------------------------
DEV.GetStatus	sta K.S.IOCTL+S.IOCTL.S

				ldx #IOCTL.STATUS
				.HS 2C					BIT ABS
*--------------------------------------
DEV.CLOSE		ldx #IOCTL.CLOSE
*--------------------------------------
DEV.pDrvJmp		>LDYAI K.S.IOCTL
DEV.pDrvJmp.1	jmp (pDrv)
*--------------------------------------
* For REG, DIR, PIPE
*--------------------------------------
DEV.MkFD		sta .8+1				A = S.FD.T

				jsr DEV.GetFreeFD
				bcs .9

				stx IO.hFD

				ldy #16					Y = Size, MAX=16 for S.FD.DIR
				lda #0
				ldx #S.MEM.F.INIT0+S.MEM.F.FD
				jsr MEM.GetMem.YAX
				bcs .9

				>STYA pFD
				ldx IO.hFD
				sta FDs.pHI-1,x
				tya
				sta FDs.pLO-1,x
				
				jsr Mem.SetOwnerPS0
				
.8				lda	#$ff				SELF MODIFIED
				sta (pFD)

*				clc

.9				rts
*--------------------------------------
DEV.GetFreeFD	ldx #1

				clc

.1				lda FDs.pHI-1,x
				beq .8
				
				inx
				cpx #K.FD.MAX+1
				bcc .1
				
				lda #E.OOH

*				sec

.8				rts
*--------------------------------------
DEV.GetPFD		sta IO.hFD
				tay
				
				lda FDs.pLO-1,y
				sta pFD
				lda FDs.pHI-1,y
				sta pFD+1

DEV.GetPDrv		ldy #S.FD.DEV.DRVPTR
				lda (pFD),y
				sta pDRV
				iny
				lda (pFD),y
				sta pDRV+1

				rts
*--------------------------------------
MAN
SAVE usr/src/sys/kernel.s.dev
LOAD usr/src/sys/kernel.s
ASM
