1 package net.mograsim.logic.model.am2900.components;
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;
9 import java.util.Arrays;
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;
23 public class Modeldff4_finewe extends SimpleRectangularHardcodedModelComponent
25 public Modeldff4_finewe(LogicModelModifiable model, String name)
27 super(model, "dff4_finewe", name, "D flip flop\n4 bits", false);
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);
47 public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
49 Bit[] QC = castAndInitState(lastState);
51 Bit CVal = readEnds.get("C").getValue();
53 if (QC[0] == ZERO && CVal == ONE)
54 for (int i = 1; i < 5; i++)
56 Bit WEiVal = readEnds.get("_WE" + i).getValue();
57 if (WEiVal == X || WEiVal == Z)
61 else if (WEiVal == ZERO)
62 QC[i] = readEnds.get("D" + i).getValue();
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]);
76 protected Object getHighLevelState(Object state, String stateID)
78 Bit[] QC = castAndInitState(state);
80 if ("q".equals(stateID))
81 return BitVector.of(Arrays.copyOfRange(QC, 1, 5));
82 if (stateID.length() == 2 && stateID.charAt(0) == 'q')
84 char secondChar = stateID.charAt(1);
85 if (secondChar >= '1' && secondChar <= '4')
86 return BitVector.of(QC[secondChar - '0']);
88 return super.getHighLevelState(state, stateID);
92 protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
94 Bit[] QC = castAndInitState(lastState);
96 if ("q".equals(stateID))
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);
104 if (stateID.length() == 2 && stateID.charAt(0) == 'q')
106 char secondChar = stateID.charAt(1);
107 if (secondChar >= '1' && secondChar <= '4')
109 Bit newHighLevelStateCasted;
110 if (newHighLevelState instanceof Bit)
111 newHighLevelStateCasted = (Bit) newHighLevelState;
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);
119 QC[secondChar - '0'] = newHighLevelStateCasted;
123 return super.setHighLevelState(QC, stateID, newHighLevelState);
126 private static Bit[] castAndInitState(Object state)
128 Bit[] QC = (Bit[]) state;
130 QC = new Bit[] { U, U, U, U, U };
136 IndirectModelComponentCreator.setComponentSupplier(Modeldff4_finewe.class.getCanonicalName(),
137 (m, p, n) -> new Modeldff4_finewe(m, n));