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 LARGE_INTEGER StartPerfCount
;
82 /* Set the handler routine */
83 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
86 UNREFERENCED_PARAMETER(argc
);
87 UNREFERENCED_PARAMETER(argv
);
89 /* The DOS command line must be ASCII */
90 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
92 if (argc
== 2 && argv
[1] != NULL
)
94 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
98 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
99 L
"Usage: NTVDM <executable>\n");
104 if (!EmulatorInitialize())
106 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
110 /* Initialize the performance counter (needed for hardware timers) */
111 if (!QueryPerformanceFrequency(&Frequency
))
113 wprintf(L
"FATAL: Performance counter not available\n");
117 /* Initialize the system BIOS */
118 if (!BiosInitialize())
120 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
124 /* Initialize the PC Speaker */
127 /* Initialize the VDM DOS kernel */
128 if (!DosInitialize())
130 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
134 /* Start the process from the command line */
135 if (!DosCreateProcess(CommandLine
, 0))
137 DisplayMessage(L
"Could not start program: %S", CommandLine
);
141 /* Find the starting performance and tick count */
142 StartTickCount
= GetTickCount();
143 QueryPerformanceCounter(&StartPerfCount
);
145 /* Set the last timer ticks to the current time */
146 LastTimerTick
= LastRtcTick
= StartPerfCount
;
151 DWORD PitResolution
= PitGetResolution();
152 DWORD RtcFrequency
= RtcGetTicksPerSecond();
154 /* Get the current number of ticks */
155 CurrentTickCount
= GetTickCount();
157 if ((PitResolution
<= 1000) && (RtcFrequency
<= 1000))
159 /* Calculate the approximate performance counter value instead */
160 Counter
.QuadPart
= StartPerfCount
.QuadPart
161 + (CurrentTickCount
- StartTickCount
)
162 * (Frequency
.QuadPart
/ 1000);
166 /* Get the current performance counter value */
167 QueryPerformanceCounter(&Counter
);
170 /* Get the number of PIT ticks that have passed */
171 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
172 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
177 PitDecrementCount(TimerTicks
);
178 LastTimerTick
= Counter
;
181 /* Check for RTC update */
182 if ((CurrentTickCount
- LastClockUpdate
) >= 1000)
185 LastClockUpdate
= CurrentTickCount
;
188 /* Check for RTC periodic tick */
189 if ((Counter
.QuadPart
- LastRtcTick
.QuadPart
)
190 >= (Frequency
.QuadPart
/ (LONGLONG
)RtcFrequency
))
193 LastRtcTick
= Counter
;
196 /* Check for vertical retrace */
197 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
200 LastVerticalRefresh
= CurrentTickCount
;
203 /* Horizontal retrace occurs as fast as possible */
204 VgaHorizontalRetrace();
206 /* Continue CPU emulation */
207 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
213 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
215 DPRINT1("NTVDM: %lu Instructions Per Second\n", Cycles
);
216 LastCyclePrintout
= CurrentTickCount
;
221 /* Perform another screen refresh */