/
ChipInstance.cs
77 lines (72 loc) · 2.17 KB
/
ChipInstance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Logically;
public class ChipInstance
{
public ChipDefinition Parent;
public Dictionary<string, bool> WireStates = new();
public Dictionary<string, bool> NewWireStates = new();
public HashSet<ChipConnectionInstance> Connections = new();
public ChipInstance(ChipDefinition parent)
{
Parent = parent;
parent.Inputs.ForEach(c => NewWireStates[RemoveHigh(c)] = StartHigh(c));
parent.Outputs.ForEach(c => NewWireStates[RemoveHigh(c)] = StartHigh(c));
parent.Buslines.ForEach(c => NewWireStates[RemoveHigh(c)] = StartHigh(c));
parent.Connections.ToList().ForEach(c => Connections.Add(new ChipConnectionInstance()
{
Chip = new ChipInstance(ChipDefinition.AllChips[c.TargetChip]),
Parent = c
}));
Swap();
}
private static bool StartHigh(string name)
{
return name.EndsWith("_HIGH");
}
public static string RemoveHigh(string name)
{
if (StartHigh(name)) return name.Replace("_HIGH", "");
return name;
}
public void Swap()
{
foreach (var kv in NewWireStates)
{
WireStates[kv.Key] = kv.Value;
}
foreach (var chipConnInst in Connections)
{
chipConnInst.Chip.Swap();
}
}
public bool Satisfied()
{
foreach (var kv in NewWireStates)
{
if (!WireStates.ContainsKey(kv.Key)) return false;
if (WireStates[kv.Key] != kv.Value) return false;
}
foreach (var chipConnInst in Connections)
{
if (!chipConnInst.Chip.Satisfied()) return false;
}
return true;
}
public override string ToString()
{
StringBuilder sb = new();
sb.Append('@');
sb.AppendLine(Parent.Name);
foreach (var kv in this.NewWireStates)
{
sb.Append(kv.Key);
sb.Append(": ");
sb.Append(this.WireStates[kv.Key] ? "HIGH" : "LOW");
sb.Append(" -> ");
sb.AppendLine(kv.Value ? "HIGH" : "LOW");
}
return sb.ToString();
}
}