home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l350 / 3.ddi / EXAMPLES / MDRAW / REALBRK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-05  |  3.4 KB  |  95 lines

  1. /************************************************************************/
  2. /*    Copyright (C) 1986-1993 Phar Lap Software, Inc.            */
  3. /*    Unpublished - rights reserved under the Copyright Laws of the    */
  4. /*    United States.  Use, duplication, or disclosure by the         */
  5. /*    Government is subject to restrictions as set forth in         */
  6. /*    subparagraph (c)(1)(ii) of the Rights in Technical Data and     */
  7. /*    Computer Software clause at 252.227-7013.            */
  8. /*    Phar Lap Software, Inc., 60 Aberdeen Ave., Cambridge, MA 02138    */
  9. /************************************************************************/
  10. /* REALBREAK.C:  Get address of function in loaded into conventional */
  11. /*         memory with the -REALBREAK switch */
  12.  
  13. #include <stdio.h>
  14. #include <pltypes.h>
  15. #include <pharlap.h>
  16.  
  17. BOOL RealBreak(ULONG FuncOffs, ULONG EndOffs, REALPTR *RealFuncpp)
  18. /*
  19. Description:
  20.     This routine takes the protected mode address of a function, and
  21.     calculates the real mode address of the function in conventional
  22.     memory.  The function must have been loaded into conventional
  23.     memory by using the 386|DOS-Extender -REALBREAK switch.
  24.  
  25. Calling arguments:
  26.     FuncOffs    PROTECTED mode address (offset in code segment)
  27.             of the function
  28.     EndOffs        PROTECTED mode address (offset in code/data segment)
  29.             of end of real mode code and data
  30.     RealFuncpp    Returned;  REAL mode address of the function.  The
  31.             real mode offset of the function (RP_OFF(*RealFuncpp))
  32.             will always be equal to the protected mode address
  33.             of the function (FuncOffs).  This property of the
  34.             real mode address allows the real mode code to
  35.             reference code and data symbolically, because the
  36.             real mode offsets and the protected mode offsets
  37.             are the same.
  38.  
  39. Returned values:
  40.     TRUE        if error
  41.     FALSE        if success
  42. */
  43. {
  44.     int    errv;        // error value from 386|DOS-Extender call
  45.     REALPTR    Realp;        // Real mode addr of function
  46.     ULONG    RealLinAdr;    // Linear address of real mode function
  47.     ULONG    RealSeg;    // Real mode segment address for real mode code
  48.  
  49. //
  50. // Check to make sure all our real mode offsets fall in the first 64K of
  51. // our protected mode segment.  If this is not true, we can't set up
  52. // our real mode pointers the way we want to, with real mode == protected mode
  53. // offsets
  54. //
  55.     if (EndOffs < FuncOffs || EndOffs > 0x10000)
  56.     {
  57.         printf("Illegal real mode offsets: %08Xh, %08Xh\n", FuncOffs,
  58.                             EndOffs);
  59.         return TRUE;
  60.     }
  61.  
  62. //
  63. // Get the address of where the real mode code was
  64. // loaded by calling 386|DOS-Extender.
  65. //
  66.     errv = _dx_torealn((void *) FuncOffs, EndOffs - FuncOffs, &Realp);
  67.     if (errv != 0)
  68.     {
  69.         printf("Error from _dx_torealn, function at offset %08Xh \
  70. not in conventional memory\n", FuncOffs);
  71.         return TRUE;
  72.     }
  73.  
  74. //
  75. // Now adjust the real mode address so offsets in the real mode segment
  76. // are the same as offsets in the protected mode segment.  This is required
  77. // in order for real mode code to set DS = CS and reference real mode
  78. // code and data symbolically, using link-time offsets calculated by
  79. // the protected-mode link.
  80. //
  81.     RealLinAdr = ((ULONG) RP_SEG(Realp) << 4) + (ULONG) RP_OFF(Realp);
  82.     RealLinAdr -= FuncOffs;
  83.     if ((RealLinAdr & 0xF) != 0)
  84.     {
  85.         // This should NEVER happen, since all protected mode code
  86.         // is always 4K page-aligned
  87.         printf("Error:  Real mode code base not paragraph-aligned\n");
  88.         return TRUE;
  89.     }
  90.     RealSeg = (USHORT) (RealLinAdr >> 4);
  91.     RP_SET(*RealFuncpp, FuncOffs, RealSeg);
  92.  
  93.     return FALSE;
  94. }
  95.