[NTVDM]
[reactos.git] / subsystems / ntvdm / ntvdm.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: ntvdm.c
5 * PURPOSE: Virtual DOS Machine
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "ntvdm.h"
14 #include "emulator.h"
15 #include "cmos.h"
16 #include "bios.h"
17 #include "speaker.h"
18 #include "vga.h"
19 #include "dos.h"
20 #include "timer.h"
21 #include "pic.h"
22 #include "ps2.h"
23
24 /*
25 * Activate this line if you want to be able to test NTVDM with:
26 * ntvdm.exe <program>
27 */
28 #define TESTING
29
30 /* PUBLIC VARIABLES ***********************************************************/
31
32 BOOLEAN VdmRunning = TRUE;
33 LPVOID BaseAddress = NULL;
34
35 /* PUBLIC FUNCTIONS ***********************************************************/
36
37 VOID DisplayMessage(LPCWSTR Format, ...)
38 {
39 WCHAR Buffer[256];
40 va_list Parameters;
41
42 va_start(Parameters, Format);
43 _vsnwprintf(Buffer, 256, Format, Parameters);
44 DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer);
45 MessageBoxW(NULL, Buffer, L"NTVDM Subsystem", MB_OK);
46 va_end(Parameters);
47 }
48
49 BOOL WINAPI ConsoleCtrlHandler(DWORD ControlType)
50 {
51 switch (ControlType)
52 {
53 case CTRL_C_EVENT:
54 case CTRL_BREAK_EVENT:
55 {
56 /* Perform interrupt 0x23 */
57 EmulatorInterrupt(0x23);
58 break;
59 }
60 default:
61 {
62 /* Stop the VDM if the user logs out or closes the console */
63 VdmRunning = FALSE;
64 }
65 }
66 return TRUE;
67 }
68
69 INT wmain(INT argc, WCHAR *argv[])
70 {
71 INT i;
72 CHAR CommandLine[DOS_CMDLINE_LENGTH];
73 DWORD CurrentTickCount;
74 DWORD Cycles = 0;
75 DWORD LastCyclePrintout = GetTickCount();
76 DWORD LastVerticalRefresh = GetTickCount();
77 DWORD LastClockUpdate = GetTickCount();
78 LARGE_INTEGER Frequency, LastTimerTick, LastRtcTick, Counter;
79 LONGLONG TimerTicks;
80 LARGE_INTEGER StartPerfCount;
81 DWORD StartTickCount;
82
83 /* Set the handler routine */
84 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
85
86 #ifndef TESTING
87 UNREFERENCED_PARAMETER(argc);
88 UNREFERENCED_PARAMETER(argv);
89
90 /* The DOS command line must be ASCII */
91 WideCharToMultiByte(CP_ACP, 0, GetCommandLine(), -1, CommandLine, sizeof(CommandLine), NULL, NULL);
92 #else
93 if (argc == 2 && argv[1] != NULL)
94 {
95 WideCharToMultiByte(CP_ACP, 0, argv[1], -1, CommandLine, sizeof(CommandLine), NULL, NULL);
96 }
97 else
98 {
99 wprintf(L"\nReactOS Virtual DOS Machine\n\n"
100 L"Usage: NTVDM <executable>\n");
101 return 0;
102 }
103 #endif
104
105 DPRINT1("\n\n\nNTVDM - Starting '%s'...\n\n\n", CommandLine);
106
107 if (!EmulatorInitialize())
108 {
109 wprintf(L"FATAL: Failed to initialize the CPU emulator\n");
110 goto Cleanup;
111 }
112
113 /* Initialize the performance counter (needed for hardware timers) */
114 if (!QueryPerformanceFrequency(&Frequency))
115 {
116 wprintf(L"FATAL: Performance counter not available\n");
117 goto Cleanup;
118 }
119
120 /* Initialize the CMOS */
121 if (!CmosInitialize())
122 {
123 wprintf(L"FATAL: Failed to initialize the VDM CMOS.\n");
124 goto Cleanup;
125 }
126
127 /* Initialize the system BIOS */
128 if (!BiosInitialize())
129 {
130 wprintf(L"FATAL: Failed to initialize the VDM BIOS.\n");
131 goto Cleanup;
132 }
133
134 /* Initialize the PC Speaker */
135 SpeakerInitialize();
136
137 /* Initialize the VDM DOS kernel */
138 if (!DosInitialize())
139 {
140 wprintf(L"FATAL: Failed to initialize the VDM DOS kernel.\n");
141 goto Cleanup;
142 }
143
144 /* Start the process from the command line */
145 if (!DosCreateProcess(CommandLine, 0))
146 {
147 DisplayMessage(L"Could not start program: %S", CommandLine);
148 goto Cleanup;
149 }
150
151 /* Find the starting performance and tick count */
152 StartTickCount = GetTickCount();
153 QueryPerformanceCounter(&StartPerfCount);
154
155 /* Set the last timer ticks to the current time */
156 LastTimerTick = LastRtcTick = StartPerfCount;
157
158 /* Main loop */
159 while (VdmRunning)
160 {
161 DWORD PitResolution = PitGetResolution();
162 DWORD RtcFrequency = RtcGetTicksPerSecond();
163
164 /* Get the current number of ticks */
165 CurrentTickCount = GetTickCount();
166
167 if ((PitResolution <= 1000) && (RtcFrequency <= 1000))
168 {
169 /* Calculate the approximate performance counter value instead */
170 Counter.QuadPart = StartPerfCount.QuadPart
171 + (CurrentTickCount - StartTickCount)
172 * (Frequency.QuadPart / 1000);
173 }
174 else
175 {
176 /* Get the current performance counter value */
177 QueryPerformanceCounter(&Counter);
178 }
179
180 /* Get the number of PIT ticks that have passed */
181 TimerTicks = ((Counter.QuadPart - LastTimerTick.QuadPart)
182 * PIT_BASE_FREQUENCY) / Frequency.QuadPart;
183
184 /* Update the PIT */
185 if (TimerTicks > 0)
186 {
187 PitDecrementCount(TimerTicks);
188 LastTimerTick = Counter;
189 }
190
191 /* Check for RTC update */
192 if ((CurrentTickCount - LastClockUpdate) >= 1000)
193 {
194 RtcTimeUpdate();
195 LastClockUpdate = CurrentTickCount;
196 }
197
198 /* Check for RTC periodic tick */
199 if ((Counter.QuadPart - LastRtcTick.QuadPart)
200 >= (Frequency.QuadPart / (LONGLONG)RtcFrequency))
201 {
202 RtcPeriodicTick();
203 LastRtcTick = Counter;
204 }
205
206 /* Check for vertical retrace */
207 if ((CurrentTickCount - LastVerticalRefresh) >= 16)
208 {
209 VgaRefreshDisplay();
210 LastVerticalRefresh = CurrentTickCount;
211 }
212
213 /* Horizontal retrace occurs as fast as possible */
214 VgaHorizontalRetrace();
215
216 /* Continue CPU emulation */
217 for (i = 0; (i < STEPS_PER_CYCLE) && VdmRunning; i++)
218 {
219 EmulatorStep();
220 Cycles++;
221 }
222
223 if ((CurrentTickCount - LastCyclePrintout) >= 1000)
224 {
225 DPRINT1("NTVDM: %lu Instructions Per Second\n", Cycles);
226 LastCyclePrintout = CurrentTickCount;
227 Cycles = 0;
228 }
229 }
230
231 /* Perform another screen refresh */
232 VgaRefreshDisplay();
233
234 Cleanup:
235 SpeakerCleanup();
236 BiosCleanup();
237 CmosCleanup();
238 EmulatorCleanup();
239
240 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");
241
242 return 0;
243 }
244
245 /* EOF */