4abc5d56dcbfba84bc95b72dee91dd7f34dde765
[Mograsim.git] /
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 = castAndInitState(lastState);
38
39                 Bit CVal = readEnds.get("C").getValue();
40
41                 if (QC[12] == ZERO && CVal == ONE)
42                         System.arraycopy(readEnds.get("D").getValues().getBits(), 0, QC, 0, 12);
43
44                 readWriteEnds.get("Y").feedSignals(Arrays.copyOfRange(QC, 0, 12));
45                 QC[12] = CVal;
46
47                 return QC;
48         }
49
50         @Override
51         protected Object getHighLevelState(Object state, String stateID)
52         {
53                 Bit[] QC = castAndInitState(state);
54
55                 switch (stateID)
56                 {
57                 case "q":
58                         return BitVector.of(Arrays.copyOfRange(QC, 0, 12));
59                 default:
60                         return super.getHighLevelState(QC, stateID);
61                 }
62         }
63
64         @Override
65         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
66         {
67                 Bit[] QC = castAndInitState(lastState);
68
69                 switch (stateID)
70                 {
71                 case "q":
72                         BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
73                         if (newHighLevelStateCasted.length() != 12)
74                                 throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
75                         System.arraycopy(newHighLevelStateCasted.getBits(), 0, QC, 0, 12);
76                         return QC;
77                 default:
78                         return super.setHighLevelState(QC, stateID, newHighLevelState);
79                 }
80         }
81
82         private static Bit[] castAndInitState(Object state)
83         {
84                 Bit[] QC = (Bit[]) state;
85                 if (QC == null)
86                 {
87                         QC = new Bit[13];
88                         Arrays.fill(QC, U);
89                 }
90                 return QC;
91         }
92
93         static
94         {
95                 IndirectModelComponentCreator.setComponentSupplier(Modeldff12.class.getCanonicalName(), (m, p, n) -> new Modeldff12(m, n));
96         }
97 }