[NTVDM] Improve the PCH situation.
[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: subsystems/mvdm/ntvdm/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 /* DEFINES ********************************************************************/
13
14 /* Basic Memory Management */
15 #define MEM_ALIGN_DOWN(ptr, align) (PVOID)((ULONG_PTR)(ptr) & ~((align) - 1l))
16 #define MEM_ALIGN_UP(ptr, align) MEM_ALIGN_DOWN((ULONG_PTR)(ptr) + (align) - 1l, (align))
17
18 #define TO_LINEAR(seg, off) (((seg) << 4) + (off))
19 #define MAX_SEGMENT 0xFFFF
20 #define MAX_OFFSET 0xFFFF
21 #define MAX_ADDRESS 0x1000000 // 16 MB of RAM; see also: kernel32/client/vdm.c!BaseGetVdmConfigInfo
22 C_ASSERT(0x100000 <= MAX_ADDRESS); // A minimum of 1 MB is required for PC emulation.
23
24 #define SEG_OFF_TO_PTR(seg, off) \
25 (PVOID)((ULONG_PTR)BaseAddress + TO_LINEAR((seg), (off)))
26
27 #define FAR_POINTER(x) SEG_OFF_TO_PTR(HIWORD(x), LOWORD(x))
28
29 #define REAL_TO_PHYS(ptr) (PVOID)((ULONG_PTR)(ptr) + (ULONG_PTR)BaseAddress)
30 #define PHYS_TO_REAL(ptr) (PVOID)((ULONG_PTR)(ptr) - (ULONG_PTR)BaseAddress)
31
32 #define ARRAY_INDEX(ptr, array) ((ULONG)(((ULONG_PTR)(ptr) - (ULONG_PTR)(array)) / sizeof(*array)))
33
34 /* BCD-Binary conversion */
35
36 FORCEINLINE
37 USHORT
38 BINARY_TO_BCD(USHORT Value)
39 {
40 USHORT Result;
41
42 Result = (Value / 1000) << 12;
43 Value %= 1000;
44 Result |= (Value / 100) << 8;
45 Value %= 100;
46 Result |= (Value / 10) << 4;
47 Value %= 10;
48 Result |= Value;
49
50 return Result;
51 }
52
53 FORCEINLINE
54 USHORT
55 BCD_TO_BINARY(USHORT Value)
56 {
57 USHORT Result;
58
59 Result = Value & 0xF;
60 Value >>= 4;
61 Result += (Value & 0xF) * 10;
62 Value >>= 4;
63 Result += (Value & 0xF) * 100;
64 Value >>= 4;
65 Result += Value * 1000;
66
67 return Result;
68 }
69
70 enum
71 {
72 EMULATOR_EXCEPTION_DIVISION_BY_ZERO,
73 EMULATOR_EXCEPTION_DEBUG,
74 EMULATOR_EXCEPTION_NMI,
75 EMULATOR_EXCEPTION_BREAKPOINT,
76 EMULATOR_EXCEPTION_OVERFLOW,
77 EMULATOR_EXCEPTION_BOUND,
78 EMULATOR_EXCEPTION_INVALID_OPCODE,
79 EMULATOR_EXCEPTION_NO_FPU,
80 EMULATOR_EXCEPTION_DOUBLE_FAULT,
81 EMULATOR_EXCEPTION_FPU_SEGMENT,
82 EMULATOR_EXCEPTION_INVALID_TSS,
83 EMULATOR_EXCEPTION_NO_SEGMENT,
84 EMULATOR_EXCEPTION_STACK_SEGMENT,
85 EMULATOR_EXCEPTION_GPF,
86 EMULATOR_EXCEPTION_PAGE_FAULT
87 };
88
89 extern FAST486_STATE EmulatorContext;
90 extern LPVOID BaseAddress;
91 extern BOOLEAN VdmRunning;
92
93 /* FUNCTIONS ******************************************************************/
94
95 VOID DumpMemory(BOOLEAN TextFormat);
96
97 VOID MountFloppy(IN ULONG DiskNumber);
98 VOID EjectFloppy(IN ULONG DiskNumber);
99
100 UCHAR FASTCALL EmulatorIntAcknowledge
101 (
102 PFAST486_STATE State
103 );
104
105 VOID FASTCALL EmulatorFpu
106 (
107 PFAST486_STATE State
108 );
109
110 VOID EmulatorInterruptSignal(VOID);
111 VOID EmulatorException(BYTE ExceptionNumber, LPWORD Stack);
112
113 VOID EmulatorPause(VOID);
114 VOID EmulatorResume(VOID);
115 VOID EmulatorTerminate(VOID);
116
117 BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput);
118 VOID EmulatorCleanup(VOID);
119
120 #endif /* _EMULATOR_H_ */