Initial commit
This commit is contained in:
30
C#/DigiSim/Logic/Gates/AndGate.cs
Normal file
30
C#/DigiSim/Logic/Gates/AndGate.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DigiSim.Logic.Gates {
|
||||
public class AndGate : Gate {
|
||||
public AndGate(double x, double y) : base(x, y, 50, 50) { }
|
||||
protected override void InternalSetup(Canvas canvas) {
|
||||
TruthTable.Add(false, false, false);
|
||||
TruthTable.Add(false, true, false);
|
||||
TruthTable.Add(true, false, false);
|
||||
TruthTable.Add(true, true, true);
|
||||
|
||||
Shape = new Rectangle();
|
||||
SetInputs(2);
|
||||
SetOutputs(1);
|
||||
CreateConnections();
|
||||
|
||||
Text = "&";
|
||||
|
||||
Instantiate();
|
||||
}
|
||||
|
||||
public override void Update(Gate source) {
|
||||
ConnectedOutputs[0].IsPowered = TruthTable[ConnectedInputs[0].IsPowered, ConnectedInputs[1].IsPowered];
|
||||
|
||||
UpdateShapes();
|
||||
UpdateConnectedGates(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
142
C#/DigiSim/Logic/Gates/CustomGate.cs
Normal file
142
C#/DigiSim/Logic/Gates/CustomGate.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DigiSim.Logic.Gates {
|
||||
public class CustomGate : Gate {
|
||||
public int Inputs { get; }
|
||||
public int Outputs { get; }
|
||||
public string Symbol { get; }
|
||||
public LookupTable Lookup { get; }
|
||||
|
||||
public CustomGate(double x, double y) : base(x, y, 50, 50) { }
|
||||
|
||||
private CustomGate(string symbol, LookupTable lookup) {
|
||||
Symbol = symbol;
|
||||
Lookup = lookup;
|
||||
Inputs = lookup.InputCount;
|
||||
Outputs = lookup.OutputCount;
|
||||
Constructor(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
protected override void InternalSetup(Canvas canvas) {
|
||||
Shape = new Rectangle();
|
||||
int connectorCount = Math.Max(Inputs, Outputs);
|
||||
Size = new Size(50, connectorCount * 25);
|
||||
|
||||
SetInputs(Inputs);
|
||||
SetOutputs(Outputs);
|
||||
CreateConnections();
|
||||
|
||||
Text = Symbol;
|
||||
|
||||
Instantiate();
|
||||
}
|
||||
|
||||
public override void Update(Gate source) {
|
||||
bool[] snapshot = new bool[ConnectedInputs.Length];
|
||||
for (var i = 0; i < snapshot.Length; i++) {
|
||||
snapshot[i] = ConnectedInputs[i].IsPowered;
|
||||
}
|
||||
|
||||
bool[] outputs = Lookup.GetOutputs(snapshot);
|
||||
|
||||
for (var i = 0; i < ConnectedOutputs.Length; i++) {
|
||||
ConnectedOutputs[i].IsPowered = outputs[i];
|
||||
}
|
||||
|
||||
UpdateShapes();
|
||||
UpdateConnectedGates(source);
|
||||
}
|
||||
|
||||
public struct LookupTable {
|
||||
public bool[,] Inputs { get; }
|
||||
public bool[,] Outputs { get; }
|
||||
|
||||
public int InputCount { get; }
|
||||
public int OutputCount { get; }
|
||||
public int Length { get; }
|
||||
|
||||
public LookupTable(int inputCount, int outputCount, int length) {
|
||||
Inputs = new bool[length, inputCount];
|
||||
Outputs = new bool[length, outputCount];
|
||||
InputCount = inputCount;
|
||||
OutputCount = outputCount;
|
||||
Length = length;
|
||||
}
|
||||
|
||||
public bool[] GetOutputs(bool[] inputs) {
|
||||
if (inputs.Length != InputCount) throw new ArgumentOutOfRangeException(nameof(inputs));
|
||||
for (int i = 0; i < Length; i++) {
|
||||
bool found = true;
|
||||
for (int j = 0; j < InputCount; j++) {
|
||||
if (Inputs[i, j] != inputs[j]) {
|
||||
found = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
bool[] output = new bool[OutputCount];
|
||||
for (int o = 0; o < OutputCount; o++) {
|
||||
output[o] = Outputs[i, o];
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
return new bool[OutputCount];
|
||||
}
|
||||
|
||||
public void SetLookup(bool[] inputs, bool[] outputs, int index) {
|
||||
if (index >= Length) throw new IndexOutOfRangeException(nameof(index));
|
||||
for (var i = 0; i < inputs.Length; i++) {
|
||||
Inputs[index, i] = inputs[i];
|
||||
}
|
||||
for (var i = 0; i < outputs.Length; i++) {
|
||||
Outputs[index, i] = outputs[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static LookupTable CreateLookup(Gate[] gates) {
|
||||
List<Input> inputs = new List<Input>();
|
||||
List<Output> outputs = new List<Output>();
|
||||
|
||||
foreach (var gate in gates) {
|
||||
if (gate is Input)
|
||||
inputs.Add(gate as Input);
|
||||
if (gate is Output)
|
||||
outputs.Add(gate as Output);
|
||||
}
|
||||
|
||||
int possibilities = (int) Math.Pow(2, inputs.Count);
|
||||
LookupTable lookupTable = new LookupTable(inputs.Count, outputs.Count, possibilities);
|
||||
for (var i = 0; i < possibilities; i++) {
|
||||
bool[] inputSequenze = new bool[inputs.Count];
|
||||
bool[] outputSequenze = new bool[outputs.Count];
|
||||
|
||||
char[] sequenze = Convert.ToString(i, 2).PadLeft(inputs.Count, '0').ToCharArray();
|
||||
for (var j = 0; j < sequenze.Length; j++) {
|
||||
inputSequenze[j] = sequenze[j] == '1';
|
||||
inputs[j].Powered = sequenze[j] == '1';
|
||||
inputs[j].Update(inputs[j]);
|
||||
}
|
||||
|
||||
for (var j = 0; j < outputs.Count; j++) {
|
||||
outputSequenze[j] = outputs[j].ConnectedInputs[0].IsPowered;
|
||||
}
|
||||
|
||||
lookupTable.SetLookup(inputSequenze, outputSequenze, i);
|
||||
}
|
||||
|
||||
return lookupTable;
|
||||
}
|
||||
|
||||
public static CustomGate CreateCustomGateFromBoard(Gate[] gates, string symbol) {
|
||||
return new CustomGate(symbol, CreateLookup(gates));
|
||||
}
|
||||
}
|
||||
}
|
||||
30
C#/DigiSim/Logic/Gates/NandGate.cs
Normal file
30
C#/DigiSim/Logic/Gates/NandGate.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DigiSim.Logic.Gates {
|
||||
public class NandGate : Gate {
|
||||
public NandGate(double x, double y) : base(x, y, 50, 50) { }
|
||||
protected override void InternalSetup(Canvas canvas) {
|
||||
TruthTable.Add(false, false, true);
|
||||
TruthTable.Add(false, true, true);
|
||||
TruthTable.Add(true, false, true);
|
||||
TruthTable.Add(true, true, false);
|
||||
|
||||
Shape = new Rectangle();
|
||||
SetInputs(2);
|
||||
SetOutputs(1);
|
||||
CreateConnections();
|
||||
|
||||
Text = "!&";
|
||||
|
||||
Instantiate();
|
||||
}
|
||||
|
||||
public override void Update(Gate source) {
|
||||
ConnectedOutputs[0].IsPowered = TruthTable[ConnectedInputs[0].IsPowered, ConnectedInputs[1].IsPowered];
|
||||
|
||||
UpdateShapes();
|
||||
UpdateConnectedGates(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
C#/DigiSim/Logic/Gates/NorGate.cs
Normal file
30
C#/DigiSim/Logic/Gates/NorGate.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DigiSim.Logic.Gates {
|
||||
public class NorGate : Gate {
|
||||
public NorGate(double x, double y) : base(x, y, 50, 50) { }
|
||||
protected override void InternalSetup(Canvas canvas) {
|
||||
TruthTable.Add(false, false, true);
|
||||
TruthTable.Add(false, true, false);
|
||||
TruthTable.Add(true, false, false);
|
||||
TruthTable.Add(true, true, false);
|
||||
|
||||
Shape = new Rectangle();
|
||||
SetInputs(2);
|
||||
SetOutputs(1);
|
||||
CreateConnections();
|
||||
|
||||
Text = "!>1";
|
||||
|
||||
Instantiate();
|
||||
}
|
||||
|
||||
public override void Update(Gate source) {
|
||||
ConnectedOutputs[0].IsPowered = TruthTable[ConnectedInputs[0].IsPowered, ConnectedInputs[1].IsPowered];
|
||||
|
||||
UpdateShapes();
|
||||
UpdateConnectedGates(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
C#/DigiSim/Logic/Gates/NotGate.cs
Normal file
25
C#/DigiSim/Logic/Gates/NotGate.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DigiSim.Logic.Gates {
|
||||
public class NotGate : Gate {
|
||||
public NotGate(double x, double y) : base(x, y, 25, 25) { }
|
||||
protected override void InternalSetup(Canvas canvas) {
|
||||
Shape = new Rectangle();
|
||||
SetInputs(1);
|
||||
SetOutputs(1);
|
||||
CreateConnections();
|
||||
|
||||
Text = "!1";
|
||||
|
||||
Instantiate();
|
||||
}
|
||||
|
||||
public override void Update(Gate source) {
|
||||
ConnectedOutputs[0].IsPowered = !ConnectedInputs[0].IsPowered;
|
||||
|
||||
UpdateShapes();
|
||||
UpdateConnectedGates(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
C#/DigiSim/Logic/Gates/OrGate.cs
Normal file
30
C#/DigiSim/Logic/Gates/OrGate.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DigiSim.Logic.Gates {
|
||||
public class OrGate : Gate {
|
||||
public OrGate(double x, double y) : base(x, y, 50, 50) { }
|
||||
protected override void InternalSetup(Canvas canvas) {
|
||||
TruthTable.Add(false, false, false);
|
||||
TruthTable.Add(false, true, true);
|
||||
TruthTable.Add(true, false, true);
|
||||
TruthTable.Add(true, true, true);
|
||||
|
||||
Shape = new Rectangle();
|
||||
SetInputs(2);
|
||||
SetOutputs(1);
|
||||
CreateConnections();
|
||||
|
||||
Text = ">1";
|
||||
|
||||
Instantiate();
|
||||
}
|
||||
|
||||
public override void Update(Gate source) {
|
||||
ConnectedOutputs[0].IsPowered = TruthTable[ConnectedInputs[0].IsPowered, ConnectedInputs[1].IsPowered];
|
||||
|
||||
UpdateShapes();
|
||||
UpdateConnectedGates(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
C#/DigiSim/Logic/Gates/XnorGate.cs
Normal file
30
C#/DigiSim/Logic/Gates/XnorGate.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DigiSim.Logic.Gates {
|
||||
public class XnorGate : Gate {
|
||||
public XnorGate(double x, double y) : base(x, y, 50, 50) { }
|
||||
protected override void InternalSetup(Canvas canvas) {
|
||||
TruthTable.Add(false, false, true);
|
||||
TruthTable.Add(false, true, false);
|
||||
TruthTable.Add(true, false, false);
|
||||
TruthTable.Add(true, true, true);
|
||||
|
||||
Shape = new Rectangle();
|
||||
SetInputs(2);
|
||||
SetOutputs(1);
|
||||
CreateConnections();
|
||||
|
||||
Text = "!=1";
|
||||
|
||||
Instantiate();
|
||||
}
|
||||
|
||||
public override void Update(Gate source) {
|
||||
ConnectedOutputs[0].IsPowered = TruthTable[ConnectedInputs[0].IsPowered, ConnectedInputs[1].IsPowered];
|
||||
|
||||
UpdateShapes();
|
||||
UpdateConnectedGates(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
C#/DigiSim/Logic/Gates/XorGate.cs
Normal file
30
C#/DigiSim/Logic/Gates/XorGate.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace DigiSim.Logic.Gates {
|
||||
public class XorGate : Gate {
|
||||
public XorGate(double x, double y) : base(x, y, 50, 50) { }
|
||||
protected override void InternalSetup(Canvas canvas) {
|
||||
TruthTable.Add(false, false, false);
|
||||
TruthTable.Add(false, true, true);
|
||||
TruthTable.Add(true, false, true);
|
||||
TruthTable.Add(true, true, false);
|
||||
|
||||
Shape = new Rectangle();
|
||||
SetInputs(2);
|
||||
SetOutputs(1);
|
||||
CreateConnections();
|
||||
|
||||
Text = "=1";
|
||||
|
||||
Instantiate();
|
||||
}
|
||||
|
||||
public override void Update(Gate source) {
|
||||
ConnectedOutputs[0].IsPowered = TruthTable[ConnectedInputs[0].IsPowered, ConnectedInputs[1].IsPowered];
|
||||
|
||||
UpdateShapes();
|
||||
UpdateConnectedGates(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user