5a13ebc109cc20ce27fb51151a402adf6a6c1662
[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.types.BitVector;
12 import net.mograsim.logic.core.wires.Wire.ReadEnd;
13 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
14 import net.mograsim.logic.model.model.ViewModelModifiable;
15 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
16 import net.mograsim.logic.model.model.wires.Pin;
17 import net.mograsim.logic.model.model.wires.PinUsage;
18 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
19 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
20
21 public class GUIAm2910RegCntr extends SimpleRectangularHardcodedGUIComponent
22 {
23         public GUIAm2910RegCntr(ViewModelModifiable model, String name)
24         {
25                 super(model, name, "Register/\nCounter");
26                 setSize(40, 40);
27                 addPin(new Pin(this, "D", 12, PinUsage.INPUT, 20, 0), Position.BOTTOM);
28                 addPin(new Pin(this, "_RLD", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
29                 addPin(new Pin(this, "WE", 1, PinUsage.INPUT, 0, 20), Position.RIGHT);
30                 addPin(new Pin(this, "DEC", 1, PinUsage.INPUT, 0, 30), Position.RIGHT);
31                 addPin(new Pin(this, "C", 1, PinUsage.INPUT, 40, 20), Position.LEFT);
32                 addPin(new Pin(this, "Y", 12, PinUsage.OUTPUT, 20, 40), Position.TOP);
33         }
34
35         @Override
36         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
37         {
38                 Bit[] QC = (Bit[]) lastState;
39                 if (QC == null)
40                 {
41                         QC = new Bit[13];
42                         Arrays.fill(QC, U);
43                 }
44
45                 ReadEnd D = readEnds.get("D");
46                 ReadEnd _RLD = readEnds.get("_RLD");
47                 ReadEnd WE = readEnds.get("WE");
48                 ReadEnd DEC = readEnds.get("DEC");
49                 ReadEnd C = readEnds.get("C");
50                 ReadWriteEnd Y = readWriteEnds.get("Y");
51
52                 Bit oldCVal = QC[12];
53                 Bit CVal = C.getValue();
54
55                 // TODO handle U/X/Z
56                 if (oldCVal == ZERO && CVal == ONE)
57                 {
58                         if ((DEC.getValue() == ZERO && WE.getValue() == ONE) || _RLD.getValue() == ZERO)
59                                 System.arraycopy(D.getValues().getBits(), 0, QC, 0, 12);
60                         else if (WE.getValue() == ONE)
61                         {
62                                 Bit carry = Bit.ZERO;
63                                 // TODO extract to helper. This code almost also exists in GUIinc12.
64                                 // TODO maybe invert loop direction
65                                 for (int i = 11; i >= 0; i--)
66                                 {
67                                         Bit a = QC[i];
68                                         QC[i] = a.xnor(carry);
69                                         carry = a.or(carry);
70                                 }
71                         }
72                 }
73                 QC[12] = CVal;
74                 Y.feedSignals(Arrays.copyOfRange(QC, 0, 12));
75
76                 return QC;
77         }
78
79         @Override
80         protected Object getHighLevelState(Object state, String stateID)
81         {
82                 switch (stateID)
83                 {
84                 case "q":
85                         return BitVector.of(Arrays.copyOfRange((Bit[]) state, 0, 12));
86                 default:
87                         return super.getHighLevelState(state, stateID);
88                 }
89         }
90
91         @Override
92         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
93         {
94                 switch (stateID)
95                 {
96                 case "q":
97                         BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
98                         if (newHighLevelStateCasted.length() != 12)
99                                 throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
100                         System.arraycopy(newHighLevelStateCasted.getBits(), 0, lastState, 0, 12);
101                         return lastState;
102                 default:
103                         return super.setHighLevelState(lastState, stateID, newHighLevelState);
104                 }
105         }
106
107         static
108         {
109                 IndirectGUIComponentCreator.setComponentSupplier(GUIAm2910RegCntr.class.getCanonicalName(),
110                                 (m, p, n) -> new GUIAm2910RegCntr(m, n));
111         }
112 }