a00fc302b8aa4d9bc2d6f08cb4148000c8905b0b
[Mograsim.git] / plugins / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / am2910 / ModelAm2910InstrPLA.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.wires.CoreWire.ReadEnd;
12 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
13 import net.mograsim.logic.model.model.LogicModelModifiable;
14 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedModelComponent;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.model.wires.PinUsage;
17 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
18 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
19
20 public class ModelAm2910InstrPLA extends SimpleRectangularHardcodedModelComponent
21 {
22         public ModelAm2910InstrPLA(LogicModelModifiable model, String name)
23         {
24                 super(model, "Am2910InstrPLA", name, "Instr.\nPLA", false);
25                 setSize(30, 85);
26                 addPin(new Pin(model, this, "PASS", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
27                 addPin(new Pin(model, this, "I", 4, PinUsage.INPUT, 0, 20), Position.RIGHT);
28                 addPin(new Pin(model, this, "R=0", 1, PinUsage.INPUT, 15, 0), Position.BOTTOM);
29                 addPin(new Pin(model, this, "_PL", 1, PinUsage.OUTPUT, 5, 85), Position.TOP);
30                 addPin(new Pin(model, this, "_MAP", 1, PinUsage.OUTPUT, 15, 85), Position.TOP);
31                 addPin(new Pin(model, this, "_VECT", 1, PinUsage.OUTPUT, 25, 85), Position.TOP);
32                 addPin(new Pin(model, this, "RWE", 1, PinUsage.OUTPUT, 30, 5), Position.LEFT);
33                 addPin(new Pin(model, this, "RDEC", 1, PinUsage.OUTPUT, 30, 15), Position.LEFT);
34                 addPin(new Pin(model, this, "YD", 1, PinUsage.OUTPUT, 30, 25), Position.LEFT);
35                 addPin(new Pin(model, this, "YR", 1, PinUsage.OUTPUT, 30, 35), Position.LEFT);
36                 addPin(new Pin(model, this, "YF", 1, PinUsage.OUTPUT, 30, 45), Position.LEFT);
37                 addPin(new Pin(model, this, "YmuPC", 1, PinUsage.OUTPUT, 30, 55), Position.LEFT);
38                 addPin(new Pin(model, this, "STKI0", 1, PinUsage.OUTPUT, 30, 65), Position.LEFT);
39                 addPin(new Pin(model, this, "STKI1", 1, PinUsage.OUTPUT, 30, 75), Position.LEFT);
40
41                 init();
42         }
43
44         @Override
45         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
46         {
47                 ReadEnd PASS = readEnds.get("PASS");
48                 ReadEnd I = readEnds.get("I");
49                 ReadEnd Req0 = readEnds.get("R=0");
50                 ReadWriteEnd _PL = readWriteEnds.get("_PL");
51                 ReadWriteEnd _MAP = readWriteEnds.get("_MAP");
52                 ReadWriteEnd _VECT = readWriteEnds.get("_VECT");
53                 ReadWriteEnd RWE = readWriteEnds.get("RWE");
54                 ReadWriteEnd RDEC = readWriteEnds.get("RDEC");
55                 ReadWriteEnd YD = readWriteEnds.get("YD");
56                 ReadWriteEnd YR = readWriteEnds.get("YR");
57                 ReadWriteEnd YF = readWriteEnds.get("YF");
58                 ReadWriteEnd YmuPC = readWriteEnds.get("YmuPC");
59                 ReadWriteEnd STKI0 = readWriteEnds.get("STKI0");
60                 ReadWriteEnd STKI1 = readWriteEnds.get("STKI1");
61
62                 Bit PASSVal = PASS.getValue();
63                 Bit I3Val = I.getValue(3);
64                 Bit I2Val = I.getValue(2);
65                 Bit I1Val = I.getValue(1);
66                 Bit I0Val = I.getValue(0);
67                 Bit Req0Val = Req0.getValue();
68
69                 if (!I3Val.isBinary() || !I2Val.isBinary() || !I1Val.isBinary() || !I0Val.isBinary())
70                         if ((I3Val == U || I3Val.isBinary()) || (I2Val == U || I2Val.isBinary()) || (I1Val == U || I1Val.isBinary())
71                                         || (I0Val == U || I0Val.isBinary()))
72                         {
73                                 _PL.feedSignals(U);
74                                 _MAP.feedSignals(U);
75                                 _VECT.feedSignals(U);
76                                 RWE.feedSignals(U);
77                                 RDEC.feedSignals(U);
78                                 YD.feedSignals(U);
79                                 YR.feedSignals(U);
80                                 YF.feedSignals(U);
81                                 YmuPC.feedSignals(U);
82                                 STKI0.feedSignals(U);
83                                 STKI1.feedSignals(U);
84                         } else
85                         {
86                                 _PL.feedSignals(X);
87                                 _MAP.feedSignals(X);
88                                 _VECT.feedSignals(X);
89                                 RWE.feedSignals(X);
90                                 RDEC.feedSignals(X);
91                                 YD.feedSignals(X);
92                                 YR.feedSignals(X);
93                                 YF.feedSignals(X);
94                                 YmuPC.feedSignals(X);
95                                 STKI0.feedSignals(X);
96                                 STKI1.feedSignals(X);
97                         }
98                 else
99                 {
100                         int IAsInt = (I3Val == ONE ? 8 : 0) + (I2Val == ONE ? 4 : 0) + (I1Val == ONE ? 2 : 0) + (I0Val == ONE ? 1 : 0);
101                         Bit _PLVal = ONE;
102                         Bit _MAPVal = ONE;
103                         Bit _VECTVal = ONE;
104                         if (IAsInt == 2)
105                                 _MAPVal = ZERO;
106                         else if (IAsInt == 6)
107                                 _VECTVal = ZERO;
108                         else
109                                 _PLVal = ZERO;
110                         _PL.feedSignals(_PLVal);
111                         _MAP.feedSignals(_MAPVal);
112                         _VECT.feedSignals(_VECTVal);
113                         if (IAsInt == 8 || IAsInt == 9 || IAsInt == 15)
114                         {
115                                 RWE.feedSignals(Req0Val);
116                                 RDEC.feedSignals(Req0Val);// "forward" X/U/Z
117                         } else if (IAsInt == 4)
118                         {
119                                 RWE.feedSignals(PASSVal);
120                                 RDEC.feedSignals(PASSVal == ONE ? ZERO : PASSVal);// "forward" X/U/Z
121                         } else if (IAsInt == 12)
122                         {
123                                 RWE.feedSignals(ONE);
124                                 RDEC.feedSignals(ZERO);
125                         } else
126                         {
127                                 RWE.feedSignals(ZERO);
128                                 RDEC.feedSignals(ZERO);
129                         }
130                         if (!PASSVal.isBinary())
131                         {
132                                 YD.feedSignals(PASSVal);// "forward" X/U/Z
133                                 YR.feedSignals(PASSVal);// "forward" X/U/Z
134                                 YF.feedSignals(PASSVal);// "forward" X/U/Z
135                                 YmuPC.feedSignals(PASSVal);// "forward" X/U/Z
136                         } else
137                         {
138                                 Bit YDVal = ZERO;
139                                 Bit YRVal = ZERO;
140                                 Bit YFVal = ZERO;
141                                 Bit YmuPCVal = ZERO;
142                                 switch (IAsInt + (PASSVal == ONE ? 16 : 0))
143                                 {
144                                 case 0:
145                                 case 0 + 16:
146                                         break;
147                                 case 2:
148                                 case 1 + 16:
149                                 case 2 + 16:
150                                 case 3 + 16:
151                                 case 5 + 16:
152                                 case 6 + 16:
153                                 case 7 + 16:
154                                 case 11 + 16:
155                                         YDVal = ONE;
156                                         break;
157                                 case 5:
158                                 case 7:
159                                         YRVal = ONE;
160                                         break;
161                                 case 13:
162                                 case 10 + 16:
163                                         YFVal = ONE;
164                                         break;
165                                 case 1:
166                                 case 3:
167                                 case 4:
168                                 case 6:
169                                 case 10:
170                                 case 11:
171                                 case 12:
172                                 case 14:
173                                 case 4 + 16:
174                                 case 12 + 16:
175                                 case 13 + 16:
176                                 case 14 + 16:
177                                 case 15 + 16:
178                                         YmuPCVal = ONE;
179                                         break;
180                                 case 8:
181                                 case 8 + 16:
182                                         YFVal = Req0Val.not();// "forward" X/U/Z
183                                         YmuPCVal = Req0Val;// "forward" X/U/Z
184                                         break;
185                                 case 9:
186                                 case 9 + 16:
187                                         YDVal = Req0Val.not();// "forward" X/U/Z
188                                         YmuPCVal = Req0Val;// "forward" X/U/Z
189                                         break;
190                                 case 15:
191                                         YFVal = Req0Val.not();// "forward" X/U/Z
192                                         YDVal = Req0Val;// "forward" X/U/Z
193                                         break;
194                                 default:
195                                         throw new IllegalStateException("shouldn't happen");
196                                 }
197                                 YD.feedSignals(YDVal);
198                                 YR.feedSignals(YRVal);
199                                 YF.feedSignals(YFVal);
200                                 YmuPC.feedSignals(YmuPCVal);
201                                 Bit STKI0Val;
202                                 Bit STKI1Val;
203                                 switch (IAsInt + (PASSVal == ONE ? 16 : 0))
204                                 {
205                                 case 1:
206                                 case 2:
207                                 case 3:
208                                 case 6:
209                                 case 7:
210                                 case 9:
211                                 case 10:
212                                 case 11:
213                                 case 12:
214                                 case 13:
215                                 case 14:
216                                 case 2 + 16:
217                                 case 3 + 16:
218                                 case 6 + 16:
219                                 case 7 + 16:
220                                 case 9 + 16:
221                                 case 12 + 16:
222                                 case 14 + 16:
223                                         // HOLD
224                                         STKI1Val = ZERO;
225                                         STKI0Val = ZERO;
226                                         break;
227                                 case 4:
228                                 case 5:
229                                 case 1 + 16:
230                                 case 4 + 16:
231                                 case 5 + 16:
232                                         // PUSH
233                                         STKI1Val = ZERO;
234                                         STKI0Val = ONE;
235                                         break;
236                                 case 0:
237                                 case 0 + 16:
238                                         // CLEAR
239                                         STKI1Val = ONE;
240                                         STKI0Val = ZERO;
241                                         break;
242                                 case 10 + 16:
243                                 case 11 + 16:
244                                 case 13 + 16:
245                                 case 15 + 16:
246                                         // POP
247                                         STKI1Val = ONE;
248                                         STKI0Val = ONE;
249                                         break;
250                                 case 8:
251                                 case 15:
252                                 case 8 + 16:
253                                         STKI1Val = Req0Val;// "forward" X/U/Z
254                                         STKI0Val = Req0Val;// "forward" X/U/Z
255                                         break;
256                                 default:
257                                         throw new IllegalStateException("shouldn't happen");
258                                 }
259                                 STKI0.feedSignals(STKI0Val);
260                                 STKI1.feedSignals(STKI1Val);
261                         }
262                 }
263                 return null;
264         }
265
266         static
267         {
268                 IndirectModelComponentCreator.setComponentSupplier(ModelAm2910InstrPLA.class.getCanonicalName(),
269                                 (m, p, n) -> new ModelAm2910InstrPLA(m, n));
270         }
271 }