0b94583c2be5e4f42b3afc0566c55198a05a535b
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / GUIinc12.java
1 package net.mograsim.logic.model.am2900.components;
2
3 import static net.mograsim.logic.core.types.Bit.ONE;
4 import static net.mograsim.logic.core.types.Bit.U;
5 import static net.mograsim.logic.core.types.Bit.X;
6 import static net.mograsim.logic.core.types.Bit.Z;
7 import static net.mograsim.logic.core.types.Bit.ZERO;
8
9 import java.util.Arrays;
10 import java.util.Map;
11
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.wires.Wire.ReadEnd;
14 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
16 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
19 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
20
21 public class GUIinc12 extends SimpleRectangularHardcodedGUIComponent
22 {
23         public GUIinc12(ViewModelModifiable model, String name)
24         {
25                 super(model, name, "Incrementer");
26                 setSize(40, 20);
27                 addPin(new Pin(this, "A", 12, 20, 20), Usage.INPUT, Position.TOP);
28                 addPin(new Pin(this, "CI", 1, 40, 10), Usage.INPUT, Position.LEFT);
29                 addPin(new Pin(this, "Y", 12, 20, 0), Usage.OUTPUT, Position.BOTTOM);
30         }
31
32         @Override
33         protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
34         {
35                 Bit[] ABits = readEnds.get("A").getValues().getBits();
36                 Bit CIVal = readEnds.get("CI").getValue();
37                 Bit[] YBits = new Bit[12];
38                 if (CIVal == X)
39                         Arrays.fill(YBits, X);
40                 else if (CIVal == U)
41                         Arrays.fill(YBits, U);
42                 else if (CIVal == Z)
43                         Arrays.fill(YBits, X);
44                 else if (CIVal == ZERO)
45                         YBits = ABits;
46                 else
47                 {
48                         Bit carry = Bit.ONE;
49                         // TODO extract to helper. This code almost also exists in GUIAM2910RegCntr.
50                         // TODO maybe invert loop direction
51                         for (int i = 11; i >= 0; i--)
52                         {
53                                 Bit a = ABits[i];
54                                 Bit z;
55                                 if (a.isBinary() && carry.isBinary())
56                                 {
57                                         boolean aBool = a == ONE;
58                                         boolean carryBool = carry == ONE;
59                                         z = aBool ^ carryBool ? ONE : ZERO;
60                                         carry = aBool && carryBool ? ONE : ZERO;
61                                 } else
62                                 {
63                                         carry = carry.join(a);
64                                         z = carry;
65                                 }
66                                 YBits[i] = z;
67                         }
68                 }
69                 readWriteEnds.get("Y").feedSignals(YBits);
70                 return null;
71         }
72
73         static
74         {
75                 IndirectGUIComponentCreator.setComponentSupplier(GUIinc12.class.getCanonicalName(), (m, p, n) -> new GUIinc12(m, n));
76         }
77 }