* Sync up to trunk head (r64921).
[reactos.git] / subsystems / ntvdm / io.c
index 3c6782f..53ff8dc 100644 (file)
@@ -55,33 +55,34 @@ typedef struct _EMULATOR_IOPORT_HANDLERS
  */
 EMULATOR_IOPORT_HANDLERS IoPortProc[EMULATOR_MAX_IOPORTS_NUM] = {{NULL}};
 
-/* PRIVATE FUNCTIONS **********************************************************/
+/* PUBLIC FUNCTIONS ***********************************************************/
 
-static VOID
-IOReadB(ULONG  Port,
-        PUCHAR Buffer)
+UCHAR
+IOReadB(USHORT Port)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.InB)
     {
-        *Buffer = IoPortProc[Port].IoHandlers.InB(Port);
+        return IoPortProc[Port].IoHandlers.InB(Port);
     }
     else if (IoPortProc[Port].hVdd > 0 &&
              IoPortProc[Port].VddIoHandlers.inb_handler)
     {
+        UCHAR Data;
         ASSERT(Port <= MAXWORD);
-        IoPortProc[Port].VddIoHandlers.inb_handler((WORD)Port, Buffer);
+        IoPortProc[Port].VddIoHandlers.inb_handler((WORD)Port, &Data);
+        return Data;
     }
     else
     {
         /* Return an empty port byte value */
-        DPRINT1("Read from unknown port: 0x%X\n", Port);
-        *Buffer = 0xFF;
+        DPRINT("Read from unknown port: 0x%X\n", Port);
+        return 0xFF;
     }
 }
 
-static VOID
-IOReadStrB(ULONG  Port,
+VOID
+IOReadStrB(USHORT  Port,
            PUCHAR Buffer,
            ULONG  Count)
 {
@@ -99,34 +100,34 @@ IOReadStrB(ULONG  Port,
     }
     else
     {
-        while (Count--) IOReadB(Port, Buffer++);
+        while (Count--) *Buffer++ = IOReadB(Port);
     }
 }
 
-static VOID
-IOWriteB(ULONG  Port,
-         PUCHAR Buffer)
+VOID
+IOWriteB(USHORT Port,
+         UCHAR Buffer)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.OutB)
     {
-        IoPortProc[Port].IoHandlers.OutB(Port, *Buffer);
+        IoPortProc[Port].IoHandlers.OutB(Port, Buffer);
     }
     else if (IoPortProc[Port].hVdd > 0 &&
              IoPortProc[Port].VddIoHandlers.outb_handler)
     {
         ASSERT(Port <= MAXWORD);
-        IoPortProc[Port].VddIoHandlers.outb_handler((WORD)Port, *Buffer);
+        IoPortProc[Port].VddIoHandlers.outb_handler((WORD)Port, Buffer);
     }
     else
     {
         /* Do nothing */
-        DPRINT1("Write to unknown port: 0x%X\n", Port);
+        DPRINT("Write to unknown port: 0x%X\n", Port);
     }
 }
 
-static VOID
-IOWriteStrB(ULONG  Port,
+VOID
+IOWriteStrB(USHORT  Port,
             PUCHAR Buffer,
             ULONG  Count)
 {
@@ -144,38 +145,39 @@ IOWriteStrB(ULONG  Port,
     }
     else
     {
-        while (Count--) IOWriteB(Port, Buffer++);
+        while (Count--) IOWriteB(Port, *Buffer++);
     }
 }
 
-static VOID
-IOReadW(ULONG   Port,
-        PUSHORT Buffer)
+USHORT
+IOReadW(USHORT Port)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.InW)
     {
-        *Buffer = IoPortProc[Port].IoHandlers.InW(Port);
+        return IoPortProc[Port].IoHandlers.InW(Port);
     }
     else if (IoPortProc[Port].hVdd > 0 &&
              IoPortProc[Port].VddIoHandlers.inw_handler)
     {
+        USHORT Data;
         ASSERT(Port <= MAXWORD);
-        IoPortProc[Port].VddIoHandlers.inw_handler((WORD)Port, Buffer);
+        IoPortProc[Port].VddIoHandlers.inw_handler((WORD)Port, &Data);
+        return Data;
     }
     else
     {
         UCHAR Low, High;
 
         // FIXME: Is it ok on Little endian and Big endian ??
-        IOReadB(Port, &Low);
-        IOReadB(Port + sizeof(UCHAR), &High);
-        *Buffer = MAKEWORD(Low, High);
+        Low  = IOReadB(Port);
+        High = IOReadB(Port + sizeof(UCHAR));
+        return MAKEWORD(Low, High);
     }
 }
 
