mirror of
https://github.com/Stone-Red-Code/StoneRed.LogicSimulator.git
synced 2026-09-04 09:06:29 +02:00
Notify watchers only on output changes and pass previous values to callbacks
This commit is contained in:
@@ -6,6 +6,7 @@ public sealed class CycleCircuitSimulator : SimulatorBase
|
||||
{
|
||||
private int[] nextInputMasks = [];
|
||||
private Action<int[], int[], int[]> computeOutputs = (_, _, _) => { };
|
||||
private int[] previousOutputMasks = [];
|
||||
|
||||
protected override void EnsureStorage()
|
||||
{
|
||||
@@ -13,6 +14,7 @@ public sealed class CycleCircuitSimulator : SimulatorBase
|
||||
if (nextInputMasks.Length != gateKinds.Count)
|
||||
{
|
||||
nextInputMasks = new int[gateKinds.Count];
|
||||
previousOutputMasks = new int[gateKinds.Count];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,10 +22,9 @@ public sealed class CycleCircuitSimulator : SimulatorBase
|
||||
{
|
||||
base.Reset();
|
||||
Array.Clear(nextInputMasks);
|
||||
Array.Clear(previousOutputMasks);
|
||||
}
|
||||
|
||||
protected override void OnSourceChanged(int gateId) { }
|
||||
|
||||
public override void Step()
|
||||
{
|
||||
EnsureCompiled();
|
||||
@@ -32,9 +33,13 @@ public sealed class CycleCircuitSimulator : SimulatorBase
|
||||
Reset();
|
||||
}
|
||||
|
||||
(outputMasks, previousOutputMasks) = (previousOutputMasks, outputMasks);
|
||||
computeOutputs(inputMasks, outputMasks, sourceStates);
|
||||
PropagateAndSwap();
|
||||
NotifyAllWatchers();
|
||||
if (hasAnyWatchers)
|
||||
{
|
||||
NotifyAllWatchers(previousOutputMasks);
|
||||
}
|
||||
}
|
||||
|
||||
private void PropagateAndSwap()
|
||||
@@ -103,9 +108,13 @@ public sealed class CycleCircuitSimulator : SimulatorBase
|
||||
while (changed && steps < maxSteps)
|
||||
{
|
||||
steps++;
|
||||
(outputMasks, previousOutputMasks) = (previousOutputMasks, outputMasks);
|
||||
computeOutputs(inputMasks, outputMasks, sourceStates);
|
||||
changed = PropagateAndSwapDetectChange();
|
||||
NotifyAllWatchers();
|
||||
if (hasAnyWatchers)
|
||||
{
|
||||
NotifyAllWatchers(previousOutputMasks);
|
||||
}
|
||||
}
|
||||
return !changed;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user