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 *******************************************************************/
25 * Activate this line if you want to be able to test NTVDM with:
30 /* PUBLIC VARIABLES ***********************************************************/
32 BOOLEAN VdmRunning
= TRUE
;
33 LPVOID BaseAddress
= NULL
;
35 /* PUBLIC FUNCTIONS ***********************************************************/
37 VOID
DisplayMessage(LPCWSTR Format
, ...)
42 va_start(Parameters
, Format
);
43 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
44 MessageBoxW(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
[DOS_CMDLINE_LENGTH
];
72 DWORD CurrentTickCount
;
74 DWORD LastCyclePrintout
= GetTickCount();
75 DWORD LastVerticalRefresh
= GetTickCount();
76 DWORD LastClockUpdate
= GetTickCount();
77 LARGE_INTEGER Frequency
, LastTimerTick
, LastRtcTick
, Counter
;
79 HANDLE InputThread
= NULL
;
80 LARGE_INTEGER StartPerfCount
;
83 /* Set the handler routine */
84 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
87 UNREFERENCED_PARAMETER(argc
);
88 UNREFERENCED_PARAMETER(argv
);
90 /* The DOS command line must be ASCII */
91 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
93 if (argc
== 2 && argv
[1] != NULL
)
95 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
99 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
100 L
"Usage: NTVDM <executable>\n");
105 if (!EmulatorInitialize())
107 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
111 /* Initialize the performance counter (needed for hardware timers) */
112 if (!QueryPerformanceFrequency(&Frequency
))
114 wprintf(L
"FATAL: Performance counter not available\n");
118 /* Initialize the system BIOS */
119 if (!BiosInitialize())
121 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
125 /* Initialize the PC Speaker */
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 /* Start the input thread */
143 InputThread
= CreateThread(NULL
, 0, &InputThreadProc
, NULL
, 0, NULL
);
145 /* Find the starting performance and tick count */
146 StartTickCount
= GetTickCount();
147 QueryPerformanceCounter(&StartPerfCount
);
149 /* Set the last timer ticks to the current time */
150 LastTimerTick
= LastRtcTick
= StartPerfCount
;
155 DWORD PitResolution
= PitGetResolution();
156 DWORD RtcFrequency
= RtcGetTicksPerSecond();
158 /* Get the current number of ticks */
159 CurrentTickCount
= GetTickCount();
161 if ((PitResolution
<= 1000) && (RtcFrequency
<= 1000))
163 /* Calculate the approximate performance counter value instead */
164 Counter
.QuadPart
= StartPerfCount
.QuadPart
165 + (CurrentTickCount
- StartTickCount
)
166 * (Frequency
.QuadPart
/ 1000);
170 /* Get the current performance counter value */
171 QueryPerformanceCounter(&Counter
);
174 /* Get the number of PIT ticks that have passed */
175 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
176 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
181 PitDecrementCount(TimerTicks
);
182 LastTimerTick
= Counter
;
185 /* Check for RTC update */
186 if ((CurrentTickCount
- LastClockUpdate
) >= 1000)
189 LastClockUpdate
= CurrentTickCount
;
192 /* Check for RTC periodic tick */
193 if ((Counter
.QuadPart
- LastRtcTick
.QuadPart
)
194 >= (Frequency
.QuadPart
/ (LONGLONG
)RtcFrequency
))
197 LastRtcTick
= Counter
;
200 /* Check for vertical retrace */
201 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
204 LastVerticalRefresh
= CurrentTickCount
;
207 /* Horizontal retrace occurs as fast as possible */
208 VgaHorizontalRetrace();
210 /* Continue CPU emulation */
211 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
217 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
219 DPRINT1("NTVDM: %lu Instructions Per Second\n", Cycles
);
220 LastCyclePrintout
= CurrentTickCount
;
225 /* Perform another screen refresh */
229 if (InputThread
!= NULL
) CloseHandle(InputThread
);