diff --git a/plat/msdos386/libsys/_hol0.s b/plat/msdos386/libsys/_hol0.s new file mode 100644 index 000000000..f01566fe8 --- /dev/null +++ b/plat/msdos386/libsys/_hol0.s @@ -0,0 +1,19 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .bss + +! This data block is used to store information about the current line number +! and file. + +.define hol0 +.comm hol0, 8 diff --git a/plat/msdos/libsys/brk.c b/plat/msdos386/libsys/brk.c similarity index 100% rename from plat/msdos/libsys/brk.c rename to plat/msdos386/libsys/brk.c diff --git a/plat/msdos386/libsys/build.lua b/plat/msdos386/libsys/build.lua index 2725c0f70..70c7eaa17 100644 --- a/plat/msdos386/libsys/build.lua +++ b/plat/msdos386/libsys/build.lua @@ -1,39 +1,24 @@ acklibrary { name = "lib", srcs = { + "./brk.c", + "./close.s", + "./errno.s", + "./getpid.s", + "./_hol0.s", + "./isatty.s", "./rename.s", --- "./brk.c", --- "./close.s", --- "./creat.c", --- "./errno.s", --- "./getpid.s", --- "./gettimeofday.c", --- "./_hol0.s", --- "./isatty.s", --- "./kill.c", --- "./lseek.c", --- "./open.c", --- "./read.c", --- "./setmode.c", --- "./signal.c", --- "./sys_exists.s", --- "./sys_fdmodes.c", --- "./sys_getdate.s", --- "./sys_getmode.c", --- "./sys_gettime.s", --- "./sys_initmain.c", --- "./sys_iseof.c", --- "./sys_isopen.s", --- "./sys_isreadyr.s", --- "./sys_rawcreat.s", --- "./sys_rawlseek.s", --- "./sys_rawopen.s", --- "./sys_rawrw.s", --- "./sys_seteof.c", --- "./sys_seterrno.c", --- "./sys_setmode.c", --- "./sys_xret.s", --- "./unlink.s", + "./sys_exists.s", + "./sys_getdate.s", + "./sys_gettime.s", + "./sys_isopen.s", + "./sys_isreadyr.s", + "./sys_rawcreat.s", + "./sys_rawlseek.s", + "./sys_rawopen.s", + "./sys_rawrw.s", + "./sys_xret.s", + "./unlink.s", "plat/msdos/libsys+srcs", }, deps = { diff --git a/plat/msdos386/libsys/close.s b/plat/msdos386/libsys/close.s new file mode 100644 index 000000000..b8c2f8a15 --- /dev/null +++ b/plat/msdos386/libsys/close.s @@ -0,0 +1,23 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Close a file. + +.define _close +_close: + mov bx, sp + mov bx, 2(bx) + movb ah, 0x3E + int 0x21 + jmp .sys_zret diff --git a/plat/msdos386/libsys/errno.s b/plat/msdos386/libsys/errno.s new file mode 100644 index 000000000..9858d2640 --- /dev/null +++ b/plat/msdos386/libsys/errno.s @@ -0,0 +1,28 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +#define D(e) .define e; e + +.sect .data + +! Define various ACK error numbers. Note that these are *not* ANSI C +! errnos, and are used for different purposes. + +D(ERANGE) = 1 +D(ESET) = 2 +D(EIDIVZ) = 6 +D(EHEAP) = 17 +D(EILLINS) = 18 +D(EODDZ) = 19 +D(ECASE) = 20 +D(EBADMON) = 25 + diff --git a/plat/msdos386/libsys/getpid.s b/plat/msdos386/libsys/getpid.s new file mode 100644 index 000000000..5aa80638b --- /dev/null +++ b/plat/msdos386/libsys/getpid.s @@ -0,0 +1,45 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Copyright (c) 2021--2022 TK Chia +! +! The authors hereby grant permission to use, copy, modify, distribute, +! and license this software and its documentation for any purpose, provided +! that existing copyright notices are retained in all copies and that this +! notice is included verbatim in any distributions. No written agreement, +! license, or royalty fee is required for any of the authorized uses. +! Modifications to this software may be copyrighted by their authors +! and need not follow the licensing terms described here, provided that +! the new terms are clearly indicated on the first page of each file where +! they apply. + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Get the current process identifier. For MS-DOS, use the Program Segment +! Prefix (PSP) segment as the PID, unless the system supports an actual +! `getpid' syscall, e.g. European MS-DOS 4.0. +! +! (Note that pid_t is a 32-bit type. This is to allow for PSP segments and +! MS-DOS PIDs above 0x7FFF.) + +.define _getpid +_getpid: + movb ah, 0x87 + stc + int 0x21 + jnc .eur_dos + movb ah, 0x51 + int 0x21 + xchg bx, ax +.eur_dos: + xor dx, dx + ret diff --git a/plat/msdos386/libsys/isatty.s b/plat/msdos386/libsys/isatty.s new file mode 100644 index 000000000..bd2e195e6 --- /dev/null +++ b/plat/msdos386/libsys/isatty.s @@ -0,0 +1,37 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Say whether a given file descriptor number refers to a terminal. + +.define _isatty +_isatty: + mov bx, sp + mov bx, 2(bx) + mov ax, 0x4400 + int 0x21 + jc error + testb dl, dl + jz not_tty + mov ax, 1 + ret +not_tty: + mov (_errno), 25 ! ENOTTY +not_tty_2: + xor ax, ax + ret +error: + push ax + call __sys_seterrno + pop cx + jmp not_tty_2 diff --git a/plat/msdos386/libsys/rename.s b/plat/msdos386/libsys/rename.s new file mode 100644 index 000000000..edb14266b --- /dev/null +++ b/plat/msdos386/libsys/rename.s @@ -0,0 +1,26 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Rename a file. + +.define _rename +_rename: + mov bx, sp + push di + mov dx, 2(bx) + mov di, 4(bx) + movb ah, 0x56 + int 0x21 + pop di + jmp .sys_zret diff --git a/plat/msdos386/libsys/sys_exists.s b/plat/msdos386/libsys/sys_exists.s new file mode 100644 index 000000000..e282ef687 --- /dev/null +++ b/plat/msdos386/libsys/sys_exists.s @@ -0,0 +1,25 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Say whether a file exists with the given name. + +.define __sys_exists +__sys_exists: + mov bx, sp + mov dx, 2(bx) + mov ax, 0x4300 + int 0x21 + sbb ax, ax + inc ax + ret diff --git a/plat/msdos386/libsys/sys_getdate.s b/plat/msdos386/libsys/sys_getdate.s new file mode 100644 index 000000000..41d69fc85 --- /dev/null +++ b/plat/msdos386/libsys/sys_getdate.s @@ -0,0 +1,25 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Get the current system date from MS-DOS. + +.define __sys_getdate +__sys_getdate: + movb ah, 0x2a + int 0x21 + mov bx, sp + mov bx, 2(bx) + mov (bx), dx + mov 2(bx), cx + ret diff --git a/plat/msdos386/libsys/sys_gettime.s b/plat/msdos386/libsys/sys_gettime.s new file mode 100644 index 000000000..d062fad8d --- /dev/null +++ b/plat/msdos386/libsys/sys_gettime.s @@ -0,0 +1,25 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Get the current system time from MS-DOS. + +.define __sys_gettime +__sys_gettime: + movb ah, 0x2c + int 0x21 + mov bx, sp + mov bx, 2(bx) + mov (bx), cx + mov 2(bx), dx + ret diff --git a/plat/msdos386/libsys/sys_isopen.s b/plat/msdos386/libsys/sys_isopen.s new file mode 100644 index 000000000..5ce49ed45 --- /dev/null +++ b/plat/msdos386/libsys/sys_isopen.s @@ -0,0 +1,26 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Say whether a given file descriptor number refers to a valid open file +! descriptor. + +.define __sys_isopen +__sys_isopen: + mov bx, sp + mov bx, 2(bx) + mov ax, 0x4400 + int 0x21 + sbb ax, ax + inc ax + ret diff --git a/plat/msdos386/libsys/sys_isreadyr.s b/plat/msdos386/libsys/sys_isreadyr.s new file mode 100644 index 000000000..b5d0800ee --- /dev/null +++ b/plat/msdos386/libsys/sys_isreadyr.s @@ -0,0 +1,28 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Say whether a file descriptor is ready for input, i.e. reading from the +! fd will immediately return something. + +.define __sys_isreadyr +__sys_isreadyr: + mov bx, sp + mov ax, 0x4406 + mov bx, 2(bx) + int 0x21 + jnc ok + movb al, 0 +ok: + cbw + ret diff --git a/plat/msdos386/libsys/sys_rawcreat.s b/plat/msdos386/libsys/sys_rawcreat.s new file mode 100644 index 000000000..67bc34e59 --- /dev/null +++ b/plat/msdos386/libsys/sys_rawcreat.s @@ -0,0 +1,24 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Create or truncate a file. + +.define __sys_rawcreat +__sys_rawcreat: + movb ah, 0x3c + mov bx, sp + mov dx, 2(bx) + mov cx, 4(bx) + int 0x21 + jmp .sys_axret diff --git a/plat/msdos386/libsys/sys_rawlseek.s b/plat/msdos386/libsys/sys_rawlseek.s new file mode 100644 index 000000000..992c4c655 --- /dev/null +++ b/plat/msdos386/libsys/sys_rawlseek.s @@ -0,0 +1,26 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Move the current file position for a file descriptor. + +.define __sys_rawlseek +__sys_rawlseek: + movb ah, 0x42 + mov bx, sp + mov dx, 4(bx) + mov cx, 6(bx) + movb al, 8(bx) + mov bx, 2(bx) + int 0x21 + jmp .sys_dxaxret diff --git a/plat/msdos386/libsys/sys_rawopen.s b/plat/msdos386/libsys/sys_rawopen.s new file mode 100644 index 000000000..301e2fded --- /dev/null +++ b/plat/msdos386/libsys/sys_rawopen.s @@ -0,0 +1,24 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Open an existing file. + +.define __sys_rawopen +__sys_rawopen: + movb ah, 0x3d + mov bx, sp + mov dx, 2(bx) + movb al, 4(bx) + int 0x21 + jmp .sys_axret diff --git a/plat/msdos386/libsys/sys_rawrw.s b/plat/msdos386/libsys/sys_rawrw.s new file mode 100644 index 000000000..a306d6528 --- /dev/null +++ b/plat/msdos386/libsys/sys_rawrw.s @@ -0,0 +1,33 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Read/write bytes to/to a file descriptor. These routines do not do any +! translation between CRLF and LF line endings. +! +! Note that, for MS-DOS, a raw "write" request of zero bytes will truncate +! (or extend) the file to the current file position. + +.define __sys_rawread +__sys_rawread: + movb ah, 0x3f + .data1 0x3d ! eat up the next instruction +.define __sys_rawwrite +__sys_rawwrite: + movb ah, 0x40 + mov bx, sp + mov dx, 4(bx) + mov cx, 6(bx) + mov bx, 2(bx) + int 0x21 + jmp .sys_axret diff --git a/plat/msdos386/libsys/sys_xret.s b/plat/msdos386/libsys/sys_xret.s new file mode 100644 index 000000000..57f407ef4 --- /dev/null +++ b/plat/msdos386/libsys/sys_xret.s @@ -0,0 +1,41 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! .sys_zret: if the carry flag is set, then set `errno' from the DOS error +! code in ax, and return a shortword -1. If the carry flag is clear, just +! return a shortword zero. +! +! .sys_axret: if the carry flag is set, then set `errno' from the DOS error +! code in ax, and return a shortword -1. If the carry flag is clear, just +! return ax as a return value. +! +! .sys_dxaxret: same as .sys_axret, but return a longword -1 or dx:ax as a +! return value. + +.extern .sys_zret +.extern .sys_axret +.extern .sys_dxaxret +.sys_zret: + jc error + xor ax, ax +no_error: + ret +.sys_axret: +.sys_dxaxret: + jnc no_error +error: + push ax + call __sys_seterrno + pop cx + ret diff --git a/plat/msdos386/libsys/unlink.s b/plat/msdos386/libsys/unlink.s new file mode 100644 index 000000000..912f6958d --- /dev/null +++ b/plat/msdos386/libsys/unlink.s @@ -0,0 +1,23 @@ +# +! $Source$ +! $State$ +! $Revision$ + +! Declare segments (the order is important). + +.sect .text +.sect .rom +.sect .data +.sect .bss + +.sect .text + +! Remove a file. + +.define _unlink +_unlink: + mov bx, sp + mov dx, 2(bx) + movb ah, 0x41 + int 0x21 + jmp .sys_zret diff --git a/plat/msdos86/libsys/brk.c b/plat/msdos86/libsys/brk.c new file mode 100644 index 000000000..ab333e550 --- /dev/null +++ b/plat/msdos86/libsys/brk.c @@ -0,0 +1,59 @@ +/* $Source$ + * $State$ + * $Revision$ + */ + +#include +#include +#include +#include "libsys.h" + +#define STACK_BUFFER 128 /* number of bytes to leave for stack */ + +extern char _end[1]; +static char* current = _end; + +int brk(void* newend) +{ + /* This variable is used to figure out the current stack pointer, + * by taking its address. */ + char dummy; + char* p = newend; + + if ((p > (&dummy - STACK_BUFFER)) || + (p < _end)) + { + errno = ENOMEM; + return -1; + } + + current = p; + return 0; +} + +void* sbrk(int increment) +{ + char* old; + char* new; + + if (increment == 0) + return current; + + old = current; + + new = old + increment; + + if ((increment > 0) && (new <= old)) + goto out_of_memory; + else if ((increment < 0) && (new >= old)) + goto out_of_memory; + + if (brk(new) < 0) + goto out_of_memory; + + return old; + +out_of_memory: + errno = ENOMEM; + return OUT_OF_MEMORY; +} diff --git a/plat/msdos86/libsys/build.lua b/plat/msdos86/libsys/build.lua index 58bf3e6e5..96988002d 100644 --- a/plat/msdos86/libsys/build.lua +++ b/plat/msdos86/libsys/build.lua @@ -1,6 +1,7 @@ acklibrary { name = "lib", srcs = { + "./brk.c", "./close.s", "./errno.s", "./getpid.s",