1 package net.mograsim.logic.model.am2900.components;
3 import static net.mograsim.logic.core.types.Bit.*;
5 import java.util.Arrays;
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;
17 public class GUIram5_12 extends SimpleRectangularHardcodedGUIComponent
19 public GUIram5_12(ViewModelModifiable model, String name)
21 super(model, name, "RAM\n5 x 12 Bit");
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);
32 protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
34 BitVector[] memC = (BitVector[]) lastState;
37 memC = new BitVector[6];
38 Arrays.fill(memC, 0, 5, BitVector.of(U, 12));
39 memC[5] = BitVector.of(U);
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)
46 int BInt = getAsInt(readEnds.get("B").getValues());
47 if (BInt == -1 || BInt > 4)
48 Arrays.fill(memC, BitVector.of(X, 12));
50 Arrays.fill(memC, BitVector.of(U, 12));
52 memC[BInt] = readEnds.get("D").getValues();
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);
62 * -1 means X, -2 means U
64 private static int getAsInt(BitVector vect)
66 Bit[] bits = vect.getBits();
67 for (int i = 0; i < 3; i++)
70 for (int i = 0; i < 3; i++)
73 for (int i = 0; i < 3; i++)
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);