A2osX/SYS/KERNEL.S.STDLIB.txt

602 lines
10 KiB
Plaintext
Raw Permalink Normal View History

2017-12-22 21:24:30 +00:00
NEW
2019-05-25 19:24:07 +00:00
AUTO 3,1
*/--------------------------------------
2020-03-11 16:41:45 +00:00
* # StrToF
* Convert String to 40 bits Float
2018-10-04 15:30:14 +00:00
* ## C
2020-08-02 12:19:43 +00:00
* `float strtof (const char* str, char** endptr );`
2018-10-04 15:30:14 +00:00
* ## ASM
2020-03-11 16:41:45 +00:00
* `>PUSHW str`
2018-06-14 15:31:36 +00:00
* `>PUSHWI EndPtr`
2020-03-11 16:41:45 +00:00
* `>SYSCALL StrToF`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
2018-06-14 15:31:36 +00:00
* On stack (float)
*\--------------------------------------
2020-03-11 16:41:45 +00:00
K.StrToF >PULLW ZPPtr1
>PULLYA
2019-07-10 15:39:02 +00:00
jsr K.AToF
2019-02-07 16:52:25 +00:00
lda TXTPTR
2018-06-15 15:15:48 +00:00
sta (ZPPtr1)
ldy #1
lda TXTPTR+1
2018-06-15 15:15:48 +00:00
sta (ZPPtr1),y
2019-10-03 06:25:27 +00:00
rts
*/--------------------------------------
2018-06-18 06:22:50 +00:00
* # AToF
* Convert String to 40 bits Float
2018-06-18 06:22:50 +00:00
* ## C
2020-08-02 12:19:43 +00:00
* `float atof ( const char* str );`
2018-06-18 06:22:50 +00:00
* ## ASM
2018-06-14 15:31:36 +00:00
* `>LDYA str`
2018-06-18 15:48:00 +00:00
* `>SYSCALL atof`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
2018-06-14 15:31:36 +00:00
* On stack (float)
*\--------------------------------------
2018-06-15 15:15:48 +00:00
K.AToF >STYA TXTPTR Ptr to source string
2019-09-02 15:34:10 +00:00
lda pStack
sec
sbc #5
sta pStack
2019-07-10 15:39:02 +00:00
jsr CHARGOT
2019-07-10 15:39:02 +00:00
ldx #FPU.FIN
2021-05-04 17:31:21 +00:00
jmp GP.GetFac
*/--------------------------------------
2018-06-18 06:22:50 +00:00
* # StrToL/StrToUL
2018-06-14 15:31:36 +00:00
* Convert String to 32 bits (unsigned) int
2018-06-18 06:22:50 +00:00
* ## C
2018-06-14 15:31:36 +00:00
* `long strtol (const char* str, char** endptr, int base);`
2018-06-15 15:15:48 +00:00
* `unsigned long strtoul (const char* str, char** endptr, int base);`
2018-06-18 06:22:50 +00:00
* ## ASM
2020-02-14 16:32:52 +00:00
* `>PUSHW str`
* `>PUSHW EndPtr`
2018-06-14 15:31:36 +00:00
* `>PUSHB Base`
2020-03-11 16:41:45 +00:00
* `>SYSCALL StrToL`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
2018-06-14 15:31:36 +00:00
* On stack (long)
*\--------------------------------------
K.StrToL sec Signed
.HS 90 BCC
K.StrToUL clc Unsigned
2020-02-14 16:32:52 +00:00
>PULLA Base
2020-02-14 16:32:52 +00:00
>PULLW ZPPtr1 EndPtr
>PULLW ZPPtr2 str
jsr K.AToL.I
bcs K.StrToUL.rts
* clc
2018-12-21 14:32:45 +00:00
adc ZPPtr2
2018-06-15 15:15:48 +00:00
sta (ZPPtr1)
lda #0
adc ZPPtr2+1
ldy #1
2018-06-15 15:15:48 +00:00
sta (ZPPtr1),y
2019-10-10 06:04:11 +00:00
K.StrToUL.rts rts
*/--------------------------------------
2018-06-18 15:48:00 +00:00
* # atol
* Convert String to 32 bits long
2018-06-18 06:22:50 +00:00
* ## C
2018-06-15 15:15:48 +00:00
* `long atol ( const char * str );`
2018-06-18 06:22:50 +00:00
* ## ASM
2018-06-15 15:15:48 +00:00
* `>LDYA str`
2018-06-18 15:48:00 +00:00
* `>SYSCALL atol`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
2018-06-15 15:15:48 +00:00
* On stack (long)
*\--------------------------------------
2018-06-15 15:15:48 +00:00
K.AToL >STYA ZPPtr2 C-String in Ptr2, Dst buffer in Ptr1
2019-07-15 06:41:12 +00:00
* lda #10 base 10
sec signed
2021-04-29 11:56:34 +00:00
K.AToL.I jsr MATH.Dec2ACC32
bcs .9
2019-07-15 06:41:12 +00:00
ldx #3
2019-07-26 06:28:52 +00:00
.3 lda ACC32,x
2018-06-15 15:15:48 +00:00
>PUSHA
2019-07-15 06:41:12 +00:00
dex
bpl .3
2021-04-29 11:56:34 +00:00
tya Y = A = Count processed
2019-10-10 06:04:11 +00:00
* clc
.9
2019-10-03 06:25:27 +00:00
rts
*/--------------------------------------
2018-06-18 15:48:00 +00:00
* # atoi
* Convert String to 16 bits int
2018-06-18 06:22:50 +00:00
* ## C
2018-06-15 15:15:48 +00:00
* `int atoi ( const char * str );`
2018-06-18 06:22:50 +00:00
* ## ASM
2018-06-18 15:48:00 +00:00
* `>LDYAI str`
* `>SYSCALL atoi`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
2018-06-18 15:48:00 +00:00
* Y,A = int
*\--------------------------------------
2018-06-18 15:48:00 +00:00
K.atoi >STYA ZPPtr2
2019-07-15 06:41:12 +00:00
* lda #10 base 10
sec signed
2021-04-29 11:56:34 +00:00
jsr MATH.Dec2ACC32
bcs .9
2019-07-03 15:25:07 +00:00
>LDYA ACC32
2020-02-14 07:21:56 +00:00
.9 rts
*/--------------------------------------
2018-08-11 10:57:57 +00:00
* # RealPath
* Return the canonicalized absolute pathname
2020-02-14 07:21:56 +00:00
* ## C / CSH
* `char *realpath(const char *path, char *resolvedpath);`
2018-06-18 15:48:00 +00:00
* ## ASM
2020-02-14 07:21:56 +00:00
* `>PUSHW path`
* `>PUSHW resolvedpath`
2018-06-18 15:48:00 +00:00
* `>SYSCALL realpath`
2018-10-11 15:23:06 +00:00
* ## RETURN VALUE
2018-06-18 15:48:00 +00:00
* CC : success
2019-11-25 07:05:07 +00:00
* Y,A = Ptr to Full Path (C-String Buffer, MLI.MAXPATH+1)
2018-06-18 15:48:00 +00:00
* X = hMem of Full Path
* CS : A = Error Code
*\--------------------------------------
2020-02-14 07:21:56 +00:00
K.realpath.DST .BS 2
K.realpath.RET4 >RET 4
2018-10-29 08:41:10 +00:00
2020-02-14 07:21:56 +00:00
K.realpath ldy #3
lda (pStack),y
>PUSHA
lda (pStack),y
>PUSHA
2021-07-24 20:42:23 +00:00
>PUSHWZ
2020-02-14 07:21:56 +00:00
jsr K.expand
bcs K.realpath.RET4
2019-10-10 06:04:11 +00:00
2019-01-21 06:52:04 +00:00
>STYA ZPPtr1
stx .99+1 save expanded buffer hMem
2019-10-13 08:35:31 +00:00
>PULLW K.realpath.DST resolved path
2020-02-14 07:21:56 +00:00
inc pStack discard path
inc pStack
ldx #$ff
lda (ZPPtr1)
2019-10-10 06:04:11 +00:00
cmp #'/' full path starting with '/'?
beq .3 yes, do not append to current prefix
2019-10-10 06:04:11 +00:00
2019-07-22 16:28:44 +00:00
.1 ldy #S.PS.hCWD
2020-02-28 07:21:46 +00:00
lda (pPS),y
2018-06-21 15:12:10 +00:00
jsr K.GetMemPtr
2020-02-14 07:21:56 +00:00
>STYA .20+1
2019-10-10 06:04:11 +00:00
ldy #$ff
2019-10-10 06:04:11 +00:00
.2 iny
inx
2020-02-14 07:21:56 +00:00
.20 lda $ffff,y
sta K.Buf256,x
bne .2
2019-10-10 06:04:11 +00:00
2017-09-29 06:36:27 +00:00
dex
2019-10-10 06:04:11 +00:00
.3 ldy #$ff
.4 iny
inx
lda (ZPPtr1),y
sta K.Buf256,x
bne .4
2019-10-10 06:04:11 +00:00
dex
2019-01-21 06:52:04 +00:00
beq .81 we have '/'....nothing to do...
2018-11-27 16:22:15 +00:00
*--------------------------------------
2019-01-23 16:26:48 +00:00
* X=LEN, K.Buf256 = /dir1/./../file(/)\0
2018-11-27 16:22:15 +00:00
*--------------------------------------
2019-10-13 08:35:31 +00:00
ldx #0 will skip leading /
2019-10-13 08:35:31 +00:00
.5 ldy #0 reset dot counter=0
2019-10-10 06:04:11 +00:00
2019-10-13 08:35:31 +00:00
.6 inx
lda K.Buf256,x
beq .8
2018-11-27 16:22:15 +00:00
2019-10-13 08:35:31 +00:00
.7 cmp #'/'
beq .8
2019-10-10 06:04:11 +00:00
cmp #'.'
2019-10-13 08:35:31 +00:00
bne .5
2019-10-10 06:04:11 +00:00
2017-09-29 06:36:27 +00:00
iny
2019-10-13 08:35:31 +00:00
bra .6
2019-10-10 06:04:11 +00:00
2017-09-29 06:36:27 +00:00
.8 tya
beq .80 Y was 0....nothing to do...
2019-10-10 06:04:11 +00:00
2019-10-13 08:35:31 +00:00
dey one dot ?
2017-09-29 06:36:27 +00:00
bne .9 no..
2019-10-10 06:04:11 +00:00
2018-11-27 16:22:15 +00:00
dex
dex
2019-10-13 08:35:31 +00:00
jsr K.RealPath.RemoveAtX we found "/.", remove,useless....
bra .80
2019-10-10 06:04:11 +00:00
2019-10-13 08:35:31 +00:00
.9 dey "/.." ?
bne .99 "/..." ??!!...syntax error
2018-11-27 16:22:15 +00:00
dex
dex
dex
2019-10-13 08:35:31 +00:00
txa we found "/.."
2019-01-21 06:52:04 +00:00
beq .99 at the beginning of string...cannot remove /dir/..
2017-09-29 06:36:27 +00:00
jsr K.RealPath.RemoveAtX remove "/.."
.10 dex
lda K.Buf256,x go to "/dir"
2017-09-29 06:36:27 +00:00
cmp #'/'
bne .10
2019-10-10 06:04:11 +00:00
2017-09-29 06:36:27 +00:00
jsr K.RealPath.RemoveAtX ...remove "/dir"
2019-10-13 08:35:31 +00:00
.80 lda K.Buf256,x was / or \0 ?
2018-11-27 16:22:15 +00:00
bne .5
2018-11-27 14:03:26 +00:00
*--------------------------------------
2019-01-21 06:52:04 +00:00
.81 jsr .99
2020-02-14 07:21:56 +00:00
.82 ldy K.realpath.DST
lda K.realpath.DST+1
bne .85
2019-10-10 06:04:11 +00:00
2019-11-25 07:05:07 +00:00
>LDYAI MLI.MAXPATH+1
jsr K.getmem
bcs .90
2020-02-14 07:21:56 +00:00
.85 >STYA ZPPtr1
2019-11-25 07:05:07 +00:00
ldy #$ff
2019-11-25 07:05:07 +00:00
.83 iny
lda K.Buf256,y
sta (ZPPtr1),y
bne .83
2020-02-14 07:21:56 +00:00
2019-11-25 07:05:07 +00:00
>LDYA ZPPtr1
2019-10-10 06:04:11 +00:00
2020-02-14 07:21:56 +00:00
clc
rts
2019-10-10 06:04:11 +00:00
2019-01-21 06:52:04 +00:00
.99 lda #$ff SELF MODIFIED
jsr K.FreeMem
lda #E.BADPATH
sec
2019-11-25 07:05:07 +00:00
.90 rts
*--------------------------------------
2017-09-29 06:36:27 +00:00
K.RealPath.RemoveAtX
txa X = "/something"
tay
.1 iny
lda K.Buf256,y
2019-01-23 16:26:48 +00:00
beq .2 found /something\0
2019-10-13 08:35:31 +00:00
2017-09-29 06:36:27 +00:00
cmp #'/'
2019-01-23 16:26:48 +00:00
bne .1 found /something/
2019-10-13 08:35:31 +00:00
2019-01-23 16:26:48 +00:00
.2 phx save X for exit
2019-10-13 08:35:31 +00:00
2019-01-23 16:26:48 +00:00
.3 iny K.Buf256,y=/ or 0
2017-09-29 06:36:27 +00:00
inx
2019-01-23 16:26:48 +00:00
lda K.Buf256-1,y
sta K.Buf256-1,x
2017-09-29 06:36:27 +00:00
bne .3
2019-10-13 08:35:31 +00:00
2019-01-23 16:26:48 +00:00
txa
bne .8
2019-10-13 08:35:31 +00:00
lda #'/' Make sure we have a least '/' in the buffer
sta K.Buf256
stz K.Buf256+1
2019-01-23 16:26:48 +00:00
.8 plx restore X
2017-09-29 06:36:27 +00:00
rts
2020-02-14 07:21:56 +00:00
*/--------------------------------------
* # Expand
* ## C
* `char *expand(const char *str, char *expanded);`
* ## ASM
* `>PUSHW str`
* `>PUSHW expanded`
* `>SYSCALL expand`
* ## RETURN VALUE
* if expanded == null
* Y,A = PTR to Expanded String
* X = hMem to Expanded String
* if expanded != null
* Y,A = strlen
2020-02-14 07:21:56 +00:00
*\--------------------------------------
K.Expand ldy #2
lda (pStack),y
pha
iny
lda (pStack),y
ply
2020-02-14 07:21:56 +00:00
jsr K.strdup
bcc .1
2020-03-13 07:15:58 +00:00
jmp .9
2020-02-14 07:21:56 +00:00
.1 phx Save temp string...
>STYA TXTPTR
2020-03-13 16:10:36 +00:00
stz ENV.BufPtr init Expanded String len=0
2020-02-14 07:21:56 +00:00
stz ENV.bExp No var found yet
stz ENV.bNoExp Reset no expand flag
.10 lda (TXTPTR) End of CSTR?
beq .80
2021-05-04 17:31:21 +00:00
jsr SHARED.TXTPTRn
2020-02-14 07:21:56 +00:00
.11 cmp #'''
bne .21
lda ENV.bNoExp
eor #$ff
sta ENV.bNoExp toggle flag
bra .10
.21 bit ENV.bNoExp
bpl .23
.22 jsr ENV.AddAToBuf
bra .10
.23 cmp #'$' no, found one ?
bne .22 no, store...
lda (TXTPTR)
bne .24
lda #'$' End of string, output $
bra .22
.24
2020-02-14 07:21:56 +00:00
* sec
ror ENV.bExp Toggle Expanded flag
stz ENV.VarEndChar
cmp #'{' "${VAR]"?
bne .31
dec ENV.VarEndChar "}" expected
2021-05-04 17:31:21 +00:00
jsr SHARED.TXTPTRn skip "{"
2020-02-14 07:21:56 +00:00
bra .40
.31 jsr ENV.SysVar 0-9 *#?@$!
bcs .40
2021-05-04 17:31:21 +00:00
.35 jsr SHARED.TXTPTRn skip $x
2020-02-14 07:21:56 +00:00
bra .10
2020-02-14 07:21:56 +00:00
.40 jsr ENV.ExpandStrVar
bcc .70
2020-03-13 16:10:36 +00:00
.50 ldy ENV.BufPtr
2020-02-14 07:21:56 +00:00
lda /K.Buf256
>STYA FORPNT
ldx #_GetEnv
2020-03-13 16:10:36 +00:00
jsr K.GetEnv.I
2020-02-14 07:21:56 +00:00
bcs .70
2020-03-13 16:10:36 +00:00
ldx ENV.BufPtr
2020-02-14 07:21:56 +00:00
.51 lda K.Buf256,x
beq .52
inx
bra .51
2020-03-13 16:10:36 +00:00
.52 stx ENV.BufPtr
2020-02-14 07:21:56 +00:00
.70 lda (TXTPTR)
beq .72
jsr SHARED.IsIDValid
bcs .72
2021-05-04 17:31:21 +00:00
jsr SHARED.TXTPTRn
2020-02-14 07:21:56 +00:00
bra .70
.72 lda ENV.VarEndChar
2020-02-14 07:21:56 +00:00
bne .35 skip "}" and loop
bra .10
2020-03-13 16:10:36 +00:00
.80 ldx ENV.BufPtr
2020-02-14 07:21:56 +00:00
stz K.Buf256,x
pla discard temp string
jsr K.FreeMem
bit ENV.bExp Did we expand something ?
bpl .81
jsr STRING.DupBuf256
bcs .9
2020-02-14 07:21:56 +00:00
jmp .1
.81 ldy #1
lda (pStack),y
beq .83
sta ZPPtr1+1
lda (pStack)
sta ZPPtr1
ldy #$ff
.82 iny
lda K.Buf256,y
sta (ZPPtr1),y
bne .82
* Y = len, A = 0
bra .9
.83 jsr STRING.DupBuf256
2020-02-14 07:21:56 +00:00
.9 >RET 4
*--------------------------------------
ENV.SysVar jsr ZP.IsDigit $0 ... $9 ?
bcs .1
and #$0f
jsr K.ArgV
bcs .8 Arg# is undefined, do not append anything
jsr ENV.AddYAToBuf
.8 clc
rts
.1 ldx #ENV.SysVars.Cnt-1
.2 cmp ENV.SysVars,x
beq .3
dex
bpl .2
2020-02-14 07:21:56 +00:00
sec
rts
.3 txa
beq ENV.SysVarsAllArgs
ldy ENV.SysVars.PS-1,x
lda (pPS),y
*--------------------------------------
2021-04-29 11:56:34 +00:00
ENV.SysVarsNum jsr MATH.A2STR10NP
2020-03-12 16:44:11 +00:00
2020-02-14 07:21:56 +00:00
ldy #0
2021-05-04 17:31:21 +00:00
.1 lda FOUTBuf,y
2020-02-14 07:21:56 +00:00
beq ENV.SysVarsNum.8
2020-02-14 07:21:56 +00:00
iny
jsr ENV.AddAToBuf
bra .1
2020-02-14 07:21:56 +00:00
ENV.SysVarsNum.8
clc
rts
*--------------------------------------
ENV.SysVarsAllArgs
lda #1
jsr K.ArgV
bcs ENV.SysVarsNum.8
>STYA ZPPtr3
.1 lda (ZPPtr3)
beq ENV.SysVarsNum.8
.2 jsr ENV.AddP3ToBuf
jsr ENV.NextEnvP3
lda (ZPPtr3)
beq ENV.SysVarsNum.8
lda #C.SPACE
jsr ENV.AddAToBuf
bra .2
*--------------------------------------
ENV.SysVars .AS "*#?@$!"
ENV.SysVars.Cnt .EQ *-ENV.SysVars
ENV.SysVars.PS .DA #S.PS.ARGC,#S.PS.RC,#S.PS.PPID,#S.PS.PID,#S.PS.CPID
*--------------------------------------
ENV.ExpandStrVar
>LDYAI ENV.StrVars
>STYA ZPPtr3
ldx #0
.1 lda (ZPPtr3)
beq .9
ldy #$ff
.2 iny
lda (TXTPTR),y
beq .3
jsr SHARED.IsIDValid
bcs .3
cmp (ZPPtr3),y
beq .2
bra .4
.3 lda (ZPPtr3),y
bne .4
jmp (ENV.StrVarsJmp,x)
.4 inx
inx
jsr ENV.NextEnvP3
bra .1
.9 sec
rts
*--------------------------------------
2020-03-12 16:44:11 +00:00
ENV.StrVars .AZ "PWD"
2020-02-14 07:21:56 +00:00
.DA #0
*--------------------------------------
2020-03-12 16:44:11 +00:00
ENV.StrVarsJmp .DA ENV.StrVarsPWD
2020-02-14 07:21:56 +00:00
*--------------------------------------
ENV.StrVarsPWD ldy #S.PS.hCWD
lda (pPS),y
jsr K.GetMemPtr
*--------------------------------------
ENV.AddYAToBuf >STYA ZPPtr3
*--------------------------------------
2020-03-13 16:10:36 +00:00
ENV.AddP3ToBuf ldx ENV.BufPtr
2020-02-14 07:21:56 +00:00
ldy #$ff
2020-02-14 07:21:56 +00:00
dex
2020-02-14 07:21:56 +00:00
.1 iny
inx
lda (ZPPtr3),y
sta K.Buf256,x
bne .1
2020-03-13 16:10:36 +00:00
stx ENV.BufPtr
2020-02-14 07:21:56 +00:00
rts
*--------------------------------------
2020-03-13 16:10:36 +00:00
ENV.AddAToBuf ldx ENV.BufPtr
2020-02-14 07:21:56 +00:00
sta K.Buf256,x
2020-03-13 16:10:36 +00:00
inc ENV.BufPtr
2020-02-14 07:21:56 +00:00
rts
*--------------------------------------
ENV.NextEnvP3 jsr ENV.GetP3LenY
2020-03-12 16:44:11 +00:00
tya
2020-02-14 07:21:56 +00:00
sec
adc ZPPtr3
sta ZPPtr3
bcc .8
inc ZPPtr3+1
.8 rts
*--------------------------------------
ENV.GetP3LenY ldy #$ff
.1 iny
lda (ZPPtr3),y
bne .1
rts
2017-09-29 06:36:27 +00:00
*--------------------------------------
MAN
2020-12-15 13:23:22 +00:00
SAVE usr/src/sys/kernel.s.stdlib
LOAD usr/src/sys/kernel.s
ASM