A2osX/SYS/KERNEL.S.IO.txt
2019-02-19 14:51:33 +00:00

569 lines
11 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

NEW
PREFIX
AUTO 4,1
*/--------------------------------------
* # open
* ## C
* `hFD open(const char *pathname, short int flags);`
* ## ASM
* **In:**
* `>PUSHB flags`
* `>LDYA pathname`
* `>SYSCALL open`
* ## RETURN VALUE
* A = hFD
* REG File created on ProDOS : T=TXT,X=$0000
*\--------------------------------------
K.Open.FLAGS .BS 1
K.Open.TYPE .BS 1
K.Open.AUXTYPE .BS 2
IO.hFD .BS 1
IO.Counter .BS 2
*--------------------------------------
K.Open jsr PFT.YAToMLIPATH
>PULLB K.Open.FLAGS
lda #S.FI.T.TXT
sta K.Open.TYPE
stz K.Open.AUXTYPE
stz K.Open.AUXTYPE+1
IO.Open.I ldx #5 $/DEV/
cpx K.MLI.PATH
bcs IO.OPEN.REG
.1 lda K.MLI.PATH,x
cmp IO.DEVDIR,x
bne IO.OPEN.REG
dex
bne .1
*--------------------------------------
* /DEV/xxxxxx (CDEV,BDEV,LNK,DSOCK,SSOCK,PIPE)
*--------------------------------------
>LDYAI K.MLI.PATH+6 skip $/DEV/
jsr K.GetDevByName
bcc .7
ldx #0
.10 lda Nod.Table.hPath,x
beq .6
jsr K.GetMemPtr
>STYA ZPPtr1
ldy #0
.2 lda (ZPPtr1),y
iny
cmp K.MLI.PATH,y
bne .6
cmp #0 end of string ?
bne .2
lda Nod.Table.hFD,x return hFD
clc
rts
.6 inx
cpx #K.NOD.MAX
bne .10
lda #MLI.E.NODEV
* sec
rts no OF found
.7 >STYA pFD
phx DEVID
lda (pFD) #S.FD.T
tax
pla hFILE
jmp (.8,x)
.8 .DA STDIO.IOERR REG
.DA STDIO.IOERR DIR
.DA IO.OPEN.CDEV
.DA IO.OPEN.BDEV
.DA STDIO.IOERR LNK
.DA IO.OPEN.DSOCK
.DA IO.OPEN.SSOCK
.DA IO.OPEN.PIPE
IO.OPEN.9 sec
IO.OPEN.RTS rts
*--------------------------------------
IO.OPEN.REG >MLICALL MLIGETFILEINFO
bcc IO.OPEN.REG.E Already Exists
bit K.Open.FLAGS Create if not exists ?
bpl IO.OPEN.9 No, return MLI error
ldx #2 Yes, Create...
.1 lda K.Open.TYPE,x
sta K.MLI.PARAMS+4,x File type,Aux type
dex
bpl .1
lda #S.FI.ST.STD
jsr IO.MLI.CREATE
bcs IO.OPEN.RTS
IO.OPEN.REG.E >LDYAI S.FD.REG
ldx #S.MEM.F.INIT0+S.MEM.F.FD
jsr MEM.GetMem.YAX
bcs IO.OPEN.RTS
>STYA pFD
stx .8+1 save hFD
* ldy #S.FD.T Done by GetMem0
* lda #S.FD.T.REG
* sta (pFD),y
jsr IO.MLIOPEN
bcs .9
lda K.Open.FLAGS
bit #O.WRONLY
beq .20 Write mode ?
and #O.APPEND Append ?
bne .11 yes, go to end of file
stz K.MLI.PARAMS+2
stz K.MLI.PARAMS+3
stz K.MLI.PARAMS+4
>MLICALL MLISETEOF no, reset size to 0
bra .21
.11 >MLICALL MLIGETEOF
bcs .9
>MLICALL MLISETMARK
.21 bcs .9
.20 lda K.Open.FLAGS
and #O.TEXT Text Mode ?
beq .8
lda #$FF
sta K.MLI.PARAMS+2
lda #C.CR
sta K.MLI.PARAMS+3
>MLICALL MLINEWLINE
bcs .9
.8 lda #$ff SELF MODIFIED
rts
.9 pha Save Error Code
lda .8+1
jsr K.Close
pla
sec
rts
*--------------------------------------
IO.OPEN.CDEV
IO.OPEN.BDEV sta .1+1 A=DEVID
pha
jsr K.IOCTL.GetPDrv
ldx #IOCTL.OPEN
pla pass A=DEVID To Driver
jsr K.IOCTL.pDrvJmp
bcs .9
.1 lda #$ff SELF MODIFIED
.9 rts
*--------------------------------------
IO.OPEN.DSOCK
*--------------------------------------
IO.OPEN.SSOCK
*--------------------------------------
IO.OPEN.PIPE
clc
rts
*--------------------------------------
IO.MLI.CREATE sta K.MLI.PARAMS+7 Storage Type
ldx #3
.1 lda DATELO,x
sta K.MLI.PARAMS+8,x Create Date/Time
dex
bpl .1
lda #S.FI.A.FULL
sta K.MLI.PARAMS+3 Access
>MLICALL MLICREATE
rts
*/--------------------------------------
* # close
* ## C
* `int close(hFD fd);`
* ## ASM
* **In:**
* `lda fd`
* `>SYSCALL close`
*\--------------------------------------
K.Close sta IO.hFD
jsr K.GetMemPtr
>STYA pFD
IO.Close.I lda (pFD) #S.FD.T
tax
jmp (.1,x)
.1 .DA IO.CLOSE.REG
.DA IO.CLOSE.DIR
.DA IO.CLOSE.CDEV
.DA IO.CLOSE.BDEV
.DA STDIO.IOERR LNK
.DA IO.CLOSE.DSOCK
.DA IO.CLOSE.SSOCK
.DA IO.CLOSE.PIPE
*--------------------------------------
IO.CLOSE.REG
IO.CLOSE.DIR ldy #S.FD.REG.REF
lda (pFD),y
beq .1
sta K.MLI.PARAMS+1
>MLICALL MLICLOSE
.1 ldy #S.FD.REG.IOBUF
bra IO.CLOSE.FD
*--------------------------------------
IO.CLOSE.CDEV
IO.CLOSE.BDEV
*--------------------------------------
IO.CLOSE.DSOCK
IO.CLOSE.SSOCK clc
rts
*--------------------------------------
IO.CLOSE.PIPE jsr IO.CLOSE.NOD
ldy #S.FD.PIPE.hMem
IO.CLOSE.FD lda (pFD),y
beq .1
jsr K.FreeMem
.1 lda IO.hFD
jmp K.FreeMem
*--------------------------------------
IO.CLOSE.NOD ldx #0
lda IO.hFD
.1 lda Nod.Table.hFD,x
cmp IO.hFD
bne .2
stz Nod.Table.hFD,x
lda Nod.Table.hPath,x
stz Nod.Table.hPath,x
jmp K.FreeMem
.2 inx
cpx #K.Nod.MAX
bne .1
rts
*/--------------------------------------
* # read
* ## C
* `int read(hFD fd, void *buf, int count);`
* ## ASM
* **In:**
* `>PUSHWI count`
* `>PUSHW buf`
* `lda fd`
* `>SYSCALL read`
* ## RETURN VALUE
* CC: Y,A = bytes read
* CS: A = EC
*\--------------------------------------
K.Read sta IO.hFD
jsr K.GetMemPtr
>STYA pFD
IO.Read.I lda (pFD) #S.FD.T
tax
jmp (.1,x)
.1 .DA IO.READ.REG
.DA STDIO.IOERR DIR
.DA IO.READ.CDEV
.DA STDIO.IOERR BDEV
.DA STDIO.IOERR LNK
.DA STDIO.IOERR DSOCK
.DA IO.READ.SSOCK
.DA IO.READ.PIPE
*/--------------------------------------
* # write
* ## C
* `int write(hFD fd, const void *buf, int count);`
* ## ASM
* **In:**
* `>PUSHWI count`
* `>PUSHW buf`
* `lda fd`
* `>SYSCALL write`
* ## RETURN VALUE
* CC: Y,A = bytes written
* CS: A = EC
*\--------------------------------------
K.Write sta IO.hFD
jsr K.GetMemPtr
>STYA pFD
IO.Write.I lda (pFD) #S.FD.T
tax
jmp (.1,x)
.1 .DA IO.WRITE.REG
.DA STDIO.IOERR DIR
.DA IO.WRITE.CDEV
.DA STDIO.IOERR BDEV
.DA STDIO.IOERR LNK
.DA STDIO.IOERR DSOCK
.DA IO.WRITE.SSOCK
.DA IO.WRITE.PIPE
*--------------------------------------
IO.READ.REG ldx #MLIREAD
.HS 2C BIT ABS
*--------------------------------------
IO.WRITE.REG ldx #MLIWRITE
ldy #S.FD.REG.REF
lda (pFD),y
sta K.MLI.PARAMS+1
>PULLW K.MLI.PARAMS+2
>PULLW K.MLI.PARAMS+4
lda #4 Param Count = 4 for MLIREAD & MLIWRITE
jsr GP.MLICall
bcs .9
>LDYA K.MLI.PARAMS+6
.9 rts
*--------------------------------------
IO.READ.CDEV ldx #IOCTL.READ
.HS 2C BIT ABS
*--------------------------------------
IO.WRITE.CDEV ldx #IOCTL.WRITE
>PULLW K.S.IOCTL+S.IOCTL.BUFPTR
>PULLW K.S.IOCTL+S.IOCTL.BYTECNT
jsr K.IOCTL.GetPDRV
>LDYAI K.S.IOCTL
.1 jsr K.IOCTL.pDrvJmp
bcs .9
>LDYA K.S.IOCTL+S.IOCTL.BYTECNT
.9 rts
*--------------------------------------
IO.WRITE.SSOCK ldy #S.FD.SSOCK.WRITE
.HS 2C BIT ABS
*--------------------------------------
IO.READ.SSOCK ldy #S.FD.SSOCK.READ
lda (pFD),y
tax Function Offset in LIB
ldy #S.FD.SSOCK.IOHANDLER
lda (pFD),y
jsr K.GetMemPtr
>STYA .1+1
lda IO.hFD
.1 jmp $FFFF SELF MODIFIED
*--------------------------------------
IO.READ.PIPE >PULLW .3+1 S.IOCTL.BUFPTR
>PULLA S.IOCTL.BYTECNT.LO
eor #$ff
sta IO.Counter
>PULLA S.IOCTL.BYTECNT.HI
eor #$ff
sta IO.Counter+1
ldy #S.FD.PIPE.S
lda (pFD),y
beq .99 Remote PS closed the Pipe
ldy #S.FD.PIPE.hMem
lda (pFD),y
jsr K.GetMemPtr
>STYA .2+1
ldx #0
.1 inc IO.Counter
bne .11
inc IO.Counter+1
beq .8
.11 ldy #S.FD.PIPE.Tail
lda (pFD),y
iny #S.FD.PIPE.Head
cmp (pFD),y
beq .9 PIPE is empty
pha
inc
dey #S.FD.PIPE.Tail
sta (pFD),y
ply
.2 lda $ffff,y SELF MODIFIED
.3 sta $ffff,x SELF MODIFIED
inx
bne .1
.8 txa
tay
lda #0 Y,A = bytes read
clc
rts
.9 lda #0
sec
rts
.99 lda #MLI.E.EOF
sec
rts
*--------------------------------------
IO.WRITE.PIPE >PULLW .2+1 S.IOCTL.BUFPTR
>PULLA S.IOCTL.BYTECNT.LO
eor #$ff
sta IO.Counter
>PULLA S.IOCTL.BYTECNT.HI
eor #$ff
sta IO.Counter+1
ldy #S.FD.PIPE.S
lda (pFD),y
beq .99 Remote PS closed the Pipe
ldy #S.FD.PIPE.hMem
lda (pFD),y
jsr K.GetMemPtr
>STYA .3+1
ldx #0
.1 inc IO.Counter
bne .11
inc IO.Counter+1
beq .8
.11 ldy #S.FD.PIPE.Head
lda (pFD),y
inc
dey #S.FD.PIPE.Tail
cmp (pFD),y
beq .9 PIPE is full
iny #S.FD.PIPE.Head
sta (pFD),y
dec
tay
.2 lda $ffff,x SELF MODIFIED
.3 sta $ffff,y SELF MODIFIED
inx
bne .1
.8 txa
tay
lda #0 Y,A = bytes written
clc
rts
.9 lda #MLI.E.VOLFULL
sec
rts
.99 lda #MLI.E.EOF
sec
rts
*/--------------------------------------
* # IOCTL
* ## C
* `int ioctl(short int DevID, int request, void * param );`
* ## ASM
* `PUSHWI param`
* `PUSHBI request`
* `lda hDEV`
* `>SYSCALL IOCTL`
* ## RETURN VALUE
* Y,A = ...
*\--------------------------------------
K.IOCTL tax
lda Dev.Table+1,x
beq K.IOCTL.9
sta pFD+1
lda Dev.Table,x
sta pFD
jsr K.IOCTL.GetPDrv
>PULLA
tax request
>PULLYA param
K.IOCTL.pDrvJmp jmp (pDrv)
K.IOCTL.9 sec
lda #MLI.E.NODEV
>RET 3
*--------------------------------------
K.IOCTL.GetPDrv ldy #S.FD.DEV.DRVPTR
lda (pFD),y
sta pDRV
iny
lda (pFD),y
sta pDRV+1
rts
*--------------------------------------
IO.MLIOPEN >LDYAI 1024 get a ProDOS IOBUF
ldx #S.MEM.F.ALIGN+S.MEM.F.NOMOVE
jsr MEM.GetMem.YAX
bcs .9
>STYA K.MLI.PARAMS+3 Save Ptr to IOBUF for MLIOPEN call
txa
ldy #S.FD.REG.IOBUF
sta (pFD),y
>MLICALL MLIOPEN
bcs .9
lda K.MLI.PARAMS+5 get ref_num
ldy #S.FD.REG.REF
sta (pFD),y
sta K.MLI.PARAMS+1 Next MLI Calls are REF_NUM based
* clc
.9 rts
*--------------------------------------
IO.DEVDIR >PSTR "/DEV/"
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.IO
LOAD USR/SRC/SYS/KERNEL.S
ASM