Am2910: Changed communication between reg and instrdecode
[Mograsim.git] / plugins / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / am2910 / ModelAm2910RegCntr.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.CoreWire.ReadEnd;
13 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
14 import net.mograsim.logic.model.model.LogicModelModifiable;
15 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedModelComponent;
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.IndirectModelComponentCreator;
19 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
20
21 public class ModelAm2910RegCntr extends SimpleRectangularHardcodedModelComponent
22 {
23         public ModelAm2910RegCntr(LogicModelModifiable model, String name)
24         {
25                 super(model, "Am2910RegCntr", name, "Register/\nCounter", false);
26                 setSize(40, 40);
27                 addPin(new Pin(model, this, "D", 12, PinUsage.INPUT, 20, 0), Position.BOTTOM);
28                 addPin(new Pin(model, this, "_RLD", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
29                 addPin(new Pin(model, this, "LD", 1, PinUsage.INPUT, 0, 20), Position.RIGHT);
30                 addPin(new Pin(model, this, "DEC", 1, PinUsage.INPUT, 0, 30), Position.RIGHT);
31                 addPin(new Pin(model, this, "C", 1, PinUsage.INPUT, 40, 20), Position.LEFT);
32                 addPin(new Pin(model, this, "Y", 12, PinUsage.OUTPUT, 20, 40), Position.TOP);
33
34                 init();
35         }
36
37         @Override
38         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
39         {
40                 Bit[] QC = castAndInitState(lastState);
41
42                 ReadEnd D = readEnds.get("D");
43                 ReadEnd _RLD = readEnds.get("_RLD");
44                 ReadEnd LD = readEnds.get("LD");
45                 ReadEnd DEC = readEnds.get("DEC");
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 (LD.getValue() == ONE || _RLD.getValue() == ZERO)
56                                 System.arraycopy(D.getValues().getBits(), 0, QC, 0, 12);
57                         else if (DEC.getValue() == ONE)
58                         {
59                                 Bit carry = Bit.ZERO;
60                                 // TODO extract to helper. This code almost also exists in Modelinc.
61                                 for (int i = 11; i >= 0; i--)
62                                 {
63                                         Bit a = QC[i];
64                                         QC[i] = a.xnor(carry);
65                                         carry = a.or(carry);
66                                 }
67                         }
68                 }
69                 QC[12] = CVal;
70                 Y.feedSignals(Arrays.copyOfRange(QC, 0, 12));
71
72                 return QC;
73         }
74
75         @Override
76         protected Object getHighLevelState(Object state, String stateID)
77         {
78                 Bit[] QC = castAndInitState(state);
79
80                 switch (stateID)
81                 {
82                 case "q":
83                         return BitVector.of(Arrays.copyOfRange(QC, 0, 12));
84                 default:
85                         return super.getHighLevelState(QC, stateID);
86                 }
87         }
88
89         @Override
90         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
91         {
92                 Bit[] QC = castAndInitState(lastState);
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, QC, 0, 12);
101                         return QC;
102                 default:
103                         return super.setHighLevelState(QC, stateID, newHighLevelState);
104                 }
105         }
106
107         private static Bit[] castAndInitState(Object lastState)
108         {
109                 Bit[] QC = (Bit[]) lastState;
110                 if (QC == null)
111                 {
112                         QC = new Bit[13];
113                         Arrays.fill(QC, U);
114                 }
115                 return QC;
116         }
117
118         static
119         {
120                 IndirectModelComponentCreator.setComponentSupplier(ModelAm2910RegCntr.class.getCanonicalName(),
121                                 (m, p, n) -> new ModelAm2910RegCntr(m, n));
122         }
123 }