|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | + |
| 5 | +namespace SpiceSharpParser.CustomComponents |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Resolves native A-device terminal positions into portable subcircuit nodes. |
| 9 | + /// </summary> |
| 10 | + internal sealed class LTspiceADeviceConnections |
| 11 | + { |
| 12 | + private readonly bool[] _unusedTerminals; |
| 13 | + private readonly string[] _terminals; |
| 14 | + |
| 15 | + private LTspiceADeviceConnections( |
| 16 | + string[] terminals, |
| 17 | + bool[] unusedTerminals) |
| 18 | + { |
| 19 | + _terminals = terminals; |
| 20 | + _unusedTerminals = unusedTerminals; |
| 21 | + } |
| 22 | + |
| 23 | + public static LTspiceADeviceConnections Create( |
| 24 | + LTspiceADeviceInstance instance, |
| 25 | + LTspiceADeviceDefinition definition, |
| 26 | + string portableInstanceName, |
| 27 | + bool nodeNamesAreCaseSensitive) |
| 28 | + { |
| 29 | + var terminals = (string[])instance.Terminals.Clone(); |
| 30 | + var unused = new bool[LTspiceADeviceInstance.TerminalCount]; |
| 31 | + StringComparison comparison = nodeNamesAreCaseSensitive |
| 32 | + ? StringComparison.Ordinal |
| 33 | + : StringComparison.OrdinalIgnoreCase; |
| 34 | + string commonNode = terminals[(int)ADeviceTerminal.T8Common]; |
| 35 | + |
| 36 | + for (int index = 0; index < terminals.Length; index++) |
| 37 | + { |
| 38 | + unused[index] = terminals[index].Equals(commonNode, comparison); |
| 39 | + } |
| 40 | + |
| 41 | + // Repeating terminal 8 means "not connected" in native A-device |
| 42 | + // syntax. Inputs may safely remain tied to common. Outputs cannot: |
| 43 | + // the portable macromodel has a real output stage, so give each |
| 44 | + // unused output a private node. |
| 45 | + foreach (ADeviceTerminal outputTerminal in definition.OutputTerminals) |
| 46 | + { |
| 47 | + int index = (int)outputTerminal; |
| 48 | + if (unused[index]) |
| 49 | + { |
| 50 | + terminals[index] = portableInstanceName |
| 51 | + + ".__a_nc" |
| 52 | + + (index + 1).ToString(CultureInfo.InvariantCulture); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return new LTspiceADeviceConnections(terminals, unused); |
| 57 | + } |
| 58 | + |
| 59 | + public string CommonNode => Get(ADeviceTerminal.T8Common); |
| 60 | + |
| 61 | + public bool IsUnused(ADeviceTerminal terminal) |
| 62 | + { |
| 63 | + return _unusedTerminals[(int)terminal]; |
| 64 | + } |
| 65 | + |
| 66 | + public IReadOnlyList<string> CreateSubcircuitNodes( |
| 67 | + LTspiceADeviceDefinition definition, |
| 68 | + string highRailNode, |
| 69 | + string lowRailNode) |
| 70 | + { |
| 71 | + int railCount = definition.CreatesDigitalRails ? 2 : 0; |
| 72 | + var result = new List<string>( |
| 73 | + definition.SubcircuitTerminals.Count + railCount); |
| 74 | + foreach (ADeviceTerminal terminal in definition.SubcircuitTerminals) |
| 75 | + { |
| 76 | + result.Add(Get(terminal)); |
| 77 | + } |
| 78 | + |
| 79 | + if (definition.CreatesDigitalRails) |
| 80 | + { |
| 81 | + result.Add(highRailNode); |
| 82 | + result.Add(lowRailNode); |
| 83 | + } |
| 84 | + |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + private string Get(ADeviceTerminal terminal) |
| 89 | + { |
| 90 | + return _terminals[(int)terminal]; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments