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
;
73 DWORD LastTickCount
= GetTickCount();
75 DWORD LastCyclePrintout
= GetTickCount();
76 DWORD LastVerticalRefresh
= GetTickCount();
77 LARGE_INTEGER Frequency
, LastTimerTick
, Counter
;
80 /* Set the handler routine */
81 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
83 /* The DOS command line must be ASCII */
84 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, 128, NULL
, NULL
);
86 if (!EmulatorInitialize())
88 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
92 /* Initialize the performance counter (needed for hardware timers) */
93 if (!QueryPerformanceFrequency(&Frequency
))
95 wprintf(L
"FATAL: Performance counter not available\n");
99 /* Initialize the system BIOS */
100 if (!BiosInitialize())
102 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
106 /* Initialize the VDM DOS kernel */
107 if (!DosInitialize())
109 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
113 /* Start the process from the command line */
114 if (!DosCreateProcess(CommandLine
, 0))
116 DisplayMessage(L
"Could not start program: %S", CommandLine
);
120 /* Set the last timer tick to the current time */
121 QueryPerformanceCounter(&LastTimerTick
);
126 /* Get the current number of ticks */
127 CurrentTickCount
= GetTickCount();
129 /* Get the current performance counter value */
130 QueryPerformanceCounter(&Counter
);
132 /* Get the number of PIT ticks that have passed */
133 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
134 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
137 for (i
= 0; i
< TimerTicks
; i
++) PitDecrementCount();
138 LastTimerTick
= Counter
;
140 /* Check for console input events every millisecond */
141 if (CurrentTickCount
!= LastTickCount
)
143 CheckForInputEvents();
144 LastTickCount
= CurrentTickCount
;
147 /* Check for vertical refresh */
148 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
150 BiosVerticalRefresh();
151 LastVerticalRefresh
= CurrentTickCount
;
154 /* Continue CPU emulation */
155 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
161 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
163 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles
);
164 LastCyclePrintout
= CurrentTickCount
;