1 package net.mograsim.logic.model.am2900.components.am2910;
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.ViewModelModifiable;
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 ModelAm2910RegCntr extends SimpleRectangularHardcodedModelComponent
23 public ModelAm2910RegCntr(ViewModelModifiable model, String name)
25 super(model, "Am2910RegCntr", name, "Register/\nCounter");
27 addPin(new Pin(this, "D", 12, PinUsage.INPUT, 20, 0), Position.BOTTOM);
28 addPin(new Pin(this, "_RLD", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
29 addPin(new Pin(this, "WE", 1, PinUsage.INPUT, 0, 20), Position.RIGHT);
30 addPin(new Pin(this, "DEC", 1, PinUsage.INPUT, 0, 30), Position.RIGHT);
31 addPin(new Pin(this, "C", 1, PinUsage.INPUT, 40, 20), Position.LEFT);
32 addPin(new Pin(this, "Y", 12, PinUsage.OUTPUT, 20, 40), Position.TOP);
36 public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
38 Bit[] QC = (Bit[]) lastState;
45 ReadEnd D = readEnds.get("D");
46 ReadEnd _RLD = readEnds.get("_RLD");
47 ReadEnd WE = readEnds.get("WE");
48 ReadEnd DEC = readEnds.get("DEC");
49 ReadEnd C = readEnds.get("C");
50 ReadWriteEnd Y = readWriteEnds.get("Y");
53 Bit CVal = C.getValue();
56 if (oldCVal == ZERO && CVal == ONE)
58 if ((DEC.getValue() == ZERO && WE.getValue() == ONE) || _RLD.getValue() == ZERO)
59 System.arraycopy(D.getValues().getBits(), 0, QC, 0, 12);
60 else if (WE.getValue() == ONE)
63 // TODO extract to helper. This code almost also exists in Modelinc12.
64 for (int i = 11; i >= 0; i--)
67 QC[i] = a.xnor(carry);
73 Y.feedSignals(Arrays.copyOfRange(QC, 0, 12));
79 protected Object getHighLevelState(Object state, String stateID)
84 return BitVector.of(Arrays.copyOfRange((Bit[]) state, 0, 12));
86 return super.getHighLevelState(state, stateID);
91 protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
96 BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
97 if (newHighLevelStateCasted.length() != 12)
98 throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
99 System.arraycopy(newHighLevelStateCasted.getBits(), 0, lastState, 0, 12);
102 return super.setHighLevelState(lastState, stateID, newHighLevelState);
108 IndirectModelComponentCreator.setComponentSupplier(ModelAm2910RegCntr.class.getCanonicalName(),
109 (m, p, n) -> new ModelAm2910RegCntr(m, n));