3db7b9d77b43da13ba26535254d9c2cfc37b3f1d
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / am2910 / GUIAm2910RegCntr.java
1 package net.mograsim.logic.model.am2900.components.am2910;
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.ZERO;
6
7 import java.util.Arrays;
8 import java.util.Map;
9
10 import net.mograsim.logic.core.types.Bit;
11 import net.mograsim.logic.core.wires.Wire.ReadEnd;
12 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
13 import net.mograsim.logic.model.model.ViewModelModifiable;
14 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
17
18 public class GUIAm2910RegCntr extends SimpleRectangularHardcodedGUIComponent
19 {
20         public GUIAm2910RegCntr(ViewModelModifiable model, String name)
21         {
22                 super(model, name, "Register/\nCounter");
23                 setSize(40, 40);
24                 addPin(new Pin(this, "D", 12, 20, 0), Usage.INPUT, Position.BOTTOM);
25                 addPin(new Pin(this, "_RLD", 1, 0, 5), Usage.INPUT, Position.RIGHT);
26                 addPin(new Pin(this, "RWE", 1, 0, 20), Usage.INPUT, Position.RIGHT);
27                 addPin(new Pin(this, "RDEC", 1, 0, 30), Usage.INPUT, Position.RIGHT);
28                 addPin(new Pin(this, "C", 1, 40, 20), Usage.INPUT, Position.LEFT);
29                 addPin(new Pin(this, "Y", 12, 20, 40), Usage.OUTPUT, Position.TOP);
30         }
31
32         @Override
33         protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
34         {
35                 Bit[] QC = (Bit[]) lastState;
36                 if (QC == null)
37                 {
38                         QC = new Bit[13];
39                         Arrays.fill(QC, U);
40                 }
41
42                 ReadEnd D = readEnds.get("D");
43                 ReadEnd _RLD = readEnds.get("_RLD");
44                 ReadEnd RWE = readEnds.get("RWE");
45                 ReadEnd RDEC = readEnds.get("RDEC");
46                 ReadEnd C = readEnds.get("C");
47                 ReadWriteEnd Y = readWriteEnds.get("Y");
48
49                 Bit oldCVal = QC[12];
50                 Bit CVal = C.getValue();
51
52                 // TODO handle U/X/Z
53                 if (oldCVal == ZERO && CVal == ONE)
54                 {
55                         if ((RDEC.getValue() == ZERO && RWE.getValue() == ONE) || _RLD.getValue() == ZERO)
56                                 System.arraycopy(D.getValues().getBits(), 0, QC, 0, 12);
57                         else if (RWE.getValue() == ONE)
58                         {
59                                 Bit carry = Bit.ZERO;
60                                 // TODO extract to helper. This code almost also exists in GUIinc12.
61                                 // TODO maybe invert loop direction
62                                 for (int i = 11; i >= 0; i--)
63                                 {
64                                         Bit a = QC[i];
65                                         Bit z;
66                                         if (a.isBinary() && carry.isBinary())
67                                         {
68                                                 boolean aBool = a == ONE;
69                                                 boolean carryBool = carry == ONE;
70                                                 z = !aBool ^ carryBool ? ONE : ZERO;
71                                                 carry = aBool || carryBool ? ONE : ZERO;
72                                         } else
73                                         {
74                                                 carry = carry.join(a);
75                                                 z = carry;
76                                         }
77                                         QC[i] = z;
78                                 }
79                         }
80                 }
81                 QC[12] = CVal;
82                 Y.feedSignals(Arrays.copyOfRange(QC, 0, 12));
83
84                 return QC;
85         }
86 }