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