//*** TDLPortIO: DriverLINX Port IO Driver wrapper component *****************
//**                                                                        **
//** File: TDLPortIO.cpp                                                    **
//**                                                                        **
//** Copyright (c) 1999 John Pappas (DiskDude). All rights reserved.        **
//**     This software is FreeWare.                                         **
//**                                                                        **
//**     Please notify me if you make any changes to this file.             **
//**     Email: diskdude@poboxes.com                                        **
//**                                                                        **
//**                                                                        **
//** The following resources helped in developing the install, start, stop  **
//** and remove code for dynamically opening/closing the DriverLINX WinNT   **
//** kernel mode driver.                                                    **
//**                                                                        **
//**   "Dynamically Loading Drivers in Windows NT" by Paula Tomlinson       **
//**   from "Windows Developer's Journal", Volume 6, Issue 5. (C code)      **
//**      ftp://ftp.mfi.com/pub/windev/1995/may95.zip                       **
//**                                                                        **
//**   "Hardware I/O Port Programming with Delphi and NT" by Graham Wideman **
//**      http://www.wideman-one.com/tech/Delphi/IOPM/index.htm             **
//**                                                                        **
//**                                                                        **
//** Special thanks to Peter Holm <comtext3@post4.tele.dk> for his          **
//** algorithm and code for detecting the number and addresses of the       **
//** installed printer ports, on which the detection code below is based.   **
//**                                                                        **
//** this Version add support for Win2000 and XP                            **
//** Modified 07.12.03 DJMSoftware                                          **
//**                                                                        **
//**                                                                        **
//**                                                                        **
//*** http://diskdude.cjb.net/ ***********************************************
//---------------------------------------------------------------------------

#include <basepch.h>

#pragma hdrstop
#include "DLPortIO.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
//  ValidCtrCheck est utilisé pour vérifier que les composants créés n'ont
// aucune fonction virtuelle pure.
//

static inline void ValidCtrCheck(TDLPortIO *)
{
        new TDLPortIO(NULL);
}
//---------------------------------------------------------------------------
__fastcall TDLPortIO::TDLPortIO(TComponent* Owner)
        : TComponent(Owner)
{
  mDriver.DetectPorts();
}
//---------------------------------------------------------------------------
namespace Dlportio
{
        void __fastcall PACKAGE Register()
        {
                 TComponentClass classes[1] = {__classid(TDLPortIO)};
                 RegisterComponents("DiskDude", classes, 0);
        }
}
//---------------------------------------------------------------------------

void __fastcall TDLPortIO::PortControl(TPortRec Ports[], WORD NumPorts)
{
#ifndef FAST
   if (! mDriver.IsLoaded()) return;
#endif

   for (int Index=0; Index<NumPorts; Index++)
      if (Ports[Index].fWrite)
         mDriver.DlWriteByte(Ports[Index].PortAddr, Ports[Index].PortData);
      else
         Ports[Index].PortData=mDriver.DlReadByte(Ports[Index].PortAddr);
}
//---------------------------------------------------------------------------

void __fastcall TDLPortIO::PortCommand(TPortCommand Ports[], WORD NumPorts)
{
#ifndef FAST
   if (!mDriver.IsLoaded()) return;
#endif

   for (int Index=0; Index<NumPorts; Index++)
      switch (Ports[Index].PortMode)
      {
         case tmReadByte:
            Ports[Index].PortData.Byte=mDriver.DlReadByte(Ports[Index].PortAddr);
            break;

         case tmReadWord:
            Ports[Index].PortData.Word=mDriver.DlReadWord(Ports[Index].PortAddr);
            break;

         case tmReadDWord:
            Ports[Index].PortData.DWord=mDriver.DlReadDWord(Ports[Index].PortAddr);
            break;

         case tmWriteByte:
            mDriver.DlWriteByte(Ports[Index].PortAddr, Ports[Index].PortData.Byte);
            break;

         case tmWriteWord:
            mDriver.DlWriteWord(Ports[Index].PortAddr, Ports[Index].PortData.Word);
            break;

         case tmWriteDWord:
            mDriver.DlWriteDWord(Ports[Index].PortAddr, Ports[Index].PortData.DWord);
            break;

         default:
            break; // Ignore it
      }
}

//---------------------------------------------------------------------------
int __fastcall TDLPortIO::GetLPTBasePort(char PortNr)
{
 if((PortNr<1) ||(PortNr >8)) return 0;
 return mDriver.FLPTAddress[PortNr];
}