Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Spice86.Core.Emulator.InterruptHandlers.Dos;
using Spice86.Core.Emulator.InterruptHandlers.Common.MemoryWriter;
using Spice86.Core.Emulator.InterruptHandlers.Input.Keyboard;
using Spice86.Core.Emulator.Memory;
using Spice86.Core.Emulator.Memory.Mmu;
using Spice86.Core.Emulator.OperatingSystem;
using Spice86.Core.Emulator.OperatingSystem.Devices;
using Spice86.Core.Emulator.OperatingSystem.Enums;
Expand Down Expand Up @@ -2372,8 +2373,10 @@ public void RecordCallerStack(ushort ss, ushort callerSp) {
if (pspSegment == 0) {
return;
}
Memory.UInt16[ss, (ushort)(callerSp - Stack.InterruptFrameSize - 4), SegmentAccessKind.Stack] = State.DS;
Memory.UInt16[ss, (ushort)(callerSp - Stack.InterruptFrameSize - 2), SegmentAccessKind.Stack] = State.ES;
Comment on lines +2376 to +2377
DosProgramSegmentPrefix psp = new(Memory, MemoryUtils.ToPhysicalAddress(pspSegment, 0));
psp.StackPointer = new SegmentedAddress(ss, (ushort)(callerSp - 6));
psp.StackPointer = new SegmentedAddress(ss, (ushort)(callerSp - Stack.InterruptFrameSize));
}

/// <summary>
Expand Down
25 changes: 18 additions & 7 deletions src/Spice86.Core/Emulator/OperatingSystem/DosProcessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,8 +516,7 @@ private DosExecResult HandleExeFileLoading(DosExecParameterBlock paramBlock,
DosProgramSegmentPrefix exePsp = new(_memory, MemoryUtils.ToPhysicalAddress(block.DataBlockSegment, 0));
// FreeDOS: parent PSP's ps_stack field stores the parent's current iregs frame (only for LoadAndExecute).
if (loadType == DosExecLoadType.LoadAndExecute) {
DosProgramSegmentPrefix parentPsp = new(_memory, MemoryUtils.ToPhysicalAddress(parentPspSegment, 0));
parentPsp.StackPointer = new SegmentedAddress(parentSS, parentReservedSP);
SaveParentStack(parentPspSegment, parentSS, parentReservedSP);
}
CopyFcbFromPointer(paramBlock.FirstFcbPointer, exePsp.FirstFileControlBlock);
CopyFcbFromPointer(paramBlock.SecondFcbPointer, exePsp.SecondFileControlBlock);
Expand Down Expand Up @@ -582,8 +581,7 @@ private DosExecResult HandleComFileLoading(DosExecParameterBlock paramBlock,
DosProgramSegmentPrefix comPsp = new(_memory, MemoryUtils.ToPhysicalAddress(comBlock.DataBlockSegment, 0));
// FreeDOS: parent PSP's ps_stack field stores the parent's current iregs frame (only for LoadAndExecute).
if (loadType == DosExecLoadType.LoadAndExecute) {
DosProgramSegmentPrefix parentPspForCom = new(_memory, MemoryUtils.ToPhysicalAddress(parentPspSegment, 0));
parentPspForCom.StackPointer = new SegmentedAddress(parentSS, parentReservedSP);
SaveParentStack(parentPspSegment, parentSS, parentReservedSP);
}
CopyFcbFromPointer(paramBlock.FirstFcbPointer, comPsp.FirstFileControlBlock);
CopyFcbFromPointer(paramBlock.SecondFcbPointer, comPsp.SecondFileControlBlock);
Expand Down Expand Up @@ -658,6 +656,15 @@ private void RestoreParentPspForLoadOnly(DosExecLoadType loadType, ushort parent
}
}

private void SaveParentStack(ushort parentPspSegment, ushort parentSS, ushort parentReservedSP) {
if (parentPspSegment != CommandComSegment) {
_stack.Poke16(-4, _state.DS);
_stack.Poke16(-2, _state.ES);
}
DosProgramSegmentPrefix parentPsp = new(_memory, MemoryUtils.ToPhysicalAddress(parentPspSegment, 0));
parentPsp.StackPointer = new SegmentedAddress(parentSS, parentReservedSP);
}

private static void FillEPBForLoadOnlyMode(DosExecParameterBlock paramBlock, DosExecLoadType loadType, DosExecResult execResult) {
if (loadType == DosExecLoadType.LoadOnly) {
paramBlock.InitialCS = execResult.InitialCS;
Expand Down Expand Up @@ -774,6 +781,13 @@ public void TerminateProcess(byte exitCode, DosTerminationType terminationType)
SegmentedAddress parentStackPtr = parentPsp.StackPointer;
_state.SS = parentStackPtr.Segment;
_state.SP = parentStackPtr.Offset;
if (parentIsRootCommandCom) {
_state.DS = parentPspSegment;
_state.ES = parentPspSegment;
} else {
_state.DS = _stack.Peek16(-4);
_state.ES = _stack.Peek16(-2);
}
// Patch the iregs frame with child's TerminateAddress (INT 22h vector).
_stack.Poke16(0, terminateAddr.Offset);
_stack.Poke16(2, terminateAddr.Segment);
Expand All @@ -783,9 +797,6 @@ public void TerminateProcess(byte exitCode, DosTerminationType terminationType)
_state.SS, _state.SP, _stack.PeekInterruptFrameReturnAddress());
}

_state.DS = parentPspSegment;
_state.ES = parentPspSegment;

if (parentIsRootCommandCom) {
_batchExecutionEngine.RestoreStandardHandlesAfterLaunch();

Expand Down
Loading