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

				ldx #1

.1				lda Dev.Table,x
				beq .4

				sta pFD+1
				pha

				lda Dev.Table-1,x
				sta pFD

				clc
				adc #S.FD.DEV
				tay

				pla

				adc /S.FD.DEV

				jsr .8
				bcs .3

				jsr DEV.GetPDrv

				>LDYA pFD				X = hDevNod

*				clc
				rts

.3				inx						no....loop
				inx
				cpx #K.DEV.MAX*2+1
				bne .1

.4				ldx #2

.5				lda Nod.Table.hFD-2,x
				beq .7

				lda Nod.Table.hName-2,x
				beq .7
				jsr K.GetMemPtr

				jsr .8
				bcs .7

				lda Nod.Table.hFD-2,x
				jmp K.GetMemPtr			Y,A = pPD, X = hDevNod

.7				inx
				inx
				cpx #K.NOD.MAX*2+2
				bne .5

				lda #MLI.E.NODEV
*				sec
				rts
*--------------------------------------
.8				>STYA ZPPtr2

				ldy #$ff

.80				iny
				lda (ZPPtr1),y
				cmp (ZPPtr2),y
				bne .89

				eor #0
				bne .80

				clc
				rts

.89				sec

K.GetDev.9		rts
*/--------------------------------------
* # GetDevStatus
* ## C
* `int getdevstatus(short int DevID, S.DIB *dstat);`
* ## ASM
* `>PUSHB DevID`
* `>PUSHW S.DIB`
* `>SYSCALL GetDevStatus`
* ## RETURN VALUE
*\--------------------------------------
K.GetDevStatus	>PULLW K.S.IOCTL+S.IOCTL.BUFPTR
				>PULLA

				jsr DEV.GetPFD
				bcs K.GetDev.9

				lda #S.IOCTL.S.GETDIB
				.HS 2C					BIT ABS
*--------------------------------------
DEV.EOF			lda #S.IOCTL.S.EOF
*--------------------------------------
DEV.GetStatus	sta K.S.IOCTL+S.IOCTL.S

				ldy #S.FD.DEV.DEVID
				lda (pFD),y
				sta K.S.IOCTL+S.IOCTL.UNITNUM

				>LDYAI K.S.IOCTL
				ldx #IOCTL.STATUS
*--------------------------------------
DEV.pDrvJmp		jmp (pDrv)
*--------------------------------------
DEV.OPEN		ldx #IOCTL.OPEN
				lda IO.hDevNod
				jsr DEV.pDrvJmp
				bcs .9

				lda IO.hDevNod

.9				rts
*--------------------------------------
DEV.CLOSE		ldx #IOCTL.CLOSE
				lda IO.hDevNod
				jmp (pDrv)
*--------------------------------------
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

				>LDYAI K.S.IOCTL
				jsr DEV.pDrvJmp
				bcs .9

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

.9				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=name,tr1=fd

				ldx #1

.10				lda Dev.Table,x
				beq .11

				inx
				inx
				cpx #K.DEV.MAX*2+1
				bne .10

				bra .9

.11				stx IO.hDevNod

				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.GetKrnlBuf
				bcs .99

				>STYA pFD
				>STYA .4+1

				ldx IO.hDevNod
				sta Dev.Table,x
				tya
				sta Dev.Table-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

				jsr DEV.GetPDrv

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

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

				ldx #0

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

				inx
				inx
				cpx #K.IRQDEV.MAX*2+1
				bne .5

.9				lda #E.OOH
*				sec
.99				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.hDevNod
*				clc
				rts
*/--------------------------------------
* # IOCTL
* ## C
* `int ioctl(short int DevID, short int request, void *param);`
* ## ASM
* `>PUSHB hDEV`
* `>PUSHB request`
* `>PUSHW param`
* `>SYSCALL IOCTL`
* ## RETURN VALUE
*  Y,A = ...
*\--------------------------------------
K.IOCTL			ldy #3					hDev
				lda (pStack),y

				jsr DEV.GetPFD
				bcs .9

				ldy #2					request
				lda (pStack),y
				tax

				>PULLYA					param
				inc pStack
				inc pStack

				jmp (pDrv)

.9				>RET 4
*--------------------------------------
DEV.GetPFD		sta IO.hDevNod

				tax

				lsr
				bcs .1

				lda Nod.Table.hFD-2,x			empty slot ?
				beq DEV.GetPFD.9

				sta IO.hFD

				jsr K.GetMemPtr
				>STYA pFD

*				clc
				rts

.1				stz IO.hFD

				ldy Dev.Table-1,x
				lda Dev.Table,x
				beq DEV.GetPFD.9

				>STYA pFD

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

				clc
				rts

DEV.GetPFD.9	lda #E.INVH
				sec
				rts
*--------------------------------------
* X = 0 > REG
* X = 1 > DIR
* X = 2 > PIPE
*--------------------------------------
DEV.MkFD		stx .8+1

				ldy DEV.MkFD.Y,x
				lda #0
				ldx #S.MEM.F.INIT0+S.MEM.F.FD
				jsr MEM.GetMem.YAX
				bcs .9

				>STYA pFD
				stx IO.hFD

.8				ldx #$ff				SELF MODIFIED
				lda DEV.MkFD.T,x
				sta (pFD)				X = hFD, A = T

*				clc
.9				rts

DEV.MkFD.Y		.DA #S.FD.REG,#S.FD.DIR,#S.FD.PIPE
DEV.MkFD.T		.DA #S.FD.T.REG,#S.FD.T.DIR,#S.FD.T.PIPE
*--------------------------------------
DEV.Destroy		ldx IO.hDevNod
				lda Nod.Table.hName-2,x
				beq .1

				stz Nod.Table.hName-2,x
				jsr K.FreeMem

				ldx IO.hDevNod

.1				lda Nod.Table.hFD-2,x
				stz Nod.Table.hFD-2,x
				jmp K.FreeMem
*--------------------------------------
MAN
SAVE usr/src/sys/kernel.s.dev
LOAD usr/src/sys/kernel.s
ASM
