Added a state to SimpleRectangularHardcodedGUIComponent.recalculate
[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.am2900.components.am2910.GUIAm2910InstrPLA;
9 import net.mograsim.logic.model.model.ViewModelModifiable;
10 import net.mograsim.logic.model.model.components.GUIComponent;
11 import net.mograsim.logic.model.model.components.atomic.GUIBitDisplay;
12 import net.mograsim.logic.model.model.components.atomic.GUIManualSwitch;
13 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
14 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent.Usage;
15 import net.mograsim.logic.model.model.wires.GUIWire;
16 import net.mograsim.logic.model.model.wires.Pin;
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, "GUIAm2901", "Am2901");
29                 GUIComponent comp = new GUIAm2910InstrPLA(model, "Am2910");
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                         if (comp.getPin(pinName).logicWidth == 1)
58                         {
59                                 GUIManualSwitch sw = new GUIManualSwitch(model);
60                                 sw.moveTo(0, 20 * i);
61                                 new GUIWire(model, comp.getPin(pinName), sw.getOutputPin());
62                         }
63                 }
64                 for (int i = 0; i < outputPinNames.size(); i++)
65                 {
66                         String pinName = outputPinNames.get(i);
67                         if (comp.getPin(pinName).logicWidth == 1)
68                         {
69                                 GUIBitDisplay bd = new GUIBitDisplay(model);
70                                 bd.moveTo(200, 20 * i);
71                                 new GUIWire(model, comp.getPin(pinName), bd.getInputPin());
72                         }
73                 }
74         }
75 }