2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: VDM DOS Kernel
6 * PROGRAMMERS: Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
9 /* INCLUDES *******************************************************************/
15 /* PRIVATE VARIABLES **********************************************************/
17 static WORD CurrentPsp
= SYSTEM_PSP
;
18 static DWORD DiskTransferArea
;
20 /* PRIVATE FUNCTIONS **********************************************************/
22 static VOID
DosCombineFreeBlocks(WORD StartBlock
)
24 PDOS_MCB CurrentMcb
= SEGMENT_TO_MCB(StartBlock
), NextMcb
;
26 /* If this is the last block or it's not free, quit */
27 if (CurrentMcb
->BlockType
== 'Z' || CurrentMcb
->OwnerPsp
!= 0) return;
31 /* Get a pointer to the next MCB */
32 NextMcb
= SEGMENT_TO_MCB(StartBlock
+ CurrentMcb
->Size
+ 1);
34 /* Check if the next MCB is free */
35 if (NextMcb
->OwnerPsp
== 0)
38 CurrentMcb
->Size
+= NextMcb
->Size
+ 1;
39 CurrentMcb
->BlockType
= NextMcb
->BlockType
;
40 NextMcb
->BlockType
= 'I';
44 /* No more adjoining free blocks */
50 static WORD
DosCopyEnvironmentBlock(WORD SourceSegment
)
52 PCHAR Ptr
, SourceBuffer
, DestBuffer
= NULL
;
56 Ptr
= SourceBuffer
= (PCHAR
)((ULONG_PTR
)BaseAddress
+ TO_LINEAR(SourceSegment
, 0));
58 /* Calculate the size of the environment block */
61 TotalSize
+= strlen(Ptr
) + 1;
62 Ptr
+= strlen(Ptr
) + 1;
66 /* Allocate the memory for the environment block */
67 DestSegment
= DosAllocateMemory((TotalSize
+ 0x0F) >> 4, NULL
);
68 if (!DestSegment
) return 0;
72 DestBuffer
= (PCHAR
)((ULONG_PTR
)BaseAddress
+ TO_LINEAR(DestSegment
, 0));
76 strcpy(DestBuffer
, Ptr
);
78 /* Advance to the next string */
79 Ptr
+= strlen(Ptr
) + 1;
80 DestBuffer
+= strlen(Ptr
) + 1;
83 /* Set the final zero */
89 static VOID
DosChangeMemoryOwner(WORD Segment
, WORD NewOwner
)
91 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
- 1);
93 /* Just set the owner */
94 Mcb
->OwnerPsp
= NewOwner
;
97 /* PUBLIC FUNCTIONS ***********************************************************/
99 WORD
DosAllocateMemory(WORD Size
, WORD
*MaxAvailable
)
101 WORD Result
= 0, Segment
= FIRST_MCB_SEGMENT
, MaxSize
= 0;
102 PDOS_MCB CurrentMcb
, NextMcb
;
106 /* Get a pointer to the MCB */
107 CurrentMcb
= SEGMENT_TO_MCB(Segment
);
109 /* Make sure it's valid */
110 if (CurrentMcb
->BlockType
!= 'M' && CurrentMcb
->BlockType
!= 'Z')
115 /* Only check free blocks */
116 if (CurrentMcb
->OwnerPsp
!= 0) goto Next
;
118 /* Combine this free block with adjoining free blocks */
119 DosCombineFreeBlocks(Segment
);
121 /* Update the maximum block size */
122 if (CurrentMcb
->Size
> MaxSize
) MaxSize
= CurrentMcb
->Size
;
124 /* Check if this block is big enough */
125 if (CurrentMcb
->Size
< Size
) goto Next
;
127 /* It is, update the smallest found so far */
128 if ((Result
== 0) || (CurrentMcb
->Size
< SEGMENT_TO_MCB(Result
)->Size
))
134 /* If this was the last MCB in the chain, quit */
135 if (CurrentMcb
->BlockType
== 'Z') break;
137 /* Otherwise, update the segment and continue */
138 Segment
+= CurrentMcb
->Size
+ 1;
141 /* If we didn't find a free block, return 0 */
144 if (MaxAvailable
) *MaxAvailable
= MaxSize
;
148 /* Get a pointer to the MCB */
149 CurrentMcb
= SEGMENT_TO_MCB(Result
);
151 /* Check if the block is larger than requested */
152 if (CurrentMcb
->Size
> Size
)
154 /* It is, split it into two blocks */
155 NextMcb
= SEGMENT_TO_MCB(Result
+ Size
+ 1);
157 /* Initialize the new MCB structure */
158 NextMcb
->BlockType
= CurrentMcb
->BlockType
;
159 NextMcb
->Size
= CurrentMcb
->Size
- Size
- 1;
160 NextMcb
->OwnerPsp
= 0;
162 /* Update the current block */
163 CurrentMcb
->BlockType
= 'M';
164 CurrentMcb
->Size
= Size
;
167 /* Take ownership of the block */
168 CurrentMcb
->OwnerPsp
= CurrentPsp
;
170 /* Return the segment of the data portion of the block */
174 BOOLEAN
DosResizeMemory(WORD BlockData
, WORD NewSize
, WORD
*MaxAvailable
)
176 BOOLEAN Success
= TRUE
;
177 WORD Segment
= BlockData
- 1, ReturnSize
= 0, NextSegment
;
178 PDOS_MCB Mcb
= SEGMENT_TO_MCB(Segment
), NextMcb
;
180 /* Make sure this is a valid, allocated block */
181 if ((Mcb
->BlockType
!= 'M' && Mcb
->BlockType
!= 'Z') || Mcb
->OwnerPsp
== 0)
187 ReturnSize
= Mcb
->Size
;
189 /* Check if we need to expand or contract the block */
190 if (NewSize
> Mcb
->Size
)
192 /* We can't expand the last block */
193 if (Mcb
->BlockType
!= 'M')
199 /* Get the pointer and segment of the next MCB */
200 NextSegment
= Segment
+ Mcb
->Size
+ 1;
201 NextMcb
= SEGMENT_TO_MCB(NextSegment
);
203 /* Make sure the next segment is free */
204 if (NextMcb
->OwnerPsp
!= 0)
210 /* Combine this free block with adjoining free blocks */
211 DosCombineFreeBlocks(NextSegment
);
213 /* Set the maximum possible size of the block */
214 ReturnSize
+= NextMcb
->Size
+ 1;
216 /* Maximize the current block */
217 Mcb
->Size
= ReturnSize
;
218 Mcb
->BlockType
= NextMcb
->BlockType
;
220 /* Invalidate the next block */
221 NextMcb
->BlockType
= 'I';
223 /* Check if the block is larger than requested */
224 if (Mcb
->Size
> NewSize
)
226 /* It is, split it into two blocks */
227 NextMcb
= SEGMENT_TO_MCB(Segment
+ NewSize
+ 1);
229 /* Initialize the new MCB structure */
230 NextMcb
->BlockType
= Mcb
->BlockType
;
231 NextMcb
->Size
= Mcb
->Size
- NewSize
- 1;
232 NextMcb
->OwnerPsp
= 0;
234 /* Update the current block */
235 Mcb
->BlockType
= 'M';
239 else if (NewSize
< Mcb
->Size
)
241 /* Just split the block */
242 NextMcb
= SEGMENT_TO_MCB(Segment
+ NewSize
+ 1);
243 NextMcb
->BlockType
= Mcb
->BlockType
;
244 NextMcb
->Size
= Mcb
->Size
- NewSize
- 1;
245 NextMcb
->OwnerPsp
= 0;
248 Mcb
->BlockType
= 'M';
253 /* Check if the operation failed */
256 /* Return the maximum possible size */
257 if (MaxAvailable
) *MaxAvailable
= ReturnSize
;
263 BOOLEAN
DosFreeMemory(WORD BlockData
)
265 PDOS_MCB Mcb
= SEGMENT_TO_MCB(BlockData
- 1);
267 /* Make sure the MCB is valid */
268 if (Mcb
->BlockType
!= 'M' && Mcb
->BlockType
!= 'Z') return FALSE
;
270 /* Mark the block as free */
276 WORD
DosCreateFile(LPCSTR FilePath
)
278 // TODO: NOT IMPLEMENTED
282 WORD
DosOpenFile(LPCSTR FilePath
)
284 // TODO: NOT IMPLEMENTED
288 VOID
DosInitializePsp(WORD PspSegment
, LPCSTR CommandLine
, WORD ProgramSize
, WORD Environment
)
291 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(PspSegment
);
292 LPDWORD IntVecTable
= (LPDWORD
)((ULONG_PTR
)BaseAddress
);
294 ZeroMemory(PspBlock
, sizeof(DOS_PSP
));
296 /* Set the exit interrupt */
297 PspBlock
->Exit
[0] = 0xCD; // int 0x20
298 PspBlock
->Exit
[1] = 0x20;
300 /* Set the program size */
301 PspBlock
->MemSize
= ProgramSize
;
303 /* Save the interrupt vectors */
304 PspBlock
->TerminateAddress
= IntVecTable
[0x22];
305 PspBlock
->BreakAddress
= IntVecTable
[0x23];
306 PspBlock
->CriticalAddress
= IntVecTable
[0x24];
308 /* Set the parent PSP */
309 PspBlock
->ParentPsp
= CurrentPsp
;
311 /* Initialize the handle table */
312 for (i
= 0; i
< 20; i
++) PspBlock
->HandleTable
[i
] = 0xFF;
314 /* Did we get an environment segment? */
317 /* No, copy the one from the parent */
318 Environment
= DosCopyEnvironmentBlock((CurrentPsp
!= SYSTEM_PSP
)
319 ? SEGMENT_TO_PSP(CurrentPsp
)->EnvBlock
323 PspBlock
->EnvBlock
= Environment
;
325 /* Set the handle table pointers to the internal handle table */
326 PspBlock
->HandleTableSize
= 20;
327 PspBlock
->HandleTablePtr
= MAKELONG(0x18, PspSegment
);
329 /* Set the DOS version */
330 PspBlock
->DosVersion
= DOS_VERSION
;
332 /* Set the far call opcodes */
333 PspBlock
->FarCall
[0] = 0xCD; // int 0x21
334 PspBlock
->FarCall
[1] = 0x21;
335 PspBlock
->FarCall
[2] = 0xCB; // retf
337 /* Set the command line */
338 PspBlock
->CommandLineSize
= strlen(CommandLine
);
339 RtlCopyMemory(PspBlock
->CommandLine
, CommandLine
, PspBlock
->CommandLineSize
);
340 PspBlock
->CommandLine
[PspBlock
->CommandLineSize
] = '\r';
343 BOOLEAN
DosCreateProcess(LPCSTR CommandLine
, WORD EnvBlock
)
345 BOOLEAN Success
= FALSE
;
346 HANDLE FileHandle
= INVALID_HANDLE_VALUE
, FileMapping
= NULL
;
347 LPBYTE Address
= NULL
;
348 LPSTR ProgramFilePath
, Parameters
[128];
349 CHAR CommandLineCopy
[128];
351 WORD i
, Segment
, FileSize
, ExeSize
;
352 PIMAGE_DOS_HEADER Header
;
353 PDWORD RelocationTable
;
356 /* Save a copy of the command line */
357 strcpy(CommandLineCopy
, CommandLine
);
359 /* Get the file name of the executable */
360 ProgramFilePath
= strtok(CommandLineCopy
, " \t");
362 /* Load the parameters in the local array */
363 while ((ParamCount
< 256)
364 && ((Parameters
[ParamCount
] = strtok(NULL
, " \t")) != NULL
))
369 /* Open a handle to the executable */
370 FileHandle
= CreateFileA(ProgramFilePath
,
375 FILE_ATTRIBUTE_NORMAL
,
377 if (FileHandle
== INVALID_HANDLE_VALUE
) goto Cleanup
;
379 /* Get the file size */
380 FileSize
= GetFileSize(FileHandle
, NULL
);
382 /* Create a mapping object for the file */
383 FileMapping
= CreateFileMapping(FileHandle
,
389 if (FileMapping
== NULL
) goto Cleanup
;
391 /* Map the file into memory */
392 Address
= (LPBYTE
)MapViewOfFile(FileMapping
, FILE_MAP_READ
, 0, 0, 0);
393 if (Address
== NULL
) goto Cleanup
;
395 /* Check if this is an EXE file or a COM file */
396 if (Address
[0] == 'M' && Address
[1] == 'Z')
400 /* Get the MZ header */
401 Header
= (PIMAGE_DOS_HEADER
)Address
;
403 // TODO: Verify checksum and executable!
405 /* Get the base size of the file, in paragraphs (rounded up) */
406 ExeSize
= (((Header
->e_cp
- 1) << 8) + Header
->e_cblp
+ 0x0F) >> 4;
408 /* Loop from the maximum to the minimum number of extra paragraphs */
409 for (i
= Header
->e_maxalloc
; i
>= Header
->e_minalloc
; i
--)
411 /* Try to allocate that much memory */
412 Segment
= DosAllocateMemory(ExeSize
+ (sizeof(DOS_PSP
) >> 4) + i
, NULL
);
413 if (Segment
!= 0) break;
416 /* Check if at least the lowest allocation was successful */
417 if (Segment
== 0) goto Cleanup
;
419 /* Initialize the PSP */
420 DosInitializePsp(Segment
,
421 CommandLine
, ExeSize
+ (sizeof(DOS_PSP
) >> 4) + i
,
424 /* The process owns its own memory */
425 DosChangeMemoryOwner(Segment
, Segment
);
427 /* Copy the program to Segment:0100 */
428 RtlCopyMemory((PVOID
)((ULONG_PTR
)BaseAddress
429 + TO_LINEAR(Segment
, 0x100)),
430 Address
+ (Header
->e_cparhdr
<< 4),
431 FileSize
- (Header
->e_cparhdr
<< 4));
433 /* Get the relocation table */
434 RelocationTable
= (PDWORD
)(Address
+ Header
->e_lfarlc
);
436 /* Perform relocations */
437 for (i
= 0; i
< Header
->e_crlc
; i
++)
439 /* Get a pointer to the word that needs to be patched */
440 RelocWord
= (PWORD
)((ULONG_PTR
)BaseAddress
441 + TO_LINEAR(Segment
+ HIWORD(RelocationTable
[i
]),
442 0x100 + LOWORD(RelocationTable
[i
])));
444 /* Add the number of the EXE segment to it */
445 *RelocWord
+= Segment
+ (sizeof(DOS_PSP
) >> 4);
448 /* Set the initial segment registers */
449 EmulatorSetRegister(EMULATOR_REG_DS
, Segment
);
450 EmulatorSetRegister(EMULATOR_REG_ES
, Segment
);
452 /* Set the stack to the location from the header */
453 EmulatorSetStack(Segment
+ (sizeof(DOS_PSP
) >> 4) + Header
->e_ss
,
457 CurrentPsp
= Segment
;
458 DiskTransferArea
= MAKELONG(0x80, Segment
);
459 EmulatorExecute(Segment
+ Header
->e_cs
, sizeof(DOS_PSP
) + Header
->e_ip
);
467 /* Allocate memory for the whole program and the PSP */
468 Segment
= DosAllocateMemory((FileSize
+ sizeof(DOS_PSP
)) >> 4, NULL
);
469 if (Segment
== 0) goto Cleanup
;
471 /* Copy the program to Segment:0100 */
472 RtlCopyMemory((PVOID
)((ULONG_PTR
)BaseAddress
473 + TO_LINEAR(Segment
, 0x100)),
477 /* Initialize the PSP */
478 DosInitializePsp(Segment
,
480 (FileSize
+ sizeof(DOS_PSP
)) >> 4,
483 /* Set the initial segment registers */
484 EmulatorSetRegister(EMULATOR_REG_DS
, Segment
);
485 EmulatorSetRegister(EMULATOR_REG_ES
, Segment
);
487 /* Set the stack to the last word of the segment */
488 EmulatorSetStack(Segment
, 0xFFFE);
491 CurrentPsp
= Segment
;
492 DiskTransferArea
= MAKELONG(0x80, Segment
);
493 EmulatorExecute(Segment
, 0x100);
500 if (Address
!= NULL
) UnmapViewOfFile(Address
);
502 /* Close the file mapping object */
503 if (FileMapping
!= NULL
) CloseHandle(FileMapping
);
505 /* Close the file handle */
506 if (FileHandle
!= INVALID_HANDLE_VALUE
) CloseHandle(FileHandle
);
511 VOID
DosTerminateProcess(WORD Psp
, BYTE ReturnCode
)
513 WORD McbSegment
= FIRST_MCB_SEGMENT
;
515 LPDWORD IntVecTable
= (LPDWORD
)((ULONG_PTR
)BaseAddress
);
516 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(Psp
);
518 /* Check if this PSP is it's own parent */
519 if (PspBlock
->ParentPsp
== Psp
) goto Done
;
521 // TODO: Close all handles opened by the process
523 /* Free the memory used by the process */
526 /* Get a pointer to the MCB */
527 CurrentMcb
= SEGMENT_TO_MCB(McbSegment
);
529 /* Make sure the MCB is valid */
530 if (CurrentMcb
->BlockType
!= 'M' && CurrentMcb
->BlockType
!='Z') break;
532 /* If this block was allocated by the process, free it */
533 if (CurrentMcb
->OwnerPsp
== Psp
) DosFreeMemory(McbSegment
);
535 /* If this was the last block, quit */
536 if (CurrentMcb
->BlockType
== 'Z') break;
538 /* Update the segment and continue */
539 McbSegment
+= CurrentMcb
->Size
+ 1;
543 /* Restore the interrupt vectors */
544 IntVecTable
[0x22] = PspBlock
->TerminateAddress
;
545 IntVecTable
[0x23] = PspBlock
->BreakAddress
;
546 IntVecTable
[0x24] = PspBlock
->CriticalAddress
;
548 /* Update the current PSP */
549 if (Psp
== CurrentPsp
)
551 CurrentPsp
= PspBlock
->ParentPsp
;
552 if (CurrentPsp
== SYSTEM_PSP
) VdmRunning
= FALSE
;
555 /* Return control to the parent process */
556 EmulatorExecute(HIWORD(PspBlock
->TerminateAddress
),
557 LOWORD(PspBlock
->TerminateAddress
));
560 CHAR
DosReadCharacter()
562 // TODO: STDIN can be redirected under DOS 2.0+
565 /* A zero value for the character indicates a special key */
566 do Character
= BiosGetCharacter();
572 VOID
DosPrintCharacter(CHAR Character
)
574 // TODO: STDOUT can be redirected under DOS 2.0+
575 if (Character
== '\r') Character
= '\n';
579 VOID
DosInt20h(WORD CodeSegment
)
581 /* This is the exit interrupt */
582 DosTerminateProcess(CodeSegment
, 0);
585 VOID
DosInt21h(WORD CodeSegment
)
589 SYSTEMTIME SystemTime
;
591 PDOS_INPUT_BUFFER InputBuffer
;
592 DWORD Eax
= EmulatorGetRegister(EMULATOR_REG_AX
);
593 DWORD Ecx
= EmulatorGetRegister(EMULATOR_REG_CX
);
594 DWORD Edx
= EmulatorGetRegister(EMULATOR_REG_DX
);
595 DWORD Ebx
= EmulatorGetRegister(EMULATOR_REG_BX
);
596 WORD DataSegment
= EmulatorGetRegister(EMULATOR_REG_DS
);
597 WORD ExtSegment
= EmulatorGetRegister(EMULATOR_REG_ES
);
599 /* Check the value in the AH register */
602 /* Terminate Program */
605 DosTerminateProcess(CodeSegment
, 0);
609 /* Read Character And Echo */
612 Character
= DosReadCharacter();
613 DosPrintCharacter(Character
);
614 EmulatorSetRegister(EMULATOR_REG_AX
, (Eax
& 0xFFFFFF00) | Character
);
618 /* Print Character */
621 DosPrintCharacter(LOBYTE(Edx
));
625 /* Read Character Without Echo */
628 EmulatorSetRegister(EMULATOR_REG_AX
,
629 (Eax
& 0xFFFFFF00) | DosReadCharacter());
636 String
= (PCHAR
)((ULONG_PTR
)BaseAddress
637 + TO_LINEAR(DataSegment
, LOWORD(Edx
)));
639 while ((*String
) != '$')
641 DosPrintCharacter(*String
);
648 /* Read Buffered Input */
651 InputBuffer
= (PDOS_INPUT_BUFFER
)((ULONG_PTR
)BaseAddress
652 + TO_LINEAR(DataSegment
,
655 InputBuffer
->Length
= 0;
656 for (i
= 0; i
< InputBuffer
->MaxLength
; i
++)
658 Character
= DosReadCharacter();
659 DosPrintCharacter(Character
);
660 InputBuffer
->Buffer
[InputBuffer
->Length
] = Character
;
661 if (Character
== '\r') break;
662 InputBuffer
->Length
++;
668 /* Set Disk Transfer Area */
671 DiskTransferArea
= MAKELONG(LOWORD(Edx
), DataSegment
);
675 /* Set Interrupt Vector */
678 DWORD FarPointer
= MAKELONG(LOWORD(Edx
), DataSegment
);
680 /* Write the new far pointer to the IDT */
681 ((PDWORD
)BaseAddress
)[LOBYTE(Eax
)] = FarPointer
;
686 /* Get system date */
689 GetLocalTime(&SystemTime
);
690 EmulatorSetRegister(EMULATOR_REG_CX
,
691 (Ecx
& 0xFFFF0000) | SystemTime
.wYear
);
692 EmulatorSetRegister(EMULATOR_REG_DX
,
694 | (SystemTime
.wMonth
<< 8)
696 EmulatorSetRegister(EMULATOR_REG_AX
,
697 (Eax
& 0xFFFFFF00) | SystemTime
.wDayOfWeek
);
701 /* Set system date */
704 GetLocalTime(&SystemTime
);
705 SystemTime
.wYear
= LOWORD(Ecx
);
706 SystemTime
.wMonth
= HIBYTE(Edx
);
707 SystemTime
.wDay
= LOBYTE(Edx
);
709 if (SetLocalTime(&SystemTime
))
712 EmulatorSetRegister(EMULATOR_REG_AX
, Eax
& 0xFFFFFF00);
717 EmulatorSetRegister(EMULATOR_REG_AX
, Eax
| 0xFF);
723 /* Get system time */
726 GetLocalTime(&SystemTime
);
727 EmulatorSetRegister(EMULATOR_REG_CX
,
729 | (SystemTime
.wHour
<< 8)
730 | SystemTime
.wMinute
);
731 EmulatorSetRegister(EMULATOR_REG_DX
,
733 | (SystemTime
.wSecond
<< 8)
734 | (SystemTime
.wMilliseconds
/ 10));
738 /* Set system time */
741 GetLocalTime(&SystemTime
);
742 SystemTime
.wHour
= HIBYTE(Ecx
);
743 SystemTime
.wMinute
= LOBYTE(Ecx
);
744 SystemTime
.wSecond
= HIBYTE(Edx
);
745 SystemTime
.wMilliseconds
= LOBYTE(Edx
) * 10;
747 if (SetLocalTime(&SystemTime
))
750 EmulatorSetRegister(EMULATOR_REG_AX
, Eax
& 0xFFFFFF00);
755 EmulatorSetRegister(EMULATOR_REG_AX
, Eax
| 0xFF);
761 /* Get Disk Transfer Area */
764 EmulatorSetRegister(EMULATOR_REG_ES
, HIWORD(DiskTransferArea
));
765 EmulatorSetRegister(EMULATOR_REG_BX
, LOWORD(DiskTransferArea
));
770 /* Get DOS Version */
773 PDOS_PSP PspBlock
= SEGMENT_TO_PSP(CurrentPsp
);
775 EmulatorSetRegister(EMULATOR_REG_AX
, PspBlock
->DosVersion
);
779 /* Get Interrupt Vector */
782 DWORD FarPointer
= ((PDWORD
)BaseAddress
)[LOBYTE(Eax
)];
784 /* Read the address from the IDT into ES:BX */
785 EmulatorSetRegister(EMULATOR_REG_ES
, HIWORD(FarPointer
));
786 EmulatorSetRegister(EMULATOR_REG_BX
, LOWORD(FarPointer
));
791 /* Create Directory */
794 String
= (PCHAR
)((ULONG_PTR
)BaseAddress
795 + TO_LINEAR(DataSegment
, LOWORD(Edx
)));
797 if (CreateDirectoryA(String
, NULL
))
799 EmulatorClearFlag(EMULATOR_FLAG_CF
);
803 EmulatorSetFlag(EMULATOR_FLAG_CF
);
804 EmulatorSetRegister(EMULATOR_REG_AX
,
805 (Eax
& 0xFFFF0000) | LOWORD(GetLastError()));
811 /* Remove Directory */
814 String
= (PCHAR
)((ULONG_PTR
)BaseAddress
815 + TO_LINEAR(DataSegment
, LOWORD(Edx
)));
817 if (RemoveDirectoryA(String
))
819 EmulatorClearFlag(EMULATOR_FLAG_CF
);
823 EmulatorSetFlag(EMULATOR_FLAG_CF
);
824 EmulatorSetRegister(EMULATOR_REG_AX
,
825 (Eax
& 0xFFFF0000) | LOWORD(GetLastError()));
832 /* Set Current Directory */
835 String
= (PCHAR
)((ULONG_PTR
)BaseAddress
836 + TO_LINEAR(DataSegment
, LOWORD(Edx
)));
838 if (SetCurrentDirectoryA(String
))
840 EmulatorClearFlag(EMULATOR_FLAG_CF
);
844 EmulatorSetFlag(EMULATOR_FLAG_CF
);
845 EmulatorSetRegister(EMULATOR_REG_AX
,
846 (Eax
& 0xFFFF0000) | LOWORD(GetLastError()));
852 /* Allocate Memory */
855 WORD MaxAvailable
= 0;
856 WORD Segment
= DosAllocateMemory(LOWORD(Ebx
), &MaxAvailable
);
860 EmulatorSetRegister(EMULATOR_REG_AX
, Segment
);
861 EmulatorSetRegister(EMULATOR_REG_BX
, MaxAvailable
);
862 EmulatorClearFlag(EMULATOR_FLAG_CF
);
864 else EmulatorSetFlag(EMULATOR_FLAG_CF
);
872 if (DosFreeMemory(ExtSegment
))
874 EmulatorClearFlag(EMULATOR_FLAG_CF
);
876 else EmulatorSetFlag(EMULATOR_FLAG_CF
);
881 /* Resize Memory Block */
886 if (DosResizeMemory(ExtSegment
, LOWORD(Ebx
), &Size
))
888 EmulatorClearFlag(EMULATOR_FLAG_CF
);
892 EmulatorSetFlag(EMULATOR_FLAG_CF
);
893 EmulatorSetRegister(EMULATOR_REG_BX
, Size
);
899 /* Terminate With Return Code */
902 DosTerminateProcess(CurrentPsp
, LOBYTE(Eax
));
909 DPRINT1("DOS Function INT 0x21, AH = 0x%02X NOT IMPLEMENTED!\n", HIBYTE(Eax
));
910 EmulatorSetFlag(EMULATOR_FLAG_CF
);
915 VOID
DosBreakInterrupt()
920 BOOLEAN
DosInitialize()
922 PDOS_MCB Mcb
= SEGMENT_TO_MCB(FIRST_MCB_SEGMENT
);
925 LPWSTR SourcePtr
, Environment
;
927 LPSTR DestPtr
= (LPSTR
)((ULONG_PTR
)BaseAddress
+ TO_LINEAR(SYSTEM_ENV_BLOCK
, 0));
930 /* Initialize the MCB */
931 Mcb
->BlockType
= 'Z';
932 Mcb
->Size
= (WORD
)USER_MEMORY_SIZE
;
935 /* Get the environment strings */
936 SourcePtr
= Environment
= GetEnvironmentStringsW();
937 if (Environment
== NULL
) return FALSE
;
939 /* Fill the DOS system environment block */
942 /* Get the size of the ASCII string */
943 AsciiSize
= WideCharToMultiByte(CP_ACP
,
952 /* Allocate memory for the ASCII string */
953 AsciiString
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, AsciiSize
);
954 if (AsciiString
== NULL
)
956 FreeEnvironmentStringsW(Environment
);
960 /* Convert to ASCII */
961 WideCharToMultiByte(CP_ACP
,
970 /* Copy the string into DOS memory */
971 strcpy(DestPtr
, AsciiString
);
973 /* Free the memory */
974 HeapFree(GetProcessHeap(), 0, AsciiString
);
976 /* Move to the next string */
977 SourcePtr
+= wcslen(SourcePtr
) + 1;
978 DestPtr
+= strlen(AsciiString
) + 1;
981 /* Free the memory allocated for environment strings */
982 FreeEnvironmentStringsW(Environment
);
984 /* Read CONFIG.SYS */
985 Stream
= _wfopen(DOS_CONFIG_PATH
, L
"r");
988 while (fgetws(Buffer
, 256, Stream
))
990 // TODO: Parse the line