commit ebf779f0b38bd6bdd896e911424356e2edd4f308
parent 7370d318a3829e12598aced4d5b895becf3d7055
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sat, 4 Jun 2022 10:27:17 -0400
Add "lnxcall" native word and move fopen and f< to boot.fs
Diffstat:
3 files changed, 22 insertions(+), 51 deletions(-)
diff --git a/boot.fs b/boot.fs
@@ -50,6 +50,18 @@ current to (psufl)
: create code compile (cell) ;
: value code compile (val) , ;
: alias ' code compile (alias) , ;
+\ transform a fstring into a null-terminated string.
+create _ $100 allot
+: tocstr ( sa sl -- a ) >r _ r@ move 0 _ r> + c! _ ;
+0 value curfd
+: fclose ( fd -- ) 6 ( close ) swap 0 0 ( close fd 0 0 ) lnxcall drop ;
+: fopen ( sa sl -- )
+ curfd ?dup if fclose 0 to curfd then
+ tocstr 5 ( open ) swap 0 0 ( open cstr noflag O_RDONLY ) lnxcall
+ dup 0< if S" Can't open" stype abort then to curfd ;
+create _ 1 allot
+: f< ( -- c-or-0 ) 3 ( read ) curfd _ 1 lnxcall 1 = if _ c@ else 0 then ;
+
64 value LNSZ
create in( LNSZ allot
: in) in( 64 + ;
@@ -71,6 +83,6 @@ create in( LNSZ allot
rdln in( to in> SPC then ;
: rdln$ ['] rdln< to in< in) to in> ;
: fin< f< ?dup not if ['] rdln< to in< SPC then ;
-: f<< word fopen not if S" Can't open" stype abort then ['] fin< to in< ;
+: f<< word fopen ['] fin< to in< ;
: init S" Dusk OS" stype rdln$ ;
init
diff --git a/dusk.asm b/dusk.asm
@@ -109,7 +109,6 @@ inptr: resd 1 ; in>
inrd: resd 1 ; in<
wnf: resd 1 ; (wnf)
psufl: resd 1 ; (psufl)
-curfd: resd 1 ; file descriptor being read by key
scratchpad: resd 0x100
resd PS_SZ
ps_top:
@@ -131,7 +130,6 @@ _start:
mov dword [inrd], word_bootrd
mov dword [wnf], word_bye
mov dword [psufl], word_bye
- mov dword [curfd], 0 ; nothing opened
mov eax, SYSCALL_CHDIR
mov ebx, rootfspath
int 0x80
@@ -245,56 +243,17 @@ defword 'key', 3, word_key, word_emit
int 0x80
ret
-; ( sa sl -- f )
-; open specified file in rootfs and set curfd to its file descriptor
-; if there was a file opened, close it.
-; f is false if there was an error during open().
-defword 'fopen', 5, word_fopen, word_key
- test dword [curfd], -1
- jz _fopen_noclose
- mov eax, SYSCALL_CLOSE
- mov ebx, [curfd]
- int 0x80
- mov dword [curfd], 0
-_fopen_noclose:
- pspop ecx ; sl
- pspop esi ; src
- mov edi, scratchpad ; dst
- mov byte [edi+ecx], 0 ; null terminate
- rep movsb
- mov eax, SYSCALL_OPEN
- mov ebx, scratchpad
- xor ecx, ecx ; flags
- mov edx, O_RDONLY ; mode
- int 0x80
- xor ebx, ebx ; f value
- test eax, eax
- js _fopen_error
- mov [curfd], eax
- inc ebx
-_fopen_error:
- pspush ebx
- ret
-
-defword 'f<', 2, word_fread, word_fopen
- pspush 0
- test dword [curfd], -1
- jnz _fread_hasfd
- ret ; no fd, no read!
-_fread_hasfd:
- mov eax, SYSCALL_READ
- mov ebx, [curfd]
- mov ecx, ebp ; buffer
- mov edx, 1 ; len
+; ( eax ebx ecx edx -- eax )
+defword 'lnxcall', 7, word_lnxcall, word_key
+ pspop edx
+ pspop ecx
+ pspop ebx
+ pspop eax
int 0x80
- dec eax ; error or EOF
- jns _fread_success
- mov dword [ebp], 0 ; return 0 if negative
-_fread_success:
+ pspush eax
ret
-
-defword 'drop', 4, word_drop, word_fread
+defword 'drop', 4, word_drop, word_lnxcall
add ebp, 4
ret
diff --git a/tests/testcc.fs b/tests/testcc.fs
@@ -8,7 +8,7 @@ f<< cc/gen.fs
f<< cc/ops.fs
f<< cc/ast.fs
f<< cc/cc1.fs
-: opentestc S" test.c" fopen not if abort" can't open" then ;
+: opentestc S" test.c" fopen ;
opentestc
' f< to cc<
cc1,