-static VOID
-IOReadStrW(ULONG   Port,
+VOID
+IOReadStrW(USHORT   Port,
            PUSHORT Buffer,
            ULONG   Count)
 {
@@ -193,39 +195,35 @@ IOReadStrW(ULONG   Port,
     }
     else
     {
-        while (Count--) IOReadW(Port, Buffer++);
+        while (Count--) *Buffer++ = IOReadW(Port);
     }
 }
 
-static VOID
-IOWriteW(ULONG   Port,
-         PUSHORT Buffer)
+VOID
+IOWriteW(USHORT  Port,
+         USHORT Buffer)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.OutW)
     {
-        IoPortProc[Port].IoHandlers.OutW(Port, *Buffer);
+        IoPortProc[Port].IoHandlers.OutW(Port, Buffer);
     }
     else if (IoPortProc[Port].hVdd > 0 &&
              IoPortProc[Port].VddIoHandlers.outw_handler)
     {
         ASSERT(Port <= MAXWORD);
-        IoPortProc[Port].VddIoHandlers.outw_handler((WORD)Port, *Buffer);
+        IoPortProc[Port].VddIoHandlers.outw_handler((WORD)Port, Buffer);
     }
     else
     {
-        UCHAR Low, High;
-
         // FIXME: Is it ok on Little endian and Big endian ??
-        Low  = LOBYTE(*Buffer);
-        High = HIBYTE(*Buffer);
-        IOWriteB(Port, &Low);
-        IOWriteB(Port + sizeof(UCHAR), &High);
+        IOWriteB(Port, LOBYTE(Buffer));
+        IOWriteB(Port + sizeof(UCHAR), HIBYTE(Buffer));
     }
 }
 
-static VOID
-IOWriteStrW(ULONG   Port,
+VOID
+IOWriteStrW(USHORT   Port,
             PUSHORT Buffer,
             ULONG   Count)
 {
@@ -243,32 +241,31 @@ IOWriteStrW(ULONG   Port,
     }
     else
     {
-        while (Count--) IOWriteW(Port, Buffer++);
+        while (Count--) IOWriteW(Port, *Buffer++);
     }
 }
 
-static VOID
-IOReadD(ULONG  Port,
-        PULONG Buffer)
+ULONG
+IOReadD(USHORT Port)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.InD)
     {
-        *Buffer = IoPortProc[Port].IoHandlers.InD(Port);
+        return IoPortProc[Port].IoHandlers.InD(Port);
     }
     else
     {
         USHORT Low, High;
 
         // FIXME: Is it ok on Little endian and Big endian ??
-        IOReadW(Port, &Low);
-        IOReadW(Port + sizeof(USHORT), &High);
-        *Buffer = MAKELONG(Low, High);
+        Low  = IOReadW(Port);
+        High = IOReadW(Port + sizeof(USHORT));
+        return MAKELONG(Low, High);
     }
 }
 
-static VOID
-IOReadStrD(ULONG  Port,
+VOID
+IOReadStrD(USHORT  Port,
            PULONG Buffer,
            ULONG  Count)
 {
@@ -279,33 +276,29 @@ IOReadStrD(ULONG  Port,
     }
     else
     {
-        while (Count--) IOReadD(Port, Buffer++);
+        while (Count--) *Buffer++ = IOReadD(Port);
     }
 }
 
-static VOID
-IOWriteD(ULONG  Port,
-         PULONG Buffer)
+VOID
+IOWriteD(USHORT Port,
+         ULONG Buffer)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.OutD)
     {
-        IoPortProc[Port].IoHandlers.OutD(Port, *Buffer);
+        IoPortProc[Port].IoHandlers.OutD(Port, Buffer);
     }
     else
     {
-        USHORT Low, High;
-
         // FIXME: Is it ok on Little endian and Big endian ??
-        Low  = LOWORD(*Buffer);
-        High = HIWORD(*Buffer);
-        IOWriteW(Port, &Low);
-        IOWriteW(Port + sizeof(USHORT), &High);
+        IOWriteW(Port, LOWORD(Buffer));
+        IOWriteW(Port + sizeof(USHORT), HIWORD(Buffer));
     }
 }
 
