d9d1a60eacb3818ec25ed75c9513019cdf84c4d5
[Mograsim.git] / plugins / 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", false);
26                 setSize(40, 30);
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);
33
34                 init();
35         }
36
37         @Override
38         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
39         {
40                 BitAndInt SPC = castAndInitState(lastState);
41                 int SP = SPC.i;
42
43                 Bit STKI0Val = readEnds.get("STKI0").getValue();
44                 Bit STKI1Val = readEnds.get("STKI1").getValue();
45                 Bit CVal = readEnds.get("C").getValue();
46                 if (SPC.bit == ZERO && CVal == ONE)
47                         if (STKI0Val == U && STKI1Val == U)
48                                 SP = -2;
49                         else if (!STKI0Val.isBinary() || !STKI1Val.isBinary())
50                                 SP = -1;
51                         else if (STKI0Val == ONE && STKI1Val == ZERO)
52                         {
53                                 // PUSH
54                                 if (SP >= 0)
55                                         SP = SP == 5 ? 5 : SP + 1;
56                         } else if (STKI0Val == ZERO && STKI1Val == ONE)
57                                 // CLEAR
58                                 SP = 0;
59                         else if (STKI0Val == ONE && STKI1Val == ONE)
60                                 // POP
61                                 SP = SP <= 0 ? SP : SP - 1;
62                 readWriteEnds.get("A").feedSignals(getAsBitVector(SP == 0 ? 7 : SP < 0 ? SP : SP - 1));
63                 readWriteEnds.get("B").feedSignals(getAsBitVector(SP == 5 ? 4 : SP));
64                 readWriteEnds.get("_FULL").feedSignals(SP == -2 ? U : SP == -1 ? X : SP == 5 ? ZERO : ONE);
65
66                 SPC.i = SP;
67                 SPC.bit = CVal;
68                 return SPC;
69         }
70
71         @Override
72         protected Object getHighLevelState(Object state, String stateID)
73         {
74                 BitAndInt SPC = castAndInitState(state);
75
76                 switch (stateID)
77                 {
78                 case "q":
79                         return getAsBitVector(SPC.i);
80                 default:
81                         return super.getHighLevelState(SPC, stateID);
82                 }
83         }
84
85         @Override
86         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
87         {
88                 BitAndInt SPC = castAndInitState(lastState);
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                         SPC.i = i;
104                         return SPC;
105                 default:
106                         return super.setHighLevelState(SPC, stateID, newHighLevelState);
107                 }
108         }
109
110         private static BitAndInt castAndInitState(Object lastState)
111         {
112                 BitAndInt SPC = (BitAndInt) lastState;
113                 if (SPC == null)
114                 {
115                         SPC = new BitAndInt();
116                         SPC.bit = U;
117                         SPC.i = -2;
118                 }
119                 return SPC;
120         }
121
122         private static class BitAndInt
123         {
124                 Bit bit;
125                 int i;
126         }
127
128         /**
129          * -1 means X, -2 means U
130          */
131         private static BitVector getAsBitVector(int i)
132         {
133                 if (i == -1)
134                         return BitVector.of(X, 3);
135                 if (i == -2)
136                         return BitVector.of(U, 3);
137                 return BitVector.of((i & 0b100) > 0 ? ONE : ZERO, (i & 0b10) > 0 ? ONE : ZERO, (i & 0b1) > 0 ? ONE : ZERO);
138         }
139
140         static
141         {
142                 IndirectModelComponentCreator.setComponentSupplier(ModelAm2910SP.class.getCanonicalName(), (m, p, n) -> new ModelAm2910SP(m, n));
143         }
144 }