4a8e389b3b7957762a8abaee5c3a4abfdfba1104
[Mograsim.git] / net.mograsim.logic.model.am2900 / test / net / mograsim / logic / model / am2900 / am2901 / Am2901Testbench.java
1 package net.mograsim.logic.model.am2900.am2901;
2
3 import java.util.ArrayList;
4 import java.util.Comparator;
5 import java.util.List;
6
7 import net.mograsim.logic.core.types.Bit;
8 import net.mograsim.logic.core.types.BitVector;
9 import net.mograsim.logic.model.SimpleLogicUIStandalone;
10 import net.mograsim.logic.model.SimpleLogicUIStandalone.VisualisationObjects;
11 import net.mograsim.logic.model.model.ViewModelModifiable;
12 import net.mograsim.logic.model.model.components.GUIComponent;
13 import net.mograsim.logic.model.model.components.atomic.GUIAndGate;
14 import net.mograsim.logic.model.model.components.atomic.GUIBitDisplay;
15 import net.mograsim.logic.model.model.components.atomic.GUIManualSwitch;
16 import net.mograsim.logic.model.model.components.atomic.GUINotGate;
17 import net.mograsim.logic.model.model.components.atomic.TextComponent;
18 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
19 import net.mograsim.logic.model.model.wires.Pin;
20 import net.mograsim.logic.model.model.wires.PinUsage;
21 import net.mograsim.logic.model.model.wires.WireCrossPoint;
22 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
23 import net.mograsim.logic.model.util.ModellingTool;
24
25 public class Am2901Testbench
26 {
27         public static void main(String[] args)
28         {
29                 SimpleLogicUIStandalone.executeVisualisation(Am2901Testbench::createTestbench, Am2901Testbench::beforeRun);
30         }
31
32         public static void createTestbench(ViewModelModifiable model)
33         {
34                 GUIComponent comp = IndirectGUIComponentCreator.createComponent(model, "GUIAm2901");
35                 ModellingTool tool = ModellingTool.createFor(model);
36
37                 comp.moveTo(240, 0);
38
39                 GUIManualSwitch enable = new GUIManualSwitch(model, 1);
40                 WireCrossPoint wcp0 = new WireCrossPoint(model, 1);
41                 GUINotGate not1 = new GUINotGate(model, 1);
42                 GUINotGate not2 = new GUINotGate(model, 1);
43                 GUINotGate not3 = new GUINotGate(model, 1);
44                 GUIAndGate and = new GUIAndGate(model, 1);
45                 tool.connect(wcp0, enable, "");
46                 tool.connect(wcp0, and, "A");
47                 tool.connect(wcp0, not1, "A");
48                 tool.connect(not1, not2, "Y", "A");
49                 tool.connect(not2, not3, "Y", "A");
50                 tool.connect(not3, and, "Y", "B");
51                 enable.moveTo(20, -32.5);
52                 wcp0.moveTo(35, -26);
53                 not1.moveTo(50, -20);
54                 not2.moveTo(80, -20);
55                 not3.moveTo(110, -20);
56                 and.moveTo(135, -30);
57                 Pin last = and.getPin("Y");
58
59                 List<String> inputPinNames = new ArrayList<>();
60                 List<String> outputPinNames = new ArrayList<>();
61                 for (Pin p : comp.getPins().values())
62                         if (p.usage == PinUsage.INPUT)
63                                 inputPinNames.add(p.name);
64                         else
65                                 outputPinNames.add(p.name);
66
67                 inputPinNames.sort(Comparator.comparing(comp::getPin, Comparator.comparing(Pin::getRelY)));
68                 outputPinNames.sort(Comparator.comparing(comp::getPin, Comparator.comparing(Pin::getRelY)));
69
70                 for (int i = 0; i < inputPinNames.size(); i++)
71                 {
72                         double x = 55 + 70 * (i % 2);
73                         double y = 10 * i;
74
75                         WireCrossPoint wcp = new WireCrossPoint(model, 1);
76                         GUIComponent d_ff = IndirectGUIComponentCreator.createComponent(model, "GUIdff");
77                         GUIManualSwitch sw = new GUIManualSwitch(model, 1);
78
79                         tool.connect(last, wcp);
80                         tool.connect(wcp, d_ff, "C");
81                         tool.connect(sw, d_ff, "", "D");
82                         tool.connect(d_ff, comp, "Q", inputPinNames.get(i));
83                         last = wcp.getPin();
84
85                         TextComponent label = new TextComponent(model, inputPinNames.get(i));
86
87                         sw.moveTo(x, y + 7.5);
88                         wcp.moveTo(160, y);
89                         d_ff.moveTo(170, y);
90                         label.moveTo(x - 48, y + 8);
91                 }
92
93                 for (int i = 0; i < outputPinNames.size(); i++)
94                 {
95                         double x = 300 + 75 * (i % 2);
96                         double y = 10 * i - 2.5;
97                         GUIBitDisplay bd = new GUIBitDisplay(model, 1);
98                         bd.moveTo(x, y);
99                         tool.connect(bd.getInputPin(), comp, outputPinNames.get(i));
100
101                         TextComponent label = new TextComponent(model, outputPinNames.get(i));
102                         label.moveTo(x + 25, y);
103                 }
104         }
105
106         public static void beforeRun(VisualisationObjects vis)
107         {
108                 ((SubmodelComponent) vis.model.getComponentsByName().get("testbench")).submodel.getComponentsByName().values().forEach(c ->
109                 {
110                         if (c instanceof GUIManualSwitch)
111                         {
112                                 GUIManualSwitch cCasted = (GUIManualSwitch) c;
113                                 cCasted.setHighLevelState("out", BitVector.of(Bit.ZERO, cCasted.logicWidth));
114                         }
115                 });
116         }
117 }