[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 #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 DWORD CurrentTickCount, LastTickCount = 0, Cycles = 0, LastCyclePrintout = 0;
61 LARGE_INTEGER Frequency, LastTimerTick, Counter;
62 LONGLONG TimerTicks;
63
64 /* Set the handler routine */
65 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
66
67 /* Parse the command line arguments */
68 for (i = 1; i < argc; i++)
69 {
70 if (argv[i][0] != L'-' && argv[i][0] != L'/') continue;
71
72 switch (argv[i][1])
73 {
74 case L'f':
75 case L'F':
76 {
77 if (argv[i+1] != NULL)
78 {
79 /* The DOS command line must be ASCII */
80 WideCharToMultiByte(CP_ACP, 0, argv[i+1], -1, CommandLine, 128, NULL, NULL);
81
82 /* This is the only mandatory parameter */
83 PrintUsage = FALSE;
84 }
85 break;
86 }
87 default:
88 {
89 wprintf(L"Unknown option: %s", argv[i]);
90 }
91 }
92 }
93
94 if (PrintUsage)
95 {
96 wprintf(L"ReactOS Virtual DOS Machine\n\n");
97 wprintf(L"Usage: NTVDM /F <PROGRAM>\n");
98 return 0;
99 }
100
101 if (!EmulatorInitialize()) return 1;
102
103 /* Initialize the performance counter (needed for hardware timers) */
104 if (!QueryPerformanceFrequency(&Frequency))
105 {
106 wprintf(L"FATAL: Performance counter not available\n");
107 goto Cleanup;
108 }
109
110 /* Initialize the system BIOS */
111 if (!BiosInitialize())
112 {
113 wprintf(L"FATAL: Failed to initialize the VDM BIOS.\n");
114 goto Cleanup;
115 }
116
117 /* Initialize the VDM DOS kernel */
118 if (!DosInitialize())
119 {
120 wprintf(L"FATAL: Failed to initialize the VDM DOS kernel.\n");
121 goto Cleanup;
122 }
123
124 /* Start the process from the command line */
125 if (!DosCreateProcess(CommandLine, 0))
126 {
127 DisplayMessage(L"Could not start program: %S", CommandLine);
128 return -1;
129 }
130
131 /* Set the last timer tick to the current time */
132 QueryPerformanceCounter(&LastTimerTick);
133
134 /* Main loop */
135 while (VdmRunning)
136 {
137 /* Get the current number of ticks */
138 CurrentTickCount = GetTickCount();
139
140 /* Get the current performance counter value */
141 QueryPerformanceCounter(&Counter);
142
143 /* Get the number of PIT ticks that have passed */
144 TimerTicks = ((Counter.QuadPart - LastTimerTick.QuadPart)
145 * PIT_BASE_FREQUENCY) / Frequency.QuadPart;
146
147 /* Update the PIT */
148 for (i = 0; i < TimerTicks; i++) PitDecrementCount();
149 LastTimerTick = Counter;
150
151 /* Check for console input events every millisecond */
152 if (CurrentTickCount != LastTickCount)
153 {
154 CheckForInputEvents();
155 LastTickCount = CurrentTickCount;
156 }
157
158 /* Continue CPU emulation */
159 for (i = 0; i < STEPS_PER_CYCLE; i++) EmulatorStep();
160
161 Cycles += STEPS_PER_CYCLE;
162 if ((CurrentTickCount - LastCyclePrintout) >= 1000)
163 {
164 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles);
165 LastCyclePrintout = CurrentTickCount;
166 Cycles = 0;
167 }
168 }
169
170 Cleanup:
171 EmulatorCleanup();
172
173 return 0;
174 }
175
176 /* EOF */