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 / Modeldff4_finewe.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.X;
6 import static net.mograsim.logic.core.types.Bit.Z;
7 import static net.mograsim.logic.core.types.Bit.ZERO;
8
9 import java.util.Arrays;
10 import java.util.Map;
11
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.types.BitVector;
14 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
15 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
16 import net.mograsim.logic.model.model.LogicModelModifiable;
17 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedModelComponent;
18 import net.mograsim.logic.model.model.wires.Pin;
19 import net.mograsim.logic.model.model.wires.PinUsage;
20 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
21 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
22
23 public class Modeldff4_finewe extends SimpleRectangularHardcodedModelComponent
24 {
25         public Modeldff4_finewe(LogicModelModifiable model, String name)
26         {
27                 super(model, "dff4_finewe", name, "D flip flop\n4 bits", false);
28                 setSize(35, 90);
29                 addPin(new Pin(model, this, "C", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
30                 addPin(new Pin(model, this, "_WE1", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
31                 addPin(new Pin(model, this, "_WE2", 1, PinUsage.INPUT, 0, 25), Position.RIGHT);
32                 addPin(new Pin(model, this, "_WE3", 1, PinUsage.INPUT, 0, 35), Position.RIGHT);
33                 addPin(new Pin(model, this, "_WE4", 1, PinUsage.INPUT, 0, 45), Position.RIGHT);
34                 addPin(new Pin(model, this, "D1", 1, PinUsage.INPUT, 0, 55), Position.RIGHT);
35                 addPin(new Pin(model, this, "D2", 1, PinUsage.INPUT, 0, 65), Position.RIGHT);
36                 addPin(new Pin(model, this, "D3", 1, PinUsage.INPUT, 0, 75), Position.RIGHT);
37                 addPin(new Pin(model, this, "D4", 1, PinUsage.INPUT, 0, 85), Position.RIGHT);
38                 addPin(new Pin(model, this, "Q1", 1, PinUsage.OUTPUT, 35, 5), Position.LEFT);
39                 addPin(new Pin(model, this, "Q2", 1, PinUsage.OUTPUT, 35, 15), Position.LEFT);
40                 addPin(new Pin(model, this, "Q3", 1, PinUsage.OUTPUT, 35, 25), Position.LEFT);
41                 addPin(new Pin(model, this, "Q4", 1, PinUsage.OUTPUT, 35, 35), Position.LEFT);
42
43                 init();
44         }
45
46         @Override
47         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
48         {
49                 Bit[] QC = (Bit[]) lastState;
50                 if (QC == null)
51                         QC = new Bit[] { U, U, U, U, U };
52
53                 Bit CVal = readEnds.get("C").getValue();
54
55                 if (QC[0] == ZERO && CVal == ONE)
56                         for (int i = 1; i < 5; i++)
57                         {
58                                 Bit WEiVal = readEnds.get("_WE" + i).getValue();
59                                 if (WEiVal == X || WEiVal == Z)
60                                         QC[i] = X;
61                                 else if (WEiVal == U)
62                                         QC[i] = U;
63                                 else if (WEiVal == ZERO)
64                                         QC[i] = readEnds.get("D" + i).getValue();
65                         }
66
67                 QC[0] = CVal;
68
69                 readWriteEnds.get("Q1").feedSignals(QC[1]);
70                 readWriteEnds.get("Q2").feedSignals(QC[2]);
71                 readWriteEnds.get("Q3").feedSignals(QC[3]);
72                 readWriteEnds.get("Q4").feedSignals(QC[4]);
73
74                 return QC;
75         }
76
77         @Override
78         protected Object getHighLevelState(Object state, String stateID)
79         {
80                 switch (stateID)
81                 {
82                 case "q":
83                         return BitVector.of(Arrays.copyOfRange((Bit[]) state, 1, 5));
84                 default:
85                         return super.getHighLevelState(state, stateID);
86                 }
87         }
88
89         @Override
90         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
91         {
92                 switch (stateID)
93                 {
94                 case "q":
95                         BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
96                         if (newHighLevelStateCasted.length() != 4)
97                                 throw new IllegalArgumentException("Expected BitVector of length 4, not " + newHighLevelStateCasted.length());
98                         System.arraycopy(newHighLevelStateCasted.getBits(), 0, lastState, 1, 4);
99                         return lastState;
100                 default:
101                         return super.setHighLevelState(lastState, stateID, newHighLevelState);
102                 }
103         }
104
105         static
106         {
107                 IndirectModelComponentCreator.setComponentSupplier(Modeldff4_finewe.class.getCanonicalName(),
108                                 (m, p, n) -> new Modeldff4_finewe(m, n));
109         }
110 }