Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[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.am2900.Am2900Loader;
12 import net.mograsim.logic.model.model.LogicModelModifiable;
13 import net.mograsim.logic.model.model.components.ModelComponent;
14 import net.mograsim.logic.model.model.components.atomic.ModelAndGate;
15 import net.mograsim.logic.model.model.components.atomic.ModelBitDisplay;
16 import net.mograsim.logic.model.model.components.atomic.ModelManualSwitch;
17 import net.mograsim.logic.model.model.components.atomic.ModelNotGate;
18 import net.mograsim.logic.model.model.components.atomic.ModelTextComponent;
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.ModelWireCrossPoint;
22 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
23 import net.mograsim.logic.model.util.ModellingTool;
24
25 @Deprecated
26 public class Am2901Testbench
27 {
28         public static void main(String[] args)
29         {
30                 Am2900Loader.setup();
31                 SimpleLogicUIStandalone.executeVisualisation(Am2901Testbench::createTestbench, Am2901Testbench::beforeRun);
32         }
33
34         public static void createTestbench(LogicModelModifiable model)
35         {
36                 ModelComponent comp = IndirectModelComponentCreator.createComponent(model, "Am2901");
37                 ModellingTool tool = ModellingTool.createFor(model);
38
39                 comp.moveTo(240, 0);
40
41                 ModelManualSwitch enable = new ModelManualSwitch(model, 1);
42                 ModelWireCrossPoint wcp0 = new ModelWireCrossPoint(model, 1);
43                 ModelNotGate not1 = new ModelNotGate(model, 1);
44                 ModelNotGate not2 = new ModelNotGate(model, 1);
45                 ModelNotGate not3 = new ModelNotGate(model, 1);
46                 ModelAndGate and = new ModelAndGate(model, 1);
47                 tool.connect(wcp0, enable, "");
48                 tool.connect(wcp0, and, "A");
49                 tool.connect(wcp0, not1, "A");
50                 tool.connect(not1, not2, "Y", "A");
51                 tool.connect(not2, not3, "Y", "A");
52                 tool.connect(not3, and, "Y", "B");
53                 enable.moveTo(20, -32.5);
54                 wcp0.moveTo(35, -26);
55                 not1.moveTo(50, -20);
56                 not2.moveTo(80, -20);
57                 not3.moveTo(110, -20);
58                 and.moveTo(135, -30);
59                 Pin last = and.getPin("Y");
60
61                 List<String> inputPinNames = new ArrayList<>();
62                 List<String> outputPinNames = new ArrayList<>();
63                 for (Pin p : comp.getPins().values())
64                         if (p.usage == PinUsage.INPUT)
65                                 inputPinNames.add(p.name);
66                         else
67                                 outputPinNames.add(p.name);
68
69                 inputPinNames.sort(Comparator.comparing(comp::getPin, Comparator.comparing(Pin::getRelY)));
70                 outputPinNames.sort(Comparator.comparing(comp::getPin, Comparator.comparing(Pin::getRelY)));
71
72                 for (int i = 0; i < inputPinNames.size(); i++)
73                 {
74                         double x = 55 + 70 * (i % 2);
75                         double y = 10 * i;
76
77                         ModelWireCrossPoint wcp = new ModelWireCrossPoint(model, 1);
78                         ModelComponent d_ff = IndirectModelComponentCreator.createComponent(model, "dff");
79                         ModelManualSwitch sw = new ModelManualSwitch(model, 1);
80
81                         tool.connect(last, wcp);
82                         tool.connect(wcp, d_ff, "C");
83                         tool.connect(sw, d_ff, "", "D");
84                         tool.connect(d_ff, comp, "Q", inputPinNames.get(i));
85                         last = wcp.getPin();
86
87                         ModelTextComponent label = new ModelTextComponent(model, inputPinNames.get(i));
88
89                         sw.moveTo(x, y + 7.5);
90                         wcp.moveTo(160, y);
91                         d_ff.moveTo(170, y);
92                         label.moveTo(x - 48, y + 8);
93                 }
94
95                 for (int i = 0; i < outputPinNames.size(); i++)
96                 {
97                         double x = 300 + 75 * (i % 2);
98                         double y = 10 * i - 2.5;
99                         ModelBitDisplay bd = new ModelBitDisplay(model, 1);
100                         bd.moveTo(x, y);
101                         tool.connect(bd.getInputPin(), comp, outputPinNames.get(i));
102
103                         ModelTextComponent label = new ModelTextComponent(model, outputPinNames.get(i));
104                         label.moveTo(x + 25, y);
105                 }
106         }
107
108         public static void beforeRun(VisualisationObjects vis)
109         {
110                 vis.model.getComponentsByName().values().forEach(c ->
111                 {
112                         if (c instanceof ModelManualSwitch)
113                         {
114                                 ModelManualSwitch cCasted = (ModelManualSwitch) c;
115                                 cCasted.setHighLevelState("out", BitVector.of(Bit.ZERO, cCasted.logicWidth));
116                         }
117                 });
118         }
119 }