d7b32f01c84f19b8c42f66d3ec44c1d1fad66a3c
[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, "LD", 1, PinUsage.INPUT, 0, 20), Position.RIGHT);
29                 addPin(new Pin(model, this, "DEC", 1, PinUsage.INPUT, 0, 30), Position.RIGHT);
30                 addPin(new Pin(model, this, "C", 1, PinUsage.INPUT, 40, 20), Position.LEFT);
31                 addPin(new Pin(model, this, "Y", 12, PinUsage.OUTPUT, 20, 40), Position.TOP);
32
33                 init();
34         }
35
36         @Override
37         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
38         {
39                 Bit[] QC = castAndInitState(lastState);
40
41                 ReadEnd D = readEnds.get("D");
42                 ReadEnd LD = readEnds.get("LD");
43                 ReadEnd DEC = readEnds.get("DEC");
44                 ReadEnd C = readEnds.get("C");
45                 ReadWriteEnd Y = readWriteEnds.get("Y");
46
47                 Bit oldCVal = QC[12];
48                 Bit CVal = C.getValue();
49
50                 // TODO handle U/X/Z
51                 if (oldCVal == ZERO && CVal == ONE)
52                 {
53 //                      if (LD.getValue() == ONE || _RLD.getValue() == ZERO)
54                         if (LD.getValue() == ONE)
55                                 System.arraycopy(D.getValues().getBits(), 0, QC, 0, 12);
56                         else if (DEC.getValue() == ONE)
57                         {
58                                 Bit carry = Bit.ZERO;
59                                 // TODO extract to helper. This code almost also exists in Modelinc.
60                                 for (int i = 11; i >= 0; i--)
61                                 {
62                                         Bit a = QC[i];
63                                         QC[i] = a.xnor(carry);
64                                         carry = a.or(carry);
65                                 }
66                         }
67                 }
68                 QC[12] = CVal;
69                 Y.feedSignals(Arrays.copyOfRange(QC, 0, 12));
70
71                 return QC;
72         }
73
74         @Override
75         protected Object getHighLevelState(Object state, String stateID)
76         {
77                 Bit[] QC = castAndInitState(state);
78
79                 switch (stateID)
80                 {
81                 case "q":
82                         return BitVector.of(Arrays.copyOfRange(QC, 0, 12));
83                 default:
84                         return super.getHighLevelState(QC, stateID);
85                 }
86         }
87
88         @Override
89         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
90         {
91                 Bit[] QC = castAndInitState(lastState);
92
93                 switch (stateID)
94                 {
95                 case "q":
96                         BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
97                         if (newHighLevelStateCasted.length() != 12)
98                                 throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
99                         System.arraycopy(newHighLevelStateCasted.getBits(), 0, QC, 0, 12);
100                         return QC;
101                 default:
102                         return super.setHighLevelState(QC, stateID, newHighLevelState);
103                 }
104         }
105
106         private static Bit[] castAndInitState(Object lastState)
107         {
108                 Bit[] QC = (Bit[]) lastState;
109                 if (QC == null)
110                 {
111                         QC = new Bit[13];
112                         Arrays.fill(QC, U);
113                 }
114                 return QC;
115         }
116
117         static
118         {
119                 IndirectModelComponentCreator.setComponentSupplier(ModelAm2910RegCntr.class.getCanonicalName(),
120                                 (m, p, n) -> new ModelAm2910RegCntr(m, n));
121         }
122 }