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