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
);
84 /* The DOS command line must be ASCII */
85 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, 128, NULL
, NULL
);
87 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, CommandLine
, 128, NULL
, NULL
);
90 if (!EmulatorInitialize())
92 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
96 /* Initialize the performance counter (needed for hardware timers) */
97 if (!QueryPerformanceFrequency(&Frequency
))
99 wprintf(L
"FATAL: Performance counter not available\n");
103 /* Initialize the system BIOS */
104 if (!BiosInitialize())
106 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
110 /* Initialize the VDM DOS kernel */
111 if (!DosInitialize())
113 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
117 /* Start the process from the command line */
118 if (!DosCreateProcess(CommandLine
, 0))
120 DisplayMessage(L
"Could not start program: %S", CommandLine
);
124 /* Set the last timer tick to the current time */
125 QueryPerformanceCounter(&LastTimerTick
);
130 /* Get the current number of ticks */
131 CurrentTickCount
= GetTickCount();
133 /* Get the current performance counter value */
134 QueryPerformanceCounter(&Counter
);
136 /* Get the number of PIT ticks that have passed */
137 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
138 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
141 for (i
= 0; i
< TimerTicks
; i
++) PitDecrementCount();
142 LastTimerTick
= Counter
;
144 /* Check for console input events every millisecond */
145 if (CurrentTickCount
!= LastTickCount
)
147 CheckForInputEvents();
148 LastTickCount
= CurrentTickCount
;
151 /* Check for vertical refresh */
152 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
154 BiosVerticalRefresh();
155 LastVerticalRefresh
= CurrentTickCount
;
158 /* Continue CPU emulation */
159 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
165 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
167 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles
);
168 LastCyclePrintout
= CurrentTickCount
;