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>
11 BOOLEAN VdmRunning
= TRUE
;
12 LPVOID BaseAddress
= NULL
;
13 LPCWSTR ExceptionName
[] =
20 L
"Bound Range Exceeded",
25 VOID
DisplayMessage(LPCWSTR Format
, ...)
30 va_start(Parameters
, Format
);
31 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
32 MessageBox(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
36 BOOL WINAPI
ConsoleCtrlHandler(DWORD ControlType
)
41 case CTRL_BREAK_EVENT
:
43 /* Perform interrupt 0x23 */
44 EmulatorInterrupt(0x23);
49 /* Stop the VDM if the user logs out or closes the console */
56 INT
wmain(INT argc
, WCHAR
*argv
[])
59 CHAR CommandLine
[128];
60 DWORD CurrentTickCount
, LastTickCount
= 0, Cycles
= 0, LastCyclePrintout
= 0;
61 LARGE_INTEGER Frequency
, LastTimerTick
, Counter
;
64 /* Set the handler routine */
65 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
67 /* The DOS command line must be ASCII */
68 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, 128, NULL
, NULL
);
70 if (!EmulatorInitialize()) return 1;
72 /* Initialize the performance counter (needed for hardware timers) */
73 if (!QueryPerformanceFrequency(&Frequency
))
75 wprintf(L
"FATAL: Performance counter not available\n");
79 /* Initialize the system BIOS */
80 if (!BiosInitialize())
82 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
86 /* Initialize the VDM DOS kernel */
89 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
93 /* Start the process from the command line */
94 if (!DosCreateProcess(CommandLine
, 0))
96 DisplayMessage(L
"Could not start program: %S", CommandLine
);
100 /* Set the last timer tick to the current time */
101 QueryPerformanceCounter(&LastTimerTick
);
106 /* Get the current number of ticks */
107 CurrentTickCount
= GetTickCount();
109 /* Get the current performance counter value */
110 QueryPerformanceCounter(&Counter
);
112 /* Get the number of PIT ticks that have passed */
113 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
114 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
117 for (i
= 0; i
< TimerTicks
; i
++) PitDecrementCount();
118 LastTimerTick
= Counter
;
120 /* Check for console input events every millisecond */
121 if (CurrentTickCount
!= LastTickCount
)
123 CheckForInputEvents();
124 LastTickCount
= CurrentTickCount
;
127 /* Continue CPU emulation */
128 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
134 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
136 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles
);
137 LastCyclePrintout
= CurrentTickCount
;