aboutsummaryrefslogtreecommitdiff
path: root/src/winNT_portio.c
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2005-03-22 20:58:25 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2005-03-22 20:58:25 +0000
commit2b60d55c919e7588f5aff15936e83c300b3660bb (patch)
tree14d860de7f28083d3756ad91b627de70f26788f6 /src/winNT_portio.c
parentc500bc542cb7cc78d6dac3f7da3bff626056b1aa (diff)
zexy-2.0:
- use of abstractions for objects that allow it - some objects are build both as externals and abstractions (as slower fallbacks) - code-layout is now 1:1 c-file<->object (this should allow for building of zexy as a collection of externals instead as a big library) - matrix-objects have moved to iemmatrix !! svn path=/trunk/externals/zexy/; revision=2641
Diffstat (limited to 'src/winNT_portio.c')
-rw-r--r--src/winNT_portio.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/winNT_portio.c b/src/winNT_portio.c
new file mode 100644
index 0000000..d892977
--- /dev/null
+++ b/src/winNT_portio.c
@@ -0,0 +1,107 @@
+/*
+ * this is a wrapper for the cor port i/o functions for WinNT/2000/XP.
+ * this is to be replaced by some functions that are platform/interface
+ * specific to access the data lines.
+ * for now, this is only for parport access, but in future there will be a way
+ * to plug this on the usb bus.
+ * if the interface changes, only this file has to be adopted for the target system
+ */
+#ifdef NT
+
+#include <stdio.h>
+#include <windows.h>
+
+int read_parport(int port);
+void write_parport(int port, int value);
+int open_port(int port);
+
+static BOOL bPrivException = FALSE;
+
+int read_parport(int port)
+{
+ // byte = _inp((unsigned short)port);
+ unsigned char value;
+
+ __asm mov edx,port
+ __asm in al,dx
+ __asm mov value,al
+ return (int)value;
+}
+
+void write_parport(int port, int invalue)
+{
+ // _outp((unsigned short)port, value);
+ BYTE value = (BYTE)invalue;
+ __asm mov edx,port
+ __asm mov al,value
+ __asm out dx,al
+}
+
+static LONG WINAPI HandlerExceptionFilter ( EXCEPTION_POINTERS *pExPtrs )
+{
+
+ if (pExPtrs->ExceptionRecord->ExceptionCode == EXCEPTION_PRIV_INSTRUCTION)
+ {
+ pExPtrs->ContextRecord->Eip ++; // Skip the OUT or IN instruction that caused the exception
+ bPrivException = TRUE;
+ return EXCEPTION_CONTINUE_EXECUTION;
+ }
+ else
+ return EXCEPTION_CONTINUE_SEARCH;
+}
+
+static BOOL StartUpIoPorts(UINT PortToAccess, BOOL bShowMessageBox, HWND hParentWnd)
+{
+ HANDLE hUserPort;
+
+ hUserPort = CreateFile("\\\\.\\UserPort", GENERIC_READ, 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+ CloseHandle(hUserPort); // Activate the driver
+ Sleep(100); // We must make a process switch
+
+ SetUnhandledExceptionFilter(HandlerExceptionFilter);
+
+ bPrivException = FALSE;
+ read_parport(PortToAccess); // Try to access the given port address
+
+ if (bPrivException)
+ {
+ if (bShowMessageBox)
+ {
+#if 0
+ MessageBox(hParentWnd,"Privileged instruction exception has occured!\r\n\r\n"
+ "To use this external under Windows NT, 2000 or XP\r\n"
+ "you need to install the driver 'UserPort.sys' and grant\r\n"
+ "access to the ports used by this program.\r\n\r\n"
+ "See the file README for further information!\r\n", NULL, MB_OK);
+#endif
+ }
+ return FALSE;
+ }
+ return TRUE;
+}
+ /* check if we are running NT/2k/XP */
+static int IsWinNT(void)
+{
+ OSVERSIONINFO OSVersionInfo;
+ OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+
+ GetVersionEx(&OSVersionInfo);
+
+ return OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT;
+}
+
+ /* open parport */
+int open_port(int port)
+{
+ if(IsWinNT()) /* we are under NT and need kernel driver */
+ {
+ if(StartUpIoPorts(port, 1, 0))
+ return(0);
+ return(-1);
+ }
+ else /* no need to use kernel driver */
+ {
+ return(0);
+ }
+}
+#endif /* NT */