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.ViewModelModifiable;
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(ViewModelModifiable model, String name)
27 super(model, "dff4_finewe", name, "D flip flop\n4 bits");
29 addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
30 addPin(new Pin(this, "_WE1", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
31 addPin(new Pin(this, "_WE2", 1, PinUsage.INPUT, 0, 25), Position.RIGHT);
32 addPin(new Pin(this, "_WE3", 1, PinUsage.INPUT, 0, 35), Position.RIGHT);
33 addPin(new Pin(this, "_WE4", 1, PinUsage.INPUT, 0, 45), Position.RIGHT);
34 addPin(new Pin(this, "D1", 1, PinUsage.INPUT, 0, 55), Position.RIGHT);
35 addPin(new Pin(this, "D2", 1, PinUsage.INPUT, 0, 65), Position.RIGHT);
36 addPin(new Pin(this, "D3", 1, PinUsage.INPUT, 0, 75), Position.RIGHT);
37 addPin(new Pin(this, "D4", 1, PinUsage.INPUT, 0, 85), Position.RIGHT);
38 addPin(new Pin(this, "Q1", 1, PinUsage.OUTPUT, 35, 5), Position.LEFT);
39 addPin(new Pin(this, "Q2", 1, PinUsage.OUTPUT, 35, 15), Position.LEFT);
40 addPin(new Pin(this, "Q3", 1, PinUsage.OUTPUT, 35, 25), Position.LEFT);
41 addPin(new Pin(this, "Q4", 1, PinUsage.OUTPUT, 35, 35), Position.LEFT);
45 public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
47 Bit[] QC = (Bit[]) lastState;
49 QC = new Bit[] { U, U, U, U, U };
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)
81 return BitVector.of(Arrays.copyOfRange((Bit[]) state, 1, 5));
83 return super.getHighLevelState(state, stateID);
88 protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
93 BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
94 if (newHighLevelStateCasted.length() != 4)
95 throw new IllegalArgumentException("Expected BitVector of length 4, not " + newHighLevelStateCasted.length());
96 System.arraycopy(newHighLevelStateCasted.getBits(), 0, lastState, 1, 4);
99 return super.setHighLevelState(lastState, stateID, newHighLevelState);
105 IndirectModelComponentCreator.setComponentSupplier(Modeldff4_finewe.class.getCanonicalName(),
106 (m, p, n) -> new Modeldff4_finewe(m, n));