/*****************************************************************************
   Name:             convert.c

   Description:      Contains the various conversion routines used by the
                     dump formatter.

   Contains:         SelToPhysical     - convert a selector to a physical addr
                     SelToLinear       - convert a selector to a linear addr

   Comments:

   History:          11/04/92 - crs - New
*****************************************************************************/

/*****************************************************************************
                            Include Files
*****************************************************************************/
#define  INCL_DOS
#include <os2.h>
#include "df.h"

/*****************************************************************************
                         External References
*****************************************************************************/
extern USHORT LinearToPhysical( ULONG ulLinear, ULONG *pulPhysAddr );

extern ULONG FAR *ptrGDT;              // pointer to GDT
extern ULONG FAR *ptrLDT;              // pointer to LDT

/*****************************************************************************
   SelToPhysical

   Comments:   Maps a selector to a physical address.

   Input:      sel         - selector to work with
               pulPhysAddr - gets the computed address

   Output:     The return code from LinearToPhysical()
*****************************************************************************/
USHORT SelToPhysical( USHORT sel, ULONG *pulPhysAddr )
{
   DESCRIPTOR FAR *gdtDesc;
   ULONG          ulLinear;
   USHORT         usRC;

   // First we get the GDT descriptor from the GDT Table
   gdtDesc = (DESCRIPTOR FAR *)((ULONG) ptrGDT + sel);

   // Now we get the address in the selected GDT descriptor.
   ulLinear = (((ULONG)(SHIFT_LEFT(gdtDesc->base_hi,24)) +
               ((ULONG)(SHIFT_LEFT(gdtDesc->base_mid,16)) +
               ((ULONG)gdtDesc->base_lo))));

   // Now convert the linear address to a physical address
   usRC = LinearToPhysical( ulLinear, pulPhysAddr );

   return( usRC );
}

/*****************************************************************************
   SelToLinear

   Comments:   Converts a selector to a linear address.

   Input:      sel         - selector to work with
               pulLinear   - gets the linear address

   Output:     None.
*****************************************************************************/
VOID SelToLinear( USHORT sel, ULONG *pulLinear)
{
   DESCRIPTOR FAR *ptrDesc;         // pointer to descriptor table entry
   USHORT         usOffset;         // Offset in num.of descr.table entries

   usOffset = sel / DESCRIPTOR_SIZE;// selector handle value

   if ( sel & LDT_FLAG )            // 4 bit on = a LDT selector
      ptrDesc = (DESCRIPTOR FAR *) ptrLDT + usOffset;
   else                             // 4 bit off = GDT selector
      ptrDesc = (DESCRIPTOR FAR *) ptrGDT + usOffset;

   // Now we get the address in the selected GDT descriptor.
   *pulLinear = ((ULONG)(SHIFT_LEFT(ptrDesc->base_hi,24 ))
               + (ULONG)(SHIFT_LEFT(ptrDesc->base_mid,16))
               + (ULONG)ptrDesc->base_lo );

   return;
}

/*****************************************************************************
                             End Of File
*****************************************************************************/

