[NTVDM]
[reactos.git] / reactos / subsystems / mvdm / ntvdm / emulator.h
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: emulator.h
5 * PURPOSE: Minimal x86 machine emulator for the VDM
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 #ifndef _EMULATOR_H_
10 #define _EMULATOR_H_
11
12 /* INCLUDES *******************************************************************/
13
14 #include <fast486.h>
15
16 /* DEFINES ********************************************************************/
17
18 /* Basic Memory Management */
19 #define MEM_ALIGN_UP(ptr, align) MEM_ALIGN_DOWN((ULONG_PTR)(ptr) + (align) - 1l, (align))
20 #define MEM_ALIGN_DOWN(ptr, align) (PVOID)((ULONG_PTR)(ptr) & ~((align) - 1l))
21
22 #define TO_LINEAR(seg, off) (((seg) << 4) + (off))
23 #define MAX_SEGMENT 0xFFFF
24 #define MAX_OFFSET 0xFFFF
25 #define MAX_ADDRESS 0x1000000 // 16 MB of RAM; see also: kernel32/client/vdm.c!BaseGetVdmConfigInfo
26
27 #define FAR_POINTER(x) \
28 (PVOID)((ULONG_PTR)BaseAddress + TO_LINEAR(HIWORD(x), LOWORD(x)))
29
30 #define SEG_OFF_TO_PTR(seg, off) \
31 (PVOID)((ULONG_PTR)BaseAddress + TO_LINEAR((seg), (off)))
32
33 #define REAL_TO_PHYS(ptr) (PVOID)((ULONG_PTR)(ptr) + (ULONG_PTR)BaseAddress)
34 #define PHYS_TO_REAL(ptr) (PVOID)((ULONG_PTR)(ptr) - (ULONG_PTR)BaseAddress)
35
36 #define ARRAY_INDEX(ptr, array) ((ULONG)(((ULONG_PTR)(ptr) - (ULONG_PTR)(array)) / sizeof(*array)))
37
38 /* BCD-Binary conversion */
39
40 FORCEINLINE
41 USHORT
42 BINARY_TO_BCD(USHORT Value)
43 {
44 USHORT Result;
45
46 Result = (Value / 1000) << 12;
47 Value %= 1000;
48 Result |= (Value / 100) << 8;
49 Value %= 100;
50 Result |= (Value / 10) << 4;
51 Value %= 10;
52 Result |= Value;
53
54 return Result;
55 }
56
57 FORCEINLINE
58 USHORT
59 BCD_TO_BINARY(USHORT Value)
60 {
61 USHORT Result;
62
63 Result = Value & 0xF;
64 Value >>= 4;
65 Result += (Value & 0xF) * 10;
66 Value >>= 4;
67 Result += (Value & 0xF) * 100;
68 Value >>= 4;
69 Result += Value * 1000;
70
71 return Result;
72 }
73
74 /* System I/O ports */
75 #define CONTROL_SYSTEM_PORT61H 0x61
76
77
78 enum
79 {
80 EMULATOR_EXCEPTION_DIVISION_BY_ZERO,
81 EMULATOR_EXCEPTION_DEBUG,
82 EMULATOR_EXCEPTION_NMI,
83 EMULATOR_EXCEPTION_BREAKPOINT,
84 EMULATOR_EXCEPTION_OVERFLOW,
85 EMULATOR_EXCEPTION_BOUND,
86 EMULATOR_EXCEPTION_INVALID_OPCODE,
87 EMULATOR_EXCEPTION_NO_FPU,
88 EMULATOR_EXCEPTION_DOUBLE_FAULT,
89 EMULATOR_EXCEPTION_FPU_SEGMENT,
90 EMULATOR_EXCEPTION_INVALID_TSS,
91 EMULATOR_EXCEPTION_NO_SEGMENT,
92 EMULATOR_EXCEPTION_STACK_SEGMENT,
93 EMULATOR_EXCEPTION_GPF,
94 EMULATOR_EXCEPTION_PAGE_FAULT
95 };
96
97 // extern FAST486_STATE EmulatorContext;
98 extern LPVOID BaseAddress;
99 extern BOOLEAN VdmRunning;
100
101 /* FUNCTIONS ******************************************************************/
102
103 VOID DumpMemory(BOOLEAN TextFormat);
104
105 VOID WINAPI EmulatorReadMemory
106 (
107 PFAST486_STATE State,
108 ULONG Address,
109 PVOID Buffer,
110 ULONG Size
111 );
112
113 VOID WINAPI EmulatorWriteMemory
114 (
115 PFAST486_STATE State,
116 ULONG Address,
117 PVOID Buffer,
118 ULONG Size
119 );
120
121 UCHAR WINAPI EmulatorIntAcknowledge
122 (
123 PFAST486_STATE State
124 );
125
126 VOID WINAPI EmulatorFpu
127 (
128 PFAST486_STATE State
129 );
130
131 VOID EmulatorException(BYTE ExceptionNumber, LPWORD Stack);
132
133 VOID EmulatorTerminate(VOID);
134
135 VOID EmulatorInterruptSignal(VOID);
136 VOID EmulatorSetA20(BOOLEAN Enabled);
137 BOOLEAN EmulatorGetA20(VOID);
138
139 BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput);
140 VOID EmulatorCleanup(VOID);
141
142 #endif // _EMULATOR_H_
143
144 /* EOF */