Implemented Am2910's Instruction PLA
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / modeladapter / componentadapters / Am2910InstrPLAAdapter.java
1 package net.mograsim.logic.model.modeladapter.componentadapters;
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.LogicObserver;
11 import net.mograsim.logic.core.timeline.Timeline;
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.wires.Wire;
14 import net.mograsim.logic.core.wires.Wire.ReadEnd;
15 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
16 import net.mograsim.logic.model.am2900.components.am2910.GUIAm2910InstrPLA;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.modeladapter.LogicModelParameters;
19
20 public class Am2910InstrPLAAdapter implements ComponentAdapter<GUIAm2910InstrPLA>
21 {
22         @Override
23         public Class<GUIAm2910InstrPLA> getSupportedClass()
24         {
25                 return GUIAm2910InstrPLA.class;
26         }
27
28         @Override
29         public void createAndLinkComponent(Timeline timeline, LogicModelParameters params, GUIAm2910InstrPLA guiComponent,
30                         Map<Pin, Wire> logicWiresPerPin)
31         {
32                 ReadEnd PASS = logicWiresPerPin.get(guiComponent.getPin("PASS")).createReadOnlyEnd();
33                 ReadEnd I3 = logicWiresPerPin.get(guiComponent.getPin("I3")).createReadOnlyEnd();
34                 ReadEnd I2 = logicWiresPerPin.get(guiComponent.getPin("I2")).createReadOnlyEnd();
35                 ReadEnd I1 = logicWiresPerPin.get(guiComponent.getPin("I1")).createReadOnlyEnd();
36                 ReadEnd I0 = logicWiresPerPin.get(guiComponent.getPin("I0")).createReadOnlyEnd();
37                 ReadEnd Req0 = logicWiresPerPin.get(guiComponent.getPin("R=0")).createReadOnlyEnd();
38                 ReadWriteEnd _PL = logicWiresPerPin.get(guiComponent.getPin("_PL")).createReadWriteEnd();
39                 ReadWriteEnd _MAP = logicWiresPerPin.get(guiComponent.getPin("_MAP")).createReadWriteEnd();
40                 ReadWriteEnd _VECT = logicWiresPerPin.get(guiComponent.getPin("_VECT")).createReadWriteEnd();
41                 ReadWriteEnd RWE = logicWiresPerPin.get(guiComponent.getPin("RWE")).createReadWriteEnd();
42                 ReadWriteEnd RDEC = logicWiresPerPin.get(guiComponent.getPin("RDEC")).createReadWriteEnd();
43                 ReadWriteEnd YD = logicWiresPerPin.get(guiComponent.getPin("YD")).createReadWriteEnd();
44                 ReadWriteEnd YR = logicWiresPerPin.get(guiComponent.getPin("YR")).createReadWriteEnd();
45                 ReadWriteEnd YF = logicWiresPerPin.get(guiComponent.getPin("YF")).createReadWriteEnd();
46                 ReadWriteEnd YmuPC = logicWiresPerPin.get(guiComponent.getPin("YmuPC")).createReadWriteEnd();
47                 ReadWriteEnd STKI0 = logicWiresPerPin.get(guiComponent.getPin("STKI0")).createReadWriteEnd();
48                 ReadWriteEnd STKI1 = logicWiresPerPin.get(guiComponent.getPin("STKI1")).createReadWriteEnd();
49
50                 LogicObserver updateOutputs = o ->
51                 {
52                         Bit PASSVal = PASS.getValue();
53                         Bit I3Val = I3.getValue();
54                         Bit I2Val = I2.getValue();
55                         Bit I1Val = I1.getValue();
56                         Bit I0Val = I0.getValue();
57                         Bit Req0Val = Req0.getValue();
58
59                         if (!I3Val.isBinary() || !I2Val.isBinary() || !I1Val.isBinary() || !I0Val.isBinary())
60                                 if ((I3Val == U || I3Val.isBinary()) || (I2Val == U || I2Val.isBinary()) || (I1Val == U || I1Val.isBinary())
61                                                 || (I0Val == U || I0Val.isBinary()))
62                                 {
63                                         _PL.feedSignals(U);
64                                         _MAP.feedSignals(U);
65                                         _VECT.feedSignals(U);
66                                         RWE.feedSignals(U);
67                                         RDEC.feedSignals(U);
68                                         YD.feedSignals(U);
69                                         YR.feedSignals(U);
70                                         YF.feedSignals(U);
71                                         YmuPC.feedSignals(U);
72                                         STKI0.feedSignals(U);
73                                         STKI1.feedSignals(U);
74                                 } else
75                                 {
76                                         _PL.feedSignals(X);
77                                         _MAP.feedSignals(X);
78                                         _VECT.feedSignals(X);
79                                         RWE.feedSignals(X);
80                                         RDEC.feedSignals(X);
81                                         YD.feedSignals(X);
82                                         YR.feedSignals(X);
83                                         YF.feedSignals(X);
84                                         YmuPC.feedSignals(X);
85                                         STKI0.feedSignals(X);
86                                         STKI1.feedSignals(X);
87                                 }
88                         else
89                         {
90                                 int I = (I3Val == ONE ? 8 : 0) + (I2Val == ONE ? 4 : 0) + (I1Val == ONE ? 2 : 0) + (I0Val == ONE ? 1 : 0);
91                                 Bit _PLVal = ONE;
92                                 Bit _MAPVal = ONE;
93                                 Bit _VECTVal = ONE;
94                                 if (I == 2)
95                                         _MAPVal = ZERO;
96                                 else if (I == 6)
97                                         _VECTVal = ZERO;
98                                 else
99                                         _PLVal = ZERO;
100                                 _PL.feedSignals(_PLVal);
101                                 _MAP.feedSignals(_MAPVal);
102                                 _VECT.feedSignals(_VECTVal);
103                                 if (I == 8 || I == 9 || I == 15)
104                                 {
105                                         RWE.feedSignals(Req0Val);
106                                         RDEC.feedSignals(Req0Val);// "forward" X/U/Z
107                                 } else if (I == 4)
108                                 {
109                                         RWE.feedSignals(PASSVal);
110                                         RDEC.feedSignals(PASSVal == ONE ? ZERO : PASSVal);// "forward" X/U/Z
111                                 } else
112                                 {
113                                         RWE.feedSignals(ZERO);
114                                         RDEC.feedSignals(ZERO);
115                                 }
116                                 if (!PASSVal.isBinary())
117                                 {
118                                         YD.feedSignals(PASSVal);// "forward" X/U/Z
119                                         YR.feedSignals(PASSVal);// "forward" X/U/Z
120                                         YF.feedSignals(PASSVal);// "forward" X/U/Z
121                                         YmuPC.feedSignals(PASSVal);// "forward" X/U/Z
122                                 } else
123                                 {
124                                         Bit YDVal = ZERO;
125                                         Bit YRVal = ZERO;
126                                         Bit YFVal = ZERO;
127                                         Bit YmuPCVal = ZERO;
128                                         switch (I + (PASSVal == ONE ? 16 : 0))
129                                         {
130                                         case 0:
131                                         case 0 + 16:
132                                                 break;
133                                         case 2:
134                                         case 1 + 16:
135                                         case 2 + 16:
136                                         case 3 + 16:
137                                         case 5 + 16:
138                                         case 6 + 16:
139                                         case 7 + 16:
140                                         case 11 + 16:
141                                                 YDVal = ONE;
142                                                 break;
143                                         case 5:
144                                         case 7:
145                                                 YRVal = ONE;
146                                                 break;
147                                         case 13:
148                                         case 10 + 16:
149                                                 YFVal = ONE;
150                                                 break;
151                                         case 1:
152                                         case 3:
153                                         case 4:
154                                         case 6:
155                                         case 10:
156                                         case 11:
157                                         case 12:
158                                         case 14:
159                                         case 4 + 16:
160                                         case 12 + 16:
161                                         case 13 + 16:
162                                         case 14 + 16:
163                                         case 15 + 16:
164                                                 YmuPCVal = ONE;
165                                                 break;
166                                         case 8:
167                                         case 8 + 16:
168                                                 YFVal = Req0Val.not();// "forward" X/U/Z
169                                                 YmuPCVal = Req0Val;// "forward" X/U/Z
170                                                 break;
171                                         case 9:
172                                         case 9 + 16:
173                                                 YDVal = Req0Val.not();// "forward" X/U/Z
174                                                 YmuPCVal = Req0Val;// "forward" X/U/Z
175                                                 break;
176                                         case 15:
177                                                 YFVal = Req0Val.not();// "forward" X/U/Z
178                                                 YDVal = Req0Val;// "forward" X/U/Z
179                                                 break;
180                                         default:
181                                                 throw new IllegalStateException("shouldn't happen");
182                                         }
183                                         YD.feedSignals(YDVal);
184                                         YR.feedSignals(YRVal);
185                                         YF.feedSignals(YFVal);
186                                         YmuPC.feedSignals(YmuPCVal);
187                                         Bit STKI0Val;
188                                         Bit STKI1Val;
189                                         switch (I + (PASSVal == ONE ? 16 : 0))
190                                         {
191                                         case 1:
192                                         case 2:
193                                         case 3:
194                                         case 6:
195                                         case 7:
196                                         case 9:
197                                         case 10:
198                                         case 11:
199                                         case 12:
200                                         case 13:
201                                         case 14:
202                                         case 2 + 16:
203                                         case 3 + 16:
204                                         case 6 + 16:
205                                         case 7 + 16:
206                                         case 9 + 16:
207                                         case 12 + 16:
208                                         case 14 + 16:
209                                                 STKI1Val = ZERO;
210                                                 STKI0Val = ZERO;
211                                                 break;
212                                         case 4:
213                                         case 5:
214                                         case 1 + 16:
215                                         case 4 + 16:
216                                         case 5 + 16:
217                                                 STKI1Val = ZERO;
218                                                 STKI0Val = ONE;
219                                                 break;
220                                         case 0:
221                                         case 0 + 16:
222                                                 STKI1Val = ONE;
223                                                 STKI0Val = ZERO;
224                                                 break;
225                                         case 10 + 16:
226                                         case 11 + 16:
227                                         case 13 + 16:
228                                         case 15 + 16:
229                                                 STKI1Val = ONE;
230                                                 STKI0Val = ONE;
231                                                 break;
232                                         case 8:
233                                         case 15:
234                                         case 8 + 16:
235                                                 STKI1Val = Req0Val;// "forward" X/U/Z
236                                                 STKI0Val = Req0Val;// "forward" X/U/Z
237                                                 break;
238                                         default:
239                                                 throw new IllegalStateException("shouldn't happen");
240                                         }
241                                         STKI0.feedSignals(STKI0Val);
242                                         STKI1.feedSignals(STKI1Val);
243                                 }
244                         }
245                 };
246
247                 PASS.registerObserver(updateOutputs);
248                 I3.registerObserver(updateOutputs);
249                 I2.registerObserver(updateOutputs);
250                 I1.registerObserver(updateOutputs);
251                 I0.registerObserver(updateOutputs);
252                 Req0.registerObserver(updateOutputs);
253         }
254 }