* Sync up to trunk head (r64921).
[reactos.git] / subsystems / ntvdm / io.c
index 9a85a11..53ff8dc 100644 (file)
@@ -58,7 +58,7 @@ EMULATOR_IOPORT_HANDLERS IoPortProc[EMULATOR_MAX_IOPORTS_NUM] = {{NULL}};
 /* PUBLIC FUNCTIONS ***********************************************************/
 
 UCHAR
-IOReadB(ULONG Port)
+IOReadB(USHORT Port)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.InB)
@@ -82,7 +82,7 @@ IOReadB(ULONG Port)
 }
 
 VOID
-IOReadStrB(ULONG  Port,
+IOReadStrB(USHORT  Port,
            PUCHAR Buffer,
            ULONG  Count)
 {
@@ -100,13 +100,12 @@ IOReadStrB(ULONG  Port,
     }
     else
     {
-        while (Count--)
-            *Buffer++ = IOReadB(Port);
+        while (Count--) *Buffer++ = IOReadB(Port);
     }
 }
 
 VOID
-IOWriteB(ULONG Port,
+IOWriteB(USHORT Port,
          UCHAR Buffer)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
@@ -128,7 +127,7 @@ IOWriteB(ULONG Port,
 }
 
 VOID
-IOWriteStrB(ULONG  Port,
+IOWriteStrB(USHORT  Port,
             PUCHAR Buffer,
             ULONG  Count)
 {
@@ -151,7 +150,7 @@ IOWriteStrB(ULONG  Port,
 }
 
 USHORT
-IOReadW(ULONG Port)
+IOReadW(USHORT Port)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.InW)
@@ -178,7 +177,7 @@ IOReadW(ULONG Port)
 }
 
 VOID
-IOReadStrW(ULONG   Port,
+IOReadStrW(USHORT   Port,
            PUSHORT Buffer,
            ULONG   Count)
 {
@@ -196,13 +195,12 @@ IOReadStrW(ULONG   Port,
     }
     else
     {
-        while (Count--)
-            *Buffer++ = IOReadW(Port);
+        while (Count--) *Buffer++ = IOReadW(Port);
     }
 }
 
 VOID
-IOWriteW(ULONG  Port,
+IOWriteW(USHORT  Port,
          USHORT Buffer)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
@@ -225,7 +223,7 @@ IOWriteW(ULONG  Port,
 }
 
 VOID
-IOWriteStrW(ULONG   Port,
+IOWriteStrW(USHORT   Port,
             PUSHORT Buffer,
             ULONG   Count)
 {
@@ -248,7 +246,7 @@ IOWriteStrW(ULONG   Port,
 }
 
 ULONG
-IOReadD(ULONG Port)
+IOReadD(USHORT Port)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
         IoPortProc[Port].IoHandlers.InD)
@@ -267,7 +265,7 @@ IOReadD(ULONG Port)
 }
 
 VOID
-IOReadStrD(ULONG  Port,
+IOReadStrD(USHORT  Port,
            PULONG Buffer,
            ULONG  Count)
 {
@@ -278,13 +276,12 @@ IOReadStrD(ULONG  Port,
     }
     else
     {
-        while (Count--)
-            *Buffer++ = IOReadD(Port);
+        while (Count--) *Buffer++ = IOReadD(Port);
     }
 }
 
 VOID
-IOWriteD(ULONG Port,
+IOWriteD(USHORT Port,
          ULONG Buffer)
 {
     if (IoPortProc[Port].hVdd == INVALID_HANDLE_VALUE &&
@@ -301,7 +298,7 @@ IOWriteD(ULONG Port,
 }
 
 VOID
-IOWriteStrD(ULONG  Port,
+IOWriteStrD(USHORT  Port,
             PULONG Buffer,
             ULONG  Count)
 {
@@ -317,7 +314,7 @@ IOWriteStrD(ULONG  Port,
 }
 
 
-VOID RegisterIoPort(ULONG Port,
+VOID RegisterIoPort(USHORT Port,
                     EMULATOR_INB_PROC  InHandler,
                     EMULATOR_OUTB_PROC OutHandler)
 {
@@ -335,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)
@@ -379,7 +376,7 @@ EmulatorReadIo(PFAST486_STATE State,
     }
     else
     {
-        PBYTE Address = (PBYTE)Buffer;
+        PUCHAR Address = (PUCHAR)Buffer;
 
         while (DataCount--)
         {
@@ -388,8 +385,8 @@ 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--)
             {
                 *(PULONG)Address = IOReadD(CurrentPort);
@@ -398,8 +395,8 @@ EmulatorReadIo(PFAST486_STATE State,
             }
 
             /* Read word */
-            Count       = NewDataSize / sizeof(USHORT);
-            NewDataSize = NewDataSize % sizeof(USHORT);
+            Count       = NewDataSize >> 1; // NewDataSize / sizeof(USHORT);
+            NewDataSize = NewDataSize  & 1; // NewDataSize % sizeof(USHORT);
             while (Count--)
             {
                 *(PUSHORT)Address = IOReadW(CurrentPort);
@@ -408,24 +405,21 @@ EmulatorReadIo(PFAST486_STATE State,
             }
 
             /* Read byte */
-            Count       = NewDataSize / sizeof(UCHAR);
-            NewDataSize = NewDataSize % sizeof(UCHAR);
+            Count       = NewDataSize; // NewDataSize / sizeof(UCHAR);
+            // NewDataSize = NewDataSize % sizeof(UCHAR);
             while (Count--)
             {
                 *(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)
@@ -457,7 +451,7 @@ EmulatorWriteIo(PFAST486_STATE State,
     }
     else
     {
-        PBYTE Address = (PBYTE)Buffer;
+        PUCHAR Address = (PUCHAR)Buffer;
 
         while (DataCount--)
         {
@@ -466,8 +460,8 @@ 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);
@@ -476,8 +470,8 @@ EmulatorWriteIo(PFAST486_STATE State,
             }
 
             /* 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);
@@ -486,17 +480,14 @@ EmulatorWriteIo(PFAST486_STATE State,
             }
 
             /* 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);
                 CurrentPort += sizeof(UCHAR);
                 Address     += sizeof(UCHAR);
             }
-
-            ASSERT(Count == 0);
-            ASSERT(NewDataSize == 0);
         }
     }
 }
@@ -594,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 */