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 MessageBox(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 /* The DOS command line must be ASCII */
94 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, 128, NULL
, NULL
);
96 if (argc
== 2 && argv
[1] != NULL
)
98 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, CommandLine
, 128, NULL
, NULL
);
102 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
103 L
"Usage: NTVDM <executable>\n");
108 if (!EmulatorInitialize())
110 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
114 /* Initialize the performance counter (needed for hardware timers) */
115 if (!QueryPerformanceFrequency(&Frequency
))
117 wprintf(L
"FATAL: Performance counter not available\n");
121 /* Initialize the system BIOS */
122 if (!BiosInitialize())
124 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
128 /* Initialize the VDM DOS kernel */
129 if (!DosInitialize())
131 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
135 /* Start the process from the command line */
136 if (!DosCreateProcess(CommandLine
, 0))
138 DisplayMessage(L
"Could not start program: %S", CommandLine
);
142 /* Set the last timer tick to the current time */
143 QueryPerformanceCounter(&LastTimerTick
);
148 /* Get the current number of ticks */
149 CurrentTickCount
= GetTickCount();
151 /* Get the current performance counter value */
152 QueryPerformanceCounter(&Counter
);
154 /* Get the number of PIT ticks that have passed */
155 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
156 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
159 for (i
= 0; i
< TimerTicks
; i
++) PitDecrementCount();
160 LastTimerTick
= Counter
;
162 /* Check for console input events every millisecond */
163 if (CurrentTickCount
!= LastTickCount
)
165 CheckForInputEvents();
166 LastTickCount
= CurrentTickCount
;
169 /* Check for vertical retrace */
170 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
173 LastVerticalRefresh
= CurrentTickCount
;
176 /* Horizontal retrace occurs as fast as possible */
177 VgaHorizontalRetrace();
179 /* Continue CPU emulation */
180 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
186 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
188 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles
);
189 LastCyclePrintout
= CurrentTickCount
;
194 /* Perform another screen refresh */