a96b6795b05647b285afa33fb0a3c4cf1510456e
[Mograsim.git] / 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
122                         {
123                                 RWE.feedSignals(ZERO);
124                                 RDEC.feedSignals(ZERO);
125                         }
126                         if (!PASSVal.isBinary())
127                         {
128                                 YD.feedSignals(PASSVal);// "forward" X/U/Z
129                                 YR.feedSignals(PASSVal);// "forward" X/U/Z
130                                 YF.feedSignals(PASSVal);// "forward" X/U/Z
131                                 YmuPC.feedSignals(PASSVal);// "forward" X/U/Z
132                         } else
133                         {
134                                 Bit YDVal = ZERO;
135                                 Bit YRVal = ZERO;
136                                 Bit YFVal = ZERO;
137                                 Bit YmuPCVal = ZERO;
138                                 switch (IAsInt + (PASSVal == ONE ? 16 : 0))
139                                 {
140                                 case 0:
141                                 case 0 + 16:
142                                         break;
143                                 case 2:
144                                 case 1 + 16:
145                                 case 2 + 16:
146                                 case 3 + 16:
147                                 case 5 + 16:
148                                 case 6 + 16:
149                                 case 7 + 16:
150                                 case 11 + 16:
151                                         YDVal = ONE;
152                                         break;
153                                 case 5:
154                                 case 7:
155                                         YRVal = ONE;
156                                         break;
157                                 case 13:
158                                 case 10 + 16:
159                                         YFVal = ONE;
160                                         break;
161                                 case 1:
162                                 case 3:
163                                 case 4:
164                                 case 6:
165                                 case 10:
166                                 case 11:
167                                 case 12:
168                                 case 14:
169                                 case 4 + 16:
170                                 case 12 + 16:
171                                 case 13 + 16:
172                                 case 14 + 16:
173                                 case 15 + 16:
174                                         YmuPCVal = ONE;
175                                         break;
176                                 case 8:
177                                 case 8 + 16:
178                                         YFVal = Req0Val.not();// "forward" X/U/Z
179                                         YmuPCVal = Req0Val;// "forward" X/U/Z
180                                         break;
181                                 case 9:
182                                 case 9 + 16:
183                                         YDVal = Req0Val.not();// "forward" X/U/Z
184                                         YmuPCVal = Req0Val;// "forward" X/U/Z
185                                         break;
186                                 case 15:
187                                         YFVal = Req0Val.not();// "forward" X/U/Z
188                                         YDVal = Req0Val;// "forward" X/U/Z
189                                         break;
190                                 default:
191                                         throw new IllegalStateException("shouldn't happen");
192                                 }
193                                 YD.feedSignals(YDVal);
194                                 YR.feedSignals(YRVal);
195                                 YF.feedSignals(YFVal);
196                                 YmuPC.feedSignals(YmuPCVal);
197                                 Bit STKI0Val;
198                                 Bit STKI1Val;
199                                 switch (IAsInt + (PASSVal == ONE ? 16 : 0))
200                                 {
201                                 case 1:
202                                 case 2:
203                                 case 3:
204                                 case 6:
205                                 case 7:
206                                 case 9:
207                                 case 10:
208                                 case 11:
209                                 case 12:
210                                 case 13:
211                                 case 14:
212                                 case 2 + 16:
213                                 case 3 + 16:
214                                 case 6 + 16:
215                                 case 7 + 16:
216                                 case 9 + 16:
217                                 case 12 + 16:
218                                 case 14 + 16:
219                                         // HOLD
220                                         STKI1Val = ZERO;
221                                         STKI0Val = ZERO;
222                                         break;
223                                 case 4:
224                                 case 5:
225                                 case 1 + 16:
226                                 case 4 + 16:
227                                 case 5 + 16:
228                                         // PUSH
229                                         STKI1Val = ZERO;
230                                         STKI0Val = ONE;
231                                         break;
232                                 case 0:
233                                 case 0 + 16:
234                                         // CLEAR
235                                         STKI1Val = ONE;
236                                         STKI0Val = ZERO;
237                                         break;
238                                 case 10 + 16:
239                                 case 11 + 16:
240                                 case 13 + 16:
241                                 case 15 + 16:
242                                         // POP
243                                         STKI1Val = ONE;
244                                         STKI0Val = ONE;
245                                         break;
246                                 case 8:
247                                 case 15:
248                                 case 8 + 16:
249                                         STKI1Val = Req0Val;// "forward" X/U/Z
250                                         STKI0Val = Req0Val;// "forward" X/U/Z
251                                         break;
252                                 default:
253                                         throw new IllegalStateException("shouldn't happen");
254                                 }
255                                 STKI0.feedSignals(STKI0Val);
256                                 STKI1.feedSignals(STKI1Val);
257                         }
258                 }
259                 return null;
260         }
261
262         static
263         {
264                 IndirectModelComponentCreator.setComponentSupplier(ModelAm2910InstrPLA.class.getCanonicalName(),
265                                 (m, p, n) -> new ModelAm2910InstrPLA(m, n));
266         }
267 }