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 DPRINT1("\n\nNTVDM Subsystem\n%S\n\n", Buffer
);
45 MessageBoxW(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
49 BOOL WINAPI
ConsoleCtrlHandler(DWORD ControlType
)
54 case CTRL_BREAK_EVENT
:
56 /* Perform interrupt 0x23 */
57 EmulatorInterrupt(0x23);
62 /* Stop the VDM if the user logs out or closes the console */
69 INT
wmain(INT argc
, WCHAR
*argv
[])
72 CHAR CommandLine
[DOS_CMDLINE_LENGTH
];
73 LARGE_INTEGER StartPerfCount
;
74 LARGE_INTEGER Frequency
, LastTimerTick
, LastRtcTick
, Counter
;
76 DWORD StartTickCount
, CurrentTickCount
;
77 DWORD LastClockUpdate
;
78 DWORD LastVerticalRefresh
;
79 DWORD LastCyclePrintout
;
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 DPRINT1("\n\n\nNTVDM - Starting '%s'...\n\n\n", CommandLine
);
106 if (!EmulatorInitialize())
108 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
112 /* Initialize the performance counter (needed for hardware timers) */
113 if (!QueryPerformanceFrequency(&Frequency
))
115 wprintf(L
"FATAL: Performance counter not available\n");
119 /* Initialize the CMOS */
120 if (!CmosInitialize())
122 wprintf(L
"FATAL: Failed to initialize the VDM CMOS.\n");
126 /* Initialize the system BIOS */
127 if (!BiosInitialize())
129 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
133 /* Initialize the PC Speaker */
136 /* Initialize the VDM DOS kernel */
137 if (!DosInitialize())
139 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
143 /* Start the process from the command line */
144 if (!DosCreateProcess(CommandLine
, 0))
146 DisplayMessage(L
"Could not start program: %S", CommandLine
);
150 /* Find the starting performance and tick count */
151 StartTickCount
= GetTickCount();
152 QueryPerformanceCounter(&StartPerfCount
);
154 /* Set the different last counts to the starting count */
155 LastClockUpdate
= LastVerticalRefresh
= LastCyclePrintout
= StartTickCount
;
157 /* Set the last timer ticks to the current time */
158 LastTimerTick
= LastRtcTick
= StartPerfCount
;
163 DWORD PitResolution
= PitGetResolution();
164 DWORD RtcFrequency
= RtcGetTicksPerSecond();
166 /* Get the current number of ticks */
167 CurrentTickCount
= GetTickCount();
169 if ((PitResolution
<= 1000) && (RtcFrequency
<= 1000))
171 /* Calculate the approximate performance counter value instead */
172 Counter
.QuadPart
= StartPerfCount
.QuadPart
173 + (CurrentTickCount
- StartTickCount
)
174 * (Frequency
.QuadPart
/ 1000);
178 /* Get the current performance counter value */
179 QueryPerformanceCounter(&Counter
);
182 /* Get the number of PIT ticks that have passed */
183 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
184 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
189 PitDecrementCount(TimerTicks
);
190 LastTimerTick
= Counter
;
193 /* Check for RTC update */
194 if ((CurrentTickCount
- LastClockUpdate
) >= 1000)
197 LastClockUpdate
= CurrentTickCount
;
200 /* Check for RTC periodic tick */
201 if ((Counter
.QuadPart
- LastRtcTick
.QuadPart
)
202 >= (Frequency
.QuadPart
/ (LONGLONG
)RtcFrequency
))
205 LastRtcTick
= Counter
;
208 /* Check for vertical retrace */
209 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
212 LastVerticalRefresh
= CurrentTickCount
;
215 /* Horizontal retrace occurs as fast as possible */
216 VgaHorizontalRetrace();
218 /* Continue CPU emulation */
219 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
225 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
227 DPRINT1("NTVDM: %lu Instructions Per Second\n", Cycles
);
228 LastCyclePrintout
= CurrentTickCount
;
233 /* Perform another screen refresh */
242 DPRINT1("\n\n\nNTVDM - Exiting...\n\n\n");