home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJDEV201.ZIP / include / libc / farptrgs.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-06  |  6.7 KB  |  237 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* special version for libc - uses %gs instead of %fs.  Ignore comments */
  3.  
  4. /* Copyright (c) 1995 DJ Delorie.  Permission granted to use for any
  5.    purpose, provided this copyright remains attached and unmodified.
  6.  
  7.    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  8.    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  9.    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  10.  
  11. ╔═══════════════════════════════════════════════════════════════════════╗
  12. ║        Far Pointer Simulation Functions            ║
  13. ╚═══════════════════════════════════════════════════════════════════════╝
  14.  
  15. This file attempts to make up for the lack of a "far" keyword in GCC. 
  16. Although it doesn't provide access to far call APIs (like Windows), it
  17. does allow you to do far pointer data access without the overhead of
  18. movedata() or dosmemget/dosmemput().
  19.  
  20. You should *always* include this file when using these functions and
  21. compile with optimization enabled.  They don't exist as normal functions
  22. in any library, and they compile down to only a few opcodes when used
  23. this way.  They are almost as fast as native pointer operations, and
  24. about as fast as far pointers can get. 
  25.  
  26. If you don't use optimization, this file becomes prototypes for
  27. farptr.c, which generates real functions for these when not optimizing. 
  28. When optimizing, farptr.c compiles to nothing. 
  29.  
  30. There are two types of functions here - standalone and invariant.  The
  31. standalone functions take a selector and offset.  These are used when
  32. you need only a few accesses, time isn't critical, or you don't know
  33. what's in the %gs register.  The invariant ones don't take a selector,
  34. they only take an offset.  These are used inside loops and in
  35. time-critical accesses where the selector doesn't change.  To specify
  36. the selector, use the farsetsel() function.  That selector is used for
  37. all farns*() functions until changed. 
  38.  
  39. The farpoke* and farpeek* take selectors.
  40.  
  41. The farnspoke* and farnspeek* don't (note the `ns' for `no selector').
  42.  
  43. Warning: These routines all use the %gs register for their accesses. 
  44. GCC normally uses only %ds and %es, and libc functions (movedata,
  45. dosmemget, dosmemput) use %gs.  Still, you should be careful about
  46. assumptions concerning whether or not the value you put in %gs will be
  47. preserved across calls to other functions.  If you guess wrong, your
  48. program will crash.  Better safe than sorry. 
  49.  
  50. */
  51.  
  52. #ifndef __dj_include_sys_farptr_h_
  53. #define __dj_include_sys_farptr_h_
  54.  
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58.  
  59. #ifndef __dj_ENFORCE_ANSI_FREESTANDING
  60.  
  61. #ifndef __STRICT_ANSI__
  62.  
  63. #ifndef _POSIX_SOURCE
  64.  
  65. void _farpokeb(unsigned short, unsigned long, unsigned char);
  66. void _farpokew(unsigned short, unsigned long, unsigned short);
  67. void _farpokel(unsigned short, unsigned long, unsigned long);
  68. unsigned char _farpeekb(unsigned short, unsigned long);
  69. unsigned short _farpeekw(unsigned short, unsigned long);
  70. unsigned long _farpeekl(unsigned short, unsigned long);
  71. void _farsetsel(unsigned short);
  72. void _farnspokeb(unsigned long, unsigned char);
  73. void _farnspokew(unsigned long, unsigned short);
  74. void _farnspokel(unsigned long, unsigned long);
  75. unsigned char _farnspeekb(unsigned long);
  76. unsigned short _farnspeekw(unsigned long);
  77. unsigned long _farnspeekl(unsigned long);
  78.  
  79. extern __inline__ void
  80. _farpokeb(unsigned short selector,
  81.      unsigned long offset,
  82.      unsigned char value)
  83. {
  84.   __asm__ __volatile__ ("movw %w0,%%gs\n"
  85.       "    .byte 0x65 \n"
  86.       "    movb %b1,(%k2)"
  87.       :
  88.       : "g" (selector), "qi" (value), "r" (offset));
  89. }
  90.  
  91. extern __inline__ void
  92. _farpokew(unsigned short selector,
  93.      unsigned long offset,
  94.      unsigned short value)
  95. {
  96.   __asm__ __volatile__ ("movw %w0,%%gs \n"
  97.       "    .byte 0x65 \n"
  98.       "    movw %w1,(%k2)"
  99.       :
  100.       : "g" (selector), "ri" (value), "r" (offset));
  101. }
  102.  
  103. extern __inline__ void
  104. _farpokel(unsigned short selector,
  105.      unsigned long offset,
  106.      unsigned long value)
  107. {
  108.   __asm__ __volatile__ ("movw %w0,%%gs \n"
  109.       "    .byte 0x65 \n"
  110.       "    movl %k1,(%k2)"
  111.       :
  112.       : "g" (selector), "ri" (value), "r" (offset));
  113. }
  114.  
  115. extern __inline__ unsigned char
  116. _farpeekb(unsigned short selector,
  117.      unsigned long offset)
  118. {
  119.   unsigned char result;
  120.   __asm__ __volatile__ ("movw %w1,%%gs \n"
  121.       "    .byte 0x65 \n"
  122.       "    movb (%k2),%b0"
  123.       : "=q" (result)
  124.       : "g" (selector), "r" (offset));
  125.   return result;
  126. }
  127.  
  128. extern __inline__ unsigned short
  129. _farpeekw(unsigned short selector,
  130.      unsigned long offset)
  131. {
  132.   unsigned short result;
  133.   __asm__ __volatile__ ("movw %w1, %%gs \n"
  134.       "    .byte 0x65 \n"
  135.       "    movw (%k2),%w0 \n"
  136.       : "=r" (result)
  137.       : "g" (selector), "r" (offset));
  138.   return result;
  139. }
  140.  
  141. extern __inline__ unsigned long
  142. _farpeekl(unsigned short selector,
  143.      unsigned long offset)
  144. {
  145.   unsigned long result;
  146.   __asm__ __volatile__ ("movw %w1,%%gs\n"
  147.       "    .byte 0x65\n"
  148.       "    movl (%k2),%k0"
  149.       : "=r" (result)
  150.       : "g" (selector), "r" (offset));
  151.   return result;
  152. }
  153.  
  154. extern __inline__ void
  155. _farsetsel(unsigned short selector)
  156. {
  157.   __asm__ __volatile__ ("movw %w0,%%gs"
  158.       :
  159.       : "g" (selector));
  160. }
  161.  
  162. extern __inline__ void
  163. _farnspokeb(unsigned long offset,
  164.      unsigned char value)
  165. {
  166.   __asm__ __volatile__ (".byte 0x65\n"
  167.       "    movb %b0,(%k1)"
  168.       :
  169.       : "qi" (value), "r" (offset));
  170. }
  171.  
  172. extern __inline__ void
  173. _farnspokew(unsigned long offset,
  174.      unsigned short value)
  175. {
  176.   __asm__ __volatile__ (".byte 0x65\n"
  177.       "    movw %w0,(%k1)"
  178.       :
  179.       : "ri" (value), "r" (offset));
  180. }
  181.  
  182. extern __inline__ void
  183. _farnspokel(unsigned long offset,
  184.      unsigned long value)
  185. {
  186.   __asm__ __volatile__ (".byte 0x65\n"
  187.       "    movl %k0,(%k1)"
  188.       :
  189.       : "ri" (value), "r" (offset));
  190. }
  191.  
  192. extern __inline__ unsigned char
  193. _farnspeekb(unsigned long offset)
  194. {
  195.   unsigned char result;
  196.   __asm__ __volatile__ (".byte 0x65\n"
  197.       "    movb (%k1),%b0"
  198.       : "=q" (result)
  199.       : "r" (offset));
  200.   return result;
  201. }
  202.  
  203. extern __inline__ unsigned short
  204. _farnspeekw(unsigned long offset)
  205. {
  206.   unsigned short result;
  207.   __asm__ __volatile__ (".byte 0x65\n"
  208.       "    movw (%k1),%w0"
  209.       : "=r" (result)
  210.       : "r" (offset));
  211.   return result;
  212. }
  213.  
  214. extern __inline__ unsigned long
  215. _farnspeekl(unsigned long offset)
  216. {
  217.   unsigned long result;
  218.   __asm__ __volatile__ (".byte 0x65\n"
  219.       "    movl (%k1),%k0"
  220.       : "=r" (result)
  221.       : "r" (offset));
  222.   return result;
  223. }
  224.  
  225. #endif /* !_POSIX_SOURCE */
  226. #endif /* !__STRICT_ANSI__ */
  227. #endif /* !__dj_ENFORCE_ANSI_FREESTANDING */
  228.  
  229. #ifndef __dj_ENFORCE_FUNCTION_CALLS
  230. #endif /* !__dj_ENFORCE_FUNCTION_CALLS */
  231.  
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235.  
  236. #endif /* !__dj_include_sys_farptr_h_ */
  237.