20925444619697d447322cc455d6ccb6e843a280
[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.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
19
20 public class GUIinc12 extends SimpleRectangularHardcodedGUIComponent
21 {
22         public GUIinc12(ViewModelModifiable model, String name)
23         {
24                 super(model, name, "Incrementer");
25                 setSize(40, 20);
26                 addPin(new Pin(this, "A", 12, 20, 20), Usage.INPUT, Position.TOP);
27                 addPin(new Pin(this, "CI", 1, 40, 10), Usage.INPUT, Position.LEFT);
28                 addPin(new Pin(this, "Y", 12, 20, 0), Usage.OUTPUT, Position.BOTTOM);
29         }
30
31         @Override
32         protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
33         {
34                 Bit[] ABits = readEnds.get("A").getValues().getBits();
35                 Bit CIVal = readEnds.get("CI").getValue();
36                 Bit[] YBits = new Bit[12];
37                 if (CIVal == X)
38                         Arrays.fill(YBits, X);
39                 else if (CIVal == U)
40                         Arrays.fill(YBits, U);
41                 else if (CIVal == Z)
42                         Arrays.fill(YBits, X);
43                 else if (CIVal == ZERO)
44                         YBits = ABits;
45                 else
46                 {
47                         Bit carry = Bit.ONE;
48                         // TODO extract to helper. This code almost also exists in GUIAM2910RegCntr.
49                         // TODO maybe invert loop direction
50                         for (int i = 11; i >= 0; i--)
51                         {
52                                 Bit a = ABits[i];
53                                 Bit z;
54                                 if (a.isBinary() && carry.isBinary())
55                                 {
56                                         boolean aBool = a == ONE;
57                                         boolean carryBool = carry == ONE;
58                                         z = aBool ^ carryBool ? ONE : ZERO;
59                                         carry = aBool && carryBool ? ONE : ZERO;
60                                 } else
61                                 {
62                                         carry = carry.join(a);
63                                         z = carry;
64                                 }
65                                 YBits[i] = z;
66                         }
67                 }
68                 readWriteEnds.get("Y").feedSignals(YBits);
69                 return null;
70         }
71
72 }