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 wiresIncludingSubmodels.forEach(w -> w.addRedrawListener(() ->
160 wireDebugChangeSet.add(w.toString());
163 timeline.addEventAddedListener(te -> eventCounter++);
167 public void setDest(Am2901_Dest dest)
169 var bits = of(dest.ordinal(), 3);
170 I8.setToValueOf(bits.getLSBit(2));
171 I7.setToValueOf(bits.getLSBit(1));
172 I6.setToValueOf(bits.getLSBit(0));
176 public void setFunc(Am2901_Func func)
178 var bits = of(func.ordinal(), 3);
179 I5.setToValueOf(bits.getLSBit(2));
180 I4.setToValueOf(bits.getLSBit(1));
181 I3.setToValueOf(bits.getLSBit(0));
185 public void setSrc(Am2901_Src src)
187 var bits = of(src.ordinal(), 3);
188 I2.setToValueOf(bits.getLSBit(2));
189 I1.setToValueOf(bits.getLSBit(1));
190 I0.setToValueOf(bits.getLSBit(0));
194 public void setReg_A(String val_4_bit)
196 var bits = BitVector.parse(val_4_bit);
197 A3.setToValueOf(bits.getLSBit(3));
198 A2.setToValueOf(bits.getLSBit(2));
199 A1.setToValueOf(bits.getLSBit(1));
200 A0.setToValueOf(bits.getLSBit(0));
204 public void setReg_B(String val_4_bit)
206 var bits = BitVector.parse(val_4_bit);
207 B3.setToValueOf(bits.getLSBit(3));
208 B2.setToValueOf(bits.getLSBit(2));
209 B1.setToValueOf(bits.getLSBit(1));
210 B0.setToValueOf(bits.getLSBit(0));
214 public void setCarryIn(String val_1_bit)
216 Cn.setToValueOf(Bit.parse(val_1_bit));
220 public void setNotOutEnable(String val_1_bit)
222 throw new UnsupportedOperationException(); // TODO
226 public void setD(String val_4_bit)
228 var bits = BitVector.parse(val_4_bit);
229 D4.setToValueOf(bits.getLSBit(3));
230 D3.setToValueOf(bits.getLSBit(2));
231 D2.setToValueOf(bits.getLSBit(1));
232 D1.setToValueOf(bits.getLSBit(0));
236 public void setQ_0(String val_1_bit)
238 IQn.setToValueOf(Bit.parse(val_1_bit));
242 public void setQ_3(String val_1_bit)
244 IQn_3.setToValueOf(Bit.parse(val_1_bit));
248 public void setRAM_0(String val_1_bit)
250 IRAMn.setToValueOf(Bit.parse(val_1_bit));
254 public void setRAM_3(String val_1_bit)
256 IRAMn_3.setToValueOf(Bit.parse(val_1_bit));
260 public void clockOn(boolean isClockOn)
262 C.setState(isClockOn);
266 public String getQ_0()
268 return OQn.getDisplayedValue().toString();
272 public String getQ_3()
274 return OQn_3.getDisplayedValue().toString();
278 public String getRAM_0()
280 return ORAMn.getDisplayedValue().toString();
284 public String getRAM_3()
286 return ORAMn_3.getDisplayedValue().toString();
290 public String getNotP()
292 throw new UnsupportedOperationException(); // TODO
296 public String getNotG()
298 throw new UnsupportedOperationException(); // TODO
302 public String getCarryOut()
304 return Cn_4.getDisplayedValue().toString();
308 public String getSign()
310 return F3.getDisplayedValue().toString();
314 public String getZero()
316 return F_0.getDisplayedValue().toString();
320 public String getOverflow()
322 return OVR.getDisplayedValue().toString();
328 var y3 = Y4.getDisplayedValue();
329 var y2 = Y3.getDisplayedValue();
330 var y1 = Y2.getDisplayedValue();
331 var y0 = Y1.getDisplayedValue();
332 return y3.concat(y2).concat(y1).concat(y0).toString();
335 private void setField(String name, Object value)
339 Field f = TestableAm2901Impl.class.getDeclaredField(name);
340 f.setAccessible(true);
341 f.set(this, Objects.requireNonNull(value));
349 private static BitVector of(int value, int length)
351 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
353 for (int i = length - 1; i >= 0; i--)
355 mutator.setMSBit(i, Bit.lastBitOf(val));
358 return mutator.toBitVector();
362 public void setDirectly(Register r, String val_4_bit)
364 am2901.setHighLevelState(regToStateID(r), BitVector.parse(val_4_bit));
368 public String getDirectly(Register r)
370 return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toString();
373 private static String regToStateID(Register r)
377 return "regs.c" + r.toBitString() + ".q";