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 *******************************************************************/
19 /* PUBLIC VARIABLES ***********************************************************/
21 BOOLEAN VdmRunning
= TRUE
;
22 LPVOID BaseAddress
= NULL
;
23 LPCWSTR ExceptionName
[] =
30 L
"Bound Range Exceeded",
35 /* PUBLIC FUNCTIONS ***********************************************************/
37 VOID
DisplayMessage(LPCWSTR Format
, ...)
42 va_start(Parameters
, Format
);
43 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
44 MessageBox(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
48 BOOL WINAPI
ConsoleCtrlHandler(DWORD ControlType
)
53 case CTRL_BREAK_EVENT
:
55 /* Perform interrupt 0x23 */
56 EmulatorInterrupt(0x23);
61 /* Stop the VDM if the user logs out or closes the console */
68 INT
wmain(INT argc
, WCHAR
*argv
[])
71 CHAR CommandLine
[128];
72 DWORD CurrentTickCount
, LastTickCount
= 0, Cycles
= 0, LastCyclePrintout
= 0;
73 LARGE_INTEGER Frequency
, LastTimerTick
, Counter
;
76 /* Set the handler routine */
77 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
79 /* The DOS command line must be ASCII */
80 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, 128, NULL
, NULL
);
82 if (!EmulatorInitialize()) return 1;
84 /* Initialize the performance counter (needed for hardware timers) */
85 if (!QueryPerformanceFrequency(&Frequency
))
87 wprintf(L
"FATAL: Performance counter not available\n");
91 /* Initialize the system BIOS */
92 if (!BiosInitialize())
94 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
98 /* Initialize the VDM DOS kernel */
101 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
105 /* Start the process from the command line */
106 if (!DosCreateProcess(CommandLine
, 0))
108 DisplayMessage(L
"Could not start program: %S", CommandLine
);
112 /* Set the last timer tick to the current time */
113 QueryPerformanceCounter(&LastTimerTick
);
118 /* Get the current number of ticks */
119 CurrentTickCount
= GetTickCount();
121 /* Get the current performance counter value */
122 QueryPerformanceCounter(&Counter
);
124 /* Get the number of PIT ticks that have passed */
125 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
126 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
129 for (i
= 0; i
< TimerTicks
; i
++) PitDecrementCount();
130 LastTimerTick
= Counter
;
132 /* Check for console input events every millisecond */
133 if (CurrentTickCount
!= LastTickCount
)
135 CheckForInputEvents();
136 LastTickCount
= CurrentTickCount
;
139 /* Continue CPU emulation */
140 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
146 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
148 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles
);
149 LastCyclePrintout
= CurrentTickCount
;