Fixed stack high level state internal return type (BitVector[])
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / am2900 / components / GUIram5_12.java
1 package net.mograsim.logic.model.am2900.components;
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.Z;
7 import static net.mograsim.logic.core.types.Bit.ZERO;
8
9 import java.util.Arrays;
10 import java.util.Map;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 import net.mograsim.logic.core.types.Bit;
15 import net.mograsim.logic.core.types.BitVector;
16 import net.mograsim.logic.core.wires.Wire.ReadEnd;
17 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
18 import net.mograsim.logic.model.model.ViewModelModifiable;
19 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
20 import net.mograsim.logic.model.model.wires.Pin;
21 import net.mograsim.logic.model.model.wires.PinUsage;
22 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
23 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
24
25 public class GUIram5_12 extends SimpleRectangularHardcodedGUIComponent
26 {
27         public GUIram5_12(ViewModelModifiable model, String name)
28         {
29                 super(model, name, "RAM\n5 x 12 Bit");
30                 setSize(40, 40);
31                 addPin(new Pin(this, "A", 3, PinUsage.INPUT, 10, 0), Position.BOTTOM);
32                 addPin(new Pin(this, "B", 3, PinUsage.INPUT, 30, 0), Position.BOTTOM);
33                 addPin(new Pin(this, "WE", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
34                 addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
35                 addPin(new Pin(this, "Y", 12, PinUsage.OUTPUT, 0, 30), Position.RIGHT);
36                 addPin(new Pin(this, "D", 12, PinUsage.INPUT, 20, 40), Position.TOP);
37         }
38
39         @Override
40         public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
41         {
42                 BitVector[] memC = (BitVector[]) lastState;
43                 if (memC == null)
44                 {
45                         memC = new BitVector[6];
46                         Arrays.fill(memC, 0, 5, BitVector.of(U, 12));
47                         memC[5] = BitVector.of(U);
48                 }
49                 BitVector CVal = readEnds.get("C").getValues();
50                 BitVector oldC = memC[5];
51                 // TODO is the timing right?
52                 if (oldC.getLSBit(0) == ZERO && CVal.getLSBit(0) == ONE && readEnds.get("WE").getValue() == ONE)
53                 {
54                         int BInt = getAsInt(readEnds.get("B").getValues());
55                         if (BInt == -1 || BInt > 4)
56                                 Arrays.fill(memC, BitVector.of(X, 12));
57                         else if (BInt == -2)
58                                 Arrays.fill(memC, BitVector.of(U, 12));
59                         else
60                                 memC[BInt] = readEnds.get("D").getValues();
61                 }
62                 int AInt = getAsInt(readEnds.get("A").getValues());
63                 BitVector YVal = AInt == -1 || AInt > 4 ? BitVector.of(X, 12) : AInt == -2 ? BitVector.of(U, 12) : memC[AInt];
64                 readWriteEnds.get("Y").feedSignals(YVal);
65                 memC[5] = CVal;
66                 return memC;
67         }
68
69         /**
70          * -1 means X, -2 means U
71          */
72         private static int getAsInt(BitVector vect)
73         {
74                 Bit[] bits = vect.getBits();
75                 for (int i = 0; i < 3; i++)
76                         if (bits[i] == X)
77                                 return -1;
78                 for (int i = 0; i < 3; i++)
79                         if (bits[i] == U)
80                                 return -2;
81                 for (int i = 0; i < 3; i++)
82                         if (bits[i] == Z)
83                                 return -1;
84                 // TODO maybe this is the wrong way around
85                 return (bits[0] == ONE ? 4 : 0) + (bits[1] == ONE ? 2 : 0) + (bits[2] == ONE ? 1 : 0);
86         }
87
88         Pattern stateIDPattern = Pattern.compile("c(0[10][10]|100)");
89
90         @Override
91         protected Object getHighLevelState(Object state, String stateID)
92         {
93                 Matcher m = stateIDPattern.matcher(stateID);
94                 if (m.matches())
95                         return ((BitVector[]) state)[Integer.parseInt(m.group(1), 2)];
96                 return super.getHighLevelState(state, stateID);
97         }
98
99         @Override
100         protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
101         {
102                 Matcher m = stateIDPattern.matcher(stateID);
103                 if (m.matches())
104                 {
105                         int addr = Integer.parseInt(m.group(1), 2);
106                         BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
107                         if (newHighLevelStateCasted.length() != 12)
108                                 throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
109                         BitVector[] memC = (BitVector[]) lastState;
110                         memC[addr] = newHighLevelStateCasted;
111                         return memC;
112                 }
113                 return super.setHighLevelState(lastState, stateID, newHighLevelState);
114         }
115
116         static
117         {
118                 IndirectGUIComponentCreator.setComponentSupplier(GUIram5_12.class.getCanonicalName(), (m, p, n) -> new GUIram5_12(m, n));
119         }
120 }