home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 1.ddi / DOC / README.EMB < prev    next >
Encoding:
Text File  |  1992-04-22  |  17.4 KB  |  395 lines

  1.        Copyright (C) 1992 MetaWare Incorporated; All Rights Reserved.
  2.  
  3.                           High C/C++ Version 3.0
  4.                        Globally Optimizing Compiler
  5.                 for Extended DOS 80386/486 and Windows 3.0
  6.                                 April 1992
  7.  
  8.                       +-----------------------------+
  9.                       | Embedded Applications Notes |
  10.                       +-----------------------------+
  11.  
  12. If you are developing code for use in embedded systems, and you are not
  13. writing your own library, use High C 1.x libraries (HCC.LIB or HCE.LIB)
  14. instead of the High C/C++ 3.0 libraries.  HCC.LIB and HCE.LIB are included in
  15. the High C/C++ 3.0 distribution.  HCC.LIB and HCE.LIB facilitate making an
  16. embedded product because they isolate system dependencies in a few modules.
  17.  
  18. This file describes how to modify MS-DOS-dependent modules.  Use this
  19. information in addition to the "Embedded Applications" chapter in the High
  20. C/C++ Programmer's Guide.
  21.  
  22.  
  23. No MS-DOS Support
  24. -----------------
  25.  
  26. Systems with no operating-system support are the traditional pattern for
  27. embedded systems.
  28.  
  29. A system with no MS-DOS support requires more radical modification of INIT.ASM
  30. than systems with partial support.  INIT must still be used, however, because
  31. some memory management is necessary.  Set the conditional assembly for NO_DOS,
  32. set Stack_length to a number suitable for your conditions, and create a new
  33. INIT.OBJ.
  34.  
  35. Lack of operating-system support severely limits many of the library functions
  36. you may have used previously.  The High C functions listed in  Table 1 require
  37. no DOS support.
  38.  
  39.              Table 1: Functions Requiring No
  40.                 Operating-System Support
  41.  
  42.      abs          iscntrl         outp          _strncat
  43.      acos         isdigit         outpw          strncmp
  44.      alloca       isgraph         perror         strncpy
  45.      asctime      islower         pow            strnicmp
  46.      asin        _isodigit        qsort          strnset
  47.      assert       isprint         rand           strpbrk
  48.      atan         ispunct        _rmemcpy        strrchr
  49.      atan2        isspace        _rotl           strrev
  50.      atexit       isupper        _rotr           strset
  51.      atof         isxdigit       _rstrcpy        strspn
  52.      atoi         labs           _rstrncpy       strstr
  53.      atol         ldexp           segread        strtod
  54.      bsearch      ldiv            setjmp         strtok
  55.      cabs         localeconv      setlocale      strtol
  56.      ceil         localtime      _setmode        strtoul
  57.      clearerr     log             sin            strupr
  58.      cos          log10           sinh           strxfrm
  59.      cosh         longjmp         sprintf        swab
  60.      ctime       _lrotl           sqrt           tan
  61.      difftime    _lrotr           srand          tanh
  62.      div          mblen           sscanf         tolower
  63.      errno        mbstowcs        strcat        _tolower
  64.      exp          mbtowc         _strcats        toupper
  65.      fabs         memccpy         strchr        _toupper
  66.      floor        memchr          strcmp         ultoa
  67.      fmod         memcmp          strcoll        va_arg
  68.      frexp        memcpy          strcpy         va_end
  69.      gmtime       memicmp         strcspn        va_list
  70.      inp          memmove         strftime       va_start
  71.      inpw         memset          stricmp        vsprintf
  72.      isalnum      mktime          strlen         wcstombs
  73.      isalpha      modf            strlwr         wctomb
  74.     _isascii      offsetof        strncat
  75.  
  76. Very experienced programmers may wish to modify other source-code modules to
  77. use normally DOS-dependent code in a non-DOS environment.
  78.  
  79.  
  80. Modifying MS-DOS-Dependent Modules
  81. ----------------------------------
  82.  
  83. This section is intended for very experienced programmers only.  Deleting
  84. or modifying system-level code can result in much smaller executable modules,
  85. but the changes must be very carefully prepared.  At this level many modules
  86. interact, and modifying one may not prove "user friendly" to another.
  87.  
  88. This code was not originally designed for modification by users, and not many
  89. programmers will be comfortable modifying it.  We provide this facility as a
  90. special service to our many expert customers.
  91.  
  92. Only a few modules in the Run-Time Libraries are directly dependent on the MS-
  93. DOS operating system.  Some dependent modules, such as ENV, are specific to
  94. MS-DOS and are not referenced elsewhere in the Run-Time Libraries.  Such
  95. modules are not of concern for embedded applications, and so are not discussed
  96. here.
  97.  
  98. The initialization module INIT must be implemented on any non-MS-DOS system to
  99. establish the run-time environment.  Several other modules must be ported if
  100. any significant portion of any Run-Time Library is used by the application.
  101. These modules are:
  102.  
  103.     INIT     initialize the environment
  104.     TERM     terminate the environment by closing files, and so forth
  105.     EXIT     C functions exit, _exit, and abort
  106.     SYSTEM   low-level system services
  107.     INTRUP   interrupt handling
  108.     CONSOLE  input/output from/to the console
  109.  
  110. The source for the MS-DOS implementations of these modules is provided with
  111. the distribution.  You should study these files and use them as a guide.
  112.  
  113. Non-assembly source is written in either MetaWare's Professional Pascal or
  114. High C.  If you want to modify the source, you may need both compilers.  Also,
  115. the two languages use different calling conventions.  To call functions in one
  116. language from the other language successfully, the calling module must contain
  117. a declaration that appropriately sets the calling convention for the called
  118. function.  See the Compatibility and Portability chapter in the High C/C++
  119. Programmer's Guide for information about calling conventions.
  120.  
  121. Unless otherwise indicated in the following sections, the object version of
  122. each function is aliased to the source name preceded by _mw, to avoid conflict
  123. with the Standard C library and likely user-coined names.  For example, INIT
  124. is known externally -- that is, to the linker -- as _mwINIT.
  125.  
  126.  
  127. INIT -- Environment Initialization
  128. ----------------------------------
  129.  
  130. INIT is an assembly-language module that contains the function INIT which is
  131. the initial entry point of each linked program.  INIT establishes the
  132. environment in which High C programs are to run.  More specifically, this
  133. module:
  134.  
  135.  * sets up the stack
  136.  
  137.  * initializes public variables as required
  138.  
  139.  * initializes the floating-point processor
  140.  
  141.  * calls a function to set up interrupt vectors
  142.  
  143.  * calls a function to initialize the environment table
  144.  
  145.  * calls a function to initialize I/O for High C
  146.  
  147.  * calls the main program
  148.  
  149.  * calls a function to close any open files
  150.  
  151.  * terminates the program.
  152.  
  153. Minimum environment.  The MS-DOS version of this module is much more involved
  154. than is typically required for an embedded application.  If your application
  155. is completely independent of Library support, INIT need only set up a stack,
  156. invoke the main program, and then terminate.
  157.  
  158. I/O initialization.  If the High C STDIO module is used, the function cfinit()
  159. must be invoked as part of the initialization process to set up various
  160. control blocks and buffers.  The function cfterm() closes all open files and
  161. frees up buffers; this function is called from the TERM module.
  162.  
  163. Main program.  The name of the main program of any linked program is main.
  164.  
  165. argc, argv support.  If argc and argv are to be used, the public variables
  166. arglen and argp must be initialized and the function set_up_args() within INIT
  167. must be called prior to calling main().  arglen is a word that is set to the
  168. length of the parameter string.  argp is set to the address of the first byte
  169. of the parameter string.  If the program name is to be made available (as the
  170. zeroth argument), the variable prognamep must be initialized as well.
  171.  
  172. Program termination.  The MS-DOS version of INIT calls Halt.  Halt is in the
  173. TERM module; see TERM -- Environment Termination.
  174.  
  175. This function closes open files, calls a function to restore interrupt
  176. vectors, and then invokes the dos_exit() function to terminate the process.
  177. dos_exit(), which is called indirectly by Halt, is in the SYSTEM module.
  178.  
  179. Line-terminator convention.  The INIT module initializes the common block that
  180. establishes the default line-terminating convention for the host system.
  181.  
  182. Under the MS-DOS operating system, lines are terminated with the two
  183. characters CR (13(10)) and LF (10(10)).
  184.  
  185.  
  186. TERM -- Environment Termination
  187. -------------------------------
  188.  
  189. The TERM module implements the Pascal Halt library routine, called by the C
  190. exit() function and by INIT.
  191.  
  192. Halt indirectly calls cfterm to close all open files; restores interrupt
  193. vectors to their prior condition; and then calls dos_exit() to terminate.
  194. dos_exit() is in the SYSTEM module.
  195.  
  196.  
  197. EXIT -- Functions exit(), _exit(), and abort()
  198. ----------------------------------------------
  199.  
  200. The EXIT module implements the C functions exit(), _exit(), and abort().
  201. exit() is implemented by calling the Professional Pascal routine Halt.
  202. _exit() calls a routine to restore interrupts and calls dos_exit() (in
  203. SYSTEM), avoiding a call to cfterm() to terminate the file system.  abort() is
  204. a synonym for _exit(-1) which produces a return code of 255 under the MS-DOS
  205. operating system (return codes  are limited to the range 0..255).
  206.  
  207. In simpler environments, the TERM module can be eliminated by implementing
  208. exit(), _exit(), and abort() in the SYSTEM module.
  209.  
  210.  
  211. SYSTEM -- System Services
  212. -------------------------
  213.  
  214. The SYSTEM module typically takes the most work to port to another system.
  215. Of course only those functions in SYSTEM that are explicitly referenced
  216. elsewhere in the Run-Time Libraries need be ported.  Details of those
  217. functions are described here.
  218.  
  219. Source files.  The implementation of the SYSTEM module under MS-DOS is in
  220. a number of files of the form SYSTEM*.P.
  221.  
  222. I/O model.  The SYSTEM module models I/O the same way as MS-DOS and UNIX:
  223. files are treated as unformatted streams of bytes.  Open files are referenced
  224. through file handles.  A file handle is a small integer value within the range
  225. zero to MaxFiles, where MaxFiles+1 is the maximum number of files that can be
  226. open at one time.  An arbitrary number of bytes can be read from or written to
  227. a file with the read and write functions.
  228.  
  229. I/O is device independent.  Devices are accessed in exactly the same way as
  230. disk files.
  231.  
  232. No file system.  Many embedded applications do not support a file system.  In
  233. such cases, I/O functions within the module can be made to access ports.  The
  234. "file name" can be used to designate a port address when an open or create
  235. operation is performed.  Alternatively, file handles can be permanently
  236. assigned to specific ports.
  237.  
  238. Standard input/output.  File handles 0, 1, and 2 are pre-initialized to
  239. standard input, standard output, and standard error output, respectively.  The
  240. STDIO module relies on this convention in supplying the FILE* variables stdin,
  241. stdout, and stderr.
  242.  
  243. Function descriptions.  This section describes each function in the SYSTEM
  244. module that is referenced elsewhere by other modules in the Run-Time
  245. Libraries. No other functions in the SYSTEM module need be implemented unless
  246. your program explicitly references them.
  247.  
  248.  close(F)
  249.  
  250.     Closes the file associated with file handle F.  The value of F may be
  251.     subsequently reassigned in a call to c_open() or c_create().
  252.  
  253.  c_create(Name,Mode)
  254.  c_create_text(Name,Mode)
  255.  
  256.     Creates the file named Name if it does not exist; otherwise the file is
  257.     truncated to zero bytes.  Mode is the set of attributes to be assigned
  258.     to the file; the interpretation of Mode is system dependent.  If Mode is
  259.     the empty set, a default mode is used that is appropriate for the host
  260.     system.  A file handle is returned that can be used to reference the file.
  261.  
  262.     c_create_text() is called to create an ASCII-formatted file.  On systems
  263.     that make no distinction between ASCII and non-ASCII files these two
  264.     functions can be considered synonymous.
  265.  
  266.  dos_exit(RC)
  267.  
  268.     Terminates the program with a return code RC.  This function is referenced
  269.     only from the TERM module and need not be implemented if the application
  270.     has an alternate way of terminating a program.
  271.  
  272.  fileclass(F)
  273.  
  274.     Returns the type of file associated with file handle F: Console_input,
  275.     Console_output, or Disk_file.  This function is called by the STDIO module
  276.     to determine whether a file should be buffered.  I/O to the console is
  277.     unbuffered to allow prompting.
  278.  
  279.  get_date(Day,Month,Year)
  280.  get_time(Hrs,Mins,Secs)
  281.  clock()
  282.  
  283.     These are used by those functions that implement the Standard C library
  284.     functions in header file TIME.H.  get_date() returns 1-31 in Day, 1-12 in
  285.     Month, and 1980-2099 in Year.  get_time() returns elapsed wall-clock time:
  286.     0-23 in Hrs and 0-59 in Mins and Secs.  clock() returns elapsed wall-clock
  287.     time in hundredths of a second.
  288.  
  289.  lseek (F,Loc,Method)
  290.  lseek_(F,Loc,Method)
  291.  
  292.     lseek() repositions the file pointer associated with file handle F so that
  293.     the next read or write operates on the nth byte of the file (origin at
  294.     zero) where n is computed as follows:
  295.  
  296.     Value of n                 Value of Method
  297.     ------------------------------------------
  298.     Loc                        From_beginning
  299.     Loc+"current position"     From_current
  300.     Loc+"file size"            From_end
  301.  
  302.     lseek_() performs the same function as lseek(), but lseek_() also returns
  303.     the new file-pointer position.
  304.  
  305.     lseek()/lseek_() need not be implemented unless your program performs
  306.     random file access via the STDIO function fseek.
  307.  
  308.  c_open(Name,Method)
  309.  c_open_text(Name,Method)
  310.  
  311.     Opens the file named Name for input, output, or update.  If the file is
  312.     not found, errno is set to error_file_not_found; otherwise a file handle
  313.     is returned that can be used to reference the open file.  The
  314.     c_open_text() function is used to open a file that is to be interpreted as
  315.     an ASCII file with embedded end-of-line characters.  On systems that make
  316.     no distinction between ASCII and non-ASCII files, these two functions can
  317.     be considered synonymous.
  318.  
  319.     Text files need not be supported for update.
  320.  
  321.  read(F,BufP,Cnt)
  322.  
  323.     Reads Cnt bytes from the file or device associated with file handle F into
  324.     the buffer with address BufP.  Returns the number of bytes actually read,
  325.     which may be less than Cnt if fewer than Cnt bytes remain in the  file or
  326.     if reading from a line-at-a-time device such as a keyboard.
  327.  
  328.     When end-of-file is encountered, read() returns zero.
  329.  
  330.  c_unlink(Name)
  331.  
  332.     Deletes the file named Name.  This function is called when temporary High
  333.     C files are closed.
  334.  
  335.  write (F,BufP,Cnt)
  336.  write_(F,BufP,Cnt)
  337.  
  338.     Writes Cnt bytes to the file or device associated with file handle F from
  339.     the buffer with address BufP.  write() sets errno to error_write_failed if
  340.     fewer than Cnt bytes were written (often caused by a disk overflow).  The
  341.     function write_() returns the number of bytes actually written and does
  342.     not indicate an error condition if fewer than Cnt bytes are written.
  343.  
  344.  
  345. CONSOLE -- Console Input/Output
  346. -------------------------------
  347.  
  348. The CONSOLE module reads strings from the keyboard and writes strings to the
  349. screen.
  350.  
  351. Source files.  The CONSOLE module under the MS-DOS operating system consists
  352. of two source files: KB1.P and KB2.P (KB for keyboard).  gets(var S: String)
  353. reads a line from the keyboard and stores it in S.  If the line is too long to
  354. fit in S, the result is unpredictable.
  355.  
  356. This is in accordance with the Standard C gets() function which is inherently
  357. dangerous in this respect.  S is of the Professional Pascal String type.
  358. String s consist of two bytes to hold the current length len of the string,
  359. followed by an array of characters, the first len of which make up the string.
  360. When a String is passed by var, as S is here, the address of the string is
  361. passed, but in addition, the maximum length of the string must be available to
  362. the  receiving routine; thus, the Professional Pascal compiler passes the
  363. maximum length of the string as a hidden parameter.
  364.  
  365. To duplicate the interface in High C, something like the following is needed:
  366.  
  367.    pragma Calling_convention(PASCAL);
  368.    void gets(char *len_and_string, short
  369.    maxlen) {
  370.       short s_length; char *s;
  371.       s_length = *(short *)len_and_string;
  372.       s = len_and_string+2;
  373.       ...
  374.       }
  375.  
  376. puts(const S: String) writes S to the screen, without terminating the line.
  377.  
  378. Again, S is of the Professional Pascal String type.  When a String is passed
  379. by const, as S is here, the address of the string is passed.  The maximum
  380. length of the string need not be available to the receiving function, as the
  381. meaning of const is that the parameter cannot be modified; no hidden
  382. parameters are passed in this case.
  383.  
  384. newline() writes out the necessary characters to the screen to terminate the
  385. current line.
  386.  
  387. Under the MS-DOS operating system, file handle 2 is always directed to the
  388. screen for output.  Thus, puts() and newline() simply write to file handle 2
  389. via the SYSTEM function write.
  390.  
  391. Because standard input is not necessarily from the keyboard, the first call to
  392. gets() opens the keyboard for input and invokes read() from the SYSTEM module.
  393. The line-termination characters are then stripped from the string read in.
  394.  
  395.