-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #855 from OpenRakis/fix/hw_interrupts_for_cfg_cpu
Fix CfgCpu messing up graph on external interruptions. Make interrupt vector table a memory based array.
- Loading branch information
Showing
19 changed files
with
156 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 36 additions & 18 deletions
54
src/Spice86.Core/Emulator/CPU/CfgCpu/ExecutionContextManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,61 @@ | ||
namespace Spice86.Core.Emulator.CPU.CfgCpu; | ||
|
||
using Spice86.Core.Emulator.CPU.CfgCpu.ControlFlowGraph; | ||
using Spice86.Core.Emulator.CPU.CfgCpu.Feeder; | ||
using Spice86.Core.Emulator.CPU.CfgCpu.Linker; | ||
using Spice86.Core.Emulator.CPU.CfgCpu.ParsedInstruction; | ||
using Spice86.Core.Emulator.VM; | ||
using Spice86.Core.Emulator.VM.Breakpoint; | ||
using Spice86.Shared.Emulator.Memory; | ||
|
||
public class ExecutionContextManager { | ||
public class ExecutionContextManager : InstructionReplacer { | ||
private readonly EmulatorBreakpointsManager _emulatorBreakpointsManager; | ||
private readonly Dictionary<uint, ExecutionContext> _executionContextEntryPoints = new(); | ||
|
||
public ExecutionContextManager(EmulatorBreakpointsManager emulatorBreakpointsManager) { | ||
private readonly Dictionary<SegmentedAddress, ISet<ICfgNode>> _executionContextEntryPoints = new(); | ||
private readonly CfgNodeFeeder _cfgNodeFeeder; | ||
private int _currentDepth; | ||
|
||
public ExecutionContextManager(EmulatorBreakpointsManager emulatorBreakpointsManager, CfgNodeFeeder cfgNodeFeeder, | ||
InstructionReplacerRegistry replacerRegistry) : base(replacerRegistry) { | ||
_emulatorBreakpointsManager = emulatorBreakpointsManager; | ||
_cfgNodeFeeder = cfgNodeFeeder; | ||
// Initial context at init | ||
CurrentExecutionContext = new(); | ||
InitialExecutionContext = CurrentExecutionContext; | ||
CurrentExecutionContext = new(_currentDepth); | ||
} | ||
|
||
public ExecutionContext InitialExecutionContext { get; } | ||
|
||
public ExecutionContext CurrentExecutionContext { get; private set; } | ||
|
||
public void SignalNewExecutionContext(SegmentedAddress entryAddress, SegmentedAddress? expectedReturnAddress) { | ||
uint physicalEntryAddress = entryAddress.ToPhysical(); | ||
if (!_executionContextEntryPoints.TryGetValue(physicalEntryAddress, out ExecutionContext? executionContext)) { | ||
executionContext = new ExecutionContext(); | ||
_executionContextEntryPoints.Add(entryAddress.ToPhysical(), executionContext); | ||
} | ||
// Reset the execution context so that nodes it last executed are not linked to the new ones that will come | ||
executionContext.LastExecuted = null; | ||
executionContext.NodeToExecuteNextAccordingToGraph = null; | ||
// Save current execution context | ||
ExecutionContext previousExecutionContext = CurrentExecutionContext; | ||
CurrentExecutionContext = executionContext; | ||
// Create a new one at a higher depth | ||
_currentDepth++; | ||
CurrentExecutionContext = new(_currentDepth); | ||
if (expectedReturnAddress != null) { | ||
// breakpoint that deletes itself on reach. Should be triggered when the return address is reached and before it starts execution. | ||
_emulatorBreakpointsManager.ToggleBreakPoint(new AddressBreakPoint(BreakPointType.EXECUTION, expectedReturnAddress.Value.ToPhysical(), (_) => { | ||
// Restore previous execution context | ||
// Restore previous execution context and depth | ||
CurrentExecutionContext = previousExecutionContext; | ||
_currentDepth--; | ||
}, true), true); | ||
} | ||
|
||
RegisterCurrentInstructionAsEntryPoint(entryAddress); | ||
} | ||
|
||
private void RegisterCurrentInstructionAsEntryPoint(SegmentedAddress entryAddress) { | ||
// Register a new entry point | ||
ICfgNode toExecute = _cfgNodeFeeder.GetLinkedCfgNodeToExecute(CurrentExecutionContext); | ||
if (!_executionContextEntryPoints.TryGetValue(entryAddress, out ISet<ICfgNode>? nodes)) { | ||
nodes = new HashSet<ICfgNode>(); | ||
_executionContextEntryPoints.Add(entryAddress, nodes); | ||
} | ||
nodes.Add(toExecute); | ||
} | ||
|
||
public override void ReplaceInstruction(CfgInstruction old, CfgInstruction instruction) { | ||
if (_executionContextEntryPoints.TryGetValue(instruction.Address, out ISet<ICfgNode>? entriesAtAddress) | ||
&& entriesAtAddress.Remove(old)) { | ||
entriesAtAddress.Add(instruction); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
src/Spice86.Core/Emulator/CPU/CfgCpu/Feeder/IInstructionReplacer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
namespace Spice86.Core.Emulator.CPU.CfgCpu.Feeder; | ||
|
||
using Spice86.Core.Emulator.CPU.CfgCpu.ControlFlowGraph; | ||
using Spice86.Core.Emulator.CPU.CfgCpu.ParsedInstruction; | ||
|
||
public interface IInstructionReplacer<in T> where T : ICfgNode { | ||
void ReplaceInstruction(T old, T instruction); | ||
public interface IInstructionReplacer { | ||
void ReplaceInstruction(CfgInstruction old, CfgInstruction instruction); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Spice86.Core/Emulator/CPU/CfgCpu/Feeder/InstructionReplacer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Spice86.Core.Emulator.CPU.CfgCpu.Feeder; | ||
|
||
using Spice86.Core.Emulator.CPU.CfgCpu.ParsedInstruction; | ||
|
||
public abstract class InstructionReplacer : IInstructionReplacer { | ||
protected InstructionReplacer(InstructionReplacerRegistry replacerRegistry) { | ||
replacerRegistry.Register(this); | ||
} | ||
public abstract void ReplaceInstruction(CfgInstruction old, CfgInstruction instruction); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Spice86.Core/Emulator/CPU/CfgCpu/Feeder/InstructionReplacerRegistry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Spice86.Core.Emulator.CPU.CfgCpu.Feeder; | ||
|
||
using Spice86.Core.Emulator.CPU.CfgCpu.ParsedInstruction; | ||
|
||
public class InstructionReplacerRegistry : IInstructionReplacer { | ||
private List<InstructionReplacer> _replacers = new(); | ||
|
||
public void Register(InstructionReplacer replacer) { | ||
_replacers.Add(replacer); | ||
} | ||
|
||
public void ReplaceInstruction(CfgInstruction old, CfgInstruction newInstruction) { | ||
foreach (InstructionReplacer instructionReplacer in _replacers) { | ||
instructionReplacer.ReplaceInstruction(old, newInstruction); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.