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 *******************************************************************/
24 * Activate this line if you want to be able to test NTVDM with:
29 /* PUBLIC VARIABLES ***********************************************************/
31 BOOLEAN VdmRunning
= TRUE
;
32 LPVOID BaseAddress
= NULL
;
34 /* PUBLIC FUNCTIONS ***********************************************************/
36 VOID
DisplayMessage(LPCWSTR Format
, ...)
41 va_start(Parameters
, Format
);
42 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
43 MessageBoxW(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
47 BOOL WINAPI
ConsoleCtrlHandler(DWORD ControlType
)
52 case CTRL_BREAK_EVENT
:
54 /* Perform interrupt 0x23 */
55 EmulatorInterrupt(0x23);
60 /* Stop the VDM if the user logs out or closes the console */
67 INT
wmain(INT argc
, WCHAR
*argv
[])
70 CHAR CommandLine
[DOS_CMDLINE_LENGTH
];
71 DWORD CurrentTickCount
;
73 DWORD LastCyclePrintout
= GetTickCount();
74 DWORD LastVerticalRefresh
= GetTickCount();
75 LARGE_INTEGER Frequency
, LastTimerTick
, Counter
;
77 HANDLE InputThread
= NULL
;
79 /* Set the handler routine */
80 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
83 UNREFERENCED_PARAMETER(argc
);
84 UNREFERENCED_PARAMETER(argv
);
86 /* The DOS command line must be ASCII */
87 WideCharToMultiByte(CP_ACP
, 0, GetCommandLine(), -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
89 if (argc
== 2 && argv
[1] != NULL
)
91 WideCharToMultiByte(CP_ACP
, 0, argv
[1], -1, CommandLine
, sizeof(CommandLine
), NULL
, NULL
);
95 wprintf(L
"\nReactOS Virtual DOS Machine\n\n"
96 L
"Usage: NTVDM <executable>\n");
101 if (!EmulatorInitialize())
103 wprintf(L
"FATAL: Failed to initialize the CPU emulator\n");
107 /* Initialize the performance counter (needed for hardware timers) */
108 if (!QueryPerformanceFrequency(&Frequency
))
110 wprintf(L
"FATAL: Performance counter not available\n");
114 /* Initialize the system BIOS */
115 if (!BiosInitialize())
117 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
121 /* Initialize the PC Speaker */
124 /* Initialize the VDM DOS kernel */
125 if (!DosInitialize())
127 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
131 /* Start the process from the command line */
132 if (!DosCreateProcess(CommandLine
, 0))
134 DisplayMessage(L
"Could not start program: %S", CommandLine
);
138 /* Start the input thread */
139 InputThread
= CreateThread(NULL
, 0, &InputThreadProc
, NULL
, 0, NULL
);
141 /* Set the last timer tick to the current time */
142 QueryPerformanceCounter(&LastTimerTick
);
147 /* Get the current number of ticks */
148 CurrentTickCount
= GetTickCount();
150 /* Get the current performance counter value */
151 QueryPerformanceCounter(&Counter
);
153 /* Get the number of PIT ticks that have passed */
154 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
155 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
160 PitDecrementCount(TimerTicks
);
161 LastTimerTick
= Counter
;
164 /* Check for vertical retrace */
165 if ((CurrentTickCount
- LastVerticalRefresh
) >= 16)
168 LastVerticalRefresh
= CurrentTickCount
;
171 /* Horizontal retrace occurs as fast as possible */
172 VgaHorizontalRetrace();
174 /* Continue CPU emulation */
175 for (i
= 0; (i
< STEPS_PER_CYCLE
) && VdmRunning
; i
++)
181 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
183 DPRINT1("NTVDM: %lu Instructions Per Second\n", Cycles
);
184 LastCyclePrintout
= CurrentTickCount
;
189 /* Perform another screen refresh */
193 if (InputThread
!= NULL
) CloseHandle(InputThread
);