1bf80c80456bafa8d7d5a340d59c9ccaa8e4820b
[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
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.types.BitVector;
14 import net.mograsim.logic.core.wires.Wire.ReadEnd;
15 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
16 import net.mograsim.logic.model.model.ViewModelModifiable;
17 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
18 import net.mograsim.logic.model.model.wires.Pin;
19 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
20 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
21
22 public class GUIram5_12 extends SimpleRectangularHardcodedGUIComponent
23 {
24         public GUIram5_12(ViewModelModifiable model, String name)
25         {
26                 super(model, name, "RAM\n5 x 12 Bit");
27                 setSize(40, 40);
28                 addPin(new Pin(this, "A", 3, 10, 0), Usage.INPUT, Position.BOTTOM);
29                 addPin(new Pin(this, "B", 3, 30, 0), Usage.INPUT, Position.BOTTOM);
30                 addPin(new Pin(this, "WE", 1, 0, 5), Usage.INPUT, Position.RIGHT);
31                 addPin(new Pin(this, "C", 1, 0, 15), Usage.INPUT, Position.RIGHT);
32                 addPin(new Pin(this, "Y", 12, 0, 30), Usage.OUTPUT, Position.RIGHT);
33                 addPin(new Pin(this, "D", 12, 20, 40), Usage.INPUT, Position.TOP);
34         }
35
36         @Override
37         protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
38         {
39                 BitVector[] memC = (BitVector[]) lastState;
40                 if (memC == null)
41                 {
42                         memC = new BitVector[6];
43                         Arrays.fill(memC, 0, 5, BitVector.of(U, 12));
44                         memC[5] = BitVector.of(U);
45                 }
46                 BitVector CVal = readEnds.get("C").getValues();
47                 BitVector oldC = memC[5];
48                 // TODO is the timing right?
49                 if (oldC.getLSBit(0) == ZERO && CVal.getLSBit(0) == ONE && readEnds.get("WE").getValue() == ONE)
50                 {
51                         int BInt = getAsInt(readEnds.get("B").getValues());
52                         if (BInt == -1 || BInt > 4)
53                                 Arrays.fill(memC, BitVector.of(X, 12));
54                         else if (BInt == -2)
55                                 Arrays.fill(memC, BitVector.of(U, 12));
56                         else
57                                 memC[BInt] = readEnds.get("D").getValues();
58                 }
59                 int AInt = getAsInt(readEnds.get("A").getValues());
60                 BitVector YVal = AInt == -1 || AInt > 4 ? BitVector.of(X, 12) : AInt == -2 ? BitVector.of(U, 12) : memC[AInt];
61                 readWriteEnds.get("Y").feedSignals(YVal);
62                 memC[5] = CVal;
63                 return memC;
64         }
65
66         /**
67          * -1 means X, -2 means U
68          */
69         private static int getAsInt(BitVector vect)
70         {
71                 Bit[] bits = vect.getBits();
72                 for (int i = 0; i < 3; i++)
73                         if (bits[i] == X)
74                                 return -1;
75                 for (int i = 0; i < 3; i++)
76                         if (bits[i] == U)
77                                 return -2;
78                 for (int i = 0; i < 3; i++)
79                         if (bits[i] == Z)
80                                 return -1;
81                 // TODO maybe this is the wrong way around
82                 return (bits[0] == ONE ? 4 : 0) + (bits[1] == ONE ? 2 : 0) + (bits[2] == ONE ? 1 : 0);
83         }
84
85         static
86         {
87                 IndirectGUIComponentCreator.setComponentSupplier(GUIram5_12.class.getCanonicalName(), (m, p, n) -> new GUIram5_12(m, n));
88         }
89 }