Diff for /gforth/contrib/syscalls386.fs between versions 1.1 and 1.2

version 1.1, 2004/11/05 14:27:33 version 1.2, 2010/04/16 13:31:44
Line 1 Line 1
 \ syscalls386.fs  \ syscalls386.fs
 \  \
 \ Selected system calls for gforth 0.6.x on 386 machines running Linux.  \ Selected Unix system calls for gforth >=0.7.x
 \     
 \ Copyright (c) 2004 Krishna Myneni,  \ Replacement for the original non-portable syscalls386 implementation
 \ Provided under the GNU General Public License  \ this one ports to other Gforth installations, other architectures,
 \  \ and other Unix implementations.
 \ Notes:  
 \  \ The following original note is probably still applicable.
 \ 1)   System calls under Linux may be performed using a software  
 \        interrupt, $80, and placing the parameters in appropriate  
 \        registers. The corresponding C wrapper functions are another  
 \        way to do this, but the $80 int method is direct. The Forth  
 \        stack parameters are chosen to correspond to the C wrapper   
 \        function argument list.  
 \   
 \ 2)   There are about 221 system calls under Linux, but this file  
 \        provides only a select few. The provided syscalls allow   
 \        communication with device drivers, e.g. serial port drivers.   
 \        Add others as needed following the examples below and using   
 \        the man pages for the C wrapper functions. System call numbers   
 \        are listed in /usr/include/asm/unistd.h   
 \  
 \ 3)   Compatibility with low-level kForth words is maintained to allow   \ 3)   Compatibility with low-level kForth words is maintained to allow 
 \        kForth code to be used under gforth, e.g. serial.fs, terminal.fs.  \        kForth code to be used under gforth, e.g. serial.fs, terminal.fs.
 \        Other driver interface examples from kForth should also  \        Other driver interface examples from kForth should also
 \        work, e.g. the National Instruments GPIB interface nigpib.4th.  \        work, e.g. the National Instruments GPIB interface nigpib.4th.
 \  
 \ 4)   The code should be readily adaptable to other Forths running  
 \        on the same platform (386/Linux). It also demonstrates why  
 \        an assembler can be an important component of a Forth system.  
 \  
 \ Revisions:  
 \       2004-09-16  created  KM   
   
 \ syscall0  ( syscall_num -- retval | system call with no args )  
    
 code syscall0  
         .d  di )  ax   mov  
         .d  $80 #      int  
         .d  ax    di ) mov  
         next  
 end-code  
   
 \ syscall1  ( arg syscall_num -- retval | system call with one arg )  
   
 code syscall1  
         .d  di )  ax   mov  
         .d   4 #  di   add  
         .d  di )  bx   mov  
         .d  $80 #      int  
         .d  ax    di ) mov  
         next  
 end-code  
   
 \ syscall2  ( arg1 arg2 syscall_num -- retval | system call with 2 args )  
   
 code syscall2  
         .d  di )  ax   mov  
         .d   4 #  di   add  
         .d  di )  cx   mov  
         .d   4 #  di   add  
         .d  di )  bx   mov  
         .d  $80 #      int  
         .d  ax    di ) mov  
         next  
 end-code  
   
 \ syscall3  ( arg1 arg2 arg3 syscall_num -- retval | system call with 3 args )  
   
 code syscall3  
         .d  di )  ax   mov  
         .d   4 #  di   add  
         .d  di )  dx   mov  
         .d   4 #  di   add  
         .d  di )  cx   mov  
         .d   4 #  di   add  
         .d  di )  bx   mov  
         .d  $80 #      int  
         .d  ax    di ) mov  
         next  
 end-code  
   
   c-library syscalls386
   
   \c #include <sys/types.h>
   \c #include <sys/stat.h>
   \c #include <fcntl.h>
   \c #include <unistd.h>
   \c #include <sys/ioctl.h>
   
 \ sysexit ( code --  | exit to system with code )  \ sysexit ( code --  | exit to system with code )
 \   sysexit is NOT the recommended way to exit back to the   \   sysexit is NOT the recommended way to exit back to the 
 \   system from Forth. It is provided here as a demo of a very   \   system from Forth. It is provided here as a demo of a very 
 \   simple syscall.  \   simple syscall.
   c-function sysexit _exit n -- void
 : sysexit  1 syscall1 ;  c-function getpid getpid -- n  ( -- u | get process id )
   c-function open open a n -- n  ( ^zaddr  flags -- fd )
 : getpid ( -- u | get process id )  \   file descriptor is returned)
         20 syscall0 ;  
   
 : open ( ^zaddr  flags -- fd | file descriptor is returned)  
 \   Note zaddr points to a buffer containing the counted filename  \   Note zaddr points to a buffer containing the counted filename
 \   string terminated with a null character.  \   string terminated with a null character.
         swap 1+ swap  c-function close close n -- n ( fd -- flag )
         0       \ set mode to zero    c-function read read n a n -- n ( fd  buf  count --  n )
         5 syscall3 ;  \ read count byes into buf from file
   c-function write write n a n -- n ( fd  buf  count  --  n )
 : close ( fd -- flag )  6 syscall1 ;  \ write count bytes from buf to file
   c-function lseek lseek n n n -- n ( fd  offset  type  --  offs )
   \ reposition the file ptr
 : read ( fd  buf  count --  n | read count byes into buf from file )  c-function ioctl ioctl n n a -- n ( fd  request argp -- error )
         3 syscall3 ;  
   
   
 : write ( fd  buf  count  --  n  | write count bytes from buf to file )  
         4 syscall3 ;  
   
   
 : lseek ( fd  offset  type  --  offs  | reposition the file ptr )  
         19 syscall3 ;  
   
 : ioctl ( fd  request argp -- error )  
         54 syscall3 ;  
   
   end-c-library
   

Removed from v.1.1  
changed lines
  Added in v.1.2


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>