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.X;
6 import static net.mograsim.logic.core.types.Bit.ZERO;
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 ModelAm2910SP extends SimpleRectangularHardcodedModelComponent
23 public ModelAm2910SP(LogicModelModifiable model, String name)
25 super(model, "Am2910SP", name, "Stack\npointer", false);
27 addPin(new Pin(model, this, "STKI0", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
28 addPin(new Pin(model, this, "STKI1", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
29 addPin(new Pin(model, this, "C", 1, PinUsage.INPUT, 0, 25), Position.RIGHT);
30 addPin(new Pin(model, this, "A", 3, PinUsage.OUTPUT, 10, 30), Position.TOP);
31 addPin(new Pin(model, this, "B", 3, PinUsage.OUTPUT, 30, 30), Position.TOP);
32 addPin(new Pin(model, this, "_FULL", 1, PinUsage.OUTPUT, 40, 15), Position.LEFT);
38 public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
40 BitAndInt SPC = (BitAndInt) lastState;
43 SPC = new BitAndInt();
49 Bit STKI0Val = readEnds.get("STKI0").getValue();
50 Bit STKI1Val = readEnds.get("STKI1").getValue();
51 Bit CVal = readEnds.get("C").getValue();
52 if (SPC.bit == ZERO && CVal == ONE)
53 if (STKI0Val == U && STKI1Val == U)
55 else if (!STKI0Val.isBinary() || !STKI1Val.isBinary())
57 else if (STKI0Val == ONE && STKI1Val == ZERO)
61 SP = SP == 5 ? 5 : SP + 1;
62 } else if (STKI0Val == ZERO && STKI1Val == ONE)
65 else if (STKI0Val == ONE && STKI1Val == ONE)
67 SP = SP <= 0 ? SP : SP - 1;
68 readWriteEnds.get("A").feedSignals(getAsBitVector(SP == 0 ? 7 : SP < 0 ? SP : SP - 1));
69 readWriteEnds.get("B").feedSignals(getAsBitVector(SP == 5 ? 4 : SP));
70 readWriteEnds.get("_FULL").feedSignals(SP == -2 ? U : SP == -1 ? X : SP == 5 ? ZERO : ONE);
78 protected Object getHighLevelState(Object state, String stateID)
83 return getAsBitVector(((BitAndInt) state).i);
85 return super.getHighLevelState(state, stateID);
90 protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
96 BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
97 if (newHighLevelStateCasted.length() != 3)
98 throw new IllegalArgumentException("Expected BitVector of length 3, not " + newHighLevelStateCasted.length());
99 if (newHighLevelStateCasted.isBinary())
100 i = newHighLevelStateCasted.getUnsignedValue().intValue();
102 i = -1;// this makes setting to U impossible
104 throw new IllegalArgumentException("Given value not in range (0-5 incl.): " + i);
105 ((BitAndInt) lastState).i = i;
108 return super.setHighLevelState(lastState, stateID, newHighLevelState);
112 private static class BitAndInt
119 * -1 means X, -2 means U
121 private static BitVector getAsBitVector(int i)
124 return BitVector.of(X, 3);
126 return BitVector.of(U, 3);
127 return BitVector.of((i & 0b100) > 0 ? ONE : ZERO, (i & 0b10) > 0 ? ONE : ZERO, (i & 0b1) > 0 ? ONE : ZERO);
132 IndirectModelComponentCreator.setComponentSupplier(ModelAm2910SP.class.getCanonicalName(), (m, p, n) -> new ModelAm2910SP(m, n));