6aebcbd6db1426d3019c13a6eccecc47a0ae000a
[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.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 = castAndInitState(lastState);
50
51                 Bit CVal = readEnds.get("C").getValue();
52
53                 if (QC[0] == ZERO && CVal == ONE)
54                         for (int i = 1; i < 5; i++)
55                         {
56                                 Bit WEiVal = readEnds.get("_WE" + i).getValue();
57                                 if (WEiVal == X || WEiVal == Z)
58                                         QC[i] = X;
59                                 else if (WEiVal == U)
60                                         QC[i] = U;
61                                 else if (WEiVal == ZERO)
62                                         QC[i] = readEnds.get("D" + i).getValue();
63                         }
64
65                 QC[0] = CVal;
66
67                 readWriteEnds.get("Q1").feedSignals(QC[1]);
68                 readWriteEnds.get("Q2").feedSignals(QC[2]);
69                 readWriteEnds.get("Q3").feedSignals(QC[3]);
70                 readWriteEnds.get("Q4").feedSignals(QC[4]);
71
72                 return QC;
73         }
74
75         @Override
76         protected Object getHighLevelState(Object state, String stateID)
77         {
78                 Bit[] QC = castAndInitState(state);
79
80                 if ("q".equals(stateID))
81                         return BitVector.of(Arrays.copyOfRange(QC, 1, 5));
82                 if (stateID.length() == 2 && stateID.charAt(0) == 'q')
83                 {
84                         char secondChar = stateID.charAt(1);
85                         if (secondChar >= '1' && secondChar <= '4')
86                                 return BitVector.of(QC[secondChar - '0']);
87                 }
88                 return super.getHighLevelState(state, stateID);
89         }
90
91         @Override
92         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
93         {
94                 Bit[] QC = castAndInitState(lastState);
95
96                 if ("q".equals(stateID))
97                 {
98                         BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
99                         if (newHighLevelStateCasted.length() != 4)
100                                 throw new IllegalArgumentException("Expected BitVector of length 4, not " + newHighLevelStateCasted.length());
101                         System.arraycopy(newHighLevelStateCasted.getBits(), 0, QC, 1, 4);
102                         return QC;
103                 }
104                 if (stateID.length() == 2 && stateID.charAt(0) == 'q')
105                 {
106                         char secondChar = stateID.charAt(1);
107                         if (secondChar >= '1' && secondChar <= '4')
108                         {
109                                 Bit newHighLevelStateCasted;
110                                 if (newHighLevelState instanceof Bit)
111                                         newHighLevelStateCasted = (Bit) newHighLevelState;
112                                 else
113                                 {
114                                         BitVector vector = (BitVector) newHighLevelState;
115                                         if (vector.length() != 1)
116                                                 throw new IllegalArgumentException("Expected BitVector of length 1, not " + vector.length());
117                                         newHighLevelStateCasted = vector.getMSBit(0);
118                                 }
119                                 QC[secondChar - '0'] = newHighLevelStateCasted;
120                                 return QC;
121                         }
122                 }
123                 return super.setHighLevelState(QC, stateID, newHighLevelState);
124         }
125
126         private static Bit[] castAndInitState(Object state)
127         {
128                 Bit[] QC = (Bit[]) state;
129                 if (QC == null)
130                         QC = new Bit[] { U, U, U, U, U };
131                 return QC;
132         }
133
134         static
135         {
136                 IndirectModelComponentCreator.setComponentSupplier(Modeldff4_finewe.class.getCanonicalName(),
137                                 (m, p, n) -> new Modeldff4_finewe(m, n));
138         }
139 }