Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / examples / GUIComponentTestbench.java
1 package net.mograsim.logic.model.examples;
2
3 import java.util.ArrayList;
4 import java.util.Comparator;
5 import java.util.List;
6
7 import net.mograsim.logic.model.SimpleLogicUIStandalone;
8 import net.mograsim.logic.model.model.ViewModelModifiable;
9 import net.mograsim.logic.model.model.components.GUIComponent;
10 import net.mograsim.logic.model.model.components.atomic.GUIBitDisplay;
11 import net.mograsim.logic.model.model.components.atomic.GUIManualSwitch;
12 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
13 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent.Usage;
14 import net.mograsim.logic.model.model.wires.GUIWire;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
17
18 public class GUIComponentTestbench
19 {
20         public static void main(String[] args)
21         {
22                 SimpleLogicUIStandalone.executeVisualisation(GUIComponentTestbench::createTestbench);
23         }
24
25         @SuppressWarnings("unused") // for GUIWires being created
26         public static void createTestbench(ViewModelModifiable model)
27         {
28                 GUIComponent comp = IndirectGUIComponentCreator.createComponent(model, "file:components/am2904/GUIAm2904.json");
29
30                 // guess which pins are outputs and which are inputs
31                 // TODO this code exists four times... but it seems too "hacky" to put it in a helper class
32                 List<String> inputPinNames = new ArrayList<>();
33                 List<String> outputPinNames = new ArrayList<>();
34                 if (comp instanceof SimpleRectangularHardcodedGUIComponent)
35                 {
36                         SimpleRectangularHardcodedGUIComponent compCasted = (SimpleRectangularHardcodedGUIComponent) comp;
37                         for (Pin p : comp.getPins().values())
38                                 if (compCasted.getPinUsage(p) == Usage.INPUT)
39                                         inputPinNames.add(p.name);
40                                 else
41                                         outputPinNames.add(p.name);
42                 } else
43                         for (Pin p : comp.getPins().values())
44                                 if (p.getRelX() < comp.getWidth())
45                                         inputPinNames.add(p.name);
46                                 else
47                                         outputPinNames.add(p.name);
48
49                 inputPinNames.sort(Comparator.comparing(comp::getPin, Comparator.comparing(Pin::getRelY)));
50                 outputPinNames.sort(Comparator.comparing(comp::getPin, Comparator.comparing(Pin::getRelY)));
51
52                 comp.moveTo(100, 0);
53                 for (int i = 0; i < inputPinNames.size(); i++)
54                 {
55                         String pinName = inputPinNames.get(i);
56                         GUIManualSwitch sw = new GUIManualSwitch(model, comp.getPin(pinName).logicWidth);
57                         sw.moveTo(0, 20 * i);
58                         new GUIWire(model, comp.getPin(pinName), sw.getOutputPin());
59                 }
60                 for (int i = 0; i < outputPinNames.size(); i++)
61                 {
62                         String pinName = outputPinNames.get(i);
63                         GUIBitDisplay bd = new GUIBitDisplay(model, comp.getPin(pinName).logicWidth);
64                         bd.moveTo(200, 20 * i);
65                         new GUIWire(model, comp.getPin(pinName), bd.getInputPin());
66                 }
67         }
68 }