#define NDEBUG
+#include "ntvdm.h"
#include "emulator.h"
#include "cpu/cpu.h"
#include "int32.h"
#include "dos/dem.h"
#include "device.h"
#include "memory.h"
+#include "himem.h"
#include "bios/bios.h"
#include "io.h"
#include "hardware/ps2.h"
+#include "emsdrv.h"
+
/* PRIVATE VARIABLES **********************************************************/
+#define INDOS_POINTER MAKELONG(0x00FE, 0x0070)
+
CALLBACK16 DosContext;
static DWORD DiskTransferArea;
static CHAR CurrentDirectories[NUM_DRIVES][DOS_DIR_LENGTH];
static DOS_SFT_ENTRY DosSystemFileTable[DOS_SFT_SIZE];
static WORD DosErrorLevel = 0x0000;
+static PBYTE InDos;
/* PUBLIC VARIABLES ***********************************************************/
{
PDOS_SFT_ENTRY SftEntry;
HANDLE StandardHandles[3];
- PDOS_DEVICE_NODE ConIn = DosGetDevice("CONIN$");
- PDOS_DEVICE_NODE ConOut = DosGetDevice("CONOUT$");
- ASSERT(ConIn != NULL && ConOut != NULL);
+ PDOS_DEVICE_NODE Con = DosGetDevice("CON");
+ ASSERT(Con != NULL);
/* Get the native standard handles */
StandardHandles[0] = GetStdHandle(STD_INPUT_HANDLE);
for (i = 0; i < 3; i++)
{
- PDOS_DEVICE_NODE Device = (i == 0) ? ConIn : ConOut;
-
/* Find the corresponding SFT entry */
if (IsConsoleHandle(StandardHandles[i]))
{
- SftEntry = DosFindDeviceSftEntry(Device);
+ SftEntry = DosFindDeviceSftEntry(Con);
}
else
{
if (IsConsoleHandle(StandardHandles[i]))
{
SftEntry->Type = DOS_SFT_ENTRY_DEVICE;
- SftEntry->DeviceNode = Device;
+ SftEntry->DeviceNode = Con;
/* Call the open routine */
- if (Device->OpenRoutine) Device->OpenRoutine(Device);
+ if (Con->OpenRoutine) Con->OpenRoutine(Con);
}
else
{
return TRUE;
}
+static BOOLEAN DosControlBreak(VOID)
+{
+ setCF(0);
+
+ /* Call interrupt 0x23 */
+ Int32Call(&DosContext, 0x23);
+
+ if (getCF())
+ {
+ DosTerminateProcess(CurrentPsp, 0, 0);
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
/* PUBLIC FUNCTIONS ***********************************************************/
PDOS_SFT_ENTRY DosGetSftEntry(WORD DosHandle)
return &DosSystemFileTable[HandleTable[DosHandle]];
}
-VOID DosInitializePsp(WORD PspSegment, LPCSTR CommandLine, WORD ProgramSize, WORD Environment)
+VOID DosInitializePsp(WORD PspSegment,
+ LPCSTR CommandLine,
+ WORD ProgramSize,
+ WORD Environment,
+ DWORD ReturnAddress)
{
PDOS_PSP PspBlock = SEGMENT_TO_PSP(PspSegment);
LPDWORD IntVecTable = (LPDWORD)((ULONG_PTR)BaseAddress);
PspBlock->LastParagraph = PspSegment + ProgramSize - 1;
/* Save the interrupt vectors */
- PspBlock->TerminateAddress = IntVecTable[0x22];
+ PspBlock->TerminateAddress = ReturnAddress;
PspBlock->BreakAddress = IntVecTable[0x23];
PspBlock->CriticalAddress = IntVecTable[0x24];
IN LPCSTR ExecutablePath,
IN LPCSTR CommandLine,
IN LPCSTR Environment OPTIONAL,
+ IN DWORD ReturnAddress OPTIONAL,
OUT PDWORD StackLocation OPTIONAL,
OUT PDWORD EntryPoint OPTIONAL)
{
DosInitializePsp(Segment,
CommandLine,
(WORD)ExeSize,
- EnvBlock);
+ EnvBlock,
+ ReturnAddress);
/* The process owns its own memory */
DosChangeMemoryOwner(Segment, Segment);
DosInitializePsp(Segment,
CommandLine,
MaxAllocSize,
- EnvBlock);
+ EnvBlock,
+ ReturnAddress);
if (LoadType == DOS_LOAD_AND_EXECUTE)
{
IN LPCSTR Environment OPTIONAL)
{
DWORD Result;
+ LPDWORD IntVecTable = (LPDWORD)((ULONG_PTR)BaseAddress);
Result = DosLoadExecutable(DOS_LOAD_AND_EXECUTE,
ExecutablePath,
CommandLine,
Environment,
+ IntVecTable[0x20], // Use INT 20h
NULL,
NULL);
if (Result != ERROR_SUCCESS) goto Quit;
/* Attach to the console */
- VidBiosAttachToConsole(); // FIXME: And in fact, attach the full NTVDM UI to the console
+ ConsoleAttach();
+ VidBiosAttachToConsole();
// HACK: Simulate a ENTER key release scancode on the PS/2 port because
// some apps expect to read a key release scancode (> 0x80) when they
CpuSimulate();
/* Detach from the console */
- VidBiosDetachFromConsole(); // FIXME: And in fact, detach the full NTVDM UI from the console
+ VidBiosDetachFromConsole();
+ ConsoleDetach();
Quit:
return Result;
#ifndef STANDALONE
WORD DosCreateProcess(DOS_EXEC_TYPE LoadType,
LPCSTR ProgramName,
- PDOS_EXEC_PARAM_BLOCK Parameters)
+ PDOS_EXEC_PARAM_BLOCK Parameters,
+ DWORD ReturnAddress)
{
DWORD Result;
DWORD BinaryType;
CHAR PifFile[MAX_PATH];
CHAR Desktop[MAX_PATH];
CHAR Title[MAX_PATH];
- CHAR Env[MAX_PATH];
+ ULONG EnvSize = 256;
+ PVOID Env = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, EnvSize);
STARTUPINFOA StartupInfo;
PROCESS_INFORMATION ProcessInfo;
CommandInfo.Title = Title;
CommandInfo.TitleLen = sizeof(Title);
CommandInfo.Env = Env;
- CommandInfo.EnvLen = sizeof(Env);
+ CommandInfo.EnvLen = EnvSize;
+Command:
/* Get the VDM command information */
if (!GetNextVDMCommand(&CommandInfo))
{
+ if (CommandInfo.EnvLen > EnvSize)
+ {
+ /* Expand the environment size */
+ EnvSize = CommandInfo.EnvLen;
+ CommandInfo.Env = Env = RtlReAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY, Env, EnvSize);
+
+ /* Repeat the request */
+ CommandInfo.VDMState |= VDM_FLAG_RETRY;
+ goto Command;
+ }
+
/* Shouldn't happen */
ASSERT(FALSE);
}
- /* Increment the re-entry count */
- CommandInfo.VDMState = VDM_INC_REENTER_COUNT;
- GetNextVDMCommand(&CommandInfo);
-
/* Load the executable */
Result = DosLoadExecutable(LoadType,
AppName,
CmdLine,
Env,
+ ReturnAddress,
&Parameters->StackLocation,
&Parameters->EntryPoint);
- if (Result != ERROR_SUCCESS)
+ if (Result == ERROR_SUCCESS)
+ {
+ /* Increment the re-entry count */
+ CommandInfo.VDMState = VDM_INC_REENTER_COUNT;
+ GetNextVDMCommand(&CommandInfo);
+ }
+ else
{
DisplayMessage(L"Could not load '%S'. Error: %u", AppName, Result);
- // FIXME: Decrement the reenter count. Or, instead, just increment
- // the VDM reenter count *only* if this call succeeds...
}
break;
}
}
+ RtlFreeHeap(RtlGetProcessHeap(), 0, Env);
+
/* Close the handles */
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
}
#endif
-VOID DosTerminateProcess(WORD Psp, BYTE ReturnCode)
+VOID DosTerminateProcess(WORD Psp, BYTE ReturnCode, WORD KeepResident)
{
WORD i;
WORD McbSegment = FIRST_MCB_SEGMENT;
LPDWORD IntVecTable = (LPDWORD)((ULONG_PTR)BaseAddress);
PDOS_PSP PspBlock = SEGMENT_TO_PSP(Psp);
- DPRINT("DosTerminateProcess: Psp 0x%04X, ReturnCode 0x%02X\n",
+ DPRINT("DosTerminateProcess: Psp 0x%04X, ReturnCode 0x%02X, KeepResident 0x%04X\n",
Psp,
- ReturnCode);
+ ReturnCode,
+ KeepResident);
/* Check if this PSP is it's own parent */
if (PspBlock->ParentPsp == Psp) goto Done;
- for (i = 0; i < PspBlock->HandleTableSize; i++)
+ if (KeepResident == 0)
{
- /* Close the handle */
- DosCloseHandle(i);
+ for (i = 0; i < PspBlock->HandleTableSize; i++)
+ {
+ /* Close the handle */
+ DosCloseHandle(i);
+ }
}
/* Free the memory used by the process */
CurrentMcb = SEGMENT_TO_MCB(McbSegment);
/* Make sure the MCB is valid */
- if (CurrentMcb->BlockType != 'M' && CurrentMcb->BlockType !='Z') break;
+ if (CurrentMcb->BlockType != 'M' && CurrentMcb->BlockType != 'Z') break;
- /* If this block was allocated by the process, free it */
- if (CurrentMcb->OwnerPsp == Psp) DosFreeMemory(McbSegment + 1);
+ /* Check if this block was allocated by the process */
+ if (CurrentMcb->OwnerPsp == Psp)
+ {
+ if (KeepResident == 0)
+ {
+ /* Free this entire block */
+ DosFreeMemory(McbSegment + 1);
+ }
+ else if (KeepResident < CurrentMcb->Size)
+ {
+ /* Reduce the size of the block */
+ DosResizeMemory(McbSegment + 1, KeepResident, NULL);
+
+ /* No further paragraphs need to stay resident */
+ KeepResident = 0;
+ }
+ else
+ {
+ /* Just reduce the amount of paragraphs we need to keep resident */
+ KeepResident -= CurrentMcb->Size;
+ }
+ }
/* If this was the last block, quit */
if (CurrentMcb->BlockType == 'Z') break;
BOOLEAN DosHandleIoctl(BYTE ControlCode, WORD FileHandle)
{
PDOS_SFT_ENTRY SftEntry = DosGetSftEntry(FileHandle);
- PDOS_DEVICE_NODE Node;
+ PDOS_DEVICE_NODE Node = NULL;
- /* Make sure it exists and is a device */
- if (!SftEntry || SftEntry->Type != DOS_SFT_ENTRY_DEVICE)
+ /* Make sure it exists */
+ if (!SftEntry)
{
DosLastError = ERROR_FILE_NOT_FOUND;
return FALSE;
}
- Node = SftEntry->DeviceNode;
+ if (SftEntry->Type == DOS_SFT_ENTRY_DEVICE) Node = SftEntry->DeviceNode;
switch (ControlCode)
{
/* Get Device Information */
case 0x00:
{
+ WORD InfoWord = 0;
+
/*
* See Ralf Brown: http://www.ctyme.com/intr/rb-2820.htm
* for a list of possible flags.
*/
- /* Return the device information word */
- setDX(Node->DeviceAttributes);
+ if (Node)
+ {
+ /* Return the device attributes with bit 7 set */
+ InfoWord = Node->DeviceAttributes | (1 << 7);
+ }
+
+ setDX(InfoWord);
return TRUE;
}
/* Set Device Information */
case 0x01:
{
- Node->DeviceAttributes = getDX();
- return TRUE;
+ // TODO: NOT IMPLEMENTED
+ UNIMPLEMENTED;
+
+ return FALSE;
}
/* Read From Device I/O Control Channel */
{
WORD Length = getCX();
- if (!(Node->DeviceAttributes & DOS_DEVATTR_IOCTL))
+ if (Node == NULL || !(Node->DeviceAttributes & DOS_DEVATTR_IOCTL))
{
DosLastError = ERROR_INVALID_FUNCTION;
return FALSE;
{
WORD Length = getCX();
- if (!(Node->DeviceAttributes & DOS_DEVATTR_IOCTL))
+ if (Node == NULL || !(Node->DeviceAttributes & DOS_DEVATTR_IOCTL))
{
DosLastError = ERROR_INVALID_FUNCTION;
return FALSE;
VOID WINAPI DosInt20h(LPWORD Stack)
{
/* This is the exit interrupt */
- DosTerminateProcess(Stack[STACK_CS], 0);
+ DosTerminateProcess(Stack[STACK_CS], 0, 0);
}
VOID WINAPI DosInt21h(LPWORD Stack)
PDOS_COUNTRY_CODE_BUFFER CountryCodeBuffer;
INT Return;
+ (*InDos)++;
+
/* Check the value in the AH register */
switch (getAH())
{
/* Terminate Program */
case 0x00:
{
- DosTerminateProcess(Stack[STACK_CS], 0);
+ DosTerminateProcess(Stack[STACK_CS], 0, 0);
break;
}
{
DPRINT("Char input without echo\n");
- // FIXME: Under DOS 2+, input handle may be redirected!!!!
Character = DosReadCharacter(DOS_INPUT_HANDLE);
// FIXME: For 0x07, do not check Ctrl-C/Break.
// For 0x08, do check those control sequences and if needed,
// call INT 0x23.
- // /* Let the BOP repeat if needed */
- // if (getCF()) break;
-
setAL(Character);
break;
}
while (Count < InputBuffer->MaxLength)
{
- // FIXME!! This function should interpret backspaces etc...
-
/* Try to read a character (wait) */
Character = DosReadCharacter(DOS_INPUT_HANDLE);
- // FIXME: Check whether Ctrl-C / Ctrl-Break is pressed, and call INT 23h if so.
-
- /* Echo the character and append it to the buffer */
- DosPrintCharacter(DOS_OUTPUT_HANDLE, Character);
- InputBuffer->Buffer[Count] = Character;
-
- Count++; /* Carriage returns are also counted */
+ switch (Character)
+ {
+ /* Extended character */
+ case '\0':
+ {
+ /* Read the scancode */
+ DosReadCharacter(DOS_INPUT_HANDLE);
+ break;
+ }
+
+ /* Ctrl-C */
+ case 0x03:
+ {
+ DosPrintCharacter(DOS_OUTPUT_HANDLE, '^');
+ DosPrintCharacter(DOS_OUTPUT_HANDLE, 'C');
+
+ if (DosControlBreak())
+ {
+ /* Set the character to a newline to exit the loop */
+ Character = '\r';
+ }
+
+ break;
+ }
+
+ /* Backspace */
+ case '\b':
+ {
+ if (Count > 0)
+ {
+ Count--;
+
+ /* Erase the character */
+ DosPrintCharacter(DOS_OUTPUT_HANDLE, '\b');
+ DosPrintCharacter(DOS_OUTPUT_HANDLE, ' ');
+ DosPrintCharacter(DOS_OUTPUT_HANDLE, '\b');
+ }
+
+ break;
+ }
+
+ default:
+ {
+ /* Append it to the buffer */
+ InputBuffer->Buffer[Count] = Character;
+ Count++; /* Carriage returns are also counted */
+
+ /* Check if this is a special character */
+ if (Character < 0x20 && Character != 0x0A && Character != 0x0D)
+ {
+ DosPrintCharacter(DOS_OUTPUT_HANDLE, '^');
+ Character += 'A' - 1;
+ }
+
+ /* Echo the character */
+ DosPrintCharacter(DOS_OUTPUT_HANDLE, Character);
+ }
+ }
if (Character == '\r') break;
}
break;
}
+ /* Parse Filename into FCB */
+ case 0x29:
+ {
+ PCHAR FileName = (PCHAR)SEG_OFF_TO_PTR(getDS(), getSI());
+ PDOS_FCB Fcb = (PDOS_FCB)SEG_OFF_TO_PTR(getES(), getDI());
+ BYTE Options = getAL();
+ INT i;
+ CHAR FillChar = ' ';
+
+ if (FileName[1] == ':')
+ {
+ /* Set the drive number */
+ Fcb->DriveNumber = RtlUpperChar(FileName[0]) - 'A' + 1;
+
+ /* Skip to the file name part */
+ FileName += 2;
+ }
+ else
+ {
+ /* No drive number specified */
+ if (Options & (1 << 1)) Fcb->DriveNumber = CurrentDrive + 1;
+ else Fcb->DriveNumber = 0;
+ }
+
+ /* Parse the file name */
+ i = 0;
+ while ((*FileName > 0x20) && (i < 8))
+ {
+ if (*FileName == '.') break;
+ else if (*FileName == '*')
+ {
+ FillChar = '?';
+ break;
+ }
+
+ Fcb->FileName[i++] = RtlUpperChar(*FileName++);
+ }
+
+ /* Fill the whole field with blanks only if bit 2 is not set */
+ if ((FillChar != ' ') || (i != 0) || !(Options & (1 << 2)))
+ {
+ for (; i < 8; i++) Fcb->FileName[i] = FillChar;
+ }
+
+ /* Skip to the extension part */
+ while (*FileName > 0x20 && *FileName != '.') FileName++;
+ if (*FileName == '.') FileName++;
+
+ /* Now parse the extension */
+ i = 0;
+ FillChar = ' ';
+
+ while ((*FileName > 0x20) && (i < 3))
+ {
+ if (*FileName == '*')
+ {
+ FillChar = '?';
+ break;
+ }
+
+ Fcb->FileExt[i++] = RtlUpperChar(*FileName++);
+ }
+
+ /* Fill the whole field with blanks only if bit 3 is not set */
+ if ((FillChar != ' ') || (i != 0) || !(Options & (1 << 3)))
+ {
+ for (; i < 3; i++) Fcb->FileExt[i] = FillChar;
+ }
+
+ break;
+ }
+
/* Get System Date */
case 0x2A:
{
break;
}
+ /* Terminate and Stay Resident */
+ case 0x31:
+ {
+ DPRINT1("Process going resident: %u paragraphs kept\n", getDX());
+ DosTerminateProcess(CurrentPsp, getAL(), getDX());
+ break;
+ }
+
/* Extended functionalities */
case 0x33:
{
break;
}
+ /* Get Address of InDOS flag */
+ case 0x34:
+ {
+ setES(HIWORD(INDOS_POINTER));
+ setBX(LOWORD(INDOS_POINTER));
+
+ break;
+ }
+
/* Get Interrupt Vector */
case 0x35:
{
WORD BytesRead = 0;
WORD ErrorCode;
- DPRINT1("INT 21h, AH = 3Fh\n");
+ DPRINT("DosReadFile(0x%04X)\n", getBX());
DoEcho = TRUE;
ErrorCode = DosReadFile(getBX(),
DOS_EXEC_TYPE LoadType = (DOS_EXEC_TYPE)getAL();
LPSTR ProgramName = SEG_OFF_TO_PTR(getDS(), getDX());
PDOS_EXEC_PARAM_BLOCK ParamBlock = SEG_OFF_TO_PTR(getES(), getBX());
- WORD ErrorCode = DosCreateProcess(LoadType, ProgramName, ParamBlock);
+ DWORD ReturnAddress = MAKELONG(Stack[STACK_IP], Stack[STACK_CS]);
+ WORD ErrorCode;
+
+ if (LoadType != DOS_LOAD_OVERLAY)
+ {
+ ErrorCode = DosCreateProcess(LoadType, ProgramName, ParamBlock, ReturnAddress);
+ }
+ else
+ {
+ ErrorCode = DosLoadExecutable(DOS_LOAD_OVERLAY,
+ ProgramName,
+ FAR_POINTER(ParamBlock->CommandLine),
+ SEG_OFF_TO_PTR(ParamBlock->Environment, 0),
+ ReturnAddress,
+ NULL,
+ NULL);
+ }
if (ErrorCode == ERROR_SUCCESS)
{
/* Terminate With Return Code */
case 0x4C:
{
- DosTerminateProcess(CurrentPsp, getAL());
+ DosTerminateProcess(CurrentPsp, getAL(), 0);
break;
}
setES(0x0000);
setBX(0x0000);
- DisplayMessage(L"Required for AARD code, do you remember? :P");
+ DPRINT1("INT 21h, AH=52h: This application requires the internal DOS List of lists (SYSVARS). UNIMPLEMENTED\n");
break;
}
Stack[STACK_FLAGS] |= EMULATOR_FLAG_CF;
}
}
+
+ (*InDos)--;
}
VOID WINAPI DosBreakInterrupt(LPWORD Stack)
{
- UNREFERENCED_PARAMETER(Stack);
-
- /* Stop the VDM task */
- ResetEvent(VdmTaskEvent);
- CpuUnsimulate();
+ /* Set CF to terminate the running process */
+ Stack[STACK_FLAGS] |= EMULATOR_FLAG_CF;
}
VOID WINAPI DosFastConOut(LPWORD Stack)
VOID WINAPI DosInt2Fh(LPWORD Stack)
{
- DPRINT1("DOS Internal System Function INT 0x2F, AH = %xh, AL = %xh NOT IMPLEMENTED!\n",
- getAH(), getAL());
- Stack[STACK_FLAGS] |= EMULATOR_FLAG_CF;
+ switch (getAH())
+ {
+ /* Extended Memory Specification */
+ case 0x43:
+ {
+ DWORD DriverEntry;
+ if (!XmsGetDriverEntry(&DriverEntry)) break;
+
+ if (getAL() == 0x00)
+ {
+ /* The driver is loaded */
+ setAL(0x80);
+ }
+ else if (getAL() == 0x10)
+ {
+ setES(HIWORD(DriverEntry));
+ setBX(LOWORD(DriverEntry));
+ }
+ else
+ {
+ DPRINT1("Unknown DOS XMS Function: INT 0x2F, AH = 43h, AL = %xh\n", getAL());
+ }
+
+ break;
+ }
+
+ default:
+ {
+ DPRINT1("DOS Internal System Function INT 0x2F, AH = %xh, AL = %xh NOT IMPLEMENTED!\n",
+ getAH(), getAL());
+ Stack[STACK_FLAGS] |= EMULATOR_FLAG_CF;
+ }
+ }
}
BOOLEAN DosKRNLInitialize(VOID)
CHAR CurrentDirectory[MAX_PATH];
CHAR DosDirectory[DOS_DIR_LENGTH];
LPSTR Path;
- PDOS_DEVICE_NODE ConInDevice, ConOutDevice;
FILE *Stream;
WCHAR Buffer[256];
+ /* Setup the InDOS flag */
+ InDos = (PBYTE)FAR_POINTER(INDOS_POINTER);
+ *InDos = 0;
+
/* Clear the current directory buffer */
RtlZeroMemory(CurrentDirectories, sizeof(CurrentDirectories));
DosSystemFileTable[i].RefCount = 0;
}
- /* Load the EMS driver */
- EmsDrvInitialize();
-
- /* Load the CON driver */
- ConDrvInitialize(&ConInDevice, &ConOutDevice);
-
#endif
/* Initialize the callback context */
RegisterDosInt32(0x29, DosFastConOut ); // DOS 2+ Fast Console Output
RegisterDosInt32(0x2F, DosInt2Fh );
+ /* Load the EMS driver */
+ if (!EmsDrvInitialize(EMS_TOTAL_PAGES))
+ {
+ DPRINT1("Could not initialize EMS. EMS will not be available.\n"
+ "Try reducing the number of EMS pages.\n");
+ }
+
+ /* Load the XMS driver (HIMEM) */
+ XmsInitialize();
+
+ /* Load the CON driver */
+ ConDrvInitialize();
+
return TRUE;
}