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.HashMap;
7 import java.util.HashSet;
8 import java.util.LinkedList;
9 import java.util.Objects;
10 import java.util.Queue;
12 import java.util.TreeSet;
14 import net.mograsim.logic.core.components.BitDisplay;
15 import net.mograsim.logic.core.components.ManualSwitch;
16 import net.mograsim.logic.core.timeline.Timeline;
17 import net.mograsim.logic.core.types.Bit;
18 import net.mograsim.logic.core.types.BitVector;
19 import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
20 import net.mograsim.logic.ui.model.ViewModel;
21 import net.mograsim.logic.ui.model.ViewModelModifiable;
22 import net.mograsim.logic.ui.model.components.GUIBitDisplay;
23 import net.mograsim.logic.ui.model.components.GUIComponent;
24 import net.mograsim.logic.ui.model.components.GUIManualSwitch;
25 import net.mograsim.logic.ui.model.components.SubmodelComponent;
26 import net.mograsim.logic.ui.model.components.mi.nandbased.am2901.GUIAm2901;
27 import net.mograsim.logic.ui.model.wires.GUIWire;
28 import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
29 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
31 public class TestableAm2901Impl implements TestableAm2901
33 private GUIAm2901 am2901;
34 private Timeline timeline;
35 private ManualSwitch I8, I7, I6, I5, I4, I3, I2, I1, I0;
36 private ManualSwitch C;
37 private ManualSwitch Cn;
38 private ManualSwitch D1, D2, D3, D4;
39 private ManualSwitch A0, A1, A2, A3;
40 private ManualSwitch B0, B1, B2, B3;
41 private ManualSwitch IRAMn, IRAMn_3, IQn, IQn_3;
42 private BitDisplay Y1, Y2, Y3, Y4;
43 private BitDisplay F_0, Cn_4, OVR, F3;
44 private BitDisplay ORAMn, ORAMn_3, OQn, OQn_3;
46 private Set<String> wireDebugChangeSet;
47 private boolean debugWires = false;
48 public int debugEventThreshold = 10_000;
49 public int debugEventCount = 500;
51 private int eventCounter;
56 // Normal execution until completion or eventLimit
57 int eventLimit = debugEventThreshold;
60 while (eventCounter < eventLimit)
62 timeline.executeNext();
63 if (!timeline.hasNext())
65 System.out.println("run() took " + eventCounter + " events");
66 return Result.SUCCESS;
69 // Start debugging if event limit is reached
71 wireDebugChangeSet = new TreeSet<>();
72 Set<String> oldChangeSet;
73 // observe wire changes to detect, if we are really stuck in an endless loop
76 eventLimit += debugEventCount;
77 oldChangeSet = wireDebugChangeSet;
78 wireDebugChangeSet = new TreeSet<>();
79 while (eventCounter < eventLimit)
81 timeline.executeNext();
82 if (!timeline.hasNext())
84 // no endless loop, but more events needed than expected
85 System.out.println("run() took longer than expected: " + eventCounter);
86 return Result.SUCCESS;
89 } while (!oldChangeSet.equals(wireDebugChangeSet));
90 // if stuck, abort execution and print wires
91 System.err.print("Problematic Wire updates:");
92 wireDebugChangeSet.forEach(System.out::println);
93 System.err.println("run() failed: " + eventCounter);
94 return Result.OUT_OF_TIME;
97 @SuppressWarnings("unused")
102 ViewModelModifiable viewModel = new ViewModelModifiable();
103 am2901 = new GUIAm2901(viewModel);
105 HashMap<String, GUIManualSwitch> idSwitchMap = new HashMap<>();
106 for (String id : am2901.getInputPinNames())
108 GUIManualSwitch sw = new GUIManualSwitch(viewModel);
109 new GUIWire(viewModel, am2901.getPin(id), sw.getOutputPin());
110 idSwitchMap.put(id, sw);
113 HashMap<String, GUIBitDisplay> idDisplayMap = new HashMap<>();
114 for (String id : am2901.getOutputPinNames())
116 GUIBitDisplay bd = new GUIBitDisplay(viewModel);
117 new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin());
118 idDisplayMap.put(id, bd);
120 // Create logic model
121 LogicModelParameters params = new LogicModelParameters();
122 params.gateProcessTime = 50;
123 params.wireTravelTime = 10;
124 timeline = ViewLogicModelAdapter.convert(viewModel, params);
125 // Bind switches/displays to this test class
126 for (var entry : idSwitchMap.entrySet())
127 setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getManualSwitch());
128 for (var entry : idDisplayMap.entrySet())
129 setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getBitDisplay());
130 // Switch Clock off first
134 HashSet<GUIWire> wiresIncludingSubmodels = new HashSet<>();
135 Queue<ViewModel> modelsToIterate = new LinkedList<>();
136 modelsToIterate.add(viewModel);
137 while (modelsToIterate.size() > 0)
139 ViewModel model = modelsToIterate.poll();
140 wiresIncludingSubmodels.addAll(model.getWires());
141 for (GUIComponent comp : model.getComponents())
142 if (comp instanceof SubmodelComponent)
143 modelsToIterate.offer(((SubmodelComponent) comp).submodel);
145 wiresIncludingSubmodels.forEach(w -> w.addRedrawListener(() ->
149 System.out.println(w);
150 wireDebugChangeSet.add(w.toString());
153 timeline.addEventAddedListener(te -> eventCounter++);
157 public void setDest(Am2901_Dest dest)
159 var bits = of(dest.ordinal(), 3);
160 I8.setToValueOf(bits.getBit(0));
161 I7.setToValueOf(bits.getBit(1));
162 I6.setToValueOf(bits.getBit(2));
166 public void setFunc(Am2901_Func func)
168 var bits = of(func.ordinal(), 3);
169 I5.setToValueOf(bits.getBit(0));
170 I4.setToValueOf(bits.getBit(1));
171 I3.setToValueOf(bits.getBit(2));
175 public void setSrc(Am2901_Src src)
177 var bits = of(src.ordinal(), 3);
178 I2.setToValueOf(bits.getBit(0));
179 I1.setToValueOf(bits.getBit(1));
180 I0.setToValueOf(bits.getBit(2));
184 public void setReg_A(String val_4_bit)
186 var bits = BitVector.parse(val_4_bit);
187 A3.setToValueOf(bits.getBit(0));
188 A2.setToValueOf(bits.getBit(1));
189 A1.setToValueOf(bits.getBit(2));
190 A0.setToValueOf(bits.getBit(3));
194 public void setReg_B(String val_4_bit)
196 var bits = BitVector.parse(val_4_bit);
197 B3.setToValueOf(bits.getBit(0));
198 B2.setToValueOf(bits.getBit(1));
199 B1.setToValueOf(bits.getBit(2));
200 B0.setToValueOf(bits.getBit(3));
204 public void setCarryIn(String val_1_bit)
206 Cn.setToValueOf(Bit.parse(val_1_bit));
210 public void setNotOutEnable(String val_1_bit)
212 throw new UnsupportedOperationException(); // TODO
216 public void setD(String val_4_bit)
218 var bits = BitVector.parse(val_4_bit);
219 D4.setToValueOf(bits.getBit(0));
220 D3.setToValueOf(bits.getBit(1));
221 D2.setToValueOf(bits.getBit(2));
222 D1.setToValueOf(bits.getBit(3));
226 public void setQ_0(String val_1_bit)
228 IQn.setToValueOf(Bit.parse(val_1_bit));
232 public void setQ_3(String val_1_bit)
234 IQn_3.setToValueOf(Bit.parse(val_1_bit));
238 public void setRAM_0(String val_1_bit)
240 IRAMn.setToValueOf(Bit.parse(val_1_bit));
244 public void setRAM_3(String val_1_bit)
246 IRAMn_3.setToValueOf(Bit.parse(val_1_bit));
250 public void toogleClock()
256 public String getQ_0()
258 return OQn.getDisplayedValue().toString();
262 public String getQ_3()
264 return OQn_3.getDisplayedValue().toString();
268 public String getRAM_0()
270 return ORAMn.getDisplayedValue().toString();
274 public String getRAM_3()
276 return ORAMn_3.getDisplayedValue().toString();
280 public String getNotP()
282 throw new UnsupportedOperationException(); // TODO
286 public String getNotG()
288 throw new UnsupportedOperationException(); // TODO
292 public String getCarryOut()
294 return Cn_4.getDisplayedValue().toString();
298 public String getSign()
300 return F3.getDisplayedValue().toString();
304 public String getZero()
306 return F_0.getDisplayedValue().toString();
310 public String getOverflow()
312 return OVR.getDisplayedValue().toString();
318 var y3 = Y4.getDisplayedValue();
319 var y2 = Y3.getDisplayedValue();
320 var y1 = Y2.getDisplayedValue();
321 var y0 = Y1.getDisplayedValue();
322 return y3.concat(y2).concat(y1).concat(y0).toString();
325 private void setField(String name, Object value)
329 Field f = TestableAm2901Impl.class.getDeclaredField(name);
330 f.setAccessible(true);
331 f.set(this, Objects.requireNonNull(value));
339 private static BitVector of(int value, int length)
341 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
343 for (int i = length - 1; i >= 0; i--)
345 mutator.setBit(i, Bit.lastBitOf(val));
348 return mutator.toBitVector();