Merge remote-tracking branch 'origin/development' into development
[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,
29                                 "class:net.mograsim.logic.model.am2900.components.am2904.RegCTInstrDecode");
30
31                 // guess which pins are outputs and which are inputs
32                 // TODO this code exists four times... but it seems too "hacky" to put it in a helper class
33                 List<String> inputPinNames = new ArrayList<>();
34                 List<String> outputPinNames = new ArrayList<>();
35                 if (comp instanceof SimpleRectangularHardcodedGUIComponent)
36                 {
37                         SimpleRectangularHardcodedGUIComponent compCasted = (SimpleRectangularHardcodedGUIComponent) comp;
38                         for (Pin p : comp.getPins().values())
39                                 if (compCasted.getPinUsage(p) == Usage.INPUT)
40                                         inputPinNames.add(p.name);
41                                 else
42                                         outputPinNames.add(p.name);
43                 } else
44                         for (Pin p : comp.getPins().values())
45                                 if (p.getRelX() == 0)
46                                         inputPinNames.add(p.name);
47                                 else
48                                         outputPinNames.add(p.name);
49
50                 inputPinNames.sort(Comparator.comparing(comp::getPin, Comparator.comparing(Pin::getRelY)));
51                 outputPinNames.sort(Comparator.comparing(comp::getPin, Comparator.comparing(Pin::getRelY)));
52
53                 comp.moveTo(100, 0);
54                 for (int i = 0; i < inputPinNames.size(); i++)
55                 {
56                         String pinName = inputPinNames.get(i);
57                         GUIManualSwitch sw = new GUIManualSwitch(model, comp.getPin(pinName).logicWidth);
58                         sw.moveTo(0, 20 * i);
59                         new GUIWire(model, comp.getPin(pinName), sw.getOutputPin());
60                 }
61                 for (int i = 0; i < outputPinNames.size(); i++)
62                 {
63                         String pinName = outputPinNames.get(i);
64                         GUIBitDisplay bd = new GUIBitDisplay(model, comp.getPin(pinName).logicWidth);
65                         bd.moveTo(200, 20 * i);
66                         new GUIWire(model, comp.getPin(pinName), bd.getInputPin());
67                 }
68         }
69 }