d1a2fa809195862763993fea24f52b66e5cd9f13
[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 #include "ntvdm.h"
10
11 BOOLEAN VdmRunning = TRUE;
12 LPVOID BaseAddress = NULL;
13 LPCWSTR ExceptionName[] =
14 {
15 L"Division By Zero",
16 L"Debug",
17 L"Unexpected Error",
18 L"Breakpoint",
19 L"Integer Overflow",
20 L"Bound Range Exceeded",
21 L"Invalid Opcode",
22 L"FPU Not Available"
23 };
24
25 VOID DisplayMessage(LPCWSTR Format, ...)
26 {
27 WCHAR Buffer[256];
28 va_list Parameters;
29
30 va_start(Parameters, Format);
31 _vsnwprintf(Buffer, 256, Format, Parameters);
32 MessageBox(NULL, Buffer, L"NTVDM Subsystem", MB_OK);
33 va_end(Parameters);
34 }
35
36 BOOL WINAPI ConsoleCtrlHandler(DWORD ControlType)
37 {
38 switch (ControlType)
39 {
40 case CTRL_C_EVENT:
41 case CTRL_BREAK_EVENT:
42 {
43 /* Perform interrupt 0x23 */
44 EmulatorInterrupt(0x23);
45 }
46 default:
47 {
48 /* Stop the VDM if the user logs out or closes the console */
49 VdmRunning = FALSE;
50 }
51 }
52 return TRUE;
53 }
54
55 INT wmain(INT argc, WCHAR *argv[])
56 {
57 INT i;
58 BOOLEAN PrintUsage = TRUE;
59 CHAR CommandLine[128];
60 LARGE_INTEGER Frequency, LastTimerTick, Counter;
61 LONGLONG TimerTicks;
62
63 /* Set the handler routine */
64 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
65
66 /* Parse the command line arguments */
67 for (i = 1; i < argc; i++)
68 {
69 if (argv[i][0] != L'-' && argv[i][0] != L'/') continue;
70
71 switch (argv[i][1])
72 {
73 case L'f':
74 case L'F':
75 {
76 if (argv[i+1] != NULL)
77 {
78 /* The DOS command line must be ASCII */
79 WideCharToMultiByte(CP_ACP, 0, argv[i+1], -1, CommandLine, 128, NULL, NULL);
80
81 /* This is the only mandatory parameter */
82 PrintUsage = FALSE;
83 }
84 break;
85 }
86 default:
87 {
88 wprintf(L"Unknown option: %s", argv[i]);
89 }
90 }
91 }
92
93 if (PrintUsage)
94 {
95 wprintf(L"ReactOS Virtual DOS Machine\n\n");
96 wprintf(L"Usage: NTVDM /F <PROGRAM>\n");
97 return 0;
98 }
99
100 if (!EmulatorInitialize()) return 1;
101
102 /* Initialize the performance counter (needed for hardware timers) */
103 if (!QueryPerformanceFrequency(&Frequency))
104 {
105 wprintf(L"FATAL: Performance counter not available\n");
106 goto Cleanup;
107 }
108
109 /* Initialize the system BIOS */
110 if (!BiosInitialize())
111 {
112 wprintf(L"FATAL: Failed to initialize the VDM BIOS.\n");
113 goto Cleanup;
114 }
115
116 /* Initialize the VDM DOS kernel */
117 if (!DosInitialize())
118 {
119 wprintf(L"FATAL: Failed to initialize the VDM DOS kernel.\n");
120 goto Cleanup;
121 }
122
123 /* Start the process from the command line */
124 if (!DosCreateProcess(CommandLine, 0))
125 {
126 DisplayMessage(L"Could not start program: %S", CommandLine);
127 return -1;
128 }
129
130 /* Set the last timer tick to the current time */
131 QueryPerformanceCounter(&LastTimerTick);
132
133 /* Main loop */
134 while (VdmRunning)
135 {
136 /* Get the current time */
137 QueryPerformanceCounter(&Counter);
138
139 /* Get the number of PIT ticks that have passed */
140 TimerTicks = ((Counter.QuadPart - LastTimerTick.QuadPart)
141 * PIT_BASE_FREQUENCY) / Frequency.QuadPart;
142
143 /* Update the PIT */
144 for (i = 0; i < TimerTicks; i++) PitDecrementCount();
145 LastTimerTick = Counter;
146
147 /* Check for console input events */
148 CheckForInputEvents();
149
150 /* Continue CPU emulation */
151 EmulatorStep();
152 }
153
154 Cleanup:
155 EmulatorCleanup();
156
157 return 0;
158 }
159
160 /* EOF */