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.ZERO;
7 import java.util.Arrays;
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.LogicModelModifiable;
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;
21 public class Modeldff12 extends SimpleRectangularHardcodedModelComponent
23 public Modeldff12(LogicModelModifiable model, String name)
25 super(model, "dff12", name, "D flip flop\n12 bits", false);
27 addPin(new Pin(model, this, "D", 12, PinUsage.INPUT, 20, 20), Position.TOP);
28 addPin(new Pin(model, this, "C", 1, PinUsage.INPUT, 0, 10), Position.RIGHT);
29 addPin(new Pin(model, this, "Y", 12, PinUsage.OUTPUT, 20, 0), Position.BOTTOM);
35 public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
37 Bit[] QC = castAndInitState(lastState);
39 Bit CVal = readEnds.get("C").getValue();
41 if (QC[12] == ZERO && CVal == ONE)
42 System.arraycopy(readEnds.get("D").getValues().getBits(), 0, QC, 0, 12);
44 readWriteEnds.get("Y").feedSignals(Arrays.copyOfRange(QC, 0, 12));
51 protected Object getHighLevelState(Object state, String stateID)
53 Bit[] QC = castAndInitState(state);
58 return BitVector.of(Arrays.copyOfRange(QC, 0, 12));
60 return super.getHighLevelState(QC, stateID);
65 protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
67 Bit[] QC = castAndInitState(lastState);
72 BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
73 if (newHighLevelStateCasted.length() != 12)
74 throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
75 System.arraycopy(newHighLevelStateCasted.getBits(), 0, QC, 0, 12);
78 return super.setHighLevelState(QC, stateID, newHighLevelState);
82 private static Bit[] castAndInitState(Object state)
84 Bit[] QC = (Bit[]) state;
95 IndirectModelComponentCreator.setComponentSupplier(Modeldff12.class.getCanonicalName(), (m, p, n) -> new Modeldff12(m, n));