a6eb79107b9794a03b18648721b22146a0428f2f
[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.HashMap;
7 import java.util.HashSet;
8 import java.util.LinkedList;
9 import java.util.Objects;
10 import java.util.Queue;
11 import java.util.Set;
12 import java.util.TreeSet;
13
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;
30
31 public class TestableAm2901Impl implements TestableAm2901
32 {
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;
45
46         private Set<String> wireDebugChangeSet;
47         private boolean debugWires = false;
48         public int debugEventThreshold = 10_000;
49         public int debugEventCount = 500;
50
51         private int eventCounter;
52
53         @Override
54         public Result run()
55         {
56                 // Normal execution until completion or eventLimit
57                 int eventLimit = debugEventThreshold;
58                 eventCounter = 0;
59                 debugWires = false;
60                 while (eventCounter < eventLimit)
61                 {
62                         timeline.executeNext();
63                         if (!timeline.hasNext())
64                         {
65                                 System.out.println("run() took " + eventCounter + " events");
66                                 return Result.SUCCESS;
67                         }
68                 }
69                 // Start debugging if event limit is reached
70                 debugWires = true;
71                 wireDebugChangeSet = new TreeSet<>();
72                 Set<String> oldChangeSet;
73                 // observe wire changes to detect, if we are really stuck in an endless loop
74                 do
75                 {
76                         eventLimit += debugEventCount;
77                         oldChangeSet = wireDebugChangeSet;
78                         wireDebugChangeSet = new TreeSet<>();
79                         while (eventCounter < eventLimit)
80                         {
81                                 timeline.executeNext();
82                                 if (!timeline.hasNext())
83                                 {
84                                         // no endless loop, but more events needed than expected
85                                         System.out.println("run() took longer than expected: " + eventCounter);
86                                         return Result.SUCCESS;
87                                 }
88                         }
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;
95         }
96
97         @SuppressWarnings("unused")
98         @Override
99         public void setup()
100         {
101                 // Create view model
102                 ViewModelModifiable viewModel = new ViewModelModifiable();
103                 am2901 = new GUIAm2901(viewModel);
104                 // Get switches
105                 HashMap<String, GUIManualSwitch> idSwitchMap = new HashMap<>();
106                 for (String id : am2901.getInputPinNames())
107                 {
108                         GUIManualSwitch sw = new GUIManualSwitch(viewModel);
109                         new GUIWire(viewModel, am2901.getPin(id), sw.getOutputPin());
110                         idSwitchMap.put(id, sw);
111                 }
112                 // Get displays
113                 HashMap<String, GUIBitDisplay> idDisplayMap = new HashMap<>();
114                 for (String id : am2901.getOutputPinNames())
115                 {
116                         GUIBitDisplay bd = new GUIBitDisplay(viewModel);
117                         new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin());
118                         idDisplayMap.put(id, bd);
119                 }
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
131                 C.switchOff();
132
133                 // Debug code
134                 HashSet<GUIWire> wiresIncludingSubmodels = new HashSet<>();
135                 Queue<ViewModel> modelsToIterate = new LinkedList<>();
136                 modelsToIterate.add(viewModel);
137                 while (modelsToIterate.size() > 0)
138                 {
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);
144                 }
145                 wiresIncludingSubmodels.forEach(w -> w.addRedrawListener(() ->
146                 {
147                         if (debugWires)
148                         {
149                                 System.out.println(w);
150                                 wireDebugChangeSet.add(w.toString());
151                         }
152                 }));
153                 timeline.addEventAddedListener(te -> eventCounter++);
154         }
155
156         @Override
157         public void setDest(Am2901_Dest dest)
158         {
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));
163         }
164
165         @Override
166         public void setFunc(Am2901_Func func)
167         {
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));
172         }
173
174         @Override
175         public void setSrc(Am2901_Src src)
176         {
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));
181         }
182
183         @Override
184         public void setReg_A(String val_4_bit)
185         {
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));
191         }
192
193         @Override
194         public void setReg_B(String val_4_bit)
195         {
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));
201         }
202
203         @Override
204         public void setCarryIn(String val_1_bit)
205         {
206                 Cn.setToValueOf(Bit.parse(val_1_bit));
207         }
208
209         @Override
210         public void setNotOutEnable(String val_1_bit)
211         {
212                 throw new UnsupportedOperationException(); // TODO
213         }
214
215         @Override
216         public void setD(String val_4_bit)
217         {
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));
223         }
224
225         @Override
226         public void setQ_0(String val_1_bit)
227         {
228                 IQn.setToValueOf(Bit.parse(val_1_bit));
229         }
230
231         @Override
232         public void setQ_3(String val_1_bit)
233         {
234                 IQn_3.setToValueOf(Bit.parse(val_1_bit));
235         }
236
237         @Override
238         public void setRAM_0(String val_1_bit)
239         {
240                 IRAMn.setToValueOf(Bit.parse(val_1_bit));
241         }
242
243         @Override
244         public void setRAM_3(String val_1_bit)
245         {
246                 IRAMn_3.setToValueOf(Bit.parse(val_1_bit));
247         }
248
249         @Override
250         public void toogleClock()
251         {
252                 C.toggle();
253         }
254
255         @Override
256         public String getQ_0()
257         {
258                 return OQn.getDisplayedValue().toString();
259         }
260
261         @Override
262         public String getQ_3()
263         {
264                 return OQn_3.getDisplayedValue().toString();
265         }
266
267         @Override
268         public String getRAM_0()
269         {
270                 return ORAMn.getDisplayedValue().toString();
271         }
272
273         @Override
274         public String getRAM_3()
275         {
276                 return ORAMn_3.getDisplayedValue().toString();
277         }
278
279         @Override
280         public String getNotP()
281         {
282                 throw new UnsupportedOperationException(); // TODO
283         }
284
285         @Override
286         public String getNotG()
287         {
288                 throw new UnsupportedOperationException(); // TODO
289         }
290
291         @Override
292         public String getCarryOut()
293         {
294                 return Cn_4.getDisplayedValue().toString();
295         }
296
297         @Override
298         public String getSign()
299         {
300                 return F3.getDisplayedValue().toString();
301         }
302
303         @Override
304         public String getZero()
305         {
306                 return F_0.getDisplayedValue().toString();
307         }
308
309         @Override
310         public String getOverflow()
311         {
312                 return OVR.getDisplayedValue().toString();
313         }
314
315         @Override
316         public String getY()
317         {
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();
323         }
324
325         private void setField(String name, Object value)
326         {
327                 try
328                 {
329                         Field f = TestableAm2901Impl.class.getDeclaredField(name);
330                         f.setAccessible(true);
331                         f.set(this, Objects.requireNonNull(value));
332                 }
333                 catch (Exception e)
334                 {
335                         fail(e);
336                 }
337         }
338
339         private static BitVector of(int value, int length)
340         {
341                 BitVectorMutator mutator = BitVectorMutator.ofLength(length);
342                 int val = value;
343                 for (int i = length - 1; i >= 0; i--)
344                 {
345                         mutator.setBit(i, Bit.lastBitOf(val));
346                         val >>>= 1;
347                 }
348                 return mutator.toBitVector();
349         }
350 }