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