455a03cfbeb2c501683f5edb4369f572cdba1690
[Mograsim.git] / net.mograsim.logic.ui.am2900 / test / net / mograsim / logic / ui / am2900 / TestableAm2901Impl.java
1 package net.mograsim.logic.ui.am2900;
2
3 import static org.junit.jupiter.api.Assertions.fail;
4
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;
13 import java.util.Set;
14 import java.util.TreeSet;
15
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.ui.model.ViewModel;
23 import net.mograsim.logic.ui.model.ViewModelModifiable;
24 import net.mograsim.logic.ui.model.components.GUIComponent;
25 import net.mograsim.logic.ui.model.components.atomic.GUIBitDisplay;
26 import net.mograsim.logic.ui.model.components.atomic.GUIManualSwitch;
27 import net.mograsim.logic.ui.model.components.mi.nandbased.am2901.GUIAm2901;
28 import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
29 import net.mograsim.logic.ui.model.wires.GUIWire;
30 import net.mograsim.logic.ui.model.wires.Pin;
31 import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
32 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
33
34 public class TestableAm2901Impl implements TestableAm2901
35 {
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;
48
49         private Set<String> wireDebugChangeSet;
50         private boolean debugWires = false;
51         public int debugEventThreshold = 10_000;
52         public int debugEventCount = 500;
53
54         private int eventCounter;
55
56         @Override
57         public Result run()
58         {
59                 // Normal execution until completion or eventLimit
60                 int eventLimit = debugEventThreshold;
61                 eventCounter = 0;
62                 debugWires = false;
63                 while (eventCounter < eventLimit)
64                 {
65                         timeline.executeNext();
66                         if (!timeline.hasNext())
67                         {
68 //                              System.out.println("run() took " + eventCounter + " events");
69                                 return Result.SUCCESS;
70                         }
71                 }
72                 // Start debugging if event limit is reached
73                 debugWires = true;
74                 wireDebugChangeSet = new TreeSet<>();
75                 Set<String> oldChangeSet;
76                 // observe wire changes to detect, if we are really stuck in an endless loop
77                 do
78                 {
79                         eventLimit += debugEventCount;
80                         oldChangeSet = wireDebugChangeSet;
81                         wireDebugChangeSet = new TreeSet<>();
82                         while (eventCounter < eventLimit)
83                         {
84                                 timeline.executeNext();
85                                 if (!timeline.hasNext())
86                                 {
87                                         // no endless loop, but more events needed than expected
88                                         System.out.println("run() took longer than expected: " + eventCounter);
89                                         return Result.SUCCESS;
90                                 }
91                         }
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;
98         }
99
100         @SuppressWarnings("unused")
101         @Override
102         public void setup()
103         {
104                 // Create view model
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);
116                         else
117                                 outputPinNames.add(p.name);
118                 // Get switches
119                 HashMap<String, GUIManualSwitch> idSwitchMap = new HashMap<>();
120                 for (String id : inputPinNames)
121                 {
122                         GUIManualSwitch sw = new GUIManualSwitch(viewModel);
123                         new GUIWire(viewModel, am2901.getPin(id), sw.getOutputPin());
124                         idSwitchMap.put(id, sw);
125                 }
126                 // Get displays
127                 HashMap<String, GUIBitDisplay> idDisplayMap = new HashMap<>();
128                 for (String id : outputPinNames)
129                 {
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);
134                 }
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());
145
146                 // Debug code
147                 HashSet<GUIWire> wiresIncludingSubmodels = new HashSet<>();
148                 Queue<ViewModel> modelsToIterate = new LinkedList<>();
149                 modelsToIterate.add(viewModel);
150                 while (modelsToIterate.size() > 0)
151                 {
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);
157                 }
158                 wiresIncludingSubmodels.forEach(w -> w.addRedrawListener(() ->
159                 {
160                         if (debugWires)
161                         {
162                                 wireDebugChangeSet.add(w.toString());
163                         }
164                 }));
165                 timeline.addEventAddedListener(te -> eventCounter++);
166         }
167
168         @Override
169         public void setDest(Am2901_Dest dest)
170         {
171                 var bits = of(dest.ordinal(), 3);
172                 I8.setToValueOf(bits.getBit(0));
173                 I7.setToValueOf(bits.getBit(1));
174                 I6.setToValueOf(bits.getBit(2));
175         }
176
177         @Override
178         public void setFunc(Am2901_Func func)
179         {
180                 var bits = of(func.ordinal(), 3);
181                 I5.setToValueOf(bits.getBit(0));
182                 I4.setToValueOf(bits.getBit(1));
183                 I3.setToValueOf(bits.getBit(2));
184         }
185
186         @Override
187         public void setSrc(Am2901_Src src)
188         {
189                 var bits = of(src.ordinal(), 3);
190                 I2.setToValueOf(bits.getBit(0));
191                 I1.setToValueOf(bits.getBit(1));
192                 I0.setToValueOf(bits.getBit(2));
193         }
194
195         @Override
196         public void setReg_A(String val_4_bit)
197         {
198                 var bits = BitVector.parseMSBFirst(val_4_bit);
199                 A3.setToValueOf(bits.getBit(3));
200                 A2.setToValueOf(bits.getBit(2));
201                 A1.setToValueOf(bits.getBit(1));
202                 A0.setToValueOf(bits.getBit(0));
203         }
204
205         @Override
206         public void setReg_B(String val_4_bit)
207         {
208                 var bits = BitVector.parseMSBFirst(val_4_bit);
209                 B3.setToValueOf(bits.getBit(3));
210                 B2.setToValueOf(bits.getBit(2));
211                 B1.setToValueOf(bits.getBit(1));
212                 B0.setToValueOf(bits.getBit(0));
213         }
214
215         @Override
216         public void setCarryIn(String val_1_bit)
217         {
218                 Cn.setToValueOf(Bit.parse(val_1_bit));
219         }
220
221         @Override
222         public void setNotOutEnable(String val_1_bit)
223         {
224                 throw new UnsupportedOperationException(); // TODO
225         }
226
227         @Override
228         public void setD(String val_4_bit)
229         {
230                 var bits = BitVector.parseMSBFirst(val_4_bit);
231                 D4.setToValueOf(bits.getBit(3));
232                 D3.setToValueOf(bits.getBit(2));
233                 D2.setToValueOf(bits.getBit(1));
234                 D1.setToValueOf(bits.getBit(0));
235         }
236
237         @Override
238         public void setQ_0(String val_1_bit)
239         {
240                 IQn.setToValueOf(Bit.parse(val_1_bit));
241         }
242
243         @Override
244         public void setQ_3(String val_1_bit)
245         {
246                 IQn_3.setToValueOf(Bit.parse(val_1_bit));
247         }
248
249         @Override
250         public void setRAM_0(String val_1_bit)
251         {
252                 IRAMn.setToValueOf(Bit.parse(val_1_bit));
253         }
254
255         @Override
256         public void setRAM_3(String val_1_bit)
257         {
258                 IRAMn_3.setToValueOf(Bit.parse(val_1_bit));
259         }
260
261         @Override
262         public void clockOn(boolean isClockOn)
263         {
264                 C.setState(isClockOn);
265         }
266
267         @Override
268         public String getQ_0()
269         {
270                 return OQn.getDisplayedValue().toString();
271         }
272
273         @Override
274         public String getQ_3()
275         {
276                 return OQn_3.getDisplayedValue().toString();
277         }
278
279         @Override
280         public String getRAM_0()
281         {
282                 return ORAMn.getDisplayedValue().toString();
283         }
284
285         @Override
286         public String getRAM_3()
287         {
288                 return ORAMn_3.getDisplayedValue().toString();
289         }
290
291         @Override
292         public String getNotP()
293         {
294                 throw new UnsupportedOperationException(); // TODO
295         }
296
297         @Override
298         public String getNotG()
299         {
300                 throw new UnsupportedOperationException(); // TODO
301         }
302
303         @Override
304         public String getCarryOut()
305         {
306                 return Cn_4.getDisplayedValue().toString();
307         }
308
309         @Override
310         public String getSign()
311         {
312                 return F3.getDisplayedValue().toString();
313         }
314
315         @Override
316         public String getZero()
317         {
318                 return F_0.getDisplayedValue().toString();
319         }
320
321         @Override
322         public String getOverflow()
323         {
324                 return OVR.getDisplayedValue().toString();
325         }
326
327         @Override
328         public String getY()
329         {
330                 var y3 = Y4.getDisplayedValue();
331                 var y2 = Y3.getDisplayedValue();
332                 var y1 = Y2.getDisplayedValue();
333                 var y0 = Y1.getDisplayedValue();
334                 return y0.concat(y1).concat(y2).concat(y3).toBitStringMSBFirst();
335         }
336
337         private void setField(String name, Object value)
338         {
339                 try
340                 {
341                         Field f = TestableAm2901Impl.class.getDeclaredField(name);
342                         f.setAccessible(true);
343                         f.set(this, Objects.requireNonNull(value));
344                 }
345                 catch (Exception e)
346                 {
347                         fail(e);
348                 }
349         }
350
351         private static BitVector of(int value, int length)
352         {
353                 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
354                 int val = value;
355                 for (int i = length - 1; i >= 0; i--)
356                 {
357                         mutator.setBit(i, Bit.lastBitOf(val));
358                         val >>>= 1;
359                 }
360                 return mutator.toBitVector();
361         }
362
363         @Override
364         public void setDirectly(Register r, String val_4_bit)
365         {
366                 am2901.setHighLevelState(regToStateID(r), BitVector.parseMSBFirst(val_4_bit));
367         }
368
369         @Override
370         public String getDirectly(Register r)
371         {
372                 return ((BitVector) am2901.getHighLevelState(regToStateID(r))).toBitStringMSBFirst();
373         }
374
375         private static String regToStateID(Register r)
376         {
377                 if (r == Register.Q)
378                         return "qreg.q";
379                 return "regs.c" + r.toBitString() + ".q";
380         }
381 }