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>
11 BOOLEAN VdmRunning
= TRUE
;
12 LPVOID BaseAddress
= NULL
;
13 LPCWSTR ExceptionName
[] =
20 L
"Bound Range Exceeded",
25 VOID
DisplayMessage(LPCWSTR Format
, ...)
30 va_start(Parameters
, Format
);
31 _vsnwprintf(Buffer
, 256, Format
, Parameters
);
32 MessageBox(NULL
, Buffer
, L
"NTVDM Subsystem", MB_OK
);
36 BOOL WINAPI
ConsoleCtrlHandler(DWORD ControlType
)
41 case CTRL_BREAK_EVENT
:
43 /* Perform interrupt 0x23 */
44 EmulatorInterrupt(0x23);
48 /* Stop the VDM if the user logs out or closes the console */
55 INT
wmain(INT argc
, WCHAR
*argv
[])
58 BOOLEAN PrintUsage
= TRUE
;
59 CHAR CommandLine
[128];
60 DWORD CurrentTickCount
, LastTickCount
= 0, Cycles
= 0, LastCyclePrintout
= 0;
61 LARGE_INTEGER Frequency
, LastTimerTick
, Counter
;
64 /* Set the handler routine */
65 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
67 /* Parse the command line arguments */
68 for (i
= 1; i
< argc
; i
++)
70 if (argv
[i
][0] != L
'-' && argv
[i
][0] != L
'/') continue;
77 if (argv
[i
+1] != NULL
)
79 /* The DOS command line must be ASCII */
80 WideCharToMultiByte(CP_ACP
, 0, argv
[i
+1], -1, CommandLine
, 128, NULL
, NULL
);
82 /* This is the only mandatory parameter */
89 wprintf(L
"Unknown option: %s", argv
[i
]);
96 wprintf(L
"ReactOS Virtual DOS Machine\n\n");
97 wprintf(L
"Usage: NTVDM /F <PROGRAM>\n");
101 if (!EmulatorInitialize()) return 1;
103 /* Initialize the performance counter (needed for hardware timers) */
104 if (!QueryPerformanceFrequency(&Frequency
))
106 wprintf(L
"FATAL: Performance counter not available\n");
110 /* Initialize the system BIOS */
111 if (!BiosInitialize())
113 wprintf(L
"FATAL: Failed to initialize the VDM BIOS.\n");
117 /* Initialize the VDM DOS kernel */
118 if (!DosInitialize())
120 wprintf(L
"FATAL: Failed to initialize the VDM DOS kernel.\n");
124 /* Start the process from the command line */
125 if (!DosCreateProcess(CommandLine
, 0))
127 DisplayMessage(L
"Could not start program: %S", CommandLine
);
131 /* Set the last timer tick to the current time */
132 QueryPerformanceCounter(&LastTimerTick
);
137 /* Get the current number of ticks */
138 CurrentTickCount
= GetTickCount();
140 /* Get the current performance counter value */
141 QueryPerformanceCounter(&Counter
);
143 /* Get the number of PIT ticks that have passed */
144 TimerTicks
= ((Counter
.QuadPart
- LastTimerTick
.QuadPart
)
145 * PIT_BASE_FREQUENCY
) / Frequency
.QuadPart
;
148 for (i
= 0; i
< TimerTicks
; i
++) PitDecrementCount();
149 LastTimerTick
= Counter
;
151 /* Check for console input events every millisecond */
152 if (CurrentTickCount
!= LastTickCount
)
154 CheckForInputEvents();
155 LastTickCount
= CurrentTickCount
;
158 /* Continue CPU emulation */
159 for (i
= 0; i
< STEPS_PER_CYCLE
; i
++) EmulatorStep();
161 Cycles
+= STEPS_PER_CYCLE
;
162 if ((CurrentTickCount
- LastCyclePrintout
) >= 1000)
164 DPRINT1("NTVDM: %d Instructions Per Second\n", Cycles
);
165 LastCyclePrintout
= CurrentTickCount
;