1 package net.mograsim.logic.model.am2900.components;
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;
9 import java.util.Arrays;
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;
22 public class GUIram5_12 extends SimpleRectangularHardcodedGUIComponent
24 public GUIram5_12(ViewModelModifiable model, String name)
26 super(model, name, "RAM\n5 x 12 Bit");
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);
37 protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
39 BitVector[] memC = (BitVector[]) lastState;
42 memC = new BitVector[6];
43 Arrays.fill(memC, 0, 5, BitVector.of(U, 12));
44 memC[5] = BitVector.of(U);
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)
51 int BInt = getAsInt(readEnds.get("B").getValues());
52 if (BInt == -1 || BInt > 4)
53 Arrays.fill(memC, BitVector.of(X, 12));
55 Arrays.fill(memC, BitVector.of(U, 12));
57 memC[BInt] = readEnds.get("D").getValues();
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);
67 * -1 means X, -2 means U
69 private static int getAsInt(BitVector vect)
71 Bit[] bits = vect.getBits();
72 for (int i = 0; i < 3; i++)
75 for (int i = 0; i < 3; i++)
78 for (int i = 0; i < 3; i++)
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);
87 IndirectGUIComponentCreator.setComponentSupplier(GUIram5_12.class.getCanonicalName(), (m, p, n) -> new GUIram5_12(m, n));