Implement new simulator

This commit is contained in:
Stone_Red
2026-03-26 20:44:40 +01:00
parent 057802c43d
commit 4db32460a8
14 changed files with 200 additions and 230 deletions
+10 -110
View File
@@ -1,25 +1,20 @@
using Microsoft.Xna.Framework.Graphics;
using StoneRed.LogicSimulator.Api.Utilities;
using StoneRed.LogicSimulator.Simulation;
namespace StoneRed.LogicSimulator.Api;
#pragma warning disable S112 // General exceptions should never be thrown
internal abstract class LogicGate
{
private readonly List<LogicGateConnection> logicGateConnections = new();
private bool logicGateConnectionsChnaged = false;
private int currentInput;
private int newInput;
private int output;
private int cachedOutput;
private LogicGateConnection[] logicGateConnectionsArray = Array.Empty<LogicGateConnection>();
private readonly List<LogicGateConnection> logicGateConnections = [];
public abstract int OutputCount { get; set; }
public abstract int InputCount { get; set; }
internal IReadOnlyList<LogicGateConnection> LogicGateConnections => logicGateConnections.AsReadOnly();
internal GraphicsDevice? GraphicsDevice { get; set; }
internal LogicGateWorldData WorldData { get; init; } = new LogicGateWorldData();
internal ulong Id { get; set; }
internal int SimulatorGateId { get; set; } = -1;
public override bool Equals(object? obj)
{
@@ -32,44 +27,14 @@ internal abstract class LogicGate
return Id.GetHashCode();
}
internal void NextTick()
{
currentInput = newInput;
newInput = 0;
output = 0;
}
internal void SetInput(int value, int index)
{
if (index >= InputCount)
{
throw new IndexOutOfRangeException();
}
// Inputs should act as or gates
if (value == 1)
{
newInput.SetBit(1, index);
}
}
internal void Connect(LogicGate logicGate, int inputIndex, int outputIndex)
{
if (inputIndex >= logicGate.InputCount)
{
throw new IndexOutOfRangeException();
}
if (outputIndex >= OutputCount)
{
throw new IndexOutOfRangeException();
}
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(inputIndex, logicGate.InputCount);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(outputIndex, OutputCount);
lock (logicGateConnections)
{
logicGateConnections.Add(new LogicGateConnection(logicGate, inputIndex, outputIndex));
logicGateConnectionsChnaged = true;
}
}
@@ -93,89 +58,24 @@ internal abstract class LogicGate
(!inputIndex.HasValue || c.InputIndex == inputIndex) &&
(!outputIndex.HasValue || c.OutputIndex == outputIndex));
logicGateConnections.RemoveAt(index);
logicGateConnectionsChnaged = true;
}
}
internal void Update()
{
Execute();
cachedOutput = output;
PublishOutput();
}
internal int GetCachedOutputBit(int index)
{
if (index < 0 || index >= OutputCount)
{
throw new IndexOutOfRangeException();
}
return cachedOutput.GetBit(index);
}
protected internal virtual void Initialize()
{ }
protected abstract void Execute();
protected int GetInputBit(int index)
{
if (index < 0 || index >= InputCount)
{
throw new IndexOutOfRangeException();
}
return currentInput.GetBit(index);
}
protected void SetOutputBit(int value, int index)
{
if (index < 0 || index >= OutputCount)
{
throw new IndexOutOfRangeException();
}
output.SetBit(value, index);
}
protected internal abstract void Register(ICircuitSimulator circuitSimulator);
protected Texture2D CreateTexture(int width, int height)
{
return new Texture2D(GraphicsDevice, width, height);
}
private void PublishOutput()
{
if (logicGateConnectionsChnaged)
{
lock (logicGateConnections)
{
logicGateConnectionsArray = logicGateConnections.ToArray();
logicGateConnectionsChnaged = false;
}
}
for (int i = 0; i < logicGateConnectionsArray.Length; i++)
{
LogicGateConnection connection = logicGateConnectionsArray[i];
connection.LogicGate.SetInput(output.GetBit(connection.OutputIndex), connection.InputIndex);
}
}
}
#pragma warning restore S112 // General exceptions should never be thrown
internal class LogicGateConnection
internal class LogicGateConnection(LogicGate logicGate, int inputIndex, int outputIndex)
{
public LogicGate LogicGate { get; }
public LogicGate LogicGate { get; } = logicGate;
public int InputIndex { get; }
public int OutputIndex { get; }
public LogicGateConnection(LogicGate logicGate, int inputIndex, int outputIndex)
{
LogicGate = logicGate;
InputIndex = inputIndex;
OutputIndex = outputIndex;
}
public int InputIndex { get; } = inputIndex;
public int OutputIndex { get; } = outputIndex;
};
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
@@ -13,5 +13,8 @@
<PackageReference Include="MonoGame.Extended.Input" Version="3.8.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StoneRed.LogicSimulator.Simulation\StoneRed.LogicSimulator.Simulation.csproj" />
</ItemGroup>
</Project>