Add simulator type selection (Cycle/Event) to UI and simulation logic

This commit is contained in:
Stone_Red
2026-03-26 22:29:36 +01:00
parent 2849bc162a
commit b28f88fc81
5 changed files with 49 additions and 4 deletions
@@ -15,7 +15,12 @@
<VerticalStackPanel Id="settings">
<Label Text="Frequency:" TextColor="#000000FF" />
<SpinButton Maximum="1E+09" Minimum="1" HorizontalAlignment="Stretch" Value="100" Integer="True" Id="frequency" />
<CheckBox Text="High Performance" TextColor="#000000FF" Id="highPerformance" />
<CheckBox Text="High Performance" TextColor="#000000FF" Id="highPerformance" />
<Label Text="Simulator Type:" TextColor="#000000FF" />
<ComboBox HorizontalAlignment="Stretch" Id="simulatorType">
<ListItem Text="Cycle" />
<ListItem Text="Event" />
</ComboBox>
<CheckBox Text="Hide cables behind gates" TextColor="#000000FF" Id="hideCablesBehindGates" />
<Label Text="Native components:" Wrap="True" TextColor="#000000FF" />
<ListBox HorizontalAlignment="Stretch" Id="nativeComponents" />
@@ -26,6 +26,16 @@ internal class LogicGateSimulator
public bool HighPerformanceClock { get; set; }
public SimulatorType SimulatorType
{
get;
set
{
field = value;
logicGatesUpdated = true;
}
} = SimulatorType.Cycle;
public LogicGateSimulator(IEnumerable<LogicGate> logicGates)
{
foreach (LogicGate logicGate in logicGates)
@@ -103,7 +113,7 @@ internal class LogicGateSimulator
float sleepDelayIterations = 10000;
int sleepDelayMs = 10;
circuitSimulator = new CycleCircuitSimulator();
circuitSimulator = CreateSimulator();
Timer timeCheckTimer = new Timer(_ =>
{
@@ -132,7 +142,7 @@ internal class LogicGateSimulator
if (logicGatesUpdated)
{
circuitSimulator = new CycleCircuitSimulator();
circuitSimulator = CreateSimulator();
// Register all gates first
foreach (LogicGate gate in logicGates.Values)
@@ -173,4 +183,14 @@ internal class LogicGateSimulator
_ = timeCheckTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
private ICircuitSimulator CreateSimulator()
{
return SimulatorType switch
{
SimulatorType.Event => new EventCircuitSimulator(),
SimulatorType.Cycle => new CycleCircuitSimulator(),
_ => throw new NotImplementedException()
};
}
}
@@ -0,0 +1,7 @@
namespace StoneRed.LogicSimulator.Simulation;
public enum SimulatorType
{
Cycle,
Event
}
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<RollForward>Major</RollForward>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
@@ -36,6 +36,7 @@ internal class WorldScreen : SrlsScreen<Grid>
private Label positionLabel = null!;
private SpinButton frequencySpinButton = null!;
private CheckBox highPerformanceCheckBox = null!;
private ComboBox simulatorTypeComboBox = null!;
private CheckBox hideCablesBehindGatesCheckBox = null!;
private ListBox nativeComponentsListBox = null!;
@@ -73,6 +74,9 @@ internal class WorldScreen : SrlsScreen<Grid>
VerticalStackPanel settingsPanel = Root.FindChildById<VerticalStackPanel>("settings");
frequencySpinButton = settingsPanel.FindChildById<SpinButton>("frequency");
highPerformanceCheckBox = settingsPanel.FindChildById<CheckBox>("highPerformance");
simulatorTypeComboBox = settingsPanel.FindChildById<ComboBox>("simulatorType");
simulatorTypeComboBox.SelectedIndex = 0; // Default to Cycle
simulatorTypeComboBox.SelectedIndexChanged += SimulatorTypeComboBox_SelectedIndexChanged;
hideCablesBehindGatesCheckBox = settingsPanel.FindChildById<CheckBox>("hideCablesBehindGates");
nativeComponentsListBox = Root.FindChildById<ListBox>("nativeComponents");
@@ -328,6 +332,15 @@ internal class WorldScreen : SrlsScreen<Grid>
SelectLogicGate();
}
private void SimulatorTypeComboBox_SelectedIndexChanged(object? sender, EventArgs e)
{
simulator.SimulatorType = simulatorTypeComboBox.SelectedIndex switch
{
1 => SimulatorType.Event,
_ => SimulatorType.Cycle
};
}
private void ShowConnectionContextMenu(LogicGate logicGate, Point position, bool showInputs)
{
showInputs = logicGate.OutputCount <= 0 || (showInputs && logicGate.InputCount > 0);