1 package net.mograsim.logic.model.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.submodels.SubmodelComponent;
28 import net.mograsim.logic.model.model.wires.GUIWire;
29 import net.mograsim.logic.model.model.wires.Pin;
30 import net.mograsim.logic.model.modeladapter.LogicModelParameters;
31 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
32 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
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 am2901 = IndirectGUIComponentCreator.createComponent(viewModel, "GUIAm2901");
107 // guess which pins are outputs and which are inputs
108 // TODO this code exists four times... but it seems too "hacky" to put it in a helper class
109 List<String> inputPinNames = new ArrayList<>();
110 List<String> outputPinNames = new ArrayList<>();
111 for (Pin p : am2901.getPins().values())
112 if (p.getRelX() == 0)
113 inputPinNames.add(p.name);
115 outputPinNames.add(p.name);
117 HashMap<String, GUIManualSwitch> idSwitchMap = new HashMap<>();
118 for (String id : inputPinNames)
120 GUIManualSwitch sw = new GUIManualSwitch(viewModel);
121 new GUIWire(viewModel, am2901.getPin(id), sw.getOutputPin());
122 idSwitchMap.put(id, sw);
125 HashMap<String, GUIBitDisplay> idDisplayMap = new HashMap<>();
126 for (String id : outputPinNames)
128 GUIBitDisplay bd = new GUIBitDisplay(viewModel);
129 // bd.addRedrawListener(() -> System.out.println(id + " " + bd.getBitDisplay().getDisplayedValue()));
130 new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin());
131 idDisplayMap.put(id, bd);
133 // Create logic model
134 LogicModelParameters params = new LogicModelParameters();
135 params.gateProcessTime = 50;
136 params.wireTravelTime = 10;
137 timeline = ViewLogicModelAdapter.convert(viewModel, params);
138 // Bind switches/displays to this test class
139 for (var entry : idSwitchMap.entrySet())
140 setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getManualSwitch());
141 for (var entry : idDisplayMap.entrySet())
142 setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getBitDisplay());
145 HashSet<GUIWire> wiresIncludingSubmodels = new HashSet<>();
146 Queue<ViewModel> modelsToIterate = new LinkedList<>();
147 modelsToIterate.add(viewModel);
148 while (modelsToIterate.size() > 0)
150 ViewModel model = modelsToIterate.poll();
151 wiresIncludingSubmodels.addAll(model.getWiresByName().values());
152 for (GUIComponent comp : model.getComponentsByName().values())
153 if (comp instanceof SubmodelComponent)
154 modelsToIterate.offer(((SubmodelComponent) comp).submodel);
156 System.out.println(wiresIncludingSubmodels.size());
157 // viewModel.setRedrawHandler(() -> wiresIncludingSubmodels.forEach(w ->
161 // wireDebugChangeSet.add(w.toString());
164 timeline.addEventAddedListener(te -> eventCounter++);
168 public void setDest(Am2901_Dest dest)
170 var bits = of(dest.ordinal(), 3);
171 I8.setToValueOf(bits.getLSBit(2));
172 I7.setToValueOf(bits.getLSBit(1));
173 I6.setToValueOf(bits.getLSBit(0));
177 public void setFunc(Am2901_Func func)
179 var bits = of(func.ordinal(), 3);
180 I5.setToValueOf(bits.getLSBit(2));
181 I4.setToValueOf(bits.getLSBit(1));
182 I3.setToValueOf(bits.getLSBit(0));
186 public void setSrc(Am2901_Src src)
188 var bits = of(src.ordinal(), 3);
189 I2.setToValueOf(bits.getLSBit(2));
190 I1.setToValueOf(bits.getLSBit(1));
191 I0.setToValueOf(bits.getLSBit(0));
195 public void setReg_A(String val_4_bit)
197 var bits = BitVector.parse(val_4_bit);
198 A3.setToValueOf(bits.getLSBit(3));
199 A2.setToValueOf(bits.getLSBit(2));
200 A1.setToValueOf(bits.getLSBit(1));
201 A0.setToValueOf(bits.getLSBit(0));
205 public void setReg_B(String val_4_bit)
207 var bits = BitVector.parse(val_4_bit);
208 B3.setToValueOf(bits.getLSBit(3));
209 B2.setToValueOf(bits.getLSBit(2));
210 B1.setToValueOf(bits.getLSBit(1));
211 B0.setToValueOf(bits.getLSBit(0));
215 public void setCarryIn(String val_1_bit)
217 Cn.setToValueOf(Bit.parse(val_1_bit));
221 public void setNotOutEnable(String val_1_bit)
223 throw new UnsupportedOperationException(); // TODO
227 public void setD(String val_4_bit)
229 var bits = BitVector.parse(val_4_bit);
230 D4.setToValueOf(bits.getLSBit(3));
231 D3.setToValueOf(bits.getLSBit(2));
232 D2.setToValueOf(bits.getLSBit(1));
233 D1.setToValueOf(bits.getLSBit(0));
237 public void setQ_0(String val_1_bit)
239 IQn.setToValueOf(Bit.parse(val_1_bit));
243 public void setQ_3(String val_1_bit)
245 IQn_3.setToValueOf(Bit.parse(val_1_bit));
249 public void setRAM_0(String val_1_bit)
251 IRAMn.setToValueOf(Bit.parse(val_1_bit));
255 public void setRAM_3(String val_1_bit)
257 IRAMn_3.setToValueOf(Bit.parse(val_1_bit));
261 public void clockOn(boolean isClockOn)
263 C.setState(isClockOn);
267 public String getQ_0()
269 return OQn.getDisplayedValue().toString();
273 public String getQ_3()
275 return OQn_3.getDisplayedValue().toString();
279 public String getRAM_0()
281 return ORAMn.getDisplayedValue().toString();
285 public String getRAM_3()
287 return ORAMn_3.getDisplayedValue().toString();
291 public String getNotP()
293 throw new UnsupportedOperationException(); // TODO
297 public String getNotG()
299 throw new UnsupportedOperationException(); // TODO
303 public String getCarryOut()
305 return Cn_4.getDisplayedValue().toString();
309 public String getSign()
311 return F3.getDisplayedValue().toString();
315 public String getZero()
317 return F_0.getDisplayedValue().toString();
321 public String getOverflow()
323 return OVR.getDisplayedValue().toString();
329 var y3 = Y4.getDisplayedValue();
330 var y2 = Y3.getDisplayedValue();
331 var y1 = Y2.getDisplayedValue();
332 var y0 = Y1.getDisplayedValue();
333 return y3.concat(y2).concat(y1).concat(y0).toString();
336 private void setField(String name, Object value)
340 Field f = TestableAm2901Impl.class.getDeclaredField(name);
341 f.setAccessible(true);
342 f.set(this, Objects.requireNonNull(value));
350 private static BitVector of(int value, int length)
352 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
354 for (int i = length - 1; i >= 0; i--)
356 mutator.setMSBit(i, Bit.lastBitOf(val));
359 return mutator.toBitVector();
363 public void setDirectly(Register r, String val_4_bit)
365 am2901.setHighLevelState(regToStateID(r), BitVector.parse(val_4_bit));
369 public String getDirectly(Register r)
371 return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toString();
374 private static String regToStateID(Register r)
378 return "regs.c" + r.toBitString() + ".q";