The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / Modeldff12.java
1 package net.mograsim.logic.model.am2900.components;
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 Modeldff12 extends SimpleRectangularHardcodedModelComponent
22 {
23         public Modeldff12(LogicModelModifiable model, String name)
24         {
25                 super(model, "dff12", name, "D flip flop\n12 bits", false);
26                 setSize(40, 20);
27                 addPin(new Pin(model, this, "D", 12, PinUsage.INPUT, 20, 20), Position.TOP);
28                 addPin(new Pin(model, this, "C", 1, PinUsage.INPUT, 0, 10), Position.RIGHT);
29                 addPin(new Pin(model, this, "Y", 12, PinUsage.OUTPUT, 20, 0), Position.BOTTOM);
30
31                 init();
32         }
33
34         @Override
35         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
36         {
37                 Bit[] QC = (Bit[]) lastState;
38                 if (QC == null)
39                 {
40                         QC = new Bit[13];
41                         Arrays.fill(QC, U);
42                 }
43
44                 Bit CVal = readEnds.get("C").getValue();
45
46                 if (QC[12] == ZERO && CVal == ONE)
47                         System.arraycopy(readEnds.get("D").getValues().getBits(), 0, QC, 0, 12);
48
49                 readWriteEnds.get("Y").feedSignals(Arrays.copyOfRange(QC, 0, 12));
50                 QC[12] = CVal;
51
52                 return QC;
53         }
54
55         @Override
56         protected Object getHighLevelState(Object state, String stateID)
57         {
58                 switch (stateID)
59                 {
60                 case "q":
61                         return BitVector.of(Arrays.copyOfRange((Bit[]) state, 0, 12));
62                 default:
63                         return super.getHighLevelState(state, stateID);
64                 }
65         }
66
67         @Override
68         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
69         {
70                 switch (stateID)
71                 {
72                 case "q":
73                         BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
74                         if (newHighLevelStateCasted.length() != 12)
75                                 throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
76                         System.arraycopy(newHighLevelStateCasted.getBits(), 0, lastState, 0, 12);
77                         return lastState;
78                 default:
79                         return super.setHighLevelState(lastState, stateID, newHighLevelState);
80                 }
81         }
82
83         static
84         {
85                 IndirectModelComponentCreator.setComponentSupplier(Modeldff12.class.getCanonicalName(), (m, p, n) -> new Modeldff12(m, n));
86         }
87 }