-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix CfgCpu messing up graph on external interruptions. Make interrupt vector table a memory based array. #855
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Missed 'readonly' opportunity Note