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