import net.mograsim.logic.model.model.ViewModelModifiable;
import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.model.wires.PinUsage;
import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
{
super(model, name, "Stack\npointer");
setSize(40, 30);
- addPin(new Pin(this, "STKI0", 1, 0, 5), Usage.INPUT, Position.RIGHT);
- addPin(new Pin(this, "STKI1", 1, 0, 15), Usage.INPUT, Position.RIGHT);
- addPin(new Pin(this, "C", 1, 0, 25), Usage.INPUT, Position.RIGHT);
- addPin(new Pin(this, "A", 3, 10, 30), Usage.OUTPUT, Position.TOP);
- addPin(new Pin(this, "B", 3, 30, 30), Usage.OUTPUT, Position.TOP);
- addPin(new Pin(this, "_FULL", 1, 40, 15), Usage.OUTPUT, Position.LEFT);
+ addPin(new Pin(this, "STKI0", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
+ addPin(new Pin(this, "STKI1", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
+ addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 25), Position.RIGHT);
+ addPin(new Pin(this, "A", 3, PinUsage.OUTPUT, 10, 30), Position.TOP);
+ addPin(new Pin(this, "B", 3, PinUsage.OUTPUT, 30, 30), Position.TOP);
+ addPin(new Pin(this, "_FULL", 1, PinUsage.OUTPUT, 40, 15), Position.LEFT);
}
@Override
- protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
+ public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
{
BitAndInt SPC = (BitAndInt) lastState;
if (SPC == null)
else if (STKI0Val == ONE && STKI1Val == ONE)
// POP
SP = SP <= 0 ? SP : SP - 1;
- readWriteEnds.get("A").feedSignals(getAsBitVector(SP == 0 ? 7 : SP - 1));
+ readWriteEnds.get("A").feedSignals(getAsBitVector(SP == 0 ? 7 : SP < 0 ? SP : SP - 1));
readWriteEnds.get("B").feedSignals(getAsBitVector(SP == 5 ? 4 : SP));
readWriteEnds.get("_FULL").feedSignals(SP == -2 ? U : SP == -1 ? X : SP == 5 ? ZERO : ONE);
return SPC;
}
+ @Override
+ protected Object getHighLevelState(Object state, String stateID)
+ {
+ switch (stateID)
+ {
+ case "q":
+ return getAsBitVector(((BitAndInt) state).i);
+ default:
+ return super.getHighLevelState(state, stateID);
+ }
+ }
+
+ @Override
+ protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
+ {
+ switch (stateID)
+ {
+ case "q":
+ int i;
+ BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
+ if (newHighLevelStateCasted.length() != 3)
+ throw new IllegalArgumentException("Expected BitVector of length 3, not " + newHighLevelStateCasted.length());
+ if (newHighLevelStateCasted.isBinary())
+ i = newHighLevelStateCasted.getUnsignedValue().intValue();
+ else
+ i = -1;// this makes setting to U impossible
+ if (i > 5)
+ throw new IllegalArgumentException("Given value not in range (0-5 incl.): " + i);
+ ((BitAndInt) lastState).i = i;
+ return lastState;
+ default:
+ return super.setHighLevelState(lastState, stateID, newHighLevelState);
+ }
+ }
+
private static class BitAndInt
{
Bit bit;
return BitVector.of(X, 3);
if (i == -2)
return BitVector.of(U, 3);
- // TODO maybe this is the wrong way around
return BitVector.of((i & 0b100) > 0 ? ONE : ZERO, (i & 0b10) > 0 ? ONE : ZERO, (i & 0b1) > 0 ? ONE : ZERO);
}