home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kompon / d2345 / ZLPORTIO.ZIP / zlportio.pas < prev   
Pascal/Delphi Source File  |  2001-06-30  |  9KB  |  266 lines

  1. { -----------------------------------------------------------------------------}
  2. { Copyright 2000-2001, Zloba Alexander.  All Rights Reserved.                  }
  3. { This unit can be freely used and distributed in commercial and private       }
  4. { environments, provided this notice is not modified in any way.               }
  5. { -----------------------------------------------------------------------------}
  6. { Feel free to contact me if you have any questions, comments or suggestions at}
  7. {   zal@specosoft.com (Zloba Alexander)                                        }
  8. { You can always find the latest version of this unit at:                      }
  9. {   http://www.specosoft.com                                                   }
  10.  
  11. { -----------------------------------------------------------------------------}
  12. { Date last modified:  30/06/2001                                              }
  13. { -----------------------------------------------------------------------------}
  14. { ZLPortIO driver interface unit v1.20                                         }
  15. { -----------------------------------------------------------------------------}
  16. { Description:                                                                 }
  17. {   This unit allow your application direct access port input and output under }
  18. {   all versions of Microsoft Windows«                                         }
  19. { Depends:                                                                     }
  20. {   zlportio.sys ddkint.pas                                                    }
  21. {   You must distribute zlportio.sys with your application                     }
  22. { Procedures and functions:                                                    }
  23. {   procedure zlioportread( const Port,DataType:dword ):dword;                 }
  24. {   procedure zlioportwrite( const Port,DataType,Data:dword );                 }
  25. {                                                                              }
  26. {  function portreadb( const Port:dword ):byte;                                }
  27. {  function portreadw( const Port:dword ):word;                                }
  28. {  function portreadl( const Port:dword ):dword;                               }
  29. {                                                                              }
  30. {  procedure portwriteb( const Port:Dword;const Data:byte );                   }
  31. {  procedure portwritew( const Port:dword;const Data:word );                   }
  32. {  procedure portwritel( const Port,Data:dword );                              }
  33. {                                                                              }
  34. {   Examples:                                                                  }
  35. {    //  get data bits from LPT port                                           }
  36. {      databits := portreadb( $378 )                                           }
  37. {    //  set data bits from LPT port                                           }
  38. {      portwriteb( $378, databits )                                            }
  39. {    //  The second parameter determine the databus length for operation       }
  40. { -----------------------------------------------------------------------------}
  41. { Revision History:                                                            }
  42. { 1.00:  + First public release                                                }
  43. { 1.10:  + Added new functions (portreadX,portwriteX) for convenience of usage }
  44. { 1.20:  + Added new function (zliosetiopm) for enabling direct access to ports}
  45. {------------------------------------------------------------------------------}
  46.  
  47. unit zlportio;
  48.  
  49. interface
  50.  
  51. uses windows,sysutils,ddkint;
  52.  
  53. Const
  54.   ZLIO_BYTE  = 0;
  55.   ZLIO_WORD  = 1;
  56.   ZLIO_DWORD = 2;
  57.  
  58. var
  59.  
  60. // if TRUE then driver was started
  61. // in other case something wrong
  62. // We start driver in initialization section of unit.
  63.  
  64.   ZlIOStarted:boolean = false;
  65.  
  66. // if TRUE then we can use asm IN,OUT under NT/2000
  67. // see zliosetiopm for more details
  68.   ZlIODirect:boolean = false;
  69.  
  70.  
  71. function portreadb( const Port:dword ):byte;
  72. function portreadw( const Port:dword ):word;
  73. function portreadl( const Port:dword ):dword;
  74.  
  75. procedure portwriteb( const Port:Dword;const Data:byte );
  76. procedure portwritew( const Port:dword;const Data:word );
  77. procedure portwritel( const Port,Data:dword );
  78.  
  79.  
  80. procedure zlioportwrite( const Port,DataType,Data:dword );
  81. function zlioportread( const Port,DataType:dword ):dword;
  82.  
  83.  
  84. // if you need the best perfomance for your IO operations
  85. // call zliosetiopm(TRUE). This allow your application
  86. // to use asm command IN,OUT directly in your code.
  87.  
  88. procedure zliosetiopm( const Direct:boolean );
  89.  
  90. // internal
  91.  
  92. function zliostart:boolean;
  93. procedure zliostop;
  94.  
  95.  
  96. implementation
  97.  
  98. const
  99.   ZLIODriverName='zlportio';
  100.  
  101. var
  102.   IOCTL_ZLUNI_PORT_READ:cardinal;
  103.   IOCTL_ZLUNI_PORT_WRITE:cardinal;
  104.   IOCTL_ZLUNI_IOPM_ON:cardinal;
  105.   IOCTL_ZLUNI_IOPM_OFF:cardinal;
  106.  
  107.   HZLIO:THandle;
  108.  
  109. type
  110. TzlIOData = record
  111.   Port,DataType,Data:dword;
  112. end;
  113.  
  114.  
  115. procedure zlioportwrite( const Port,DataType,Data:dword );
  116. var resdata:TZLIOData;
  117.     cBR:cardinal;
  118. begin
  119.  if (not ZLIODirect) then begin
  120.   resdata.Port := Port;
  121.   resdata.Data :=  Data;
  122.   resdata.DataType :=  DataType;
  123.   if ZLIOStarted then
  124.    DeviceIoControl(HZLIO,IOCTL_ZLUNI_PORT_WRITE,@resdata,sizeof(resdata),nil,0,cBR,nil );
  125.  end
  126.  else begin
  127.    Case DataType of
  128.     ZLIO_BYTE : asm mov edx,Port;mov eax,data;out dx,al; end;
  129.     ZLIO_WORD : asm mov edx,Port;mov eax,data;out dx,ax; end;
  130.     ZLIO_DWORD: asm mov edx,Port;mov eax,data;out dx,eax; end;
  131.    end;
  132.  end;
  133. end;
  134.  
  135. function zlioportread(const Port,DataType:dword):dword;
  136. var resdata:TZLIOData;
  137.     cBR:cardinal;i:dword;
  138. begin
  139.  if (not ZLIODirect) then begin
  140.    resdata.Port := Port;
  141.    resdata.DataType :=  DataType;
  142.    if ZLIOStarted then
  143.     DeviceIoControl(HZLIO,IOCTL_ZLUNI_PORT_READ,@resdata,sizeof(resdata),@i,sizeof(dword),cBR,nil );
  144.  end
  145.  else begin
  146.    Case DataType of
  147.     ZLIO_BYTE : asm mov edx,Port;xor eax,eax;in al,dx;mov i,eax; end;
  148.     ZLIO_WORD : asm mov edx,Port;xor eax,eax;in ax,dx;mov i,eax; end;
  149.     ZLIO_DWORD: asm mov edx,Port;xor eax,eax;in eax,dx;mov i,eax end;
  150.    end;
  151.  end;
  152.   result := i;
  153. end;
  154.  
  155. function portreadb( const Port:dword ):byte;
  156. begin
  157.  Result := zlioportread(Port,ZLIO_BYTE);
  158. end;
  159.  
  160.  
  161. function portreadw( const Port:dword ):word;
  162. begin
  163.  Result := zlioportread(Port,ZLIO_WORD);
  164. end;
  165.  
  166. function portreadl( const Port:dword ):dword;
  167. begin
  168.  Result := zlioportread(Port,ZLIO_DWORD);
  169. end;
  170.  
  171. procedure portwriteb( const Port:Dword;const Data:byte );
  172. begin
  173.  zlioportwrite(Port,ZLIO_BYTE,Data);
  174. end;
  175.  
  176. procedure portwritew( const Port:dword;const Data:word );
  177. begin
  178.  zlioportwrite(Port,ZLIO_WORD,Data);
  179. end;
  180.  
  181. procedure portwritel( const Port,Data:dword );
  182. begin
  183.  zlioportwrite(Port,ZLIO_DWORD,Data);
  184. end;
  185.  
  186. procedure zliosetiopm( const Direct:boolean );
  187. var cBR:cardinal;
  188. begin
  189.  if Win32Platform=VER_PLATFORM_WIN32_NT then
  190.   if ZLIOStarted then begin
  191.    if Direct then
  192.      DeviceIoControl(HZLIO,IOCTL_ZLUNI_IOPM_ON,nil,0,nil,0,cBR,nil )
  193.    else
  194.      DeviceIoControl(HZLIO,IOCTL_ZLUNI_IOPM_OFF,nil,0,nil,0,cBR,nil );
  195.    ZLIODirect := Direct;
  196.  end
  197. end;
  198.  
  199.  
  200. function zliostart;
  201. var dir:shortstring;
  202. begin
  203.  if Win32Platform<>VER_PLATFORM_WIN32_NT then begin
  204.   result := true;
  205.   exit;
  206.  end;
  207.  Result := false;
  208.  zliostop;
  209.  dir := ExtractFileDir(ParamStr(0))+'\'+ZLIODriverName+'.sys'#0;
  210.  driverinstall(pchar(@dir[1]),ZLIODriverName+#0);
  211.  Result := driverstart(ZLIODriverName) = 0;
  212. end;
  213.  
  214. procedure zliostop;
  215. begin
  216.  if Win32Platform<>VER_PLATFORM_WIN32_NT then
  217.   exit;
  218.  driverstop(ZLIODriverName);
  219.  driverremove(ZLIODriverName);
  220. end;
  221.  
  222. function zlioopen( var Handle:thandle):boolean;
  223. var cERR:integer;
  224. begin
  225.  if Win32Platform<>VER_PLATFORM_WIN32_NT then begin
  226.   result := true;
  227.   exit;
  228.  end;
  229.  Result := false;
  230.  Handle := THandle(-1);
  231.  Handle := createFile('\\.\ZLPORTIO',
  232.                   GENERIC_READ or GENERIC_WRITE,
  233.                   0,
  234.                   nil,
  235.                   OPEN_EXISTING,
  236.                   FILE_ATTRIBUTE_NORMAL,
  237.                   0 );
  238.  cERR := getlasterror;
  239.  if (cERR = ERROR_ALREADY_EXISTS)or(cERR = 0) then Result := True;
  240. end;
  241.  
  242. procedure zlioclose( const Handle:thandle);
  243. begin
  244.  if (Win32Platform=VER_PLATFORM_WIN32_NT) then
  245.  closehandle(Handle);
  246. end;
  247.  
  248.  
  249. initialization
  250. IOCTL_ZLUNI_PORT_READ := CTL_CODE(FILE_DEVICE_KRNLDRVR, 1, METHOD_BUFFERED, FILE_ANY_ACCESS);
  251. IOCTL_ZLUNI_PORT_WRITE := CTL_CODE(FILE_DEVICE_KRNLDRVR, 2, METHOD_BUFFERED, FILE_ANY_ACCESS);
  252. IOCTL_ZLUNI_IOPM_ON := CTL_CODE(FILE_DEVICE_KRNLDRVR, 3, METHOD_BUFFERED, FILE_ANY_ACCESS);
  253. IOCTL_ZLUNI_IOPM_OFF := CTL_CODE(FILE_DEVICE_KRNLDRVR, 4, METHOD_BUFFERED, FILE_ANY_ACCESS);
  254.  
  255.  if zliostart then
  256.   ZLIOStarted := zlioopen(HZLIO) or (Win32Platform<>VER_PLATFORM_WIN32_NT);
  257.  
  258. finalization
  259.  
  260. if ZLIOStarted then
  261.  zliostop;
  262.  
  263.  
  264.  
  265. end.
  266.