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 = (Bit[]) lastState;
51 QC = new Bit[] { U, U, U, U, U };
53 Bit CVal = readEnds.get("C").getValue();
55 if (QC[0] == ZERO && CVal == ONE)
56 for (int i = 1; i < 5; i++)
58 Bit WEiVal = readEnds.get("_WE" + i).getValue();
59 if (WEiVal == X || WEiVal == Z)
63 else if (WEiVal == ZERO)
64 QC[i] = readEnds.get("D" + i).getValue();
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]);
78 protected Object getHighLevelState(Object state, String stateID)
83 return BitVector.of(Arrays.copyOfRange((Bit[]) state, 1, 5));
85 return super.getHighLevelState(state, stateID);
90 protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
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);
101 return super.setHighLevelState(lastState, stateID, newHighLevelState);
107 IndirectModelComponentCreator.setComponentSupplier(Modeldff4_finewe.class.getCanonicalName(),
108 (m, p, n) -> new Modeldff4_finewe(m, n));