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