-static VOID
-IOWriteStrD(ULONG  Port,
+VOID
+IOWriteStrD(USHORT  Port,
             PULONG Buffer,
             ULONG  Count)
 {
@@ -316,13 +309,12 @@ IOWriteStrD(ULONG  Port,
     }
     else
     {
-        while (Count--) IOWriteD(Port, Buffer++);
+        while (Count--) IOWriteD(Port, *Buffer++);
     }
 }
 
-/* PUBLIC FUNCTIONS ***********************************************************/
 
-VOID RegisterIoPort(ULONG Port,
+VOID RegisterIoPort(USHORT Port,
                     EMULATOR_INB_PROC  InHandler,
                     EMULATOR_OUTB_PROC OutHandler)
 {
@@ -340,19 +332,19 @@ VOID RegisterIoPort(ULONG Port,
     IoPortProc[Port].hVdd = INVALID_HANDLE_VALUE;
 }
 
-VOID UnregisterIoPort(ULONG Port)
+VOID UnregisterIoPort(USHORT Port)
 {
     /*
      * Put automagically all the fields to zero:
      * the hVdd gets unregistered as well as all the handlers.
      */
     // IoPortProc[Port] = {NULL};
-    ZeroMemory(&IoPortProc[Port], sizeof(IoPortProc[Port]));
+    RtlZeroMemory(&IoPortProc[Port], sizeof(IoPortProc[Port]));
 }
 
 VOID WINAPI
 EmulatorReadIo(PFAST486_STATE State,
-               ULONG Port,
+               USHORT Port,
                PVOID Buffer,
                ULONG DataCount,
                UCHAR DataSize)
@@ -364,27 +356,27 @@ EmulatorReadIo(PFAST486_STATE State,
     if (DataSize == sizeof(UCHAR))
     {
         if (DataCount == 1)
-            IOReadB(Port, Buffer);
+            *(PUCHAR)Buffer = IOReadB(Port);
         else
             IOReadStrB(Port, Buffer, DataCount);
     }
     else if (DataSize == sizeof(USHORT))
     {
         if (DataCount == 1)
-            IOReadW(Port, Buffer);
+            *(PUSHORT)Buffer = IOReadW(Port);
         else
             IOReadStrW(Port, Buffer, DataCount);
     }
     else if (DataSize == sizeof(ULONG))
     {
         if (DataCount == 1)
-            IOReadD(Port, Buffer);
+            *(PULONG)Buffer = IOReadD(Port);
         else
             IOReadStrD(Port, Buffer, DataCount);
     }
     else
     {
-        PBYTE Address = (PBYTE)Buffer;
+        PUCHAR Address = (PUCHAR)Buffer;
 
         while (DataCount--)
         {
@@ -393,44 +385,41 @@ EmulatorReadIo(PFAST486_STATE State,
             UCHAR NewDataSize = DataSize;
 
             /* Read dword */
-            Count       = NewDataSize / sizeof(ULONG);
-            NewDataSize = NewDataSize % sizeof(ULONG);
+            Count       = NewDataSize >> 2; // NewDataSize / sizeof(ULONG);
+            NewDataSize = NewDataSize  & 3; // NewDataSize % sizeof(ULONG);
             while (Count--)
             {
-                IOReadD(CurrentPort, (PULONG)Address);
+                *(PULONG)Address = IOReadD(CurrentPort);
                 CurrentPort += sizeof(ULONG);
                 Address     += sizeof(ULONG);
             }
 
             /* Read word */
-            Count       = NewDataSize / sizeof(USHORT);
-            NewDataSize = NewDataSize % sizeof(USHORT);
+            Count       = NewDataSize >> 1; // NewDataSize / sizeof(USHORT);
+            NewDataSize = NewDataSize  & 1; // NewDataSize % sizeof(USHORT);
             while (Count--)
             {
-                IOReadW(CurrentPort, (PUSHORT)Address);
+                *(PUSHORT)Address = IOReadW(CurrentPort);
                 CurrentPort += sizeof(USHORT);
                 Address     += sizeof(USHORT);
             }
 
             /* Read byte */
-            Count       = NewDataSize / sizeof(UCHAR);
-            NewDataSize = NewDataSize % sizeof(UCHAR);
+            Count       = NewDataSize; // NewDataSize / sizeof(UCHAR);
+            // NewDataSize = NewDataSize % sizeof(UCHAR);
             while (Count--)
             {
-                IOReadB(CurrentPort, (PUCHAR)Address);
+                *(PUCHAR)Address = IOReadB(CurrentPort);
                 CurrentPort += sizeof(UCHAR);
                 Address     += sizeof(UCHAR);
             }
-
-            ASSERT(Count == 0);
-            ASSERT(NewDataSize == 0);
         }
     }
 }
 
 VOID WINAPI
 EmulatorWriteIo(PFAST486_STATE State,
-                ULONG Port,
+                USHORT Port,
                 PVOID Buffer,
                 ULONG DataCount,
                 UCHAR DataSize)
@@ -442,27 +431,27 @@ EmulatorWriteIo(PFAST486_STATE State,
     if (DataSize == sizeof(UCHAR))
     {
         if (DataCount == 1)
-            IOWriteB(Port, Buffer);
+            IOWriteB(Port, *(PUCHAR)Buffer);
         else
             IOWriteStrB(Port, Buffer, DataCount);
     }
     else if (DataSize == sizeof(USHORT))
     {
         if (DataCount == 1)
-            IOWriteW(Port, Buffer);
+            IOWriteW(Port, *(PUSHORT)Buffer);
         else
             IOWriteStrW(Port, Buffer, DataCount);
     }
     else if (DataSize == sizeof(ULONG))
     {
         if (DataCount == 1)
-            IOWriteD(Port, Buffer);
+            IOWriteD(Port, *(PULONG)Buffer);
         else
             IOWriteStrD(Port, Buffer, DataCount);
     }
     else
     {
-        PBYTE Address = (PBYTE)Buffer;
+        PUCHAR Address = (PUCHAR)Buffer;
 
         while (DataCount--)
         {
@@ -471,37 +460,34 @@ EmulatorWriteIo(PFAST486_STATE State,
             UCHAR NewDataSize = DataSize;
 
             /* Write dword */
-            Count       = NewDataSize / sizeof(ULONG);
-            NewDataSize = NewDataSize % sizeof(ULONG);
+            Count       = NewDataSize >> 2; // NewDataSize / sizeof(ULONG);
+            NewDataSize = NewDataSize  & 3; // NewDataSize % sizeof(ULONG);
             while (Count--)
             {
-                IOWriteD(CurrentPort, (PULONG)Address);
+                IOWriteD(CurrentPort, *(PULONG)Address);
                 CurrentPort += sizeof(ULONG);
                 Address     += sizeof(ULONG);
             }
 
             /* Write word */
-            Count       = NewDataSize / sizeof(USHORT);
-            NewDataSize = NewDataSize % sizeof(USHORT);
+            Count       = NewDataSize >> 1; // NewDataSize / sizeof(USHORT);
+            NewDataSize = NewDataSize  & 1; // NewDataSize % sizeof(USHORT);
             while (Count--)
             {
-                IOWriteW(CurrentPort, (PUSHORT)Address);
+                IOWriteW(CurrentPort, *(PUSHORT)Address);
                 CurrentPort += sizeof(USHORT);
                 Address     += sizeof(USHORT);
             }
 
             /* Write byte */
-            Count       = NewDataSize / sizeof(UCHAR);
-            NewDataSize = NewDataSize % sizeof(UCHAR);
+            Count       = NewDataSize; // NewDataSize / sizeof(UCHAR);
+            // NewDataSize = NewDataSize % sizeof(UCHAR);
             while (Count--)
             {
-                IOWriteB(CurrentPort, (PUCHAR)Address);
+                IOWriteB(CurrentPort, *(PUCHAR)Address);
                 CurrentPort += sizeof(UCHAR);
                 Address     += sizeof(UCHAR);
             }
-
-            ASSERT(Count == 0);
-            ASSERT(NewDataSize == 0);
         }
     }
 }
@@ -599,7 +585,7 @@ VDDDeInstallIOHook(HANDLE            hVdd,
              * the hVdd gets unregistered as well as all the handlers.
              */
             // IoPortProc[i] = {NULL};
-            ZeroMemory(&IoPortProc[i], sizeof(IoPortProc[i]));
+            RtlZeroMemory(&IoPortProc[i], sizeof(IoPortProc[i]));
         }
 
         /* Go to the next range */