diff --git a/src/Spice86.Core/Emulator/InterruptHandlers/Dos/DosInt21Handler.cs b/src/Spice86.Core/Emulator/InterruptHandlers/Dos/DosInt21Handler.cs index 50c0aaff9a..38151a34ad 100644 --- a/src/Spice86.Core/Emulator/InterruptHandlers/Dos/DosInt21Handler.cs +++ b/src/Spice86.Core/Emulator/InterruptHandlers/Dos/DosInt21Handler.cs @@ -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; @@ -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; 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)); } /// diff --git a/src/Spice86.Core/Emulator/OperatingSystem/DosProcessManager.cs b/src/Spice86.Core/Emulator/OperatingSystem/DosProcessManager.cs index b421dfcce5..e006e3d30a 100644 --- a/src/Spice86.Core/Emulator/OperatingSystem/DosProcessManager.cs +++ b/src/Spice86.Core/Emulator/OperatingSystem/DosProcessManager.cs @@ -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); @@ -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); @@ -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; @@ -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); @@ -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();