[NTVDM]
[reactos.git] / subsystems / ntvdm / io.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: io.c
5 * PURPOSE: I/O Port Handlers
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 * Hermes Belusca-Maito (hermes.belusca@sfr.fr)
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #define NDEBUG
13
14 #include "emulator.h"
15 #include "io.h"
16
17 /* PRIVATE VARIABLES **********************************************************/
18
19 typedef struct _EMULATOR_IOPORT_HANDLER
20 {
21 EMULATOR_IN_PROC In;
22 EMULATOR_OUT_PROC Out;
23 } EMULATOR_IOPORT_HANDLER, *PEMULATOR_IOPORT_HANDLER;
24
25 /*
26 * This is the list of registered I/O Port handlers.
27 */
28 EMULATOR_IOPORT_HANDLER IoPortProc[EMULATOR_MAX_IOPORTS_NUM];
29
30 /* PUBLIC FUNCTIONS ***********************************************************/
31
32 VOID WINAPI RegisterIoPort(ULONG Port,
33 EMULATOR_IN_PROC InHandler,
34 EMULATOR_OUT_PROC OutHandler)
35 {
36 if (IoPortProc[Port].In == NULL)
37 IoPortProc[Port].In = InHandler;
38 else
39 DPRINT1("IoPortProc[0x%X].In already registered\n", Port);
40
41 if (IoPortProc[Port].Out == NULL)
42 IoPortProc[Port].Out = OutHandler;
43 else
44 DPRINT1("IoPortProc[0x%X].Out already registered\n", Port);
45 }
46
47 VOID WINAPI UnregisterIoPort(ULONG Port)
48 {
49 IoPortProc[Port].In = NULL;
50 IoPortProc[Port].Out = NULL;
51 }
52
53 VOID WINAPI
54 EmulatorReadIo(PFAST486_STATE State,
55 ULONG Port,
56 PVOID Buffer,
57 ULONG DataCount,
58 UCHAR DataSize)
59 {
60 INT i, j;
61 LPBYTE Address = (LPBYTE)Buffer;
62
63 UNREFERENCED_PARAMETER(State);
64
65 for (i = 0; i < DataCount; i++) for (j = 0; j < DataSize; j++)
66 {
67 ULONG CurrentPort = Port + j;
68
69 /* Call the IN Port handler */
70 if (IoPortProc[CurrentPort].In != NULL)
71 {
72 *(Address++) = IoPortProc[CurrentPort].In(CurrentPort);
73 }
74 else
75 {
76 DPRINT1("Read from unknown port: 0x%X\n", CurrentPort);
77 *(Address++) = 0xFF; // Empty port value
78 }
79 }
80 }
81
82 VOID WINAPI
83 EmulatorWriteIo(PFAST486_STATE State,
84 ULONG Port,
85 PVOID Buffer,
86 ULONG DataCount,
87 UCHAR DataSize)
88 {
89 INT i, j;
90 LPBYTE Address = (LPBYTE)Buffer;
91
92 UNREFERENCED_PARAMETER(State);
93
94 for (i = 0; i < DataCount; i++) for (j = 0; j < DataSize; j++)
95 {
96 ULONG CurrentPort = Port + j;
97
98 /* Call the OUT Port handler */
99 if (IoPortProc[CurrentPort].Out != NULL)
100 IoPortProc[CurrentPort].Out(CurrentPort, *(Address++));
101 else
102 DPRINT1("Write to unknown port: 0x%X\n", CurrentPort);
103 }
104 }
105
106 /* EOF */