Implemented GUIAm2910RegCntr
[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                 if (oldCVal == ZERO && CVal == ONE && _RLD.getValue() == ZERO && RWE.getValue() == ONE)
53                 {
54                         if (RDEC.getValue() == ONE)
55                         {
56                                 Bit carry = Bit.ZERO;
57                                 // TODO maybe invert loop direction
58                                 for (int i = 12; i >= 0; i--)
59                                 {
60                                         Bit a = QC[i];
61                                         Bit z;
62                                         if (a.isBinary() && carry.isBinary())
63                                         {
64                                                 boolean aBool = a == ONE;
65                                                 boolean carryBool = carry == ONE;
66                                                 z = !aBool ^ carryBool ? ONE : ZERO;
67                                                 carry = aBool || carryBool ? ONE : ZERO;
68                                         } else
69                                         {
70                                                 carry = carry.join(a);
71                                                 z = carry;
72                                         }
73                                         QC[i] = z;
74                                 }
75                         } else
76                                 System.arraycopy(D.getValues().getBits(), 0, QC, 0, 12);
77                 }
78                 QC[12] = CVal;
79                 Y.feedSignals(Arrays.copyOfRange(QC, 0, 12));
80
81                 return QC;
82         }
83 }