1 package net.mograsim.logic.ui.am2900;
3 import static org.junit.jupiter.api.Assertions.fail;
5 import java.lang.reflect.Field;
6 import java.util.ArrayList;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Objects;
12 import java.util.Queue;
14 import java.util.TreeSet;
16 import net.mograsim.logic.core.components.BitDisplay;
17 import net.mograsim.logic.core.components.ManualSwitch;
18 import net.mograsim.logic.core.timeline.Timeline;
19 import net.mograsim.logic.core.types.Bit;
20 import net.mograsim.logic.core.types.BitVector;
21 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
22 import net.mograsim.logic.model.model.ViewModel;
23 import net.mograsim.logic.model.model.ViewModelModifiable;
24 import net.mograsim.logic.model.model.components.GUIComponent;
25 import net.mograsim.logic.model.model.components.atomic.GUIBitDisplay;
26 import net.mograsim.logic.model.model.components.atomic.GUIManualSwitch;
27 import net.mograsim.logic.model.model.components.mi.nandbased.am2901.GUIAm2901;
28 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
29 import net.mograsim.logic.model.model.wires.GUIWire;
30 import net.mograsim.logic.model.model.wires.Pin;
31 import net.mograsim.logic.model.modeladapter.LogicModelParameters;
32 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
34 public class TestableAm2901Impl implements TestableAm2901
36 private GUIComponent am2901;
37 private Timeline timeline;
38 private ManualSwitch I8, I7, I6, I5, I4, I3, I2, I1, I0;
39 private ManualSwitch C;
40 private ManualSwitch Cn;
41 private ManualSwitch D1, D2, D3, D4;
42 private ManualSwitch A0, A1, A2, A3;
43 private ManualSwitch B0, B1, B2, B3;
44 private ManualSwitch IRAMn, IRAMn_3, IQn, IQn_3;
45 private BitDisplay Y1, Y2, Y3, Y4;
46 private BitDisplay F_0, Cn_4, OVR, F3;
47 private BitDisplay ORAMn, ORAMn_3, OQn, OQn_3;
49 private Set<String> wireDebugChangeSet;
50 private boolean debugWires = false;
51 public int debugEventThreshold = 10_000;
52 public int debugEventCount = 500;
54 private int eventCounter;
59 // Normal execution until completion or eventLimit
60 int eventLimit = debugEventThreshold;
63 while (eventCounter < eventLimit)
65 timeline.executeNext();
66 if (!timeline.hasNext())
68 // System.out.println("run() took " + eventCounter + " events");
69 return Result.SUCCESS;
72 // Start debugging if event limit is reached
74 wireDebugChangeSet = new TreeSet<>();
75 Set<String> oldChangeSet;
76 // observe wire changes to detect, if we are really stuck in an endless loop
79 eventLimit += debugEventCount;
80 oldChangeSet = wireDebugChangeSet;
81 wireDebugChangeSet = new TreeSet<>();
82 while (eventCounter < eventLimit)
84 timeline.executeNext();
85 if (!timeline.hasNext())
87 // no endless loop, but more events needed than expected
88 System.out.println("run() took longer than expected: " + eventCounter);
89 return Result.SUCCESS;
92 } while (!oldChangeSet.equals(wireDebugChangeSet));
93 // if stuck, abort execution and print wires
94 System.err.print("Problematic Wire updates:");
95 wireDebugChangeSet.forEach(System.out::println);
96 System.err.println("run() failed: " + eventCounter);
97 return Result.OUT_OF_TIME;
100 @SuppressWarnings("unused")
105 ViewModelModifiable viewModel = new ViewModelModifiable();
106 // TODO replace with deserialized version as soon as high level states work for deserialized components
107 am2901 = new GUIAm2901(viewModel);
108 // am2901 = IndirectGUIComponentCreator.createComponent(viewModel, "GUIAm2901");
109 // guess which pins are outputs and which are inputs
110 // TODO this code exists three times... but it seems too "hacky" to put it in a helper class
111 List<String> inputPinNames = new ArrayList<>();
112 List<String> outputPinNames = new ArrayList<>();
113 for (Pin p : am2901.getPins().values())
114 if (p.getRelX() == 0)
115 inputPinNames.add(p.name);
117 outputPinNames.add(p.name);
119 HashMap<String, GUIManualSwitch> idSwitchMap = new HashMap<>();
120 for (String id : inputPinNames)
122 GUIManualSwitch sw = new GUIManualSwitch(viewModel);
123 new GUIWire(viewModel, am2901.getPin(id), sw.getOutputPin());
124 idSwitchMap.put(id, sw);
127 HashMap<String, GUIBitDisplay> idDisplayMap = new HashMap<>();
128 for (String id : outputPinNames)
130 GUIBitDisplay bd = new GUIBitDisplay(viewModel);
131 // bd.addRedrawListener(() -> System.out.println(id + " " + bd.getBitDisplay().getDisplayedValue()));
132 new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin());
133 idDisplayMap.put(id, bd);
135 // Create logic model
136 LogicModelParameters params = new LogicModelParameters();
137 params.gateProcessTime = 50;
138 params.wireTravelTime = 10;
139 timeline = ViewLogicModelAdapter.convert(viewModel, params);
140 // Bind switches/displays to this test class
141 for (var entry : idSwitchMap.entrySet())
142 setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getManualSwitch());
143 for (var entry : idDisplayMap.entrySet())
144 setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getBitDisplay());
147 HashSet<GUIWire> wiresIncludingSubmodels = new HashSet<>();
148 Queue<ViewModel> modelsToIterate = new LinkedList<>();
149 modelsToIterate.add(viewModel);
150 while (modelsToIterate.size() > 0)
152 ViewModel model = modelsToIterate.poll();
153 wiresIncludingSubmodels.addAll(model.getWires());
154 for (GUIComponent comp : model.getComponentsByName().values())
155 if (comp instanceof SubmodelComponent)
156 modelsToIterate.offer(((SubmodelComponent) comp).submodel);
158 wiresIncludingSubmodels.forEach(w -> w.addRedrawListener(() ->
162 wireDebugChangeSet.add(w.toString());
165 timeline.addEventAddedListener(te -> eventCounter++);
169 public void setDest(Am2901_Dest dest)
171 var bits = of(dest.ordinal(), 3);
172 I8.setToValueOf(bits.getLSBit(2));
173 I7.setToValueOf(bits.getLSBit(1));
174 I6.setToValueOf(bits.getLSBit(0));
178 public void setFunc(Am2901_Func func)
180 var bits = of(func.ordinal(), 3);
181 I5.setToValueOf(bits.getLSBit(2));
182 I4.setToValueOf(bits.getLSBit(1));
183 I3.setToValueOf(bits.getLSBit(0));
187 public void setSrc(Am2901_Src src)
189 var bits = of(src.ordinal(), 3);
190 I2.setToValueOf(bits.getLSBit(2));
191 I1.setToValueOf(bits.getLSBit(1));
192 I0.setToValueOf(bits.getLSBit(0));
196 public void setReg_A(String val_4_bit)
198 var bits = BitVector.parse(val_4_bit);
199 A3.setToValueOf(bits.getLSBit(3));
200 A2.setToValueOf(bits.getLSBit(2));
201 A1.setToValueOf(bits.getLSBit(1));
202 A0.setToValueOf(bits.getLSBit(0));
206 public void setReg_B(String val_4_bit)
208 var bits = BitVector.parse(val_4_bit);
209 B3.setToValueOf(bits.getLSBit(3));
210 B2.setToValueOf(bits.getLSBit(2));
211 B1.setToValueOf(bits.getLSBit(1));
212 B0.setToValueOf(bits.getLSBit(0));
216 public void setCarryIn(String val_1_bit)
218 Cn.setToValueOf(Bit.parse(val_1_bit));
222 public void setNotOutEnable(String val_1_bit)
224 throw new UnsupportedOperationException(); // TODO
228 public void setD(String val_4_bit)
230 var bits = BitVector.parse(val_4_bit);
231 D4.setToValueOf(bits.getLSBit(3));
232 D3.setToValueOf(bits.getLSBit(2));
233 D2.setToValueOf(bits.getLSBit(1));
234 D1.setToValueOf(bits.getLSBit(0));
238 public void setQ_0(String val_1_bit)
240 IQn.setToValueOf(Bit.parse(val_1_bit));
244 public void setQ_3(String val_1_bit)
246 IQn_3.setToValueOf(Bit.parse(val_1_bit));
250 public void setRAM_0(String val_1_bit)
252 IRAMn.setToValueOf(Bit.parse(val_1_bit));
256 public void setRAM_3(String val_1_bit)
258 IRAMn_3.setToValueOf(Bit.parse(val_1_bit));
262 public void clockOn(boolean isClockOn)
264 C.setState(isClockOn);
268 public String getQ_0()
270 return OQn.getDisplayedValue().toString();
274 public String getQ_3()
276 return OQn_3.getDisplayedValue().toString();
280 public String getRAM_0()
282 return ORAMn.getDisplayedValue().toString();
286 public String getRAM_3()
288 return ORAMn_3.getDisplayedValue().toString();
292 public String getNotP()
294 throw new UnsupportedOperationException(); // TODO
298 public String getNotG()
300 throw new UnsupportedOperationException(); // TODO
304 public String getCarryOut()
306 return Cn_4.getDisplayedValue().toString();
310 public String getSign()
312 return F3.getDisplayedValue().toString();
316 public String getZero()
318 return F_0.getDisplayedValue().toString();
322 public String getOverflow()
324 return OVR.getDisplayedValue().toString();
330 var y3 = Y4.getDisplayedValue();
331 var y2 = Y3.getDisplayedValue();
332 var y1 = Y2.getDisplayedValue();
333 var y0 = Y1.getDisplayedValue();
334 return y3.concat(y2).concat(y1).concat(y0).toString();
337 private void setField(String name, Object value)
341 Field f = TestableAm2901Impl.class.getDeclaredField(name);
342 f.setAccessible(true);
343 f.set(this, Objects.requireNonNull(value));
351 private static BitVector of(int value, int length)
353 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
355 for (int i = length - 1; i >= 0; i--)
357 mutator.setMSBit(i, Bit.lastBitOf(val));
360 return mutator.toBitVector();
364 public void setDirectly(Register r, String val_4_bit)
366 am2901.setHighLevelState(regToStateID(r), BitVector.parse(val_4_bit));
370 public String getDirectly(Register r)
372 return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toString();
375 private static String regToStateID(Register r)
379 return "regs.c" + r.toBitString() + ".q";