Sync with trunk r63383 .
[reactos.git] / subsystems / 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 "ntvdm.h"
15 #include <fast486.h>
16
17 /* DEFINES ********************************************************************/
18
19 /* FLAGS */
20 #define EMULATOR_FLAG_CF (1 << 0)
21 #define EMULATOR_FLAG_PF (1 << 2)
22 #define EMULATOR_FLAG_AF (1 << 4)
23 #define EMULATOR_FLAG_ZF (1 << 6)
24 #define EMULATOR_FLAG_SF (1 << 7)
25 #define EMULATOR_FLAG_TF (1 << 8)
26 #define EMULATOR_FLAG_IF (1 << 9)
27 #define EMULATOR_FLAG_DF (1 << 10)
28 #define EMULATOR_FLAG_OF (1 << 11)
29 #define EMULATOR_FLAG_NT (1 << 14)
30 #define EMULATOR_FLAG_RF (1 << 16)
31 #define EMULATOR_FLAG_VM (1 << 17)
32 #define EMULATOR_FLAG_AC (1 << 18)
33 #define EMULATOR_FLAG_VIF (1 << 19)
34 #define EMULATOR_FLAG_VIP (1 << 20)
35 #define EMULATOR_FLAG_ID (1 << 21)
36
37 //
38 // WARNING WARNING!!
39 // If you're changing the indices here, you then need to
40 // also fix the BOP code in callback.c !!!!!!!!!!!!!!!!!
41 //
42 #define STACK_INT_NUM 0
43 #define STACK_IP 1
44 #define STACK_CS 2
45 #define STACK_FLAGS 3
46
47
48 /* Basic Memory Management */
49 #define MEM_ALIGN_UP(ptr, align) MEM_ALIGN_DOWN((ULONG_PTR)(ptr) + (align) - 1l, (align))
50 #define MEM_ALIGN_DOWN(ptr, align) (PVOID)((ULONG_PTR)(ptr) & ~((align) - 1l))
51
52 #define TO_LINEAR(seg, off) (((seg) << 4) + (off))
53 #define MAX_SEGMENT 0xFFFF
54 #define MAX_OFFSET 0xFFFF
55 #define MAX_ADDRESS 0x1000000 // 16 MB of RAM
56
57 #define FAR_POINTER(x) \
58 (PVOID)((ULONG_PTR)BaseAddress + TO_LINEAR(HIWORD(x), LOWORD(x)))
59
60 #define SEG_OFF_TO_PTR(seg, off) \
61 (PVOID)((ULONG_PTR)BaseAddress + TO_LINEAR((seg), (off)))
62
63 #define REAL_TO_PHYS(ptr) (PVOID)((ULONG_PTR)(ptr) + (ULONG_PTR)BaseAddress)
64 #define PHYS_TO_REAL(ptr) (PVOID)((ULONG_PTR)(ptr) - (ULONG_PTR)BaseAddress)
65
66
67 /* BCD-Binary conversion */
68 #define BINARY_TO_BCD(x) ((((x) / 1000) << 12) + (((x) / 100) << 8) + (((x) / 10) << 4) + ((x) % 10))
69 #define BCD_TO_BINARY(x) (((x) >> 12) * 1000 + ((x) >> 8) * 100 + ((x) >> 4) * 10 + ((x) & 0x0F))
70
71
72 /* System I/O ports */
73 #define CONTROL_SYSTEM_PORT61H 0x61
74
75
76 enum
77 {
78 EMULATOR_EXCEPTION_DIVISION_BY_ZERO,
79 EMULATOR_EXCEPTION_DEBUG,
80 EMULATOR_EXCEPTION_NMI,
81 EMULATOR_EXCEPTION_BREAKPOINT,
82 EMULATOR_EXCEPTION_OVERFLOW,
83 EMULATOR_EXCEPTION_BOUND,
84 EMULATOR_EXCEPTION_INVALID_OPCODE,
85 EMULATOR_EXCEPTION_NO_FPU,
86 EMULATOR_EXCEPTION_DOUBLE_FAULT,
87 EMULATOR_EXCEPTION_FPU_SEGMENT,
88 EMULATOR_EXCEPTION_INVALID_TSS,
89 EMULATOR_EXCEPTION_NO_SEGMENT,
90 EMULATOR_EXCEPTION_STACK_SEGMENT,
91 EMULATOR_EXCEPTION_GPF,
92 EMULATOR_EXCEPTION_PAGE_FAULT
93 };
94
95 extern FAST486_STATE EmulatorContext;
96 extern LPVOID BaseAddress;
97 extern BOOLEAN VdmRunning;
98
99 /* FUNCTIONS ******************************************************************/
100
101 VOID DumpMemory(VOID);
102
103 VOID WINAPI EmulatorReadMemory
104 (
105 PFAST486_STATE State,
106 ULONG Address,
107 PVOID Buffer,
108 ULONG Size
109 );
110
111 VOID WINAPI EmulatorWriteMemory
112 (
113 PFAST486_STATE State,
114 ULONG Address,
115 PVOID Buffer,
116 ULONG Size
117 );
118
119 UCHAR WINAPI EmulatorIntAcknowledge
120 (
121 PFAST486_STATE State
122 );
123
124 VOID EmulatorException(BYTE ExceptionNumber, LPWORD Stack);
125
126 VOID EmulatorExecute(WORD Segment, WORD Offset);
127 VOID EmulatorStep(VOID);
128 VOID EmulatorSimulate(VOID);
129 VOID EmulatorUnsimulate(VOID);
130 VOID EmulatorTerminate(VOID);
131
132 VOID EmulatorInterrupt(BYTE Number);
133 VOID EmulatorInterruptSignal(VOID);
134 VOID EmulatorSetA20(BOOLEAN Enabled);
135
136 BOOLEAN EmulatorInitialize(HANDLE ConsoleInput, HANDLE ConsoleOutput);
137 VOID EmulatorCleanup(VOID);
138
139 #endif // _EMULATOR_H_
140
141 /* EOF */