2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: Virtual DOS Machine
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
22 * Activate this line if you want to be able to test NTVDM with:
27 /* PUBLIC VARIABLES ***********************************************************/
29 BOOLEAN VdmRunning
= TRUE
;
30 LPVOID BaseAddress
= NULL
;
31 LPCWSTR ExceptionName
[] =
38 L
"Bound Range Exceeded",
43 /* PUBLIC FUNCTIONS ***********************************************************/
45 VOID
DisplayMessage(LPCWSTR Format
, ...)
50 va_start(Parameters
, Format
);
51 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
52 MessageBox(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
56 BOOL WINAPI
ConsoleCtrlHandler(DWORD ControlType
)
61 case CTRL_BREAK_EVENT
:
63 /* Perform interrupt 0x23 */
64 EmulatorInterrupt(0x23);
69 /* Stop the VDM if the user logs out or closes the console */
76 INT
wmain(INT argc
, WCHAR
*argv
[])
79 CHAR CommandLine
[128];
80 DWORD CurrentTickCount
;
81 DWORD LastTickCount
= GetTickCount();
83 DWORD LastCyclePrintout
= GetTickCount();
84 DWORD LastVerticalRefresh
= GetTickCount();
85 LARGE_INTEGER Frequency
, LastTimerTick
, Counter
;
88 /* Set the handler routine */
89 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
92 /* The DOS command line must be ASCII */
93 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, 128, NULL
, NULL
);
95 if (argc
== 2 && argv
[1] != NULL
)
97 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, CommandLine
, 128, NULL
, NULL
);
101 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
102 L
"Usage: NTVDM <executable>\n");
107 if (!EmulatorInitialize())
109 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
113 /* Initialize the performance counter (needed for hardware timers) */
114 if (!QueryPerformanceFrequency(&Frequency
))
116 wprintf(L
"FATAL: Performance counter not available\n");
120 /* Initialize the system BIOS */
121 if (!BiosInitialize())
123 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
127 /* Initialize the VDM DOS kernel */
128 if (!DosInitialize())
130 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
134 /* Start the process from the command line */
135 if (!DosCreateProcess(CommandLine
, 0))
137 DisplayMessage(L
"Could not start program: %S", CommandLine
);
141 /* Set the last timer tick to the current time */
142 QueryPerformanceCounter(&LastTimerTick
);
147 /* Get the current number of ticks */
148 CurrentTickCount
= GetTickCount();
150 /* Get the current performance counter value */
151 QueryPerformanceCounter(&Counter
);
153 /* Get the number of PIT ticks that have passed */
154 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
155 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
158 for (i
= 0; i
< TimerTicks
; i
++) PitDecrementCount();
159 LastTimerTick
= Counter
;
161 /* Check for console input events every millisecond */
162 if (CurrentTickCount
!= LastTickCount
)
164 CheckForInputEvents();
165 LastTickCount
= CurrentTickCount
;
168 /* Check for vertical refresh */
169 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
171 BiosVerticalRefresh();
172 LastVerticalRefresh
= CurrentTickCount
;
175 /* Continue CPU emulation */
176 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
182 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
184 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles
);
185 LastCyclePrintout
= CurrentTickCount
;