Renamed ViewModel to LogicModel
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / am2910 / ModelAm2910SP.java
1 package net.mograsim.logic.model.am2900.components.am2910;
2
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;
7
8 import java.util.Map;
9
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;
20
21 public class ModelAm2910SP extends SimpleRectangularHardcodedModelComponent
22 {
23         public ModelAm2910SP(LogicModelModifiable model, String name)
24         {
25                 super(model, "Am2910SP", name, "Stack\npointer");
26                 setSize(40, 30);
27                 addPin(new Pin(this, "STKI0", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
28                 addPin(new Pin(this, "STKI1", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
29                 addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 25), Position.RIGHT);
30                 addPin(new Pin(this, "A", 3, PinUsage.OUTPUT, 10, 30), Position.TOP);
31                 addPin(new Pin(this, "B", 3, PinUsage.OUTPUT, 30, 30), Position.TOP);
32                 addPin(new Pin(this, "_FULL", 1, PinUsage.OUTPUT, 40, 15), Position.LEFT);
33         }
34
35         @Override
36         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
37         {
38                 BitAndInt SPC = (BitAndInt) lastState;
39                 if (SPC == null)
40                 {
41                         SPC = new BitAndInt();
42                         SPC.bit = U;
43                         SPC.i = -2;
44                 }
45                 int SP = SPC.i;
46
47                 Bit STKI0Val = readEnds.get("STKI0").getValue();
48                 Bit STKI1Val = readEnds.get("STKI1").getValue();
49                 Bit CVal = readEnds.get("C").getValue();
50                 if (SPC.bit == ZERO && CVal == ONE)
51                         if (STKI0Val == U && STKI1Val == U)
52                                 SP = -2;
53                         else if (!STKI0Val.isBinary() || !STKI1Val.isBinary())
54                                 SP = -1;
55                         else if (STKI0Val == ONE && STKI1Val == ZERO)
56                         {
57                                 // PUSH
58                                 if (SP >= 0)
59                                         SP = SP == 5 ? 5 : SP + 1;
60                         } else if (STKI0Val == ZERO && STKI1Val == ONE)
61                                 // CLEAR
62                                 SP = 0;
63                         else if (STKI0Val == ONE && STKI1Val == ONE)
64                                 // POP
65                                 SP = SP <= 0 ? SP : SP - 1;
66                 readWriteEnds.get("A").feedSignals(getAsBitVector(SP == 0 ? 7 : SP < 0 ? SP : SP - 1));
67                 readWriteEnds.get("B").feedSignals(getAsBitVector(SP == 5 ? 4 : SP));
68                 readWriteEnds.get("_FULL").feedSignals(SP == -2 ? U : SP == -1 ? X : SP == 5 ? ZERO : ONE);
69
70                 SPC.i = SP;
71                 SPC.bit = CVal;
72                 return SPC;
73         }
74
75         @Override
76         protected Object getHighLevelState(Object state, String stateID)
77         {
78                 switch (stateID)
79                 {
80                 case "q":
81                         return getAsBitVector(((BitAndInt) state).i);
82                 default:
83                         return super.getHighLevelState(state, stateID);
84                 }
85         }
86
87         @Override
88         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
89         {
90                 switch (stateID)
91                 {
92                 case "q":
93                         int i;
94                         BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
95                         if (newHighLevelStateCasted.length() != 3)
96                                 throw new IllegalArgumentException("Expected BitVector of length 3, not " + newHighLevelStateCasted.length());
97                         if (newHighLevelStateCasted.isBinary())
98                                 i = newHighLevelStateCasted.getUnsignedValue().intValue();
99                         else
100                                 i = -1;// this makes setting to U impossible
101                         if (i > 5)
102                                 throw new IllegalArgumentException("Given value not in range (0-5 incl.): " + i);
103                         ((BitAndInt) lastState).i = i;
104                         return lastState;
105                 default:
106                         return super.setHighLevelState(lastState, stateID, newHighLevelState);
107                 }
108         }
109
110         private static class BitAndInt
111         {
112                 Bit bit;
113                 int i;
114         }
115
116         /**
117          * -1 means X, -2 means U
118          */
119         private static BitVector getAsBitVector(int i)
120         {
121                 if (i == -1)
122                         return BitVector.of(X, 3);
123                 if (i == -2)
124                         return BitVector.of(U, 3);
125                 return BitVector.of((i & 0b100) > 0 ? ONE : ZERO, (i & 0b10) > 0 ? ONE : ZERO, (i & 0b1) > 0 ? ONE : ZERO);
126         }
127
128         static
129         {
130                 IndirectModelComponentCreator.setComponentSupplier(ModelAm2910SP.class.getCanonicalName(), (m, p, n) -> new ModelAm2910SP(m, n));
131         }
132 }