home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_04 / 3n04014a < prev    next >
Text File  |  1992-02-20  |  4KB  |  154 lines

  1. #include "dma.h"
  2.  
  3. //--- External References ----------------
  4. extern void Dev_Start_Transmission( void );
  5. extern ULONG pfnDevHlp;  
  6. extern DMA_INFO DmaInfo;   // in "DMAISR.C"
  7.  
  8. //--- Local Functions --------------------
  9. void OS2_Call_During_Init( DMA_INFO * );
  10. USHORT GetData(  char *, USHORT );
  11. USHORT PutData(  char *, USHORT );
  12.  
  13. // This function MUST be called during driver 
  14. // initialization, NOT from the interrupt 
  15. // service routine. 
  16. void OS2_Call_During_Init( DMA_INFO *pDmaInfo )
  17. {
  18. #define DEVHLP_VIRT_TO_PHYS   0x16
  19.    USHORT   HiWord;
  20.    USHORT   LoWord;
  21.    char     *pBuf;
  22.  
  23.    // Use VirtToPhys DevHelp function to determine 
  24.    // physical address of first byte of driver's
  25.    // receive buffer.
  26.    pBuf = pDmaInfo->RxBuf.pStart;
  27.    _asm
  28.    {
  29.       mov   di, pDmaInfo
  30.       mov   si, pBuf
  31.       mov   dl, 16h     ; command code for VirtToPhys
  32.       call  pfnDevHlp   ; function pointer to kernel's 
  33.                         ; DevHelp entry point
  34.       mov   HiWord, ax  ; (AX) = high-order 16 bits of 
  35.                         ; phys addr
  36.       mov   LoWord, dx  ; (DX) = low-order 16 bits of
  37.                         ; phys addr
  38.    }
  39.    pDmaInfo->RxBuf.PhysAddr = MAKEULONG( LoWord, HiWord );
  40.    // Use VirtToPhys DevHelp function to determine 
  41.    // physical address of first byte of driver's
  42.    // transmit buffer.
  43.    pBuf = pDmaInfo->TxBuf.pStart;
  44.    _asm
  45.    {
  46.       mov   di, pDmaInfo
  47.       mov   si, pBuf
  48.       mov   dl, DEVHLP_VIRT_TO_PHYS                              
  49.       call  pfnDevHlp   ; function pointer to kernel's 
  50.                         ; DevHelp entry point
  51.       mov   HiWord, ax  ; (AX) = high-order 16 bits of 
  52.                         ; phys addr
  53.       mov   LoWord, dx  ; (DX) = low-order 16 bits of 
  54.                         ; phys addr
  55.    }
  56.    pDmaInfo->TxBuf.PhysAddr = MAKEULONG( LoWord, HiWord );
  57. }
  58.  
  59. USHORT GetData(char *buffer,  //  Appl. data location
  60.                USHORT length) //  Length to be copied
  61. {
  62.    char     *pOut, *pIn;  
  63.    USHORT   size, lengthx;
  64.  
  65.    // Derefence pointers for speed in loop.
  66.    pOut = DmaInfo.RxBuf.pOut;
  67.    pIn = DmaInfo.RxBuf.pIn;
  68.    lengthx = 0;
  69.    // Compute remaining free space in receive buffer
  70.    size = pIn - pOut;
  71.    if (size < 0)
  72.       size += DmaInfo.RxBuf.Size;
  73.    // If we can't give 'em all they want, 
  74.    // give 'em all we got
  75.    if (length > size)
  76.       length = size;
  77.    // Copy data in chunks until input data buffer filled
  78.    while (length)    // buffer length not exhausted
  79.    {
  80.       if (pIn > pOut)
  81.       {
  82.          size = pIn - pOut;
  83.          if (size > length)
  84.             size = length;
  85.          memcpy( buffer, pOut, size );
  86.          pOut += size;
  87.       }
  88.       else if (pIn < pOut)
  89.       {
  90.          size = DmaInfo.RxBuf.pEnd - pOut + 1;
  91.          if (size > length)
  92.             size = length;
  93.          memcpy( buffer, pOut, size );
  94.          pOut += size;
  95.          if (pOut > DmaInfo.RxBuf.pEnd)
  96.             pOut = DmaInfo.RxBuf.pStart;
  97.       }
  98.       else     // data in our buffer exhausted
  99.          break;
  100.  
  101.       length  -= size;
  102.       lengthx += size;
  103.       buffer  += size;
  104.    }
  105.    DmaInfo.RxBuf.pOut = pOut;
  106.    return( lengthx );   // number of bytes actually copied
  107. }
  108.  
  109. USHORT PutData(char *buffer,  //  Appl. data location
  110.                USHORT length) //  Length to be copied
  111. {
  112.    char     *pIn, *pOut;
  113.    USHORT   size, lengthx;
  114.  
  115.    // Dereference pointers for speed in loop.
  116.    pIn = DmaInfo.TxBuf.pIn;
  117.    pOut = DmaInfo.TxBuf.pOut;
  118.    lengthx = 0;
  119.    // Compute remaining free space in transmit buffer
  120.    size = pOut - pIn - 1;
  121.    if (size < 0)
  122.       size += DmaInfo.TxBuf.Size;
  123.    if (length > size)
  124.       length = size;
  125.    while (length)
  126.    {
  127.       if (pIn < pOut)
  128.       {
  129.          size = pOut - pIn - 1;
  130.          if (size > length)
  131.             size = length;
  132.          memcpy( pIn, buffer, size );
  133.          pIn += size;
  134.       }
  135.       else
  136.       {
  137.          size = DmaInfo.TxBuf.pEnd - pIn + 1;
  138.          if (size > length)
  139.             size = length;
  140.          memcpy( pIn, buffer, size );
  141.          pIn += size;
  142.          if (pIn > DmaInfo.TxBuf.pEnd)
  143.          pIn = DmaInfo.TxBuf.pStart;
  144.       }
  145.       length  -= size;
  146.       lengthx += size;
  147.       buffer  += size;
  148.    }
  149.    DmaInfo.TxBuf.pIn = pIn;
  150.    if (lengthx)
  151.       Dev_StartTransmission();
  152.    return( lengthx );
  153. }
  154.