71e2741310abbfdb91dc1054cd147d9d714e0081
[reactos.git] / subsystems / ntvdm / emulator.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: emulator.c
5 * PURPOSE: Minimal x86 machine emulator for the VDM
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "emulator.h"
14 #include "bios.h"
15 #include "bop.h"
16 #include "vddsup.h"
17 #include "io.h"
18 #include "registers.h"
19 #include "vga.h"
20 #include "pic.h"
21
22 // HACK
23 typedef INT VDM_MODE;
24
25 /* PRIVATE VARIABLES **********************************************************/
26
27 FAST486_STATE EmulatorContext;
28
29 static BOOLEAN A20Line = FALSE;
30
31 /* BOP Identifiers */
32 #define BOP_DEBUGGER 0x56 // Break into the debugger from a 16-bit app
33
34 /* PRIVATE FUNCTIONS **********************************************************/
35
36 VOID WINAPI EmulatorReadMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
37 {
38 UNREFERENCED_PARAMETER(State);
39
40 /* If the A20 line is disabled, mask bit 20 */
41 if (!A20Line) Address &= ~(1 << 20);
42
43 /* Make sure the requested address is valid */
44 if ((Address + Size) >= MAX_ADDRESS) return;
45
46 /*
47 * Check if we are going to read the VGA memory and
48 * copy it into the virtual address space if needed.
49 */
50 if (((Address + Size) >= VgaGetVideoBaseAddress())
51 && (Address < VgaGetVideoLimitAddress()))
52 {
53 DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
54 DWORD ActualSize = min(Address + Size - 1, VgaGetVideoLimitAddress())
55 - VgaAddress + 1;
56 LPBYTE DestBuffer = (LPBYTE)((ULONG_PTR)BaseAddress + VgaAddress);
57
58 /* Read from the VGA memory */
59 VgaReadMemory(VgaAddress, DestBuffer, ActualSize);
60 }
61
62 /* Read the data from the virtual address space and store it in the buffer */
63 RtlCopyMemory(Buffer, (LPVOID)((ULONG_PTR)BaseAddress + Address), Size);
64 }
65
66 VOID WINAPI EmulatorWriteMemory(PFAST486_STATE State, ULONG Address, PVOID Buffer, ULONG Size)
67 {
68 UNREFERENCED_PARAMETER(State);
69
70 /* If the A20 line is disabled, mask bit 20 */
71 if (!A20Line) Address &= ~(1 << 20);
72
73 /* Make sure the requested address is valid */
74 if ((Address + Size) >= MAX_ADDRESS) return;
75
76 /* Make sure we don't write to the ROM area */
77 if ((Address + Size) >= ROM_AREA_START && (Address < ROM_AREA_END)) return;
78
79 /* Read the data from the buffer and store it in the virtual address space */
80 RtlCopyMemory((LPVOID)((ULONG_PTR)BaseAddress + Address), Buffer, Size);
81
82 /*
83 * Check if we modified the VGA memory.
84 */
85 if (((Address + Size) >= VgaGetVideoBaseAddress())
86 && (Address < VgaGetVideoLimitAddress()))
87 {
88 DWORD VgaAddress = max(Address, VgaGetVideoBaseAddress());
89 DWORD ActualSize = min(Address + Size - 1, VgaGetVideoLimitAddress())
90 - VgaAddress + 1;
91 LPBYTE SrcBuffer = (LPBYTE)((ULONG_PTR)BaseAddress + VgaAddress);
92
93 /* Write to the VGA memory */
94 VgaWriteMemory(VgaAddress, SrcBuffer, ActualSize);
95 }
96 }
97
98 UCHAR WINAPI EmulatorIntAcknowledge(PFAST486_STATE State)
99 {
100 UNREFERENCED_PARAMETER(State);
101
102 /* Get the interrupt number from the PIC */
103 return PicGetInterrupt();
104 }
105
106 VOID WINAPI EmulatorDebugBreak(LPWORD Stack)
107 {
108 DPRINT1("NTVDM: BOP_DEBUGGER\n");
109 DebugBreak();
110 }
111
112 /* PUBLIC FUNCTIONS ***********************************************************/
113
114 BOOLEAN EmulatorInitialize(VOID)
115 {
116 /* Allocate memory for the 16-bit address space */
117 BaseAddress = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_ADDRESS);
118 if (BaseAddress == NULL) return FALSE;
119
120 /* Initialize the CPU */
121 Fast486Initialize(&EmulatorContext,
122 EmulatorReadMemory,
123 EmulatorWriteMemory,
124 EmulatorReadIo,
125 EmulatorWriteIo,
126 NULL,
127 EmulatorBiosOperation,
128 EmulatorIntAcknowledge,
129 NULL /* TODO: Use a TLB */);
130
131 /* Enable interrupts */
132 setIF(1);
133
134 /* Initialize VDD support */
135 VDDSupInitialize();
136
137 /* Register the DebugBreak BOP */
138 RegisterBop(BOP_DEBUGGER, EmulatorDebugBreak);
139
140 return TRUE;
141 }
142
143 VOID EmulatorCleanup(VOID)
144 {
145 /* Free the memory allocated for the 16-bit address space */
146 if (BaseAddress != NULL) HeapFree(GetProcessHeap(), 0, BaseAddress);
147 }
148
149 // FIXME: This function assumes 16-bit mode!!!
150 VOID EmulatorExecute(WORD Segment, WORD Offset)
151 {
152 /* Tell Fast486 to move the instruction pointer */
153 Fast486ExecuteAt(&EmulatorContext, Segment, Offset);
154 }
155
156 VOID EmulatorInterrupt(BYTE Number)
157 {
158 /* Call the Fast486 API */
159 Fast486Interrupt(&EmulatorContext, Number);
160 }
161
162 VOID EmulatorInterruptSignal(VOID)
163 {
164 /* Call the Fast486 API */
165 Fast486InterruptSignal(&EmulatorContext);
166 }
167
168 VOID EmulatorStep(VOID)
169 {
170 /* Dump the state for debugging purposes */
171 // Fast486DumpState(&EmulatorContext);
172
173 /* Execute the next instruction */
174 Fast486StepInto(&EmulatorContext);
175 }
176
177 VOID EmulatorSetA20(BOOLEAN Enabled)
178 {
179 A20Line = Enabled;
180 }
181
182
183
184 PBYTE
185 WINAPI
186 Sim32pGetVDMPointer(IN ULONG Address,
187 IN BOOLEAN ProtectedMode)
188 {
189 // FIXME
190 UNREFERENCED_PARAMETER(ProtectedMode);
191
192 /*
193 * HIWORD(Address) == Segment (if ProtectedMode == FALSE)
194 * or Selector (if ProtectedMode == TRUE )
195 * LOWORD(Address) == Offset
196 */
197 return (PBYTE)FAR_POINTER(Address);
198 }
199
200 PBYTE
201 WINAPI
202 MGetVdmPointer(IN ULONG Address,
203 IN ULONG Size,
204 IN BOOLEAN ProtectedMode)
205 {
206 UNREFERENCED_PARAMETER(Size);
207 return Sim32pGetVDMPointer(Address, ProtectedMode);
208 }
209
210 PVOID
211 WINAPI
212 VdmMapFlat(IN USHORT Segment,
213 IN ULONG Offset,
214 IN VDM_MODE Mode)
215 {
216 // FIXME
217 UNREFERENCED_PARAMETER(Mode);
218
219 return SEG_OFF_TO_PTR(Segment, Offset);
220 }
221
222 BOOL
223 WINAPI
224 VdmFlushCache(IN USHORT Segment,
225 IN ULONG Offset,
226 IN ULONG Size,
227 IN VDM_MODE Mode)
228 {
229 // FIXME
230 UNIMPLEMENTED;
231 return TRUE;
232 }
233
234 BOOL
235 WINAPI
236 VdmUnmapFlat(IN USHORT Segment,
237 IN ULONG Offset,
238 IN PVOID Buffer,
239 IN VDM_MODE Mode)
240 {
241 // FIXME
242 UNIMPLEMENTED;
243 return TRUE;
244 }
245
246 /* EOF */