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 *******************************************************************/
23 * Activate this line if you want to be able to test NTVDM with:
28 /* PUBLIC VARIABLES ***********************************************************/
30 BOOLEAN VdmRunning
= TRUE
;
31 LPVOID BaseAddress
= NULL
;
32 LPCWSTR ExceptionName
[] =
39 L
"Bound Range Exceeded",
44 /* PUBLIC FUNCTIONS ***********************************************************/
46 VOID
DisplayMessage(LPCWSTR Format
, ...)
51 va_start(Parameters
, Format
);
52 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
53 MessageBoxW(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
57 BOOL WINAPI
ConsoleCtrlHandler(DWORD ControlType
)
62 case CTRL_BREAK_EVENT
:
64 /* Perform interrupt 0x23 */
65 EmulatorInterrupt(0x23);
70 /* Stop the VDM if the user logs out or closes the console */
77 INT
wmain(INT argc
, WCHAR
*argv
[])
80 CHAR CommandLine
[128];
81 DWORD CurrentTickCount
;
82 DWORD LastTickCount
= GetTickCount();
84 DWORD LastCyclePrintout
= GetTickCount();
85 DWORD LastVerticalRefresh
= GetTickCount();
86 LARGE_INTEGER Frequency
, LastTimerTick
, Counter
;
89 /* Set the handler routine */
90 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
93 UNREFERENCED_PARAMETER(argc
);
94 UNREFERENCED_PARAMETER(argv
);
96 /* The DOS command line must be ASCII */
97 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, 128, NULL
, NULL
);
99 if (argc
== 2 && argv
[1] != NULL
)
101 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, CommandLine
, 128, NULL
, NULL
);
105 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
106 L
"Usage: NTVDM <executable>\n");
111 if (!EmulatorInitialize())
113 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
117 /* Initialize the performance counter (needed for hardware timers) */
118 if (!QueryPerformanceFrequency(&Frequency
))
120 wprintf(L
"FATAL: Performance counter not available\n");
124 /* Initialize the system BIOS */
125 if (!BiosInitialize())
127 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
131 /* Initialize the VDM DOS kernel */
132 if (!DosInitialize())
134 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
138 /* Start the process from the command line */
139 if (!DosCreateProcess(CommandLine
, 0))
141 DisplayMessage(L
"Could not start program: %S", CommandLine
);
145 /* Set the last timer tick to the current time */
146 QueryPerformanceCounter(&LastTimerTick
);
151 /* Get the current number of ticks */
152 CurrentTickCount
= GetTickCount();
154 /* Get the current performance counter value */
155 QueryPerformanceCounter(&Counter
);
157 /* Get the number of PIT ticks that have passed */
158 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
159 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
162 for (i
= 0; i
< TimerTicks
; i
++) PitDecrementCount();
163 LastTimerTick
= Counter
;
165 /* Check for console input events every millisecond */
166 if (CurrentTickCount
!= LastTickCount
)
168 CheckForInputEvents();
169 LastTickCount
= CurrentTickCount
;
172 /* Check for vertical retrace */
173 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
176 LastVerticalRefresh
= CurrentTickCount
;
179 /* Horizontal retrace occurs as fast as possible */
180 VgaHorizontalRetrace();
182 /* Continue CPU emulation */
183 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
189 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
191 DPRINT1("NTVDM: %lu Instructions Per Second\n", Cycles
);
192 LastCyclePrintout
= CurrentTickCount
;
197 /* Perform another screen refresh */