[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 #include "ntvdm.h"
12 #include "emulator.h"
13 #include "bios.h"
14 #include "dos.h"
15 #include "timer.h"
16 #include "pic.h"
17 #include "ps2.h"
18
19 /* PUBLIC VARIABLES ***********************************************************/
20
21 BOOLEAN VdmRunning = TRUE;
22 LPVOID BaseAddress = NULL;
23 LPCWSTR ExceptionName[] =
24 {
25 L"Division By Zero",
26 L"Debug",
27 L"Unexpected Error",
28 L"Breakpoint",
29 L"Integer Overflow",
30 L"Bound Range Exceeded",
31 L"Invalid Opcode",
32 L"FPU Not Available"
33 };
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 MessageBox(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[128];
72 DWORD CurrentTickCount, LastTickCount = 0, Cycles = 0, LastCyclePrintout = 0;
73 LARGE_INTEGER Frequency, LastTimerTick, Counter;
74 LONGLONG TimerTicks;
75
76 /* Set the handler routine */
77 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
78
79 /* The DOS command line must be ASCII */
80 WideCharToMultiByte(CP_ACP, 0, GetCommandLine(), -1, CommandLine, 128, NULL, NULL);
81
82 if (!EmulatorInitialize()) return 1;
83
84 /* Initialize the performance counter (needed for hardware timers) */
85 if (!QueryPerformanceFrequency(&Frequency))
86 {
87 wprintf(L"FATAL: Performance counter not available\n");
88 goto Cleanup;
89 }
90
91 /* Initialize the system BIOS */
92 if (!BiosInitialize())
93 {
94 wprintf(L"FATAL: Failed to initialize the VDM BIOS.\n");
95 goto Cleanup;
96 }
97
98 /* Initialize the VDM DOS kernel */
99 if (!DosInitialize())
100 {
101 wprintf(L"FATAL: Failed to initialize the VDM DOS kernel.\n");
102 goto Cleanup;
103 }
104
105 /* Start the process from the command line */
106 if (!DosCreateProcess(CommandLine, 0))
107 {
108 DisplayMessage(L"Could not start program: %S", CommandLine);
109 return -1;
110 }
111
112 /* Set the last timer tick to the current time */
113 QueryPerformanceCounter(&LastTimerTick);
114
115 /* Main loop */
116 while (VdmRunning)
117 {
118 /* Get the current number of ticks */
119 CurrentTickCount = GetTickCount();
120
121 /* Get the current performance counter value */
122 QueryPerformanceCounter(&Counter);
123
124 /* Get the number of PIT ticks that have passed */
125 TimerTicks = ((Counter.QuadPart - LastTimerTick.QuadPart)
126 * PIT_BASE_FREQUENCY) / Frequency.QuadPart;
127
128 /* Update the PIT */
129 for (i = 0; i < TimerTicks; i++) PitDecrementCount();
130 LastTimerTick = Counter;
131
132 /* Check for console input events every millisecond */
133 if (CurrentTickCount != LastTickCount)
134 {
135 CheckForInputEvents();
136 LastTickCount = CurrentTickCount;
137 }
138
139 /* Continue CPU emulation */
140 for (i = 0; (i < STEPS_PER_CYCLE) && VdmRunning; i++)
141 {
142 EmulatorStep();
143 Cycles++;
144 }
145
146 if ((CurrentTickCount - LastCyclePrintout) >= 1000)
147 {
148 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles);
149 LastCyclePrintout = CurrentTickCount;
150 Cycles = 0;
151 }
152 }
153
154 Cleanup:
155 EmulatorCleanup();
156
157 return 0;
158 }
159
160 /* EOF */