8b33dd040c96f3b494acc29333e815b3f7cf0c04
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / modeladapter / LogicCoreAdapter.java
1 package net.mograsim.logic.model.modeladapter;
2
3 import java.util.Collection;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.HashSet;
7 import java.util.Map;
8 import java.util.Map.Entry;
9 import java.util.Set;
10 import java.util.function.Function;
11 import java.util.stream.Collectors;
12
13 import net.mograsim.logic.core.timeline.Timeline;
14 import net.mograsim.logic.core.wires.CoreWire;
15 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
16 import net.mograsim.logic.model.model.LogicModel;
17 import net.mograsim.logic.model.model.components.ModelComponent;
18 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
19 import net.mograsim.logic.model.model.components.submodels.SubmodelInterface;
20 import net.mograsim.logic.model.model.wires.ModelWire;
21 import net.mograsim.logic.model.model.wires.ModelWireCrossPoint;
22 import net.mograsim.logic.model.model.wires.Pin;
23 import net.mograsim.logic.model.modeladapter.componentadapters.ComponentAdapter;
24
25 public class LogicCoreAdapter
26 {
27         private final static Map<Class<? extends ModelComponent>, ComponentAdapter<? extends ModelComponent>> componentAdapters = new HashMap<>();
28
29         public static void addComponentAdapter(ComponentAdapter<? extends ModelComponent> componentAdapter)
30         {
31                 componentAdapters.put(componentAdapter.getSupportedClass(), componentAdapter);
32         }
33
34         public static Timeline convert(LogicModel logicModel, CoreModelParameters params)
35         {
36                 // TODO replace Timeline with CoreModel as soon as it exists
37                 Timeline timeline = new Timeline(10);
38
39                 convert(logicModel, params, timeline, Map.of());
40
41                 return timeline;
42         }
43
44         private static void convert(LogicModel logicModel, CoreModelParameters params, Timeline timeline, Map<Pin, CoreWire> externalWires)
45         {
46                 Map<Pin, CoreWire> logicWiresPerPin = convertWires(getAllPins(logicModel), logicModel.getWiresByName().values(), externalWires,
47                                 params, timeline);
48                 Map<Pin, CoreWire> logicWiresPerPinUnmodifiable = Collections.unmodifiableMap(logicWiresPerPin);
49
50                 for (ModelComponent modelComp : logicModel.getComponentsByName().values())
51                 {
52                         if (modelComp instanceof SubmodelComponent)
53                         {
54                                 SubmodelComponent modelCompCasted = (SubmodelComponent) modelComp;
55                                 Map<String, Pin> supermodelPins = modelCompCasted.getSupermodelPins();
56                                 Map<Pin, CoreWire> externalWiresForSubmodel = supermodelPins.entrySet().stream().collect(
57                                                 Collectors.toMap(e -> modelCompCasted.getSubmodelPin(e.getKey()), e -> logicWiresPerPin.get(e.getValue())));
58                                 convert(modelCompCasted.submodel, params, timeline, externalWiresForSubmodel);
59                         } else if (modelComp instanceof ModelWireCrossPoint)
60                         {
61                                 ModelWireCrossPoint modelCompCasted = (ModelWireCrossPoint) modelComp;
62                                 modelCompCasted.setCoreModelBinding(logicWiresPerPin.get(modelCompCasted.getPin()).createReadOnlyEnd());
63                         } else if (!(modelComp instanceof SubmodelInterface))// nothing to do for SubmodelInterfaces
64                                 createAndLinkComponent(timeline, params, modelComp, logicWiresPerPinUnmodifiable);
65                 }
66         }
67
68         private static Set<Pin> getAllPins(LogicModel logicModel)
69         {
70                 return logicModel.getComponentsByName().values().stream().flatMap(component -> component.getPins().values().stream())
71                                 .collect(Collectors.toSet());
72         }
73
74         private static Map<Pin, CoreWire> convertWires(Set<Pin> allPins, Collection<ModelWire> wires, Map<Pin, CoreWire> externalWires,
75                         CoreModelParameters params, Timeline timeline)
76         {
77                 Map<Pin, Set<Pin>> connectedPinGroups = getConnectedPinGroups(allPins, wires);
78                 Map<Pin, CoreWire> logicWiresPerPin = createLogicWires(params, timeline, connectedPinGroups, externalWires);
79                 setModelWiresCoreModelBinding(wires, logicWiresPerPin);
80                 return logicWiresPerPin;
81         }
82
83         private static Map<Pin, CoreWire> createLogicWires(CoreModelParameters params, Timeline timeline, Map<Pin, Set<Pin>> connectedPinGroups,
84                         Map<Pin, CoreWire> externalWires)
85         {
86                 Map<Pin, CoreWire> logicWiresPerPin = new HashMap<>();
87                 Map<Set<Pin>, CoreWire> logicWiresPerPinGroup = new HashMap<>();
88                 for (Entry<Pin, Set<Pin>> e : connectedPinGroups.entrySet())
89                         logicWiresPerPin.put(e.getKey(), logicWiresPerPinGroup.computeIfAbsent(e.getValue(), set ->
90                         {
91                                 CoreWire externalWire = null;
92                                 for (Pin p : set)
93                                 {
94                                         CoreWire externalWireCandidate = externalWires.get(p);
95                                         if (externalWireCandidate != null)
96                                                 if (externalWire == null)
97                                                         externalWire = externalWireCandidate;
98                                                 else if (externalWire.width == externalWireCandidate.width)
99                                                         CoreWire.fuse(externalWire, externalWireCandidate);
100                                                 else
101                                                         throw new IllegalArgumentException(
102                                                                         "Two pins to external wires with different logicWidths can't be connected directly");
103                                 }
104                                 return externalWire == null ? new CoreWire(timeline, e.getKey().logicWidth, params.wireTravelTime) : externalWire;
105                         }));
106                 return logicWiresPerPin;
107         }
108
109         private static void setModelWiresCoreModelBinding(Collection<ModelWire> wires, Map<Pin, CoreWire> logicWiresPerPin)
110         {
111                 Map<CoreWire, ReadEnd> modelWireSharedReadEnd = logicWiresPerPin.values().stream().distinct()
112                                 .collect(Collectors.toMap(Function.identity(), CoreWire::createReadOnlyEnd));
113                 for (ModelWire modelWire : wires)
114                         modelWire.setCoreModelBinding(modelWireSharedReadEnd.get(logicWiresPerPin.get(modelWire.getPin1())));
115         }
116
117         private static Map<Pin, Set<Pin>> getConnectedPinGroups(Set<Pin> allPins, Collection<ModelWire> wires)
118         {
119                 Map<Pin, Set<Pin>> connectedPinsPerPin = new HashMap<>();
120
121                 for (Pin p : allPins)
122                 {
123                         HashSet<Pin> connectedPins = new HashSet<>();
124                         connectedPins.add(p);
125                         connectedPinsPerPin.put(p, connectedPins);
126                 }
127
128                 wires.forEach(wire ->
129                 {
130                         Pin pin1 = wire.getPin1();
131                         Pin pin2 = wire.getPin2();
132
133                         Set<Pin> pin1ConnectedPins = connectedPinsPerPin.get(pin1);
134                         Set<Pin> pin2ConnectedPins = connectedPinsPerPin.get(pin2);
135
136                         pin1ConnectedPins.addAll(pin2ConnectedPins);
137                         pin1ConnectedPins.add(pin1);
138                         pin1ConnectedPins.add(pin2);
139
140                         pin2ConnectedPins.forEach(pin -> connectedPinsPerPin.put(pin, pin1ConnectedPins));
141                 });
142                 return connectedPinsPerPin;
143         }
144
145         public static final Map<Class<? extends ModelComponent>, Integer> gateCountsPerComponentClass = new HashMap<>();
146
147         @SuppressWarnings("unchecked")
148         private static <G extends ModelComponent> void createAndLinkComponent(Timeline timeline, CoreModelParameters params,
149                         ModelComponent modelComponent, Map<Pin, CoreWire> logicWiresPerPin)
150         {
151                 Class<?> cls = modelComponent.getClass();
152                 gateCountsPerComponentClass.merge(modelComponent.getClass(), 1, Integer::sum);
153                 ComponentAdapter<? super G> adapter = null;
154                 while (cls != ModelComponent.class && adapter == null)
155                 {
156                         adapter = (ComponentAdapter<? super G>) componentAdapters.get(cls);
157                         cls = cls.getSuperclass();
158                 }
159                 if (adapter == null)
160                         throw new IllegalArgumentException("Unknown component class: " + modelComponent.getClass());
161                 adapter.createAndLinkComponent(timeline, params, (G) modelComponent, logicWiresPerPin);
162         }
163
164         private LogicCoreAdapter()
165         {
166                 throw new UnsupportedOperationException("No LogicCoreAdapter instances");
167         }
168 }